I would like to translate a matlab code into a python one. The matlab code is equivalent to the following toy example:
a = [1 2 3; 4 5 6; 7 8 9]
b = a(:, ones(1,3))
It returns
a =
1 2 3
4 5 6
7 8 9
b =
1 1 1
4 4 4
7 7 7
I tried to translate it like this:
from numpy import array
from numpy import ones
a = array([ [1,2,3], [4,5,6], [7,8,9] ])
b = a[:][ones((1,3))]
but it returns the following error message:
Traceback (most recent call last):
File "example_slice.py", line 6, in
b =a[:, ones((1,3))]
IndexError: arrays used as indices must be of integer (or boolean) type
EDIT: maybe ones should be replaced by zeros in this particular case but it is not the problem here. The question deals with the problem of giving a list containing the same index many times to the array a in order to get the same array b as the one computed with Matlab.
The MATLAB code can also be written (more idiomatically and more clearly) as:
b = repmat(a(:,1),1,3);
In NumPy you'd write:
b = np.tile(a[:,None,0],(1,3))
(Note the None needed to preserve the orientation of the vector extracted).
You could use list comprehension with np.full() to create arrays of certain values.
import numpy as np
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = np.array([np.full(len(i), i[0]) for i in a])
print(b)
Output:
[[1 1 1]
[4 4 4]
[7 7 7]]
In [568]: a = np.array([ [1,2,3], [4,5,6], [7,8,9] ])
In [569]: a[:,0]
Out[569]: array([1, 4, 7])
In [570]: a[:,[0,0,0]]
Out[570]:
array([[1, 1, 1],
[4, 4, 4],
[7, 7, 7]])
In [571]: a[:, np.zeros(3, dtype=int)] # int dtype to avoid your error
Out[571]:
array([[1, 1, 1],
[4, 4, 4],
[7, 7, 7]])
====
In [572]: np.zeros(3)
Out[572]: array([0., 0., 0.])
In [573]: np.zeros(3, int)
Out[573]: array([0, 0, 0])
Earlier numpy versions allowed float indices, but newer ones have tightened the requirement.
Related
I am trying to find a realization of accessing elements of numpy arrays corresponding to a feature of Matlab.
Suppose given a (2,2,2) Matlab matrix m in the form
m(:,:,1) = [1,2;3,4]
m(:,:,2) = [5,6;7,8]
Even though this is a 3-d array, Matlab allows accessing its column in the fashion like
m(:,1) = [1;3]
m(:,2) = [2;4]
m(:,3) = [5;7]
m(:,4) = [6;8]
I am curious to know that if numpy supports such indexing so that given the following array
m = array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
One can also access columns in the fashion as Matlab listed above.
My answer to this question is as following, suppose given the array listed as in the question
m = array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
One can create a list, I call it m_list in the form such that
m_list = [m[i][:,j] for i in range(m.shape[0]) for j in range(m.shape[-1])]
This will output m_list in the form such that
m_list = [array([1, 3]), array([2, 4]), array([7, 9]), array([ 8, 10])]
Now we can access elements of m_list exactly as the fashion as Matlab as listed in the question.
In [41]: m = np.arange(1,9).reshape(2,2,2)
In [42]: m
Out[42]:
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
Indexing the equivalent blocks:
In [47]: m[0,:,0]
Out[47]: array([1, 3])
In [48]: m[0,:,1]
Out[48]: array([2, 4])
In [49]: m[1,:,0]
Out[49]: array([5, 7])
In [50]: m[1,:,1]
Out[50]: array([6, 8])
We can reshape, to "flatten" one pair of dimensions:
In [84]: m = np.arange(1,9).reshape(2,2,2)
In [85]: m.reshape(2,4)
Out[85]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [87]: m.reshape(2,4)[:,2]
Out[87]: array([3, 7])
and throw in a transpose:
In [90]: m.transpose(1,0,2).reshape(2,4)
Out[90]:
array([[1, 2, 5, 6],
[3, 4, 7, 8]])
MATLAB originally was strictly 2d. Then sometime around v3.9 (2000) they allowed for more, but in a kludgy way. They added a way to index the trailing dimension as though it was multidimensional. In another recent SO I noticed that when reshaping to (2,2,1,1) the result remained (2,2). Trailing size 1 dimensions are squeeze out.
I suspect the m(:,3) is a consequence of that as well.
Testing a 4d MATLAB
>> m=reshape(1:36,2,3,3,2);
>> m(:,:,1)
ans =
1 3 5
2 4 6
>> reshape(m,2,3,6)(:,:,1)
ans =
1 3 5
2 4 6
>> m(:,17)
ans =
33
34
>> reshape(m,2,18)(:,17)
ans =
33
34
I have some 3-dimensional array and I want to take from each array the value at the same position and then copy it into an array with the name of the position.
E.g I have three 2x2x2 array and I want to take the value at position (1,1,1) of each of those matrices and copy it into an array called 111array. This array then should contain three values
The same should be done for all values and all positions in a matrix
I have a for loop which iterates over all values in one array. But I dont know how to save the result to an array in a correct way, that the array name displays the position number.
My first array is called b.
for i in range(b.shape[0]):
for j in range(b.shape[1]):
for k in range(b.shape[2]):
print(b[i,j,k])
Looking for help!
Looks like someone else beat me to an answer, but here is another way of doing it. I used a dictionary to corral all the arrays and return it from a function.
import numpy as np
b = np.array([0, 1, 2, 3, 4, 5, 6, 7])
b = np.reshape(b, (2, 2, 2))
print(b, type(b))
# [[[0 1],
# [2 3]],
# [[4 5],
# [6 7]]] <class 'numpy.ndarray'>
def myfunc(arr):
for i in range(b.shape[0]):
for j in range(b.shape[1]):
for k in range(b.shape[2]):
# Create a new array name from string parts.
name = "arr"+str(i)+str(j)+str(k)
print(name, b[i, j, k])
# Example: 'arr000', 0.
# Add a new key-value pair to the dictionary.
mydict.update({name: b[i,j,k]})
return(mydict)
mydict = {}
result = myfunc(b)
print(result)
# {'arr000': 0, 'arr001': 1, 'arr010': 2, 'arr011': 3, 'arr100': 4,
# 'arr101': 5, 'arr110': 6, 'arr111': 7}
# You would need to unpack the dictionary to use the arrays separately.
# use "mydict.keys()" to get all array names.
# "for key in keys" to loop through all array names.
# mydict['arr000'] will return the value 0.
Your question tags "numpy" but does not use it in your code snippet. If you are trying to stick with numpy, there is another method called "structured data array". It's similar to a dictionary in that "name" and "value" can be stored as paired sets in a numpy array. This keeps numpy's efficient memory management and fast calculation (C optimization). This matters if you are working with large datasets.
Also if working with numpy, there may be a way to use the index values in variable names.
Later, I will think of examples for both and update my answer if possible.
See if this is what you want. This is based on your example.
import numpy as np
from itertools import product
a = np.arange(8).reshape(2,2,2)
b = a + 1
c = a + 2
indices = product(range(2), repeat=3)
all_arrays = []
for i in indices:
suffix = ''.join(map(str,i))
array_name = 'array'+suffix
value = np.array([a[i],b[i],c[i]])
exec(array_name+'= value')
exec(f'all_arrays.append({array_name})')
for name in all_arrays:
print(name)
print('\n')
print(all_arrays)
print('\n')
print(array111)
print('\n')
print(array101)
Output:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
[5 6 7]
[6 7 8]
[7 8 9]
[array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4]), array([3, 4, 5]), array([4, 5, 6]), array([5, 6, 7]), array([6, 7, 8]), array([7, 8, 9])]
[7 8 9]
[5 6 7]
As others have pointed out, this seems like a weird request. But just for fun, here's a shorter solution:
In [1]: import numpy as np
...: A = np.arange(8).reshape((2,2,2))
...: B = 10*A
...: C = 100*A
In [2]: A
Out[2]:
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
In [3]: D = np.concatenate((A[None], B[None], C[None]))
...: for (a,b,c) in np.ndindex((2,2,2)):
...: locals()[f'array{a}{b}{c}'] = D[:,a,b,c]
...:
In [4]: array000
Out[4]: array([0, 0, 0])
In [5]: array001
Out[5]: array([ 1, 10, 100])
In [6]: array010
Out[6]: array([ 2, 20, 200])
In [7]: array011
Out[7]: array([ 3, 30, 300])
In [8]: array100
Out[8]: array([ 4, 40, 400])
In [9]: array101
Out[9]: array([ 5, 50, 500])
In [10]: array110
Out[10]: array([ 6, 60, 600])
In [11]: array111
Out[11]: array([ 7, 70, 700])
I have a numpy array:
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>> arr
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
I want to take a portion of the array based on indices (not slices):
ix = np.ix_([0, 1], [0, 2])
>> arr[ix]
[[1 3]
[6 8]]
And I want to modify those elements in the original array, which would work if I did this:
arr[ix] = 0
>> arr
[[ 0 2 0 4 5]
[ 0 7 0 9 10]]
But I only want to change them if they follow a specific condition, like if they are lesser than 5. I am trying this:
subarr = arr[ix]
subarr[subarr < 5] = 0
But it doesn't modify the original one.
>> arr
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
>> subarr
[[0 0]
[6 8]]
I am not sure why this is not working, since both accessing the array by indices with np.ix_ and using a mask subarr < 5 should return a view of the array, not a copy.
Fancy indexing returns a copy; hence your original array will not be updated. You can use numpy.where to update your values:
arr[ix] = np.where(arr[ix] < 5, 0, arr[ix])
array([[ 0, 2, 0, 4, 5],
[ 6, 7, 8, 9, 10]])
When you do:
arr[ix] = 0
The python interpreter does arr.__setitem__(ix, 0) hence modifying the original object.
On the second case subarr is independent of arr, it is a copy of the subset of arr. You then modify this copy.
This question already has answers here:
Indexing one array by another in numpy
(4 answers)
Closed 6 years ago.
consider the array a
np.random.seed([3,1415])
a = np.random.choice(np.arange(8), (2, 4), False)
print(a)
[[7 1 4 5]
[6 2 3 0]]
I'll create another array b that holds the results of np.argsort along each row.
b = a.argsort(1)
print(b)
[[1 2 3 0]
[3 1 2 0]]
I want to produce the sorted version of a by doing an appropriate slice of a with b. It should look like this
idx0 = np.arange(a.shape[0]).repeat(a.shape[1])
print(a[idx0, b.ravel()].reshape(a.shape))
[[1 4 5 7]
[0 2 3 6]]
question
what is the appropriate way to slice an 2 x 4 array with another 2 x 4 array in the fashion described above?
Advanced-indexing for the help -
a[np.arange(b.shape[0])[:,None],b]
Sample run -
In [10]: a
Out[10]:
array([[7, 1, 4, 5],
[6, 2, 3, 0]])
In [11]: b
Out[11]:
array([[1, 2, 3, 0],
[3, 1, 2, 0]])
In [12]: a[np.arange(b.shape[0])[:,None],b]
Out[12]:
array([[1, 4, 5, 7],
[0, 2, 3, 6]])
So I have a n x d matrix and an n x 1 vector. I'm trying to write a code to subtract every row in the matrix by the vector.
I currently have a for loop that iterates through and subtracts the i-th row in the matrix by the vector. Is there a way to simply subtract an entire matrix by the vector?
Thanks!
Current code:
for i in xrange( len( X1 ) ):
X[i,:] = X1[i,:] - X2
This is where X1 is the matrix's i-th row and X2 is vector. Can I make it so that I don't need a for loop?
That works in numpy but only if the trailing axes have the same dimension. Here is an example of successfully subtracting a vector from a matrix:
In [27]: print m; m.shape
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
Out[27]: (4, 3)
In [28]: print v; v.shape
[0 1 2]
Out[28]: (3,)
In [29]: m - v
Out[29]:
array([[0, 0, 0],
[3, 3, 3],
[6, 6, 6],
[9, 9, 9]])
This worked because the trailing axis of both had the same dimension (3).
In your case, the leading axes had the same dimension. Here is an example, using the same v as above, of how that can be fixed:
In [35]: print m; m.shape
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Out[35]: (3, 4)
In [36]: (m.transpose() - v).transpose()
Out[36]:
array([[0, 1, 2, 3],
[3, 4, 5, 6],
[6, 7, 8, 9]])
The rules for broadcasting axes are explained in depth here.
In addition to #John1024 answer, "transposing" a one-dimensional vector in numpy can be done like this:
In [1]: v = np.arange(3)
In [2]: v
Out[2]: array([0, 1, 2])
In [3]: v = v[:, np.newaxis]
In [4]: v
Out[4]:
array([[0],
[1],
[2]])
From here, subtracting v from every column of m is trivial using broadcasting:
In [5]: print(m)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
In [6]: m - v
Out[6]:
array([[0, 1, 2, 3],
[3, 4, 5, 6],
[6, 7, 8, 9]])
If you were just creating the vector that gets subtracted, you can also create it with
column_vector = np.array([0,1,2], ndmin=2).T
to get a column vector, which is only possible if it has dimension 2 or more.
One dimensional numpy arrays are always rows and cannot be transposed!
Then you can just do
each_column_of_matrix_minus_vector = matrix - column_vector
to subtract column_vector from every column of matrix.