I have this piece of Python code that fills up a 2d matrix in a for loop
img=zeros((len(bins_x),len(bins_y)))
for i in arange(0,len(ix)):
img[ix[i]][iy[i]]=dummy[i]
Is it possible to use a vectorial operation for the last two lines of code? Is there also something that might speed up the calculation?
If ix, iy are index sequences:
img[ix, iy] = dummy
It might be useful to use numpy. In particular, the reshape method might be useful. Here is an example (adapted from the second link):
>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6])
>>> np.reshape(a, (3,2))
array([[1, 2],
[3, 4],
[5, 6]])
Related
How to convert this numpy array:
array = np.array([[np.array([1]),2],[np.array([1]),2],[np.array([1]),2]])
print (array)
[[array([1]) 2]
[array([1]) 2]
[array([1]) 2]]
to this numpy array:
print(array)
[[1 2]
[1 2]
[1 2]]
How can I achieve this without a for loop?
This is what I tried but it doesn't work:
first_col = array[:,0]
first_col = np.array([i[0] for i in first_col])
I don't even know if answering this is a good idea, since there must be a fundemental flaw in the design to even come up with a situation like this and the correct solution would be to fix that, rather than trying to mitigate the issue by converting the output.
Never-the-less, given the data, interestingly enough, it is possible to 'unpack' the structure using the numpy array method .astype():
import numpy as np
array = np.array([[np.array([1]),2],[np.array([1]),2],[np.array([1]),2]])
array = array.astype(int) # alt array = array.astype(float)
But, as stated above, this is treating the symptom of the problem, rather than the problem itself.
Try using map to convert it to a list:
map(lambda x: [list(x[0]), x[1]], array)
This is one way.
import numpy as np
arr = np.array([[np.array([1]),2],
[np.array([1]),2],
[np.array([1]),2]])
np.array([[i[0][0], i[1]] for i in arr])
# array([[1, 2],
# [1, 2],
# [1, 2]])
I'm new to Python and I need a dynamic matrix that I can manipulate adding more columns and rows to it. I read about numpy.matrix, but I can't find a method in there that does what I mentioned above. It occurred to me to use lists but I want to know if there is a simpler way to do it or a better implementation.
Example of what I look for:
matrix.addrow ()
matrix.addcolumn ()
matrix.changeValue (0, 0, "$200")
Am I asking for too much? If so, any ideas of how to implement something like that? Thanks!
You can do all of that in numpy (np.concatenate for example) or native python (my_list.append()). Which one is more efficient will depend on what else your program will do: numpy will be probably less efficient if all you are doing is adding / changing values one at a time, or do a lot of column 'adding' or 'removing'. However if you do matrix or column operations, the overhead of adding new columns to a numpy array maybe offset by the vectorized computation speed offered by numpy. So pick which ever you prefer, and if speed is an issue, then you need to experiment yourself with both approaches...
There are several ways to represent matrices in Python. You can use List of lists or numpy arrays. For example if you were to use numpy arrays
>>> import numpy as np
>>> a = np.array([[1,2,3], [2,3,4]])
>>> a
array([[1, 2, 3],
[2, 3, 4]])
To add a row
>>> np.vstack([a, [7,8,9]])
array([[1, 2, 3],
[2, 3, 4],
[7, 8, 9]])
To add a column
>>> np.hstack((a, [[7],[8]]))
array([[1, 2, 3, 7],
[2, 3, 4, 8]])
I have created a simple numpy array with shape (4, 2) called A.
import numpy as np
A = np.array([[1, 2],
[2, 2],
[3, 2],
[4, 2]])
I wanted to get the index of the rows where the first column is 2 and 3, so I did:
indices = np.where((A[:, 0] == 2) | (A[:, 0] == 3))[0]
Doing this I got an array with two items (1 and 2), which is what I wanted.
Now I would like to do this in C++ efficiently. Is there any way to do this using Eigen? I would like to avoid for loops.
Thanks.
Avoiding for loops in NumPy is admirable. But in fact all you're doing there is pushing the loops down into lower-level code implemented in C or Fortran.
There is simply no need to avoid loops in C++. On the contrary, loops are the clear and obvious way to solve this problem in C++. So use loops. They're blazing fast.
this is numpy in c++ it is using for loop to do [where]
https://github.com/dpilger26/NumCpp/blob/master/include/NumCpp/Functions/where.hpp
In the help resource for the multivariate normal sampling function in SciPy, they give the following example:
x,y = np.random.multivariate_normal(mean,cov,5000).T
My question is rather basic: what does the final .T actually do?
Thanks a lot, I know it is fairly simple, but it is hard to look in Google for ".T".
The .T accesses the attribute T of the object, which happens to be a NumPy array. The T attribute is the transpose of the array, see the documentation.
Apparently you are creating random coordinates in the plane. The output of multivariate_normal() might look like this:
>>> np.random.multivariate_normal([0, 0], [[1, 0], [0, 1]], 5)
array([[ 0.59589335, 0.97741328],
[-0.58597307, 0.56733234],
[-0.69164572, 0.17840394],
[-0.24992978, -2.57494471],
[ 0.38896689, 0.82221377]])
The transpose of this matrix is:
array([[ 0.59589335, -0.58597307, -0.69164572, -0.24992978, 0.38896689],
[ 0.97741328, 0.56733234, 0.17840394, -2.57494471, 0.82221377]])
which can be conveniently separated in x and y parts by sequence unpacking.
.T is just np.transpose().
Best of luck
Example
import numpy as np
a = [[1, 2, 3]]
b = np.array(a).T # ndarray.T The transposed array. [[1,2,3]] -> [[1][2][3]]
print("a=", a, "\nb=", b)
for i in range(3):
print(" a=", a[0][i]) # prints 1 2 3
for i in range(3):
print(" b=", b[i][0]) # prints 1 2 3
just wondering if there is any clever way to do the following.
I have an N dimensional array representing a 3x3 grid
grid = [[1,2,3],
[4,5,6],
[7,8,9]]
In order to get the first row I do the following:
grid[0][0:3]
>> [1,2,3]
In order to get the first column I would like to do something like this (even though it is not possible):
grid[0:3][0]
>> [1,4,7]
Does NumPy support anything similar to this by chance?
Any ideas?
Yes, there is something like that in Numpy:
import numpy as np
grid = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
grid[0,:]
# array([1, 2, 3])
grid[:,0]
# array([1, 4, 7])
You can use zip to transpose a matrix represented as a list of lists:
>>> zip(*grid)[0]
(1, 4, 7)
Anything more than just that, and I'd use Numpy.
To get the columns in Python you could use:
[row[0] for row in grid]
>>> [1,4,7]
You could rewrite your code for getting the row as
grid[0][:]
because [:] just copies the whole array, no need to add the indices.
However, depending on what you want to achieve, I'd say it's better to just write a small matrix class to hide this implementation stuff.