I have a homework that involves Python code and multiplying some Numpy arrays of length 100, but the following error comes up:
ValueError: maximum supported dimension for an ndarray is 32, found 100
Here's my code, xs and ys are two lists of length 100.
%inline pylab
import numpy as np
Y = np.array(ys).reshape(len(ys),1)
X = np.array([len(xs)*[1],xs]).transpose()
B = linalg.inv(X.transpose().dot(X)).dot(X.transpose(X)).dot(Y)
i guess you just have a typo in X.transpose(X) -> linalg.inv(X.transpose().dot(X)).dot( X.transpose(X) ).dot(Y)
I don't think you really want to specify the *axes along which to transpose as "X"
not sure what you're trying to compute but probably X.transpose() is what you mean or maybe you wanted X.transpose().dot(X) ..
Related
I'm trying to generate N data points for three random variables that are jointly normal in python.
If I use the following code:
import numpy as np
import scipy
import pandas
import sys
from scipy.linalg import block_diag
from pandas import *
N=100
Sigma=np.identity(3)
Mu=np.zeros((3,1))
Z=np.random.multivariate_normal(Mu, Sigma, N)
I got the following error message:
in <module>
Z=np.random.multivariate_normal(Mu, Sigma, N)
File "mtrand.pyx", line 4067, in numpy.random.mtrand.RandomState.multivariate_normal
ValueError: mean must be 1 dimensional
This means that the dimension of np.zeros((3,1)) is not 1. After changing the line Mu=np.zeros((3,1)) to Mu=np.zeros(3), it works. This implies that np.zeros(3) is 1 dimensional.
As np.zeros(3) and np.zeros((3,1)) are both an array of three zeros, I guess naturally both should be 1 dimensional. Using Mu.ndim in each case, I found that the dimension of np.zeros(3) is one and the dimension of np.zeros((3,1)) is two. My question is:
Why does Python make a distinction between np.zeros((3,1)) and np.zeros(3) regarding their dimensions (why is this distinction useful)?
It's normal for them to have different dimensions. The first one only has 1 array made of 3 zeros and the second one has 3 arrays each one made of 1 zero.
If you print Mu[0] in your example, you will get a list [0.] while if you print Mu[0] after using np.zeros(3) to define it, you will get 0.0
I can think of cases where this is distinction is useful especially when working with features in machine learning. If I have a sequence of features of size 1, I would want to use a dimension [n,1] and not [n] because that helps the model (let's say LSTM) make a difference between the sequence size and the feature size.
I have two numpy arrays, with just the 3-dimensional coordinates of two molecules.
I need to implement the following equation, and I'm having problems in the subtraction of each coordinate of one of the arrays by the second, and then square it.
I have tried the following, but since I'm still learning I feel that I am making some major mistake. The simple code I use is:
a = [math.sqrt(1/3*((i[:,0]-j[:,0])**2) + ((i[:,1] - j[:,1])**2) + ((i[:,2]-j[:,2])**2) for i, j in zip(coordenates_2, coordenates_1))]
It's numpy you can easily do it using the following example:
import numpy as np
x1 = np.random.randn(3,3,3)
x2 = np.random.randn(3,3,3)
res = np.sqrt(np.mean(np.power(x1-x2,2)))
I was given a question: I'm given 100000 sequences of 1000 coin tosses arranged in a matrix. To generate the data use the commands:
import numpy
data = numpy.random.binomial(1, 0.25, (100000,1000))
Now I need for the first 5 sequences of 1000 tosses (the first 5 rows in data) to plot (using pylab) the estimate Xm which is the sum of i from 1 to m. (Meaning the sum of all tosses up to m)
Now I was trying to do the following:
data = numpy.random.binomial(1, 0.25, (100000,1000))
x = numpy.linspace(1,1000,1000, int) // in order to create an array of 1000 ints between 1 and 1000
y = numpy.sum(data[0], x) // taking the first row
pylab.plot(x,y)
pylab.show()
And I'm getting an error
only length-1 arrays can be converted to Python scalars
furthermore, when trying to do
y = numpy.sum(data[0], tuple(x))
because I looked up the function and saw that axis needed to be a tuple of ints, I get an error
ValueError: too many values for 'axis'
So basically I'm a bit lost, could use some help.
Thanks!
I think you want to use numpy.cumsum. Furthermore, axis needs to be an integer, not an array or a tuple of an array (I think this explains the errors).
This should work:
import numpy as np
import pylab
data = np.random.binomial(1, 0.25, (100000,1000))
y = np.cumsum(data[:5, :], axis=1) # cumulative sum of first 5 rows along the rows (axis=1)
pylab.plot(np.arange(1,y.shape[1]+1), y.T)
pylab.show()
I'm quite new to Python and Numpy, so I apologize if I'm missing something obvious here.
I have a function that solves a system of 2 differential equations :
import numpy as np
import numpy.linalg as la
def solve_ode(x0, a0, beta, t):
At = np.array([[0.23*t, (-10**5)*t], [0, -beta*t]], dtype=np.float32)
# get eigenvalues and eigenvectors
evals, V = la.eig(At)
Vi = la.inv(V)
# get e^At coeff
eAt = V # np.exp(evals) # Vi
xt = eAt*x0
return xt
However, running it with this code :
import matplotlib.pyplot as plt
# initial values
x0 = 10**6
a0 = 2.5
beta = 0.05
t = np.linspace(0, 3600, 360)
plt.semilogy(t, solve_ode(x0, a0, beta, t))
... throws this error :
ValueError: setting an array element with a sequence.
At this line :
At = np.array([[0.23*t, (-10**5)*t], [0, -beta*t]], dtype=np.float32)
Note that t and beta are supposed to be floats. I think Python might not be able to infer this but I don't know how I could do this...
Thx in advance for your help.
You are supplying t as a numpy array of shape 360 from linspace and not simply a float. The resulting At numpy array you are trying to create is then ill formed as all columns must be the same length. In python there is an important difference between lists and numpy arrays. For example, you could do what you have here as a list of lists, e.g.
At = [[0.23*t, (-10**5)*t], [0, -beta*t]]
with dimensions [[360 x 360] x [1 x 360]].
Alternatively, if all elements of At are the length of t the array would work,
At = np.array([[0.23*t, (-10**5)*t], [t, -beta*t]], dtype=np.float32)
with shape [2, 2, 360].
When you give a list or a list of lists, or in this case, a list of list of listss, all of them should have the same length, so that numpy can automatically infer the dimensions (shape) of the resulting matrix.
In your example, it's all correctly put, except the part you put 0 as a column I guess. Not sure what to call it though, cause your expected output is a cube I suppose.
You can fix it by giving the correct number of zeros as bellow:
At = np.array([[0.23*t, (-10**5)*t], [np.zeros(len(t)), -beta*t]], dtype=np.float32)
But check the .shape of the resulting array, and make sure it's what you want.
As others note the problem is the 0 in the inner list. It doesn't match the 360 length arrays generated by the other expressions. np.array can make an object dtype array from that (2x2), but can't make a float one.
At = np.array([[0.23*t, (-10**5)*t], [0*t, -beta*t]])
produces a (2,2,360) array. But I suspect the rest of that function is built around the assumption that At is (2,2) - a 2d square array with eig, inv etc.
What is the return xt supposed to be?
Does this work?
S = np.array([solve_ode(x0, a0, beta, i) for i in t])
giving a 1d array with the same number of values as in t?
I'm not suggesting this is the fastest way of solving the problem, but it's the simplest, especially if you are only generating 360 values.
I have a list of several hundred 10x10 arrays that I want to stack together into a single Nx10x10 array. At first I tried a simple
newarray = np.array(mylist)
But that returned with "ValueError: setting an array element with a sequence."
Then I found the online documentation for dstack(), which looked perfect: "...This is a simple way to stack 2D arrays (images) into a single 3D array for processing." Which is exactly what I'm trying to do. However,
newarray = np.dstack(mylist)
tells me "ValueError: array dimensions must agree except for d_0", which is odd because all my arrays are 10x10. I thought maybe the problem was that dstack() expects a tuple instead of a list, but
newarray = np.dstack(tuple(mylist))
produced the same result.
At this point I've spent about two hours searching here and elsewhere to find out what I'm doing wrong and/or how to go about this correctly. I've even tried converting my list of arrays into a list of lists of lists and then back into a 3D array, but that didn't work either (I ended up with lists of lists of arrays, followed by the "setting array element as sequence" error again).
Any help would be appreciated.
newarray = np.dstack(mylist)
should work. For example:
import numpy as np
# Here is a list of five 10x10 arrays:
x = [np.random.random((10,10)) for _ in range(5)]
y = np.dstack(x)
print(y.shape)
# (10, 10, 5)
# To get the shape to be Nx10x10, you could use rollaxis:
y = np.rollaxis(y,-1)
print(y.shape)
# (5, 10, 10)
np.dstack returns a new array. Thus, using np.dstack requires as much additional memory as the input arrays. If you are tight on memory, an alternative to np.dstack which requires less memory is to
allocate space for the final array first, and then pour the input arrays into it one at a time.
For example, if you had 58 arrays of shape (159459, 2380), then you could use
y = np.empty((159459, 2380, 58))
for i in range(58):
# instantiate the input arrays one at a time
x = np.random.random((159459, 2380))
# copy x into y
y[..., i] = x