Related
This is what the dataset looks like. Type: dic len: 7500[1]
I am having trouble trying to index specific values within the arrays! There are 46 vectors per array (23 x/y pairs). If possible I am trying to select a specific vector for all the 'sequences' in the dataset. Or ideally I am trying to separate the 46 vectors into 23 different variables to cluster them!
Some of the code that I have tried:
for sequence, vectors in df.items():
df1 = np.array_split(vectors, 23, axis=1)
print(df1)
The problem was I couldn't get python to recognize the x, y.
df1 = pd.DataFrame
I tried converting this into a dataframe but still could not index it.
'sequence_7303': array([[ 38.382774 , -1.6118518 , 3.3157895 , ..., 7.757037 ,
-26.928228 , -35.36 ],
[ 38.282295 , -1.6118518 , 3.3157895 , ..., 7.6562963 ,
-27.591389 , -35.22904 ],
[ 38.282295 , -1.7125926 , 3.3157895 , ..., 7.6562963 ,
-28.264595 , -35.108147 ],
...,
[ 51.84689 , -0.60444444, 49.33493 , ..., -16.42074 ,
51.997604 , -24.127409 ],
[ 51.94737 , -0.9066667 , 49.736843 , ..., -16.42074 ,
52.36938 , -24.973629 ],
[ 51.94737 , -1.1081481 , 50.038277 , ..., -16.32 ,
52.751198 , -25.81985 ]], dtype=float32),
'sequence_7302': array([[ 40.100502 , 1.3293233, -7.8090453, ..., 2.863158 ,
-27.753767 , 5.419549 ],
[ 39.994976 , 1.2270677, -8.125628 , ..., 2.7609022,
-28.830153 , 5.153684 ],
[ 39.889446 , 1.2270677, -8.547738 , ..., 2.7609022,
-29.906534 , 4.88782 ],
...,
[ 51.497486 , 3.2721806, 34.190952 , ..., -2.5563908,
42.569847 , 19.121803 ],
[ 51.603016 , 3.3744361, 34.296482 , ..., -2.5563908,
43.023617 , 19.172932 ],
[ 51.603016 , 3.4766917, 34.296482 , ..., -2.4541354,
43.477386 , 19.22406 ]], dtype=float32),
'sequence_1465': array([[ 33.635933 , -0.09883721, -23.654943 , ..., -3.8546512 ,
-24.752851 , -3.8546512 ],
[ 33.53612 , -0.09883721, -24.153992 , ..., -4.3488374 ,
-25.13213 , -4.7046514 ],
[ 33.43631 , 0. , -24.752851 , ..., -4.94186 ,
-25.511406 , -5.5546513 ],
...,
[ 50.40399 , 8.203488 , 2.8944867 , ..., 12.156977 ,
48.95675 , 30.175 ],
[ 50.40399 , 8.401163 , 2.9942966 , ..., 12.3546505 ,
49.276142 , 30.352907 ],
[ 50.30418 , 8.5 , 3.1939163 , ..., 12.552325 ,
49.60551 , 30.540699 ]], dtype=float32),
'sequence_642': array([[ 39.011856 , -1.8658537 , 1.7638341 , ..., -20.939024 ,
-17.534584 , 23.737804 ],
[ 39.011856 , -1.7621951 , 1.5563241 , ..., -20.835365 ,
-17.596838 , 23.665243 ],
[ 39.011856 , -1.6585366 , 1.3488142 , ..., -20.731709 ,
-17.659092 , 23.603048 ],
...,
[ 43.265812 , 2.1768293 , 0. , ..., -18.34756 ,
2.2618577 , 9.598781 ],
[ 43.369564 , 2.1768293 , -0.10375495, ..., -18.34756 ,
3.0503953 , 9.515854 ],
[ 43.369564 , 2.1768293 , -0.20750989, ..., -18.34756 ,
3.838933 , 9.432927 ]], dtype=float32),
I want to add a vector as the first column of my 2D array which looks like :
[[ 1. 0. 0. nan]
[ 4. 4. 9.97 1. ]
[ 4. 4. 27.94 1. ]
[ 2. 1. 4.17 1. ]
[ 3. 2. 38.22 1. ]
[ 4. 4. 31.83 1. ]
[ 3. 4. 41.87 1. ]
[ 2. 1. 18.33 1. ]
[ 4. 4. 33.96 1. ]
[ 2. 1. 5.65 1. ]
[ 3. 3. 40.74 1. ]
[ 2. 1. 10.04 1. ]
[ 2. 2. 53.15 1. ]]
I want to add an aray [] of 13 elements as the first column of the matrix. I tried with np.stack_column, np.append but it is for 1D vector or doesn't work because I can't chose axis=1 and only do np.append(peak_values, results)
I have a very simple option for you using numpy -
x = np.array( [[ 3.9427767, -4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.942777 , -4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.9427767 ,-4.297677 ],
[ 3.9427767, -4.297677 ],
[ 3.9427772 ,-4.297677 ]])
b = np.arange(10).reshape(-1,1)
np.concatenate((b.T, x), axis=1)
Output-
array([[ 0. , 3.9427767, -4.297677 ],
[ 1. , 3.9427767, -4.297677 ],
[ 2. , 3.9427767, -4.297677 ],
[ 3. , 3.9427767, -4.297677 ],
[ 4. , 3.942777 , -4.297677 ],
[ 5. , 3.9427767, -4.297677 ],
[ 6. , 3.9427767, -4.297677 ],
[ 7. , 3.9427767, -4.297677 ],
[ 8. , 3.9427767, -4.297677 ],
[ 9. , 3.9427772, -4.297677 ]])
Improving on this answer by removing the unnecessary transposition, you can indeed use reshape(-1, 1) to transform the 1d array you'd like to prepend along axis 1 to the 2d array to a 2d array with a single column. At this point, the arrays only differ in shape along the second axis and np.concatenate accepts the arguments:
>>> import numpy as np
>>> a = np.arange(12).reshape(3, 4)
>>> b = np.arange(3)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> b
array([0, 1, 2])
>>> b.reshape(-1, 1) # preview the reshaping...
array([[0],
[1],
[2]])
>>> np.concatenate((b.reshape(-1, 1), a), axis=1)
array([[ 0, 0, 1, 2, 3],
[ 1, 4, 5, 6, 7],
[ 2, 8, 9, 10, 11]])
For the simplest answer, you probably don't even need numpy.
Try the following:
new_array = []
new_array.append(your_array)
That's it.
I would suggest using Numpy. It will allow you to easily do what you want.
Here is an example of squaring the entire set. you can use something like nums[0].
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares # Prints "[0, 4, 16]"
I have a numpy array of shape (X,2):
array([[-0.42766381, -0.14222465],
[ 0.14321355, -0.11368064],
[ 0.0004935 , -0.11368064],
[ 0.0004935 , -0.08513739],
[ 0.1432126 , -0.02805013],
[ 0.0004935 , 0.05758133],
[ 0.14321165, 0.05758095],
[ 0.1432107 , 0.08612439],
[ 0.0004935 , 0.08612439],...
I would like to change the shape of this array to (X,3,2):
array([[[-0.42766381, -0.14222465]
[ 0.14321355, -0.11368064]
[ 0.0004935 , -0.11368064]]
[[ 0.0004935 , -0.08513739]
[ 0.1432126 , -0.02805013]
[ 0.0004935 , 0.05758133]]
[[ 0.14321165, 0.05758095]
[ 0.1432107 , 0.08612439]
[ 0.0004935 , 0.08612439]]]
thanks for your help!
This is exaclty what the numpy.reshape() method is for. Assuming that your array is A, then:
A.reshape(-1,3,2) will do it.
I have two arrays in NumPy:
a1 =
array([[ 262.99182129, 213. , 1. ],
[ 311.98925781, 271.99050903, 2. ],
[ 383. , 342. , 3. ],
[ 372.16494751, 348.83505249, 4. ],
[ 214.55493164, 137.01008606, 5. ],
[ 138.29714966, 199.75 , 6. ],
[ 289.75 , 220.75 , 7. ],
[ 239. , 279. , 8. ],
[ 130.75 , 348.25 , 9. ]])
a2 =
array([[ 265.78259277, 212.99705505, 1. ],
[ 384.23312378, 340.99707031, 3. ],
[ 373.66967773, 347.96688843, 4. ],
[ 217.91461182, 137.2791748 , 5. ],
[ 141.35340881, 199.38366699, 6. ],
[ 292.24401855, 220.83808899, 7. ],
[ 241.53366089, 278.56951904, 8. ],
[ 133.26490784, 347.14279175, 9. ]])
Actually there will be thousands of rows.
But as you can see, the third column in a2 does not have the value 2.0.
What I simply want is to remove from a1 the rows whose 3rd column values are not found in any row of a2.
What's the NumPy way/shortcut to do this fast?
One option is to use np.in1d to check whether each of the values in column 2 of a1 is in column 2 of a2 and use the resulting Boolean array to index the rows of a1.
You can do this as follows:
>>> a1[np.in1d(a1[:, 2], a2[:, 2])]
array([[ 262.99182129, 213. , 1. ],
[ 383. , 342. , 3. ],
[ 372.16494751, 348.83505249, 4. ],
[ 214.55493164, 137.01008606, 5. ],
[ 138.29714966, 199.75 , 6. ],
[ 289.75 , 220.75 , 7. ],
[ 239. , 279. , 8. ],
[ 130.75 , 348.25 , 9. ]])
The row in a1 with 2 in the third column in not in this array as required.
I am trying to perform operations on specific elements within a 3d array in python. Here is an example of the array:
[[[ 0.5 0.5 50. ]
[ 50.5 50.5 100. ]
[ 0.5 100.5 50. ]
[ 135. 90. 45. ]]
[[ 50.5 50.5 100. ]
[ 100.5 0.5 50. ]
[ 100.5 100.5 50. ]
[ 45. 90. 45. ]]
[[ 100.5 100.5 50. ]
[ 100.5 100.5 0. ]
[ 0.5 100.5 50. ]
[ 90. 0. 90. ]]
An example of what I need to to is take the three values seen in the array i.e. 0.5, 0.5, 50. and take the first element from the 4th row i.e. 135. and send those four elements into a function. the function then returns new values for the 3 elements which need to be put into the array.
I am quite new to python so I'm having trouble getting it to work. Should I be making a loop? or something else?
Thanks
Nick
An attempt at a solution:
b = shape(a)
triangles = b[0]
for k in range(0,triangles):
for i in range(0,2):
a[k,i,:] = VectMath.rotate_x(a[k,i,0],a[k,i,1],a[k,i,2],a[k,3,2])
You can make your VectMath.rotate_x function to rotate an array of vector, then by using slice to get & put data in a:
a = np.array(
[[[ 0.5, 0.5, 50., ],
[ 50.5, 50.5, 100., ],
[ 0.5, 100.5, 50., ],
[ 135. , 90. , 45., ]],
[[ 50.5, 50.5, 100., ],
[ 100.5, 0.5, 50., ],
[ 100.5, 100.5, 50., ],
[ 45. , 90. , 45., ]],
[[ 100.5, 100.5, 50., ],
[ 100.5, 100.5, 0., ],
[ 0.5, 100.5, 50., ],
[ 90. , 0. , 90., ]]])
def rotate_x(v, deg):
r = np.deg2rad(deg)
c = np.cos(r)
s = np.sin(r)
m = np.array([[1, 0, 0],
[0, c,-s],
[0, s, c]])
return np.dot(m, v)
vectors = a[:, :-1, :]
angles = a[:, -1, 0]
for i, (vec, angle) in enumerate(zip(vectors, angles)):
vec_rx = rotate_x(vec.T, angle).T
a[i, :-1, :] = vec_rx
print a
output:
[[[ 5.00000000e-01 -3.57088924e+01 -3.50017857e+01]
[ 5.05000000e+01 -1.06419571e+02 -3.50017857e+01]
[ 5.00000000e-01 -1.06419571e+02 3.57088924e+01]
[ 1.35000000e+02 9.00000000e+01 4.50000000e+01]]
[[ 5.05000000e+01 -3.50017857e+01 1.06419571e+02]
[ 1.00500000e+02 -3.50017857e+01 3.57088924e+01]
[ 1.00500000e+02 3.57088924e+01 1.06419571e+02]
[ 4.50000000e+01 9.00000000e+01 4.50000000e+01]]
[[ 1.00500000e+02 -5.00000000e+01 1.00500000e+02]
[ 1.00500000e+02 6.15385017e-15 1.00500000e+02]
[ 5.00000000e-01 -5.00000000e+01 1.00500000e+02]
[ 9.00000000e+01 0.00000000e+00 9.00000000e+01]]]
If there are many triangle, it maybe faster if we can rotate all the vectors without python for loop.
Here I do the rotate calculation by expand the matrix product:
x' = x
y' = cos(t)*y - sin(t)*z
z' = sin(t)*y + cos(t)*z
So we can vectorize these formulas:
a2 = np.array(
[[[ 0.5, 0.5, 50., ],
[ 50.5, 50.5, 100., ],
[ 0.5, 100.5, 50., ],
[ 135. , 90. , 45., ]],
[[ 50.5, 50.5, 100., ],
[ 100.5, 0.5, 50., ],
[ 100.5, 100.5, 50., ],
[ 45. , 90. , 45., ]],
[[ 100.5, 100.5, 50., ],
[ 100.5, 100.5, 0., ],
[ 0.5, 100.5, 50., ],
[ 90. , 0. , 90., ]]])
vectors = a2[:, :-1, :]
angles = a2[:, -1:, 0]
def rotate_x_batch(vectors, angles):
rad = np.deg2rad(angles)
c = np.cos(rad)
s = np.sin(rad)
x = vectors[:, :, 0]
y = vectors[:, :, 1]
z = vectors[:, :, 2]
yr = c*y - s*z
zr = s*y + c*z
vectors[:, :, 1] = yr
vectors[:, :, 2] = zr
rotate_x_batch(vectors, angles)
print np.allclose(a, a2)