This question already has answers here:
Replace all elements of Python NumPy Array that are EQUAL to some values
(2 answers)
Replacing Numpy elements if condition is met
(6 answers)
Replace all elements of Python NumPy Array that are greater than some value
(8 answers)
Closed 4 days ago.
I have the following array:
array = array([4., 0., 2., 8., 8., 8., 8., 2., 0.])
and I would like to replace 0 by 0.5 so to get:
array = array([4., 0.5, 2., 8., 8., 8., 8., 2., 0.5])
so far I have tried:
array.replace(0.5, 0)
with little success:
AttributeError: 'numpy.ndarray' object has no attribute 'replace'
any idea on how to keep the array format but replace numbers inside it?
cheers!
You can boolean indexing to locate the items you want to replace and then just assign the value:
import numpy as np
array = np.array([4., 0., 2., 8., 8., 8., 8., 2., 0.])
array[array == 0.0] = 0.5
print(array)
# [4. 0.5 2. 8. 8. 8. 8. 2. 0.5]
Related
This question already has answers here:
Numpy: How to stack arrays in columns?
(3 answers)
Closed 2 years ago.
I tried np.hstack but this gave me an error. Is there an easy alternative method?
E.g.:
a = [1., 2., 3.]
b = [4., 5., 6.]
#insert code
result = [[1., 4.],
[2., 5.],
[3., 6.]]
Use numpy.column_stack
a = [1., 2., 3.]
b = [4., 5., 6.]
m = numpy.column_stack((a,b))
print(m)
output:
[[ 1., 4.],
[ 2., 5.],
[ 3., 6.]]
This question already has answers here:
Find the min/max excluding zeros in a numpy array (or a tuple) in python
(6 answers)
Closed 2 years ago.
I'm trying to find the smallest non-zero value in each row of a 2d numpy array but haven't been to find an elegant solution. I've looked at some other posts but none address the exact same problem e.g.
Minimum value in 2d array or Min/Max excluding zeros but in 1d array.
For example for the given array:
x = np.array([[3., 2., 0., 1., 6.], [8., 4., 5., 0., 6.], [0., 7., 2., 5., 0.]])
the answer would be:
[1., 4., 2.]
One way to do this is to re-assign the zeros to the np.inf, then take min per row:
np.where(x>0, x, np.inf).min(axis=1)
Output:
array([1., 4., 2.])
Masked arrays are designed exactly for these kind of purposes. You can leverage masking zeros from array (or ANY other kind of mask you desire) and do pretty much most of the stuff you do on regular arrays on your masked array now:
import numpy.ma as ma
mx = ma.masked_array(x, mask=x==0)
mx.min(1)
output:
[1.0 4.0 2.0]
I solved this way that's time complexity is o(n^2) .
import numpy as np
x = np.array([[3., 2., 0., 1., 6.], [8., 4., 5., 0., 6.], [0., 7., 2., 5., 0.]])
for i in range(len(x)) :
small=x[i][i]
for j in x[i] :
if (j!=0 and j<small):
small=j
print(small)
# example data
x = np.array([[3., 2., 0., 1., 6.], [8., 4., 5., 0., 6.], [0., 7., 2., 5., 0.]])
# set all the values inside the maxtrix which are equal to 0, to *inf*
# np.inf represents a very large number
# inf, stands for infinity
x[x==0] = np.inf
# grep the lowest value, in each array (now that there is no 0 value anymore)
np.min(x, axis=1)
This question already has answers here:
Numpy difference between neighboring elements
(2 answers)
Closed 2 years ago.
I have an array and I need to calculate delta value between adjacent elements, wondering is there a simpler way to do that like below?
import numpy as np
arr = np.array([1,2,3,4,5])
arr[1:len(arr), 0] = arr[1:(len(arr)), 0] - arr[0:(len(arr) - 1), 0]
You can use numpy.diff. See https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html for more info
arr = np.array([3., 9., 6., 5., 2., 5., 7., 5., 6., 4.])
np.diff(arr)
array([ 6., -3., -1., -3., 3., 2., -2., 1., -2.])
The question I got was this:
Create a 3 x 6-dimensional array, containing only float values.
So my solution was this:
import numpy as np
data = np.array([(1,2,3,4,5,6),(1,2,3,4,5,6),(1,2,3,4,5,6)])
data
I've tried using float(data), np.float(data) but they don't seem to work.
How do I convert them to floats or is there another way to solve this question? What's the limitations on the float function?
data.astype(float) should do what you need.
>>> data = np.array([(1,2,3,4,5,6),(1,2,3,4,5,6),(1,2,3,4,5,6)])
>>> data.astype(float)
array([[ 1., 2., 3., 4., 5., 6.],
[ 1., 2., 3., 4., 5., 6.],
[ 1., 2., 3., 4., 5., 6.]])
I have an array with 112 lines and 40 columns.
The format I need to convert to is 40 sets of 56 points each with x, y.
So, the first line has the x coordinates of the first point in each set. The second line has the x of the second points in the set... until the 56th line. After that I have the y's.
1st line : 40 x's
2nd line: 40 x's
...
56th line: 40 x's
57th line: 40 y's
...
112th line: 40 y's
Initially I thought about doing data.reshape(40, 56, 2) but that doesn't work because the values for x come before the values for y. If instead I had one line with x's and another with y's that would work though.
Edit:
for i in xrange(len(data)/2):
points.append(data[i])
points.append(data[i+len(data)/2])
points = np.array(points).T.reshape(len(data[0]), len(data)/2, 2)
return points
Just one idea:
[[(data[i,j], data[i+56,j]) for i in range(56)] for j in range(40)]
Returns a list of list of tuples.
EDIT: Your edit clarifies what you want. If you want pure Numpy, then does this work?
data.reshape(2, 56, 40).swapaxes(0,2)
I'll use a smaller array (8 x 5) so we can view the returned values easily.
import numpy as NP
# just create a smaller array to work with:
A = NP.random.randint(0, 10, 40).reshape(8, 5)
# split A in half, to separate x and y
p, q = NP.vsplit(A, 2)
# create a 'template' array of the correct dimension
xy = NP.zeros(2, 4, 5)
# now just map the x and y values onto the template
xy[0:,:] = p
xy[1:,:] = q
# the transformed matrix:
array([[[ 8., 5., 2., 5., 7.],
[ 2., 6., 0., 7., 2.],
[ 4., 4., 7., 5., 5.],
[ 8., 5., 2., 0., 5.]],
[[ 4., 8., 6., 9., 2.],
[ 2., 6., 5., 8., 1.],
[ 3., 2., 6., 2., 2.],
[ 1., 8., 0., 7., 3.]]])