Using Python 2.7 and Numpy.
I have a B/W image stored in array(20,20) and I would like to convert it to an array(400). How can this be done in Python if I have many images, that is array(x,20,20)?
Thanks a lot!
EDIT: Thanks a lot. I got the problem wrong at the beginning, thus I was not able to figure out this simple piece of code.
I think numpy.flatten() is what you are looking for
>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.flatten.html
You can use np.ravel to get a 1D view of the array if possible; otherwise a copy is returned. See the linked documentation for the definition of 'if possible'.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ravel())
# [1 2 3 4 5 6]
Related
Please feel free to let me know whether it is a duplicate question.
From
in_arr1 = np.array([[2,0], [-6,0], [3,0]])
How can I get:
diffInElements = [[5,0]] ?
I tried np.diff(in_arr1, axis=0) but it does not generate what I want.
is there a NumPy function I can use ?
Cheers,
You can negate and then sum all but the first value, and then add the first value:
diff = (-a[1:]).sum(axis=0) + a[0]
Output:
>>> diff
array([5, 0])
You want to subtract the remaining rows from the first row. The straightforward answer does just that:
>>> arr = np.array([[2, 1], [-6, 3], [3, -4]])
>>> arr[0, :] - arr[1:, :].sum(0)
array([5, 2])
There is also, however, a more advanced option making use of the somewhat obscure reduce() method of numpy ufuncs:
>>> np.subtract.reduce(arr, axis=0)
array([5, 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 need to have each numpy file from a folder like an array with 1 dimension; This is my code:
path ='E:\\t'
traces= os.listdir(path)
print("tempTracesHW_Creation=", tempTracesHW)
for i in range(len(traces)):
HW = tempHW[i]
for trace in os.listdir(path):
file_array= np.load(os.path.join(path, trace))
print file_array
tempTracesHW[HW].append(file_array)
The result of file_array is:
file_array= [[-0.0006447 -0.00094265 -0.0012406 ..., -0.02096185 -0.0210646
-0.02114679]]
But what I want is:
file_array= [-0.0006447 -0.00094265 -0.0012406 ..., -0.02096185 -0.0210646
-0.02114679]
I would be very grateful if you could help me please?
The numpy load function loads the file and return the array.
The file_array is two dimensional because your input to numpy.load is two dimensional.
Check the trace file, you need to make it one-dimensional array.
For example:
example = numpy.save("example",numpy.array([1,2,3]))
result = numpy.load("example.npy")
print result
[1,2,3]
See if this helps.
More of the code snippet would have help understanding your problem.(About the Trace file)
You can use flatten to turn this (1, x) array into a (x, ) array. flatten can be used differently, but in this case, it will do what you're looking for.
>>> import numpy as np
>>> a = np.array([[1, 2, 3, 4, 5]])
>>> a
array([[1, 2, 3, 4, 5]])
>>> a.shape
(1, 5)
>>> a.flatten()
array([1, 2, 3, 4, 5])
>>> a.flatten().shape
(5,)
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.
Good day to all.
Help me please to understand theory of function scipy.ndimage.convolve for 1D arrays. I know the formula from http://lagrange.univ-lyon1.fr/docs/scipy/0.17.1/generated/scipy.ndimage.convolve.html
C_i = \sum_j{I_{i+j-k} W_j},
but i can't understand, how can I get results manually.
For example: test_1 = scipy.ndimage.convolve([1, 2, 3], [1, 2, 3, 4, 5])
result is [24 24 30]
Or test_2 = scipy.ndimage.convolve([1, 2, 3], [3, 4, 5])
result is [15 22 31]
If I write here all attempts that I have made, it will take a lot of space.
Give me please step by step instructions on what to do with these examples manually.
Two tricky things going on here
1) the ndimage has this flag called "mode" which is set to "reflect" by default
2) two is that convolutions internally reverse one of the inputs
try comparing this piece of code
scipy.ndimage.convolve([1, 2, 3][::-1], [1, 2, 3, 4, 5],mode='constant')
to your by hand solution. (get rid of the "[::-1]" if you've already accounted for the reversal)