Suppose inputs are like below.
indexSet1 = [0,1,2]
indexSet2 = [1,2]
A = [[1,2,3],[4,5,6],[7,8,9]]
Then I want to get a matrix whose height is 3 and width is 2 respectively and elements corresponds to indexSet1's value and indexSet2's one.
In short, I want to a array [[2,3],[4,5],[7,8]] from A by indexSet1 and indexSet2.
When I code like below, I can not get my desire result.
>>> import numpy as np
>>> np.array(A)[np.array(indexSet1),np.array(indexSet2)]
array([5, 9])
Can anyone know wise methods?
Sorry for my poor English.
And thank you in advance.
Using nested list comprehension:
>>> indexSet1 = [0,1,2]
>>> indexSet2 = [1,2]
>>> A = [[1,2,3],[4,5,6],[7,8,9]]
>>> [[A[i][j] for j in indexSet2] for i in indexSet1]
[[2, 3], [5, 6], [8, 9]]
In NumPy you can do something like this:
>>> A = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> A[np.array(indexSet1)[:, None], indexSet2]
array([[2, 3],
[5, 6],
[8, 9]])
Related
Hello I'm new in python I couldn't solve my problem. Suppose I have a list (a), this list has many matricies which is the same shape. I want to get one matrix that result of mean of each elements.
here is the list and its elements:
a[0]=[1 2 3]
a[1]=[3 4 5]
a[2]=[6 7 8]
Here is the desired matrix:
mean=[10/3 13/3 16/3]
Mean of each element of a list of matrices
Actually, this answer is good for me but it's for the R, not python. Sorry if I made a mistake while asking a question.
Using Python list comprehension
a = [[1, 2, 3],
[3, 4, 5],
[6, 7, 8]]
mean = [sum(row)/len(row) for row in zip(*a)] # use zip(*a) to transpose matrix
# since sum along columns
# by summing rows of transposed a
# [3.3333333333333335, 4.333333333333333, 5.333333333333333]
Here is a pure python solution that would work with any matrice dimension:
matrice = [
[1, 2, 3],
[3, 4, 5],
[6, 7, 8]
]
def mean_mat(mat):
dim_axis_0 = mat.__len__()
mean = [0 for i in range(dim_axis_0)]
for vector in mat:
for i, value in enumerate(vector):
mean[i] += (value / dim_axis_0)
return mean
print(mean_mat(matrice))
>>> [3.333333333333333, 4.333333333333334, 5.333333333333334]
However, as user1740577 pointed out, you should checkout the Numpy library.
try this:
import numpy as np
a= [[1,2,3],[3,4,5],[6,7,8]]
np.mean(a, axis=0)
# array([3.33333333, 4.33333333, 5.33333333])
Suppose I have a torch tensor
import torch
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
and a list
b = [0,2]
Is there a built-in method to extract the rows 0 and 2 and put them in a new tensor:
tensor([[1,2,3],
[7,8,9]])
In particular, is there a function that look likes this:
extract_rows(a,b) -> c
where c contains desired rows. Sure, this can done by a for loop, but a built-in method is in general faster.
Note that the example is only an example, there could be dozens of indexes in the list, and hundreds of rows in the tensor.
have a look at torch builtin index_select() method. It would be helpful to you.
or
You can do this using slicing.
tensor = [[1,2,3],
[4,5,6],
[7,8,9]]
new_tensor = tensor[0::2]
print(new_tensor)
Output:
[[1, 2, 3], [7, 8, 9]]
Simply a[b] would work
import torch
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
b = [0,2]
a[b]
tensor([[1, 2, 3],
[7, 8, 9]])
So I'm pretty new to numpy, and I'm trying working on a project, but have encountered an error that I can't seem to solve.
Imagine we had an NDarray in the following format
[4,5,6,1]
[3,5,2,0]
[4,7,3,1]
How would I split it into two parts such that the first part is:
[4,5,6]
[3,5,2]
[4,7,3]
and the second part is
[1,0,1]
I know the solution must be pretty simple but I can't seem to figure it out
Thanks in advance!
Try:
a = np.array([[4,5,6,1],
[3,5,2,0],
[4,7,3,1]])
b,c = a[:,:-1], a[:,-1]
This uses numpy's slicing to keep all rows and split the columns on the last one.
>>> import numpy as np
>>> a=np.array([[4,5,6,1],[3,5,2,0],[4,7,3,1]])
>>> a
array([[4, 5, 6, 1],
[3, 5, 2, 0],
[4, 7, 3, 1]])
>>> b=a[:,0:3]
>>> b
array([[4, 5, 6],
[3, 5, 2],
[4, 7, 3]])
>>> c=a[:,3]
>>> c
array([1, 0, 1])
>>>
This is something called array slice in python, not too much about numpy.
For more details about array slice, see Explain Python's slice notation
Say we have a list:
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Then I want to create a new list through slicing away the first element in each element in the list:
b = a[][1:]
Obviously the above doesn't work, but what I want b to be now is:
[[2, 3], [5, 6], [8, 9]]
Of course I can just loop through it, but all I want to know is if it's possible to do it in any similar way of what I tried so wrongly to do above. Also, preferably is there a better/good way of doing this with numpy, in which case it doesn't need to be done through slicing the way I attempted to do it?
You can use list comprehensions:
b = [x[1:] for x in a]
Demo:
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = [x[1:] for x in a]
>>> b
[[2, 3], [5, 6], [8, 9]]
>>>
Using numpy indexing/slicing notation, you use commas to delimit the slice for each dimension:
import numpy as np
a = np.array([[1,2,3],[1,2,3],[1,2,3]])
print a[:,1:]
output:
[[2 3]
[2 3]
[2 3]]
For additional reading on numpy indexing:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
in python 3 you can also use *
b = [x for _,*x in a]
this approach is more flexible since you can for example left first and last elements of the inside list, no matter how long is the list:
b = [first,last for first,*middle,last in a]
I have a 2D list something like
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
and I want to convert it to a 2d numpy array. Can we do it without allocating memory like
numpy.zeros((3,3))
and then storing values to it?
Just pass the list to np.array:
a = np.array(a)
You can also take this opportunity to set the dtype if the default is not what you desire.
a = np.array(a, dtype=...)
just use following code
c = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Then it will give you
you can check shape and dimension of matrix by using following code
c.shape
c.ndim
np.array() is even more powerful than what unutbu said above.
You also could use it to convert a list of np arrays to a higher dimention array, the following is a simple example:
aArray=np.array([1,1,1])
bArray=np.array([2,2,2])
aList=[aArray, bArray]
xArray=np.array(aList)
xArray's shape is (2,3), it's a standard np array. This operation avoids a loop programming.
I am using large data sets exported to a python file in the form
XVals1 = [.........]
XVals2 = [.........]
Each list is of identical length. I use
>>> a1 = np.array(SV.XVals1)
>>> a2 = np.array(SV.XVals2)
Then
>>> A = np.matrix([a1,a2])