I am struggling to understand two things from below matrix (numpy arrays):
How can I deduce from the np.stack(cart_indexing, axis=1) function that there are 5 dimensions? I am struggling to conceptually understand the (5, 2, 5) part. I see it as (rows, column numbers, dimensions).
What does axis = -1 really mean? How to understand it?
x = np.linspace(start=-10, stop=0, num=5, endpoint=True)
y = np.linspace(start=1, stop=10, num=5)
cart_indexing = np.meshgrid(x, y, indexing="xy") # cartesian indexing
>> [array([[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ]]),
array([[ 1. , 1. , 1. , 1. , 1. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ],
[ 7.75, 7.75, 7.75, 7.75, 7.75],
[10. , 10. , 10. , 10. , 10. ]])]
np.stack(cart_indexing, axis=0)
>> array([[[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ]],
[[ 1. , 1. , 1. , 1. , 1. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ],
[ 7.75, 7.75, 7.75, 7.75, 7.75],
[ 10. , 10. , 10. , 10. , 10. ]]])
np.stack(cart_indexing, axis=1)
>> array([[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 1. , 1. , 1. , 1. , 1. ]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 7.75, 7.75, 7.75, 7.75, 7.75]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 10. , 10. , 10. , 10. , 10. ]]])
np.stack(cart_indexing, axis=1).shape
>> (5, 2, 5)
np.stack(cart_indexing, axis=-1)
>> array([[[-10. , 1. ],
[ -7.5 , 1. ],
[ -5. , 1. ],
[ -2.5 , 1. ],
[ 0. , 1. ]],
[[-10. , 3.25],
[ -7.5 , 3.25],
[ -5. , 3.25],
[ -2.5 , 3.25],
[ 0. , 3.25]],
[[-10. , 5.5 ],
[ -7.5 , 5.5 ],
[ -5. , 5.5 ],
[ -2.5 , 5.5 ],
[ 0. , 5.5 ]],
[[-10. , 7.75],
[ -7.5 , 7.75],
[ -5. , 7.75],
[ -2.5 , 7.75],
[ 0. , 7.75]],
[[-10. , 10. ],
[ -7.5 , 10. ],
[ -5. , 10. ],
[ -2.5 , 10. ],
[ 0. , 10. ]]])
np.stack(cart_indexing, axis=-1).shape
>> (5, 5, 2)
It's not clear what you mean by
there are 5 dimensions
None of your arrays have 5 dimensions. You start with a list of 2 arrays with 2 dimensions;
for i in cart_indexing:
print(f"Shape:{i.shape}; Dimensions:{i.ndim}")
Shape:(5, 5); Dimensions:2
Shape:(5, 5); Dimensions:2
Notice here how you have 5 and 5 and 2.
Then, the axis parameter in your stack comes into play:
for i in range(3):
print(f"Stacked on axis {i} my array has {np.stack(cart_indexing, axis=i).ndim} dimensions and a shape of {np.stack(cart_indexing, axis=i).shape}")
Stacked on axis 0 my array has 3 dimensions and a shape of (2, 5, 5) #the 2 is in the (axis=)0th position
Stacked on axis 1 my array has 3 dimensions and a shape of (5, 2, 5) #the 2 is in the (axis=)1st position
Stacked on axis 2 my array has 3 dimensions and a shape of (5, 5, 2) #the 2 is in the (axis=)2nd position
Put another way, stacking adds a dimension along which the arrays are stacked. The axis parameter determines which dimension is created during stacking/along which dimension they are stacked
What does axis = -1 really mean?
Why does print("Hello world"[-1]) print "d"?
Or, in other words, if we want to count our dimensions from last to first:
for i in range(-3,0):
print(f"Stacked on axis {i} my array has {np.stack(cart_indexing, axis=i).ndim} dimensions and a shape of {np.stack(cart_indexing, axis=i).shape}")
Stacked on axis -3 my array has 3 dimensions and a shape of (2, 5, 5) #dimension that is third from last
Stacked on axis -2 my array has 3 dimensions and a shape of (5, 2, 5) #dimension that is second from last
Stacked on axis -1 my array has 3 dimensions and a shape of (5, 5, 2) #last dimesnion
Related
how to replace 4th and 5th column values in utl by new_values array and keep the remaining columns as it is
utl = np.array([[ 3. , 134.4 , 17. , 135.05 , 22. , 135.25 , 0.04 ],
[ 12. , 134.3 , 17. , 135.05 , 22. , 135.8 , 0.15 ]])
new_values=np.array([[ 27., 135.45],
[ 27., 136.55]])
i tried this but it does not work
# utl[:,[4,5]] = new_values
# utl[:,4] = new_values[:,0]
output must be
#values changed
[[ 3. , 134.4 , 17. , 135.05 , | 27. , 135.45 |, 0.04 ],
[ 12. , 134.3 , 17. , 135.05 , | 27. , 136.55 |, 0.15 ]])
this works fine, as expected:
utl[:, [4,5]] = new_values
output:
array([[ 3. , 134.4 , 17. , 135.05, 27. , 135.45, 0.04],
[ 12. , 134.3 , 17. , 135.05, 27. , 136.55, 0.15]])
Input:
array([[ 1. , 5. , 1. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ]])
Expected Output:
array([[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 1. , 5. , 1. ]])
i tried doing the below:
for i in M:
ls = i.mean()
x = np.append(i,ls)
print(x) #found the mean
After this i am unable to arrange each column based on the mean value in each row. All i can do
is to arrange each row in descending order but that is not what i wanted.
You can do this:
In [405]: row_idxs = np.argsort(np.mean(a * -1, axis=1))
In [406]: a[row_idxs, :]
Out[406]:
array([[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 1. , 5. , 1. ]])
Using argsort will sort the indices. Multiplying by -1 allows you to get descending order.
I'm encountering an issue since hours, I don't understand why the V matrix below doesn't equal the Identity matrix:
A = np.random.randint(50, size=(100, 2))
V = A.dot(A.T)
D = V.dot(inv(V))
D
The result I found is below either:
array([[ 3.26611328, 7.87890625, 14.1953125 , ..., 2. ,
-5. , -24. ],
[ -5.91061401, -26.05834961, 5.30126953, ..., -10. ,
8. , -16. ],
[ -2.64431763, 3.55639648, 3.10107422, ..., -0.5 ,
-5. , -4. ],
...,
[ -2.62512207, -7.78222656, 10.26367188, ..., -6. ,
18. , 0. ],
[ -3.0625 , 14. , -4. , ..., -0.0625 ,
0. , 8. ],
[ 2. , -7. , 16. , ..., -7.5 ,
-8. , -4. ]])
Thank you for your help
I've found my issue:
I was trying to find the inv() of a matrix which det(matrix) = 0, that's why the calculus wasn't correct.
D = V.T.dot(V)
inv(D).dot(D)
then I find the Identity matrix
Thank you
Habib
I have this array:
I need create a new array like this:
I guess I need use a conditional, but I don't know how create an array with 7 columns, based on values of a 5 columns array.
If anyone could help me, I thank!
I'm going to assume you want to convert your last column into one hot concodings and then concat it to your original array. You can initialise an array of zeros, and then set the appropriate indices to 1. Finally concat the OHE array to your original.
MCVE:
print(arr)
array([[ -9.95, 15.27, 9.08, 1. ],
[ -6.81, 11.87, 8.38, 2. ],
[ -3.02, 11.08, -8.5 , 1. ],
[ -5.73, -2.29, -2.09, 2. ],
[ -7.01, -0.9 , 12.91, 2. ],
[-11.64, -10.3 , 2.09, 2. ],
[ 17.85, 13.7 , 2.14, 0. ],
[ 6.34, -9.49, -8.05, 2. ],
[ 18.62, -9.43, -1.02, 1. ],
[ -2.15, -23.65, -13.03, 1. ]])
c = arr[:, -1].astype(int)
ohe = np.zeros((c.shape[0], c.max() + 1))
ohe[np.arange(c.shape[0]), c] = 1
arr = np.hstack((arr[:, :-1], ohe))
print(arr)
array([[ -9.95, 15.27, 9.08, 0. , 1. , 0. ],
[ -6.81, 11.87, 8.38, 0. , 0. , 1. ],
[ -3.02, 11.08, -8.5 , 0. , 1. , 0. ],
[ -5.73, -2.29, -2.09, 0. , 0. , 1. ],
[ -7.01, -0.9 , 12.91, 0. , 0. , 1. ],
[-11.64, -10.3 , 2.09, 0. , 0. , 1. ],
[ 17.85, 13.7 , 2.14, 1. , 0. , 0. ],
[ 6.34, -9.49, -8.05, 0. , 0. , 1. ],
[ 18.62, -9.43, -1.02, 0. , 1. , 0. ],
[ -2.15, -23.65, -13.03, 0. , 1. , 0. ]])
One-line version of #COLDSPEED using the np.eye trick:
np.hstack([arr[:,:-1], np.eye(arr[:,-1].astype(int).max() + 1)[arr[:,-1].astype(int)]])
Let's say I have a 10 x 20 matrix of values (so 200 data points)
values = np.random.rand(10,20)
with a known regular spacing between coordinates so that the x and y coordinates are defined by
coord_x = np.arange(0,5,0.5) --> gives [0.0,0.5,1.0,1.5...4.5]
coord_y = np.arange(0,5,0.25) --> gives [0.0,0.25,0.50,0.75...4.5]
I'd like to get an array representing each coordinates points so that
the shape of the array is (200,2), 200 being the total number of points and the extra dimension simply representing x and y such as
coord[0][0]=0.0, coord[0][1]=0.0
coord[1][0]=0.0, coord[1][1]=0.25
coord[2][0]=0.0, coord[2][1]=0.50
...
coord[19][0]=0.0, coord[19][1]=5.0
coord[20][0]=0.5, coord[20][1]=0.0
coord[21][0]=0.5, coord[21][1]=0.25
coord[22][0]=0.5, coord[22][1]=0.50
...
coord[199][0]=4.5, coord[199][1]=4.5
That would a fairly easy thing to do with a double for loop, but I wonder if there is more elegant solution using built-in numpy (or else) functions.
?
I think meshgrid may be what you're looking for.
Here's an example, with smaller number of datapoints:
>>> from numpy import fliplr, dstack, meshgrid, linspace
>>> x, y, nx, ny = 4.5, 4.5, 3, 10
>>> Xs = linspace(0, x, nx)
>>> Ys = linspace(0, y, ny)
>>> fliplr(dstack(meshgrid(Xs, Ys)).reshape(nx * ny, 2))
array([[ 0. , 0. ],
[ 0. , 2.25],
[ 0. , 4.5 ],
[ 0.5 , 0. ],
[ 0.5 , 2.25],
[ 0.5 , 4.5 ],
[ 1. , 0. ],
[ 1. , 2.25],
[ 1. , 4.5 ],
[ 1.5 , 0. ],
[ 1.5 , 2.25],
[ 1.5 , 4.5 ],
[ 2. , 0. ],
[ 2. , 2.25],
[ 2. , 4.5 ],
[ 2.5 , 0. ],
[ 2.5 , 2.25],
[ 2.5 , 4.5 ],
[ 3. , 0. ],
[ 3. , 2.25],
[ 3. , 4.5 ],
[ 3.5 , 0. ],
[ 3.5 , 2.25],
[ 3.5 , 4.5 ],
[ 4. , 0. ],
[ 4. , 2.25],
[ 4. , 4.5 ],
[ 4.5 , 0. ],
[ 4.5 , 2.25],
[ 4.5 , 4.5 ]])
I think you meant coord_y = np.arange(0,5,0.25) in your question. You can do
from numpy import meshgrid,column_stack
x,y=meshgrid(coord_x,coord_y)
coord = column_stack((x.T.flatten(),y.T.flatten()))