i have the following numpy array extracted from a dataframe that i want to reshape
Extraction
x = c_df['x'].values
y = c_df['y'].values
z = c_df['z'].values
convert to array
x_y_z = np.array([x, y, z])
x_y_z
Array looks like this
array([[748260.27757, 748262.56478, 748263.52455, ..., 730354.86406,
730374.75 , 730388.45066],
[333346.25 , 333308.43521, 333296.25 , ..., 331466.13593,
331453.84365, 331446.25 ],
[ 2840. , 2840. , 2840. , ..., 2400. ,
2400. , 2400. ]])
basically i want to reshape it to be able to plot using plt.contourf which required Z to be 2D array
so i assume the array needs to be reshaped to something like
YYYYYYYYYY
Xzzzzzzzzzz
Xzzzzzzzzzz
Xzzzzzzzzzz
Xzzzzzzzzzz
is my assumption correct? if yes how to reshape the array.
If I understand you correctly Numpy Mgrid should be able to help you. However you might want some more explanation which can be found on this thread.
For the next time you can make it easier if you provide a simplified example of your problem.
Related
I'm a bit confused about how numpy's ndarray's min/max function with a given axis argument works.
import numpy as np
x = np.random.rand(2,3,4)
x.min(axis=0)
produces
array([[[0.4139181 , 0.24235588, 0.50214552, 0.38806332],
[0.63775691, 0.08142376, 0.69722379, 0.1968098 ],
[0.50496744, 0.54245416, 0.75325114, 0.67245846]],
[[0.79760899, 0.35819981, 0.5043491 , 0.75274284],
[0.54778544, 0.5597848 , 0.52325408, 0.66775091],
[0.71255276, 0.85835137, 0.60197253, 0.33060771]]])
array([[0.4139181 , 0.24235588, 0.50214552, 0.38806332],
[0.54778544, 0.08142376, 0.52325408, 0.1968098 ],
[0.50496744, 0.54245416, 0.60197253, 0.33060771]])
a 3x4 numpy array. I was thinking it would produce a size 2 array with the minimum for x[0] and x[1].
Can someone explain how this min function is working?
When you do x.min(axis=0), you request the min to be computed along the axis 0, which means this dimension is aggregated into a single value and thus the output has a (3,4) shape.
What you want is to compute the min on the combined axes 1 and 2:
x.min(axis=(1,2))
# array([0.38344152, 0.0202184 ])
You can also first reshape the array to combine those two dimensions, then compute the min along this new dimension (here, 1):
x.reshape(2,-1).min(axis=1)
# array([0.38344152, 0.0202184 ])
intermediate, reshaped, array:
x.reshape(2,-1)
array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ,
0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152,
0.79172504, 0.52889492],
[0.56804456, 0.92559664, 0.07103606, 0.0871293 , 0.0202184 ,
0.83261985, 0.77815675, 0.87001215, 0.97861834, 0.79915856,
0.46147936, 0.78052918]])
used input:
np.random.seed(0)
x = np.random.rand(2,3,4)
I have theses points that I need to be in a 2,N array. I tried using Numpy but couldn't get it to work. Any help will do.
30.90,401.01;96.91,384.22;80.70,340.79;67.38,337.90;55.80,300.26;72.01,299.68;92.28,310.10;99.81,331.53;92.86,340.79;108.49,380.16;108.49,382.48;168.13,373.22;352.83,380.74;474.43,393.48;535.80,409.69;560.70,369.16;550.28,362.79;562.44,333.26;592.55,328.05;605.28,329.79;594.28,363.95;572.28,370.32;549.12,413.17;714.14,469.33;716.45,479.17;203.45,479.75;0.00,479.17;0.22,365.11
You can use matrix:
s = '30.90,401.01;96.91,384.22;80.70,340.79'
np.array(np.matrix(s))
Output:
array([[ 30.9 , 401.01],
[ 96.91, 384.22],
[ 80.7 , 340.79]])
enter image description here
I am getting odd behavior with Jupyter/Numpy/Tranpose()/1D Arrays.
I found another post where transpose() will not transpose a 1D array, but in previous Jupyter notebooks, it does.
I have an example where it is inconsistent, and I do not understand:
Please see the picture attached of my jupyter notebook if 2 more or less identical arrays with 2 different outputs.
It seems it IS and IS NOT transposing the 1D array. Inconsistency is bad
outputs is (1000,) and (1,1000), why does this occur?
# GENERATE WAVEORM:
#---------------------------------------------------------------------------------------------------
N = 1000
fxc = []
fxn = []
for t in range(0,N):
fxc.append(A1*m.sin(2.0*pi*50.0*dt*t) + A2*m.sin(2.0*pi*120.0*dt*t))
fxn.append(A1*m.sin(2.0*pi*50.0*dt*t) + A2*m.sin(2.0*pi*120.0*dt*t) + 5*np.random.normal(u,std,size=1))
#---------------------------------------------------------------------------------------------------
# TAKE TRANSPOSE:
#---------------------------------
fc = np.transpose(np.array(fxc))
fn = np.transpose(np.array(fxn))
#---------------------------------
# PRINT DIMENSION:
#---------------------------------
print(fc.shape)
print(fn.shape)
#---------------------------------
Remove size=1 from your call to numpy.random.normal. Then it will return a scalar instead of a 1-d array of length 1.
For example,
In [2]: np.random.normal(0, 3, size=1)
Out[2]: array([0.47058288])
In [3]: np.random.normal(0, 3)
Out[3]: 4.350733438283539
Using size=1 in your code is a problem, because it results in fxn being a list of 1-d arrays (e.g. something like [[0.123], [-.4123], [0.9455], ...]. When NumPy converts that to an array, it has shape (N, 1). Transposing such an array results in the shape (1, N).
fxc, on the other hand, is a list of scalars (e.g. something like [0.123, 0.456, ...]). When converted to a NumPy array, it will be a 1-d array with shape (N,). NumPy's transpose operation swaps dimensions, but it does not create new dimensions, so transposing a 1-d array does nothing.
I am working with 3D arrays. A function takes a 2D array slice (matrix) from the user and visualizes it, using row and column names (the corresponding dimnames of the array). It works fine if the array dimensions are > 1.
However, if I have 1x1x1 array, I cannot extract the slice as a matrix:
a <- array(1, c(1,1,1), list(A="a", B="b", C="c"))
a[1,,]
[1] 1
It is a scalar with no dimnames, hence part of the necessary information is missing. If I add drop=FALSE, I don't get a matrix but retain the original array:
a[1,,,drop=FALSE]
, , C = c
B
A b
a 1
The dimnames are here but it is still 3-dimensional. Is there an easy way to get a matrix slice from 1x1x1 array that would look like the above, just without the third dimension:
B
A b
a 1
I suspect the issue is that when indexing an array, we cannot distinguish between 'take 1 value' and 'take all values' in case where 'all' is just a singleton...
The drop parameter of [ is all-or-nothing, but the abind package has an adrop function which will let you choose which dimension you want to drop:
abind::adrop(a, drop = 3)
## B
## A b
## a 1
Without any extra packages, the best I could do was to apply and return the sub-array:
apply(a, 1:2, identity)
# or
apply(a, 1:2, I)
# B
#A b
# a 1
I am working on a coding project and ran into a roadblock. I have a cell array of 1x3 matrices. (1,1) encodes the value to sort by, (1,2) and (1,3) encode coordinates that i need for reference later. Is there any way to sort the cell array by the (1,1) values in each matrix within the larger cell array?
CombList = {[1,1,1], [5,1,2];
[4,1,3], [3,1,2];
[2,1,4], [2,1,3]};
I would like to sort by the first values in each matrix within the cell array. Ideally, it would return:
CombList = [1,1,1], [2,1,3];
[2,1,4], [3,1,2];
[4,1,3], [5,1,2]};
...once sorted:)
Thank you!
I believe the following should work. The result will be a numeric array, hope that will work for you.
CombList = {[1,1,1], [5,1,2];
[4,1,3], [3,1,2];
[2,1,4], [2,1,3]}
CombMat = cell2mat(CombList);
CombMat(:, 1:3) = sortrows(CombMat(:, 1:3));
CombMat(:, 4:6) = sortrows(CombMat(:, 4:6));
You can use mat2cell to get convert it back to a cell array, like this:
CombCell = mat2cell(CombMat, [1 1 1], [3 3])
Zany one-liner based on sortrows:
CombList = reshape(mat2cell(sortrows(cell2mat(reshape(CombList,[],1))),ones(numel(CombList),1),numel(CombList{1})),2,[]).';