I have asked a previous question, but I think my example was not clear. I am still trying to subtract two different sizes of numpy arrays from a list of numpy arrays. For example:
####Data####
### For same size numpy arrays the subtraction works fine!!!!###
easy_data= [[1,2,3],[2,2,2]],[[1,2,3],[1,2,5]]
d = [np.array(i) for i in easy_data] # List of numpy arrays
res = d[1] - d[0]
>> array([[ 0, 0, 0],
[-1, 0, 3]])
##### Current Issue ####
data = [[1,2,3],[2,2,2]],[[1,2,3],[1,2,5],[1,1,1]]
d = [np.array(i) for i in data]
res = d[1] - d[0] #### As the sizes are different I can't subtract them ###
Desired Output
array([[ 0, 0, 0],
[-1, 0, 3],[1,1,1])
I am kind of slow getting how to work with numpy arrays but I can't figure out how to make this work? Can anybody help me?
It's easiest to operate on a slice. If you do not want to erase the original array, use a copy:
>>> res=d[1].copy()
>>> res[:d[0].shape[0]]-=d[0]
>>> res
array([[ 0, 0, 0],
[-1, 0, 3],
[ 1, 1, 1]])
Related
I have a 2D numpy array:
[[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]]
I also have a second 1D array:
[2,3,4]
I want to replace all occurences of the elements of the second array with 0
So eventually, my second array should look like
[[1,0,0,0,5],[0,0,5,6,7],[0,9,0,0,0]]
is there a way in python/numpy I can do this without using a loop.
I already checked at np.where, but the condition there is only for example where element = 1 value, and not multiple.
Thanks a lot !
Use numpy.isin.
>>> import numpy as np
>>> a = np.array([[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]])
>>> b = np.array([2,3,4])
>>> a[np.isin(a, b)] = 0
>>> a
array([[1, 0, 0, 0, 5],
[0, 0, 5, 6, 7],
[0, 9, 0, 0, 0]])
I'm working with numpy and I got a problem with index, I have a numpy array of zeros, and a 2D array of indexes, what I need is to use this indexes to change the values of the array of zeros by the value of 1, I tried something, but it's not working, here is what I tried.
import numpy as np
idx = np.array([0, 3, 4],
[1, 3, 5],
[0, 4, 5]]) #Array of index
zeros = np.zeros(6) #Array of zeros [0, 0, 0, 0, 0, 0]
repeat = np.tile(zeros, (idx.shape[0], 1)) #This repeats the array of zeros to match the number of rows of the index array
res = []
for i, j in zip(repeat, idx):
res.append(i[j] = 1) #Here I try to replace the matching index by the value of 1
output = np.array(res)
but I get the syntax error
expression cannot contain assignment, perhaps you meant "=="?
my desired output should be
output = [[1, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 1]]
This is just an example, the idx array can be bigger, I think the problem is the indexing, and I believe there is a much simple way of doing this without repeating the array of zeros and using the zip function, but I can't figure it out, any help would be aprecciated, thank you!
EDIT: When I change the = by == I get a boolean array which I don't need, so I don't know what's happening there either.
You can use np.put_along_axis to assign values into the array repeat based on indices in idx. This is more efficient than a loop (and easier).
import numpy as np
idx = np.array([[0, 3, 4],
[1, 3, 5],
[0, 4, 5]]) #Array of index
zeros = np.zeros(6).astype(int) #Array of zeros [0, 0, 0, 0, 0, 0]
repeat = np.tile(zeros, (idx.shape[0], 1))
np.put_along_axis(repeat, idx, 1, 1)
repeat will then be:
array([[1, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 1]])
FWIW, you can also make the array of zeros directly by passing in the shape:
np.zeros([idx.shape[0], 6])
I have a SciPy csr_matrix (a vector in this case) of 1 column and x rows. In it are float values which I need to convert to the discrete class labels -1, 0 and 1. This should be done with a threshold function which maps the float values to one of these 3 class labels.
Is there no way other than iterating over the elements as described in Iterating through a scipy.sparse vector (or matrix)? I would love to have some elegant way to just somehow map(thresholdfunc()) on all elements.
Note that while it is of type csr_matrix, it isn't actually sparse as it's just the return of another function where a sparse matrix was involved.
If you have an array, you can discretize based on some condition with the np.where function. e.g.:
>>> import numpy as np
>>> x = np.arange(10)
>>> np.where(x < 5, 0, 1)
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
The syntax is np.where(BOOLEAN_ARRAY, VALUE_IF_TRUE, VALUE_IF_FALSE).
You can chain together two where statements to have multiple conditions:
>>> np.where(x < 3, -1, np.where(x > 6, 0, 1))
array([-1, -1, -1, 1, 1, 1, 1, 0, 0, 0])
To apply this to your data in the CSR or CSC sparse matrix, you can use the .data attribute, which gives you access to the internal array containing all the nonzero entries in the sparse matrix. For example:
>>> from scipy import sparse
>>> mat = sparse.csr_matrix(x.reshape(10, 1))
>>> mat.data = np.where(mat.data < 3, -1, np.where(mat.data > 6, 0, 1))
>>> mat.toarray()
array([[ 0],
[-1],
[-1],
[ 1],
[ 1],
[ 1],
[ 1],
[ 0],
[ 0],
[ 0]])
I want to do something similar to here (in Python):
How to convert a column or row matrix to a diagonal matrix in Python?
that is :
1) set all elements of matrix A onto the diagonal of matrix B (all other elements of B should be 0) and 2) after performing some operation on B, I want to recreate matrix A, so take the elements off B's diagonal , in the same order as was performed in the first step, and put them back in A.
Can you not do just unravel your matrix onto the diagonal of another?
In [29]: import numpy as np
In [30]: a = np.array([[1,2],[3,4]])
In [31]: b = np.diag(a.ravel())
In [32]: b
Out[32]:
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
Then, to go back:
In [33]: b.diagonal().reshape((2,2))
Out[33]:
array([[1, 2],
[3, 4]])
I can't seem to convert it into an ndarray in numpy, i've read http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html but it didn't show me how i can convert my input data as shown below into an ndarray.
How to construct a ndarray from a numpy array or a list of integer lists?
*What's the difference between ndarray and array?* I could just use an array type right?
I have a list of integer counts like this
[[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
And i manage to use this code to create a np.array as shown in http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array
import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
print np.array(x)
[out]:
[[1 2 4 1 5]
[6 0 0 0 2]
[0 0 0 1 0]]
But I can't change it into a np.ndarray with this code:
import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
print np.ndarray(x)
I got an error:
Traceback (most recent call last):
File "/home/alvas/workspace/sklearntut/test.py", line 7, in <module>
print np.ndarray(x)
TypeError: an integer is required
How do I create a np.ndarray with the list of integer counts i've got? What integer is the TypeError talking about?
An ndarray is a NumPy array.
>>> x = np.array([1, 2, 3])
>>> type(x)
<type 'numpy.ndarray'>
The difference between np.ndarray and np.array is that the former is the actual type, while the latter is a flexible shorthand function for constructing arrays from data in other formats. The TypeError comes your use of np.array arguments to np.ndarray, which takes completely different arguments (see docstrings).
Though the accepted response is correct, that didn't help me actually create a 1-dimensional array of arrays.
As this thread is the first answer at Google, I post my work around, even if it isn't elegant solution (please don't hesitate to point one out to me):
import numpy as np
# Create example array
initial_array = np.ones(shape = (2,2))
# Create array of arrays
array_of_arrays = np.ndarray(shape = (1,), dtype = "object")
array_of_arrays[0] = initial_array
Be aware that array_of_arrays is in this case mutable, i.e. changing initial_array automatically changes array_of_arrays .
I will combine the accepted solution with the fixed code.
As said in the accepted solution, an ndarray is a NumPy array.
This essentially means that you should use np.array when constructing an ndarray from a list of lists, a list of tuples, a tuple of lists...
Fixed code fragment:
import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
# The difference: we call np.array
print np.array(x)
Hope this will help others out as this is the first Google answer.