Related
I have a list of shape (8,20,400). How can I remove that 8 or is it possible just to specify (20,400) as input dimension? thank you
Here is the numpy file
[array([[-2.95904625e-04, 6.40423603e-04, 6.00454747e-04, ...,
-2.27658681e-03, 4.57428713e-02, 2.19978483e-02],
[-6.20380223e-05, 4.70707138e-04, -8.86019713e-03, ...,
2.79925674e-02, 1.03238509e-02, 3.22103941e-02],
[-2.43267652e-04, 4.08970515e-04, -1.00202744e-02, ...,
1.91848757e-03, 8.25676575e-03, 2.52707651e-02],
...,
[ 1.36244897e-04, 1.28135132e-04, -2.24724998e-03, ...,
1.00482352e-02, -5.16041088e-03, 1.67575841e-02],
[-4.56173476e-04, 4.15697708e-04, -2.91441041e-03, ...,
6.27152348e-02, 5.86943632e-02, 3.60700986e-02],
[ 2.97331762e-04, 3.08865978e-04, -2.51690796e-03, ...,
1.01896998e-01, 1.18917078e-02, -1.39016820e-02]]), array([[ 2.01335059e-05, 9.97422893e-05, 1.33110081e-04, ...,
-6.56137938e-01, 2.19010739e-01, -5.99931850e-01],
[ 4.70135697e-05, 6.19096340e-05, 2.01980818e-03, ...,
2.08184651e-01, -2.32254140e-02, 2.59060825e-01],
[ 1.58539360e-04, 5.70696560e-05, 3.40425631e-03, ...,
2.40470642e-02, 3.40782234e-02, 4.70127584e-02],
...,
[ 1.47612366e-04, 3.13279823e-05, 3.65396557e-03, ...,
3.69262189e-03, 8.34838873e-02, -3.89654154e-03],
[ 2.46732289e-05, 3.03740157e-05, 9.34068810e-04, ...,
9.21966957e-02, -4.49042892e-02, 1.08487106e-01],
[-4.69590970e-05, 3.79340001e-05, -1.73730729e-03, ...,
7.42343781e-02, 2.04405703e-01, 6.21275782e-02]]), array([[ 6.48131747e-04, 3.30629899e-04, -1.79023262e-02, ...,
1.80307755e-02, 2.02918323e-02, 2.66390833e-02],
[ 7.42727070e-04, 2.43741144e-04, -3.09066842e-02, ...,
-1.43030498e-02, -8.39430839e-04, -2.06224047e-02],
[ 7.91216410e-04, 1.74964451e-04, -3.36439668e-03, ...,
1.24927181e-03, -3.01857133e-03, 1.68265128e-02],
...,
There are 8 such arrays.
As pointed out in comments,
import numpy as np
A = np.ndarray(shape=(8, 20, 400))
print (A.shape) ## Prints (8, 20, 400)
print (A[0].shape) ## Prints (20, 400)
I have a situation where sometimes, a whole series of data is not available. I'm real-time plotting values from sensors, and these can be turned on and off via user interaction, and thus I cannot be sure the values are always in a series. A user can start a sensor and later turn it off and on again, but In this case, matplotlib draws a line from the last end point and the new start point.
The data I plotted was as follows:
[[ 5. 22.57011604]
[ 6. 22.57408142]
[ 7. 22.56350136]
[ 8. 22.56394005]
[ 9. 22.56790352]
[ 10. 22.56451225]
[ 11. 22.56481743]
[ 12. 22.55789757]
#Missing x vals. Still plots straight line..
[ 29. 22.55654716]
[ 29. 22.56066513]
[ 30. 22.56110382]
[ 31. 22.55050468]
[ 32. 22.56550789]
[ 33. 22.56213379]
[ 34. 22.5588932 ]
[ 35. 22.54829407]
[ 35. 22.56697655]
[ 36. 22.56005478]
[ 37. 22.5568161 ]
[ 38. 22.54621696]
[ 39. 22.55033493]
[ 40. 22.55079269]
[ 41. 22.55475616]
[ 41. 22.54783821]
[ 42. 22.55195618]]
my plot function looks a lot simplified like this:
def plot(self, data)
for name, xy_dict in data.iteritems():
x_vals = xy_dict['x_values']
y_vals = xy_dict['y_values']
line_to_plot = xy_dict['line_number']
self.lines[line_to_plot].set_xdata(x_vals)
self.lines[line_to_plot].set_ydata(y_vals)
Does anyone know why it does like that? And do I have to take care of non-serial x and y values when plotting? It seems matplotlib should take care of this on its own.. Otherwise i have to split lists into smaller lists and plot these?
One option would be to add dummy items wherever data is missing (in your case apparently when x changes by more than 1), and set them as masked elements. That way matplotlib skips the line segments. For example:
import numpy as np
import matplotlib.pylab as pl
# Your data, with some additional elements deleted...
data = np.array(
[[ 5., 22.57011604],
[ 6., 22.57408142],
[ 9., 22.56790352],
[ 10., 22.56451225],
[ 11., 22.56481743],
[ 12., 22.55789757],
[ 29., 22.55654716],
[ 33., 22.56213379],
[ 34., 22.5588932 ],
[ 35., 22.54829407],
[ 40., 22.55079269],
[ 41., 22.55475616],
[ 41., 22.54783821],
[ 42., 22.55195618]])
x = data[:,0]
y = data[:,1]
# Difference from element to element in x
dx = x[1:]-x[:-1]
# Wherever dx > 1, insert a dummy item equal to -1
x2 = np.insert(x, np.where(dx>1)[0]+1, -1)
y2 = np.insert(y, np.where(dx>1)[0]+1, -1)
# As discussed in the comments, another option is to use e.g.:
#x2 = np.insert(x, np.where(dx>1)[0]+1, np.nan)
#y2 = np.insert(y, np.where(dx>1)[0]+1, np.nan)
# and skip the masking step below.
# Mask elements which are -1
x2 = np.ma.masked_where(x2 == -1, x2)
y2 = np.ma.masked_where(y2 == -1, y2)
pl.figure()
pl.subplot(121)
pl.plot(x,y)
pl.subplot(122)
pl.plot(x2,y2)
Another option is to include None or numpy.nan as values for y.
This, for example, shows a disconnected line:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[5,6,None,7,8])
Matplotlib will connect all your consequetive datapoints with lines.
If you want to avoid this you could split your data at the missing x-values, and plot the two splitted lists separately.
I have a numpy 2x2 matrix defined as follows:
a = np.pi/2
g = np.asarray([[-np.sin(a), -np.cos(a)],
[ np.cos(a), -np.sin(a)]])
Now, I have numpy array of 2D points that I would like to transform using this matrix. So we can simulate a bunch (25) of 2D points as follows:
p = np.random.rand(25, 2)
How can I do this matrix-vector multiplication for all these 25 points with broadcasting rather than do a for loop?
At the moment, I can do something like:
for i in range(25):
print np.dot(g, p[i])
This should give me another 2D array with the shape (25, 2).
Is there a more elegant way to do this without the for loop?
I think what you want is -
np.dot(p,g.T)
.T is to transpose an array
Example/Demo -
In [1]: import numpy as np
In [2]: a = np.pi/2
In [3]: g = np.asarray([[-np.sin(a), -np.cos(a)],
...: [ np.cos(a), -np.sin(a)]])
In [4]: p = np.random.rand(25, 2)
In [8]: for i in range(25):
...: print(np.dot(g, p[i]))
...:
[-0.56997282 -0.70151323]
[-0.65807814 -0.21773391]
[-0.533987 -0.53936287]
[-0.91982277 -0.01423868]
[-0.96648577 -0.42122831]
[-0.67169383 -0.94959473]
[-0.09013282 -0.57637376]
[-0.03937037 -0.94635173]
[ -2.59523258e-01 -4.04297667e-05]
[-0.77029438 -0.67325988]
[-0.24862373 -0.89806226]
[-0.91866799 -0.07927881]
[-0.83540497 -0.33473515]
[-0.38738641 -0.75406194]
[-0.07569734 -0.66859275]
[-0.72707983 -0.21314985]
[-0.67738699 -0.90763549]
[-0.96172981 -0.68684667]
[-0.40152064 -0.14629421]
[-0.46495457 -0.37456133]
[-0.97915149 -0.0470546 ]
[-0.76488223 -0.70756525]
[-0.21534494 -0.91354898]
[-0.25035908 -0.37841355]
[-0.17990176 -0.18436497]
In [10]: np.dot(p,g.T)
Out[10]:
array([[ -5.69972820e-01, -7.01513225e-01],
[ -6.58078138e-01, -2.17733909e-01],
[ -5.33987004e-01, -5.39362872e-01],
[ -9.19822767e-01, -1.42386768e-02],
[ -9.66485769e-01, -4.21228314e-01],
[ -6.71693832e-01, -9.49594730e-01],
[ -9.01328234e-02, -5.76373760e-01],
[ -3.93703749e-02, -9.46351732e-01],
[ -2.59523258e-01, -4.04297667e-05],
[ -7.70294378e-01, -6.73259882e-01],
[ -2.48623728e-01, -8.98062260e-01],
[ -9.18667987e-01, -7.92788080e-02],
[ -8.35404971e-01, -3.34735152e-01],
[ -3.87386412e-01, -7.54061939e-01],
[ -7.56973425e-02, -6.68592746e-01],
[ -7.27079833e-01, -2.13149846e-01],
[ -6.77386988e-01, -9.07635490e-01],
[ -9.61729810e-01, -6.86846673e-01],
[ -4.01520636e-01, -1.46294211e-01],
[ -4.64954574e-01, -3.74561327e-01],
[ -9.79151491e-01, -4.70545953e-02],
[ -7.64882230e-01, -7.07565246e-01],
[ -2.15344940e-01, -9.13548984e-01],
[ -2.50359076e-01, -3.78413552e-01],
[ -1.79901758e-01, -1.84364974e-01]])
Try:
np.dot(p, g.T)
which multiplies the points by the transpose of the rotation matrix.
I have array d with my data:
--> d
array([[ 60.41202301, 58.39997156, 55.3667636 , ..., -84.87512796,
-86.79190447, -86.19353546],
[ 60.10975935, 58.05402795, 55.3898762 , ..., -86.60428129,
-88.12205283, -89.45247056],
[ 60.14155715, 58.09627498, 55.7903388 , ..., -89.18612111,
-90.50667213, -92.67073841],
...,
[ 85.91138845, 84.50241834, 82.63664476, ..., -71.04692203,
-87.06297169, -100.36565732],
[ 82.74086966, 81.80343637, 79.96785709, ..., -64.08451752,
-79.18141593, -99.36881913],
[ 79.15278282, 78.42730549, 77.1980837 , ..., -58.38254858,
-75.93821858, -93.55287173]])
and 2 more arrays, with the values of the radius and angle in each pixel:
--> radius
array([[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 41.03656906, 40.31128874, 39.59797975, ..., 36.23534186,
36.87817783, 37.53664876],
...,
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 43.13930922, 42.44997055, 41.77319715, ..., 38.60051813,
39.20459157, 39.8246155 ]])
--> angle
array([[ 3.92699082, 3.94393835, 3.96145992, ..., 5.3664717 ,
5.38712992, 5.40712726],
[ 3.91004329, 3.92699082, 3.94453288, ..., 5.38291054,
5.40372591, 5.42384811],
[ 3.89252172, 3.90944876, 3.92699082, ..., 5.40006024,
5.42101525, 5.44124371],
...,
[ 2.37314202, 2.35619449, 2.33865243, ..., 0.90027477,
0.8794594 , 0.8593372 ],
[ 2.35619449, 2.33924696, 2.32172539, ..., 0.9167136 ,
0.89605538, 0.87605805],
[ 2.33980252, 2.32287349, 2.30539079, ..., 0.93247652,
0.91199029, 0.89213384]])
I also have p0, the object I want to optimize:
--> p0
[1.0, 500.0, 0.5, 0.5, 5.2, 0.4]
and definitions of my functions:
def rotation(B,radiusref,angleref):
return B[0] + (B[1] * radiusref * np.cos(angleref-B[2]) * np.sin(B[3]) * np.cos(B[3])**B[5] ) / (radiusref**2 * ( np.sin(angleref-B[2])**2 + np.cos(B[3])**2*np.cos(angleref-B[2])**2 ) + B[4]**2*np.cos(B[3])**2 )**B[5]/2.0
def errfunc(p0,d,radius,angle):
return rotation(p0,radius,angle) - d
Then, I try to run optimize.leastsq to find the best solution, and I get following error message:
--> fit = optimize.leastsq(errfunc,p0,args=(d,radius,angle))
ValueError: object too deep for desired array
Traceback (innermost last):
File "<console>", line 1, in <module>
File "/home/luisgdh/Ureka/python/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 379, in leastsq
gtol, maxfev, epsfcn, factor, diag)
error: Result from function call is not a proper array of floats.
Why does this happen?
If I try to run errfunc I get this
--> errfunc(p0,d,radius,angle)
array([[-311.96251984, -307.50362989, -301.89956899, ..., 121.84425081,
128.7017239 , 132.9161849 ],
[-312.24524019, -307.79621255, -302.61362546, ..., 127.21717127,
133.70343473, 139.86845861],
[-312.81581149, -308.43470253, -303.66703887, ..., 133.56598817,
139.87929082, 146.89601654],
...,
[-156.59031196, -150.97315566, -144.78021889, ..., 305.55786014,
324.82275173, 341.21407515],
[-149.69283006, -144.54525672, -138.38451089, ..., 297.67836311,
316.06244785, 339.38095877],
[-142.47209078, -137.53819725, -131.98946431, ..., 291.05301549,
311.92874028, 332.71166956]])
I solved the problem my self, if someone finds this problem again.. leastsq wasn't dealing well with 2d data, so I transformed the 2d data in 1d data via:
d1radius = radius.ravel()
d1angle=angle.ravel()
d1data=d.ravel()
and redefined the function:
def errfunc(p0,d1data,d1radius,d1angle):
return rotation(p0,d1radius,d1angle) - d1data
And finally:
fit = optimize.leastsq(errfunc,p0,args=(d1data,d1radius,d1angle))
I have a list raws of arrays that I would like to plot in ipython notebook. Here is the code I am trying to get working:
fig, axes = subplots(len(raws),1, sharex=True, tight_layout=True, figsize=(12, 6), dpi=72)
for r in range(len(raws)):
axes[r].plot(raws)
I've been lost for hours if not days trying to figure out how to index the list raws, such that I can plot each mxn array on it's own axis where n is the number of time points, i.e., x-axis and m is the number of time-series functions sampled at each point.
When I code:
for r in range(len(raws)):
axes[r].plot(raws[r])
I get an ValueError: setting an array element with a sequence.
For your information:
len(raws) = 2
type(raws) = 'list'
np.shape(raws[0][0]) = (306, 10001)
raws =
[(array([[ -4.13211217e-12, -4.13287303e-12, -4.01705259e-12, ...,
1.36386023e-12, 1.65182851e-12, 2.00368966e-12],
[ 1.08914129e-12, 1.47828466e-12, 1.82257607e-12, ...,
-2.70151520e-12, -2.48631967e-12, -2.28625548e-12],
[ -7.80962369e-14, -1.27119591e-13, -1.73610315e-13, ...,
-1.13219629e-13, -1.15031720e-13, -1.12106621e-13],
...,
[ 2.52774254e-12, 2.32293195e-12, 2.02644002e-12, ...,
4.20064191e-12, 3.94858906e-12, 3.69495394e-12],
[ -4.38122146e-12, -4.96229676e-12, -5.47782145e-12, ...,
3.93820033e-12, 4.18850823e-12, 4.34950629e-12],
[ -1.07284424e-13, -9.23447993e-14, -7.89852400e-14, ...,
7.92079631e-14, 5.60172215e-14, 3.04448868e-14]]), array([ 60. , 60.001, 60.002, ..., 69.998, 69.999, 70. ])), (array([[ -6.71363108e-12, -5.80501003e-12, -4.95944514e-12, ...,
-3.25087343e-12, -2.68982494e-12, -2.13637448e-12],
[ -5.04818633e-12, -4.65757005e-12, -4.16084140e-12, ...,
-4.26120531e-13, 2.20744290e-13, 7.81245614e-13],
[ 1.97329506e-13, 1.64543867e-13, 1.32679812e-13, ...,
2.11645494e-13, 1.94795729e-13, 1.75781773e-13],
...,
[ 3.04245661e-12, 2.28376461e-12, 1.54118900e-12, ...,
-1.14020908e-14, -8.04647589e-13, -1.52676489e-12],
[ -1.83485962e-13, -5.22949893e-13, -8.60038852e-13, ...,
7.70312553e-12, 7.20825156e-12, 6.58362857e-12],
[ -7.26357906e-14, -7.11700989e-14, -6.88759767e-14, ...,
-1.04171843e-13, -1.03084861e-13, -9.68462427e-14]]), array([ 60. , 60.001, 60.002, ..., 69.998, 69.999, 70. ]))]
Just so I can post code, I am responding here.
Looks like your data is nested in the form
[ ( array1, array2, ..., arrayN ) ]
This could be handled in one of two ways:
In [2]: raws = [np.random.rand(20, 100), np.random.rand(20, 100)]
In [3]: raws = raws[0]
In [4]: f, axes = plt.subplots(len(raws), 1)
In [5]: for i in range(len(raws)):
...: axes[i].plot(raws[i])
Or
In [3]: raws = [(np.random.rand(20, 100), np.random.rand(20, 100))]
In [4]: f, axes = plt.subplots(len(raws[0]), 1)
In [5]: for i in range(len(raws[0])):
...: axes[i].plot(raws[0][i])
If you have a list of arrays such as abac below, you can plot as following:
import numpy as np
a = np.array(range(20))
b = a * 2
c = a * 3
abac = a,b,a,c
plt.plot(*abac)