I'm trying to rebuild a song in python, but I cannot concatenate the notes of the same.
I get this error:
ValueError: operands could not be broadcast together with shapes (0)
(1250)
Here's my code:
import numpy as np, matplotlib.pyplot as plt
def nota(f,d):
ts = 0.0002
t = np.arange(0, d, ts)
X = 5500*np.cos(2*np.pi*f*t)
return X
# II.2.b)
pausa = nota(0,0)
La = nota(440,0.25)
Mi = nota(659.26,0.25)
Do = nota(253.25,0.25)
Sol = nota(783.99,0.25)
Si = nota(493.88,0.25)
Solbemol = nota(830.61,0.25)
def FurElise():
musica = np.array((pausa,pausa,La,Mi,La,pausa,pausa,Mi,Mi,Solbemol, \
pausa,pausa,La,Mi,La,pausa,pausa,pausa,La,Mi,La, \
pausa,pausa,Mi,Mi,Solbemol,pausa,pausa,La,Mi,La, \
pausa,Do,Sol,Do,pausa,pausa,Sol,Sol,Si,pausa,pausa, \
La,Mi,La,pausa,pausa,Mi,Mi,Mi,pausa))
y=0
for x in musica:
z=np.hstack((x,y))
y = y+x
z=np.hstack((x,y))
plt.plot(z)
plt.show()
FurElise()
As #filmor notes, x and y are of different shapes, and the reason for that is your definition of pausa = nota(0,0). By using a d value of 0, the resulting array is of length 0 while all other arrays are of length 1250, and y = y+x will eventually throw the error you're seeing (e.g. after 3 iterations, given your current definition of musica).
Assuming you want the pause to be of the same length as all other notes, you can re-define pausa so as to get rid of the error:
pausa = nota(0,0.25)
You create musica (assuming that you've fixed pausa as described by #fgb) by stacking the 51 notes as rows in a 2D array. So, musica.shape is (51, 1250)
I think that you want z to be a long 1D array where all the notes are in one row, instead of each in their own row. There are two solutions. One, is to say:
musica = np.array((pausa,pausa,La,Mi,La,...))
z = musica.flatten()
plt.plot(z)
and completely remove all of this:
y=0
for x in musica:
z=np.hstack((x,y))
y = y+x
z=np.hstack((x,y))
A better solution is to make musica a 1D array when you create it:
musica = np.hstack((pausa,pausa,La,Mi,La,...))
plt.plot(musica)
This takes all the notes (pausa, La, etc) and stacks them horizontally (so that they're all in one row together), so you never need to flatten or concatenate anything.
Related
The shape of Y[n,:,:] is (200,1) and so I need Z[n,,:,:]*H[n,:,:] (or something related) to be (200,1) also. But Z[n,,:,:] and H[n,:,:] are both (200,6) so I need a multiplication operator that multiplies them and gets rid of the 6 to give an answer of shape (200,1). Any suggestions? The code is below
n=10
M = 200
D=6
dW = np.sqrt(1/n)*randn(n,M,D);
H=cap(dW,1/n,np.log(n))#the generation of the Brownian motion increment
X = define_X(1,dW,1,1,1)
H[1]
H.shape
Y = np.zeros((n+1,M,1))
Z = np.zeros_like(X)
Z[n-1,:,:]=np.dot(np.transpose(Y[n,:,:]),H[n-1,:,:])
Y[n-1,:,:]= Y[n,:,:] +f(X[n-1,:,:],Y[n,:,:],Z[n-1,:,:])*(1/10)-Z[n,,:,:]*H[n,:,:]
I have done a point filter programme in a 3D plane, but I need to do a loop along a known 3D normal vector with a known length. Many thanks for the help.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
point = sta[10] #starting points
normal = axe[10] #normal vector
d = -point.dot(normal)
# create x,y
xx, yy = np.meshgrid(np.linspace(-3.,-2.,101), np.linspace(-11.,-10.,101))
# calculate corresponding z
z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]
f=[]
for i in xrange(len(xx)-1):
for j in xrange(len(xx)-1):
if (xx[i][j]-sta[10][0])**2 + (yy[i][j]-sta[10][1])**2 + (z[i][j]-sta[10][2])**2 > float(rad[0])**2:
xx[i][j]=NaN
yy[i][j]=NaN
z[i][j]=NaN
Since you're using meshgrid and xx, yy and z have the same shape, numpy's broadcasting policy will automatically do what you need. Try this:
invalid = (xx-sta[10,0])**2 + (yy-sta[10,1])**2 + (z-sta[10,2])**2 > float(rad[0])**2
xx[invalid]=np.NaN
yy[invalid]=np.NaN
z[invalid]=np.NaN
It creates a boolean mask invalid which contains True for all entries that satisfy the condition. You can then use this mask to set the corresponding values to NaN.
Note that you can use tuples to index numpy arrays. I.e. myArray[a][b] is equivalent to myArray[a, b].
Also note that I assumed you excluded the last entries by accident. If it was on purpose that you used xrange(len(xx)-1) rather than xrange(len(xx)), it is getting a bit uglier and you have to do it like this:
invalid = (xx[:-1,:-1]-sta[10,0])**2 + (yy[:-1,:-1]-sta[10,1])**2 + (z[:-1,:-1]-sta[10,2])**2 > float(rad[0])**2
xx[:-1,:-1][invalid]=np.NaN
yy[:-1,:-1][invalid]=np.NaN
z[:-1,:-1][invalid]=np.NaN
I want to produce a 2D matrix I[x,t] with two for loops. This is a 70*60 matrix.
I firstly calculate my vectors with one row and 60 columns. Then, in the inner for loop, for every t step, I calculate the matrix I by using x for my V which is a vector with one column and 70 rows.
I face with the following error for I[x,t]:
IndexError: too many indices for array
I will be glad if someone helps me to correct it.
from math import *
from pylab import *
from numpy import *
N=60 #number of cells
Ir=zeros((1,N))
Ir=Ir+1000.0
Temp=zeros((1,N))
Temp=Temp+25.0
V = arange(0,0.7,0.01)
Area=243.36
ns=1
np=1
Jsc_cell = 0.03785
Isc_cell = Jsc_cell * Area
n1=1.0
J01 = 6.2e-13
Is1 = J01 * Area
n2= 2.0
J02 = 7.3e-9
Is2= J02 * Area
T_co = 0.0005
Rs = 2.7487398e-3
Rsh = 410913.8725
k = 1.38e-23
q = 1.6e-19
Eg=1.11
Tmeas= 273+25.0
Iph=zeros((1,N))
I0=zeros((1,N))
I02=zeros((1,N))
Vt=zeros((1,N))
I=zeros((len(V),N))
P=zeros((len(V),N))
T=zeros((1,N))
Pmpp_cell=zeros((1,N))
Impp_cell=zeros((1,N))
Vmpp_cell=zeros((1,N))
Isc_cell_c=zeros((1,N))
for t in range(0,N):
T[0,t] = Temp[0,t]+273.0
Vt[0,t]=(k*T[0,t])/q
I0[0,t]=Is1*((T[0,t]/Tmeas)**(3/n1))*exp(Eg*((T[0,t]/Tmeas)-1)/(n1*Vt[0,t]));
I02[0,t]=Is2*((T[0,t]/Tmeas)**(3/n2))*exp(Eg*((T[0,t]/Tmeas)-1)/(n2*Vt[0,t]));
Iph[0,t] = Isc_cell*(Ir[0,t]/1000.0)*(1+(T_co*(Temp[0,t]-25)));
for x in range(0,len(V)):
I[x,t] = Iph[0,t] - I0[0,t]*(exp((V[x,0]+I[x,t]*Rs)/(n1*Vt[0,t]))-1)-I02[0,t]*(exp((V[x,0]+I[x,t]*Rs)/(n2*Vt[0,t]))-1)-((V[x,0]+I[x,t]*Rs)/Rsh)
P[x,t] = I[x,t]*V[x,0]
x=x+1
t=t+1
P.s. Is there any solutions that I could avoid defining the parameters and their dimensions in advance?
While running your code I saw that your variable V is 1D-array by initialization:
V = arange(0,0.7,0.01)
However, during your final computation of I[x,t] and P[x,t] you use a two-dimensional indexing of V. By replacing V[x,0]with V[x]your code finishes without errors.
I am a little confused by the documentation for scipy.interpolate.RegularGridInterpolator.
Say for instance I have a function f: R^3 => R which is sampled on the vertices of the unit cube. I would like to interpolate so as to find values inside the cube.
import numpy as np
# Grid points / sample locations
X = np.array([[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1.]])
# Function values at the grid points
F = np.random.rand(8)
Now, RegularGridInterpolator takes a points argument, and a values argument.
points : tuple of ndarray of float, with shapes (m1, ), ..., (mn, )
The points defining the regular grid in n dimensions.
values : array_like, shape (m1, ..., mn, ...)
The data on the regular grid in n dimensions.
I interpret this as being able to call as such:
import scipy.interpolate as irp
rgi = irp.RegularGridInterpolator(X, F)
However, when I do so, I get the following error:
ValueError: There are 8 point arrays, but values has 1 dimensions
What am I misinterpreting in the docs?
Ok I feel silly when I answer my own question, but I found my mistake with help from the documentation of the original regulargrid lib:
https://github.com/JohannesBuchner/regulargrid
points should be a list of arrays that specifies how the points are spaced along each axis.
For example, to take the unit cube as above, I should set:
pts = ( np.array([0,1.]), )*3
or if I had data which was sampled at higher resolution along the last axis, I might set:
pts = ( np.array([0,1.]), np.array([0,1.]), np.array([0,0.5,1.]) )
Finally, values has to be of shape corresponding to the grid laid out implicitly by points. For example,
val_size = map(lambda q: q.shape[0], pts)
vals = np.zeros( val_size )
# make an arbitrary function to test:
func = lambda pt: (pt**2).sum()
# collect func's values at grid pts
for i in range(pts[0].shape[0]):
for j in range(pts[1].shape[0]):
for k in range(pts[2].shape[0]):
vals[i,j,k] = func(np.array([pts[0][i], pts[1][j], pts[2][k]]))
So finally,
rgi = irp.RegularGridInterpolator(points=pts, values=vals)
runs and performs as desired.
Your answer is nicer, and it's perfectly OK for you to accept it. I'm just adding this as an "alternate" way to script it.
import numpy as np
import scipy.interpolate as spint
RGI = spint.RegularGridInterpolator
x = np.linspace(0, 1, 3) # or 0.5*np.arange(3.) works too
# populate the 3D array of values (re-using x because lazy)
X, Y, Z = np.meshgrid(x, x, x, indexing='ij')
vals = np.sin(X) + np.cos(Y) + np.tan(Z)
# make the interpolator, (list of 1D axes, values at all points)
rgi = RGI(points=[x, x, x], values=vals) # can also be [x]*3 or (x,)*3
tst = (0.47, 0.49, 0.53)
print rgi(tst)
print np.sin(tst[0]) + np.cos(tst[1]) + np.tan(tst[2])
returns:
1.93765972087
1.92113615659
I need to shift a 2D array field, i.e. I have a "previous_data" array which I access through shifted indices to create my "new_data" array.
I can do this in a nonpythonic (and slow) loop, but would very much appreciate some help in finding a pythonic (and faster) solution!
Any help and hints are very much appreciated!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mpl
def nonpythonic():
#this works, but is slow (for large arrays)
new_data = np.zeros((ny,nx))
for j in xrange(ny):
for i in xrange(nx):
#go through each item, check if it is within the bounds
#and assign the data to the new_data array
i_new = ix[j,i]
j_new = iy[j,i]
if ((i_new>=0) and (i_new<nx) and (j_new>=0) and (j_new<ny)):
new_data[j,i]=previous_data[j_new,i_new]
ef, axar = plt.subplots(1,2)
im = axar[0].pcolor(previous_data, vmin=0,vmax=2)
ef.colorbar(im, ax=axar[0], shrink=0.9)
im = axar[1].pcolor(new_data, vmin=0,vmax=2)
ef.colorbar(im, ax=axar[1], shrink=0.9)
plt.show()
def pythonic():
#tried a few things here, but none are working
#-tried assigning NaNs to indices (ix,iy) which are out of bounds, but NaN's don't work for indices
#-tried masked arrays, but they also don't work as indices
#-tried boolean arrays, but ended in shape mismatches
#just as in the nonworking code below
ind_y_good = np.where(iy>=0) and np.where(iy<ny)
ind_x_good = np.where(ix>=0) and np.where(ix<nx)
new_data = np.zeros((ny,nx))
new_data[ind_y_good,ind_x_good] = previous_data[iy[ind_y_good],ix[ind_x_good]]
#some 2D array:
nx = 20
ny = 30
#array indices:
iy, ix = np.indices((ny,nx))
#modify indices (shift):
iy = iy + 1
ix = ix - 4
#create some out of range indices (which might happen in my real scenario)
iy[0,2:7] = -9999
ix[0:3,-1] = 6666
#some previous data which is the basis for the new_data:
previous_data = np.ones((ny,nx))
previous_data[2:8,10:20] = 2
nonpythonic()
pythonic()
This is the result of the working (nonpythonic) code above:
I implemented a version of pythonic that replicates nonpythonic with some masking and index fiddling - see below. By the way I think the "new" indices should be the ones corresponding to the new array, rather than the old ones, but I've left it as in your existing function.
The main thing to realise is that in your attempt in the question, your conditions
ind_y_good = np.where(iy>=0) and np.where(iy<ny)
ind_x_good = np.where(ix>=0) and np.where(ix<nx)
must be combined, since we must always have pairs of x and y indices. i.e. if the x index is invalid, then so is the y.
Finally, if the indices are really all shifted by a constant factor, you can make this even simpler by using NumPy's roll function and taking a slice of the indices corresponding to the valid area.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mpl
def nonpythonic(previous_data, ix, iy, nx, ny):
#this works, but is slow (for large arrays)
new_data = np.zeros((ny,nx))
for j in xrange(ny):
for i in xrange(nx):
#go through each item, check if it is within the bounds
#and assign the data to the new_data array
i_new = ix[j,i]
j_new = iy[j,i]
if ((i_new>=0) and (i_new<nx) and (j_new>=0) and (j_new<ny)):
new_data[j,i]=previous_data[j_new,i_new]
return new_data
def pythonic(previous_data, ix, iy):
ny, nx = previous_data.shape
iy_old, ix_old = np.indices(previous_data.shape)
# note you must apply the same condition to both
# index arrays
valid = (iy >= 0) & (iy < ny) & (ix >= 0) & (ix < nx)
new_data = np.zeros((ny,nx))
new_data[iy_old[valid], ix_old[valid]] = previous_data[iy[valid], ix[valid]]
return new_data
def main():
#some 2D array:
nx = 20
ny = 30
#array indices:
iy, ix = np.indices((ny,nx))
#modify indices (shift):
iy = iy + 1
ix = ix - 4
#create some out of range indices (which might happen in my real scenario)
iy[0,2:7] = -9999
ix[0:3,-1] = 6666
#some previous data which is the basis for the new_data:
previous_data = np.ones((ny,nx))
previous_data[2:8,10:20] = 2
data_nonpythonic = nonpythonic(previous_data, ix, iy, nx, ny)
data_pythonic = pythonic(previous_data, ix, iy)
new_data = data_nonpythonic
ef, axar = plt.subplots(1,2)
im = axar[0].pcolor(previous_data, vmin=0,vmax=2)
ef.colorbar(im, ax=axar[0], shrink=0.9)
im = axar[1].pcolor(new_data, vmin=0,vmax=2)
ef.colorbar(im, ax=axar[1], shrink=0.9)
plt.show()
print(np.allclose(data_nonpythonic, data_pythonic))
if __name__ == "__main__":
main()