What is different between x.shape[0] and x.shape in numpy? I code without [0] then have a bug: "TypeError: arange: scalar arguments expected instead of a tuple.", but when I add [0] in, my code runs completely.
And why i can't type x.shape[1] or x.shape[1000]?
Looking forward to receiving answers from everyone, many thanks!!
From your error message:
"TypeError: arange: scalar arguments expected instead of a tuple."
It sounds to me like you are trying to use the shape of an existing array to define the shape of a new array using np.arange.
Your problem is that you don't understand what x.shape is giving you.
For example:
x = np.array([[1,2,3],[4,5,6]])
x.shape
produces (2,3), a tuple. If I try to use just x.shape to define a argument in np.arange like this:
np.arange(x.shape)
I get the following error:
"arange: scalar arguments expected instead of a tuple."
Reason being np.arange accepts either a scalar (which creates an array starting at 0 and increasing by 1 to the length provided) or 3 scalars which define where to start and end the array and the step size. You are giving it a tuple instead which it doesn't like.
So when you do:
np.arange(x.shape[0])
you are giving the arange function the first scalar in the tuple provided by x.shape and in my example producing an array like this [0,1] because the first index in the tuple is 2.
If I alternatively did
np.arange(x.shape[1])
I would get an array like [0,1,2] because the second index in the tuple is a 3.
If I did any of the following,
np.arange(x.shape[2])
np.arange(x.shape[1000])
np.arange(x.shape[300])
I would get an error because the tuple created by x.shape has only two dimensions and so can't be indexed any further than 0 or 1.
Hope that helps!
Related
I'm trying to write a function to compute the dot product of two 2D lists passed in as arguments, lets call them x and y.
My idea is to first create a 2D list of zeros of the proper dimensions for the result of the dot product. In order to do so, I need to find the column size of y when computing x * y
dim1 = len(x)
dim2 = len(y[0])
result = [0]*dim1*dim2
The above code was my idea for getting these dimensions, however it fails on the second line due to an error:
dim2 = len(y[0])
TypeError: object of type 'int' has no len()
My python interpreter seems to not like that I am assuming my arguments will be 2D lists. It seems to think it'll be a 1D list. How can I get the column length of the 2D list. I am assuming the 2D lists passed in will be of dimensions NxM, so it should be a clean rectangle shape list/matrix.
I am not able to use numpy for this case.
I defined a two-dimensional array as follows:
predict_result = np.zeros((69,1000))
In the loop, I was trying to inject a predicted one-dimensional array into it.
for ij in range(0,1000):
# # some code to generate Ypredict
predict_result[:,ij]=Ypredict
Ypredict is always the shape of (69,1). However, running the program gives the following error
predict_result[:,ij]=Ypredict ValueError: could not broadcast input
array from shape (69,1) into shape (69)
How can I correct this error?
Either change the (69,1) array to (69,), or make the receiving slot (69,1)
ravel is one on several ways of flattening the 2d array:
predict_result[:,ij]=Ypredict.ravel()
Index with a list rather than a scalar works the other way:
predict_result[:,[ij]]=Ypredict
You could also use broadcasting to put the smaller array into the larger without a loop - as noted in the other answer:
(69,1000) <= (69,1) works
The 2 basic rules of broadcasting:
a size 1 dimension can be added at the start to match the number of dimensions
all size 1 dimensions can be changed to match to same dimension of the other array(s).
(69,) cannot be changed to match (69,1). But (69,1) can be changed to match (69,1000).
You don't need for-loop:
predict_result[:] = Ypredict
Or you can create the result by repeat:
np.repeat(Ypredict, 1000, axis=1)
I have two lists,
list_a
list_b
whose shape are [10,50] and [40,50]
and I'm trying to combine them into one [50,50] array, starting with the following code (edited for readability)
array_a=np.array(list_a)
array_b=np.array(list_b)
array_c=np.concatenate(array_a,array_b)
But it keeps giving me an error that says
"TypeError: only length-1 arrays can be converted to Python scalars"
What's the issue here, and how can I fix it? This error isn't very helpful...
np.concatenate expects a tuple as argument, i.e. it should be
array_c=np.concatenate((array_a,array_b))
The first argument is a tuple of an arbitrary number of arrays, the second argument (in your case array_b) tells concatenate along which axis it should operate.
The issue here is that np.concatenate expects an iterable sequence of array-like objects for the first argument. Here it just takes array_a as the first argument. It is taking array_b as the second argument, which specifies which array axis to concatenate along. As this argument needs to be integer-like, it is attempting to convert array_b to an integer, but failing as it contains more than one item. Hence this error message.
To solve it, you need to wrap your two arrays in an iterable such as a tuple, like this:
cc=np.concatenate((array_a,array_b))
This results in both arrays being passed as the first argument to the function. (Edit: Wrapping in a list also works, i.e. concatenate([array_a,array_b]). Haven't tried other forms).
In your example, this will work, as the second argument defaults to 0, which means the arrays can have a different length in the first dimension only (the zeroth-indexed dimension). For you, these lengths are 10 and 40, and the other dimension is 50 for both. If your array dimensions were reversed, so they were now [50,10] and [50,40], you would need to set the axis to the second dimension (index 1) like so:
cc=np.concatenate((array_a,array_b),1)
A rather simple question for most of you I suspect, although I can't seem to find the answer to it.
I have a function that takes in a 1D array and returns another array.
At the moment I am using a for loop to run this function many times, but I was suggested that I could increase my number of iterations if I was to create a function that would make a 1D array of a length equal to the number of iterations I want and then apply my function to this array.
For example:
inputs = np.array([23.,56,69])
def do_model(array):
... do magic ...
return new_array
What I have at the moment:
outputs= np.array([np.array([do_model(inputs) for x in xrange(iterations)])
What I would like:
def run_model(iterations, inputs):
# generate a 1D array of length iteration containing the input array
my_array = np.full((1,int(iterations)), inputs)
outputs = run_model(iterations, inputs)
# run my function on the array
return do_model(empty)
At the moment I get the following error when I try to create my_array for 5 iterations:
ValueError: could not broadcast input array from shape (3) into shape (1,5)
I tried to specify that the array should be filled by another array using the dtype keyword argument as such:
my_array = np.full((1,int(iterations)), inputs,dtype=np.ndarray
)
put that didn't work.
Any help welcome,
The reason I'm asking is I try to run the code on millions of iterations and at the moment I have a memory error.
I am trying to concatenate two numpy arrays, but I got this error. Could some one give me a bit clue about what this actually means?
Import numpy as np
allValues = np.arange(-1, 1, 0.5)
tmp = np.concatenate(allValues, np.array([30], float))
Then I got
ValueError: 0-d arrays can't be concatenated
If I do
tmp = np.concatenate(allValues, np.array([50], float))
There is no error message but tmp variable does not reflect the concatenation either.
You need to put the arrays you want to concatenate into a sequence (usually a tuple or list) in the argument.
tmp = np.concatenate((allValues, np.array([30], float)))
tmp = np.concatenate([allValues, np.array([30], float)])
Check the documentation for np.concatenate. Note that the first argument is a sequence (e.g. list, tuple) of arrays. It does not take them as separate arguments.
As far as I know, this API is shared by all of numpy's concatenation functions: concatenate, hstack, vstack, dstack, and column_stack all take a single main argument that should be some sequence of arrays.
The reason you are getting that particular error is that arrays are sequences as well. But this means that concatenate is interpreting allValues as a sequence of arrays to concatenate. However, each element of allValues is a float rather than an array, and is therefore being interpreted as a zero-dimensional array. As the error says, these "arrays" cannot be concatenated.
The second argument is taken as the second (optional) argument of concatenate, which is the axis to concatenate on. This only works because there is a single element in the second argument, which can be cast as an integer and therefore is a valid value. If you had put an array with more elements in the second argument, you would have gotten a different error:
a = np.array([1, 2])
b = np.array([3, 4])
np.concatenate(a, b)
# TypeError: only length-1 arrays can be converted to Python scalars
Also make sure you are concatenating two numpy arrays. I was concatenating one python array with a numpy array and it was giving me the same error:
ValueError: 0-d arrays can't be concatenated
It took me some time to figure this out since all the answers in stackoverflow were assuming that you had two numpy arrays.
Pretty silly but easily overlooked mistake. Hence posting just in case this helps someone.
Here are the links to converting an existing python array using np.asarray
or
create np arrays, if it helps.
Another way to get this error is to have two numpy objects of different... types?
I get this error when I try np.concatenate([A,B])
and ValueError: all the input arrays must have same number of dimensions when I run np.concatenate([B,A])
Just as #mithunpaul mentioned, my types are off: A is an array of 44279x204 and B is a <44279x12 sparse matrix of type '<class 'numpy.float64'>' with 88558 stored elements in Compressed Sparse Row format>)
So that's why the error is happening. Don't know how to solve it yet though.