I've two signals stored in a 1D array a and b that I'd like to correlate. For that I've tried
corr=np.correlate(a,b, mode='full')
and I get this error:
ValueError: object too deep for desired array
I've checked the shape of the two arrays:
>>> a.shape
(1, 10501)
>>> b.shape
(1, 10501)
I would like to know what I'm doing wrong?
Related
I've just trained a classification with BERT and it has 3 classes as an output. The prediction makes it an array like you can see in the picture I've attached.
prediction result
I've tried to make it as a dataframe using this code:
data_result = pd.DataFrame(predictions)
But it gives me warning like this
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py:305: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
values = np.array([convert(v) for v in values])
And the result when I make it as a csv isn't like what I want. It didn't have three columns
Hope anyone can helps.
Thank you
Update
From the comments, reshaping array
Initial array shape is (2, 1, 3) as in x but is list of numpy arrays,
We concatenate along outer most axis = 0 of each numpy array, which will merge those numpy array into single. This will remove (2, 1, 3) --> (2, 3)
np.newaxis is to convert the inner numpy array by one more dimension.
>>> from numpy import array, float32
>>> import numpy as np
>>> x = [ array([[ 1.9392334, -2.4614801, 1.1337504]], dtype=float32), array([[-2.705459 , 3.260675 , -0.9435711]], dtype=float32)]
>>> x.shape
>>> np.concatenate(x, axis=0)
array([[ 1.9392334, -2.4614801, 1.1337504],
[-2.705459 , 3.260675 , -0.9435711]], dtype=float32)
>>> y = np.concatenate(x, axis=0)
>>> y.shape
(2, 3)
>>> z = y[np.newaxis, :]
>>> z.shape
(1, 2, 3)
This means the entries in prediction is not of uniform shape.
Re-inspect or share the prediction array's shape and confirm if all of them are same. I was able to create an example for you which gives same error. This should get you started. As you can see first entry shape is (2, 3) while second entry is (1, 3)
>>> pd.DataFrame([np.array([[1.,2.,3.], [1.,3.,4.]], dtype=np.float32), np.array([[2,3.,4.]], dtype=np.float32)])
/home/lol/anaconda3/lib/python3.8/site-packages/pandas/core/internals/construction.py:305: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
values = np.array([convert(v) for v in values])
0
0 [[1.0, 2.0, 3.0], [1.0, 3.0, 4.0]]
1 [[2.0, 3.0, 4.0]]
Also the errors will not stop there since it is indirectly a 3-d array. Depending on how you want dataframe How to transform a 3d arrays into a dataframe in python will be helpful
Getting the shape of two different numpy arrays returns tuples
a.shape
Out[131]: (3,)
A.shape
Out[132]: (3, 3)
Based on the tuples, one is a one-dimensional array (number of dimensions = 1), the other is 2d. How can I detect number of dimensions similar to how type(A) will tell me one of them is a numpy.ndarray? should I just use len(a.shape)?
You should use numpy.ndarray.ndim. So
a.ndim # gives 1
and
A.ndim # gives 2
I have a numpy array of shape (29, 10) and a list of 29 elements and I want to end up with an array of shape (29,11)
I am basically converting the list to a numpy array and trying to vstack, but it complain about dimensions not being the same.
Toy example
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*29)
b.shape
(29,)
np.vstack((a, b))
ValueError: all the input array dimensions except for the concatenation axis must match exactly
Dimensions do actually match, why am I getting this error and how can I solve it?
I think you are looking for np.hstack.
np.hstack((a, b.reshape(-1,1)))
Moreover b must be 2-dimensional, that's why I used a reshape.
The problem is that you want to append a 1D array to a 2D array.
Also, for the dimension you've given for b, you are probably looking for hstack.
Try this:
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*29)[:,None] #to ensure 2D structure
b.shape
(29,1)
np.hstack((a, b))
If you do want to vertically stack, you'd need this:
a = np.zeros((29,10))
a.shape
(29,10)
b = np.array(['A']*10)[None,:] #to ensure 2D structure
b.shape
(1,10)
np.vstack((a, b))
I have a 'row' vector cast as a numpy ndarray. I would simply like to make it a 'column' vector (I don't care too much about the type as long as it is compatible with matplotlib). Here is an example of what I'm trying:
import numpy as np
a = np.ndarray(shape=(1,4), dtype=float, order='F')
print(a.shape)
a.T #I think this performs the transpose?
print(a.shape)
The output looks like this:
(1, 4)
(1, 4)
I was hoping to get:
(1, 4)
(4, 1)
Can someone point me in the right direction? I have seen that the transpose in numpy doesn't do anything to a 1D array. But is this a 1D array?
Transposing an array does not happen in place. Writing a.T creates a view of the transpose of the array a, but this view is then lost immediately since no variable is assigned to it. a remains unchanged.
You need to write a = a.T to bind the name a to the transpose:
>>> a = a.T
>>> a.shape
(4, 1)
In your example a is indeed a 2D array. Transposing a 1D array (with shape (n,)) does not change that array at all.
you can alter the shape 'in place' which will be the same as a.T for (1,4) but see the comment by Mr E whether it's needed. i.e.
...
print(a.shape)
a.shape = (4, 1)
print(a.shape)
You probably don't want or need the singular dimension, unless you are trying to force a broadcasting operation.
Link
You can treat rank-1 arrays as either row or column vectors. dot(A,v)
treats v as a column vector, while dot(v,A) treats v as a row vector.
This can save you having to type a lot of transposes.
I'm trying to input vectors into a numpy matrix by doing:
eigvec[:,i] = null
However I keep getting the error:
ValueError: could not broadcast input array from shape (20,1) into shape (20)
I've tried using flatten and reshape, but nothing seems to work
The shapes in the error message are a good clue.
In [161]: x = np.zeros((10,10))
In [162]: x[:,1] = np.ones((1,10)) # or x[:,1] = np.ones(10)
In [163]: x[:,1] = np.ones((10,1))
...
ValueError: could not broadcast input array from shape (10,1) into shape (10)
In [166]: x[:,1].shape
Out[166]: (10,)
In [167]: x[:,[1]].shape
Out[167]: (10, 1)
In [168]: x[:,[1]] = np.ones((10,1))
When the shape of the destination matches the shape of the new value, the copy works. It also works in some cases where the new value can be 'broadcasted' to fit. But it does not try more general reshaping. Also note that indexing with a scalar reduces the dimension.
I can guess that
eigvec[:,i] = null.flat
would work (however, null.flatten() should work too). In fact, it looks like NumPy complains because of you are assigning a pseudo-1D array (shape (20, 1)) to a 1D array which is considered to be oriented differently (shape (1, 20), if you wish).
Another solution would be:
eigvec[:,i] = null.T
where you properly transpose the "vector" null.
The fundamental point here is that NumPy has "broadcasting" rules for converting between arrays with different numbers of dimensions. In the case of conversions between 2D and 1D, a 1D array of size n is broadcast into a 2D array of shape (1, n) (and not (n, 1)). More generally, missing dimensions are added to the left of the original dimensions.
The observed error message basically said that shapes (20,) and (20, 1) are not compatible: this is because (20,) becomes (1, 20) (and not (20, 1)). In fact, one is a column matrix, while the other is a row matrix.