Consider the next piece of code -
def findBestHypothesis():
bestOfBest = []
currentERMValue = 0
bestERMValue = 0
for polynom in bestOfHypothesisClasses:
for j in range(0, len(training_set)):
currentERMValue += (np.polyval(polynom, training_set[x_value_index]) -
training_set[y_value_index])**2
if currentERMValue >= bestERMValue:
bestERMValue = currentERMValue
currentERMValue = 0
bestOfBest = polynom
return bestOfBest
As you can see, currentERMValue and bestERMValue are numbers and not arrays. But yet i get this -
if np.greater_equal(currentERMValue, bestERMValue): ValueError: The
truth value of an array with more than one element is ambiguous. Use
a.any() or a.all()
polyval on an array returns an array, one element for each x value:
>>> np.polyval([1,2,3],[4,5,6])
array([27, 38, 51])
so either training_set is a two dimensional array, or x_value_index or y_value_index are not scalars.
I am not sure, but np.polyval return several values.
Returns:
values : ndarray or poly1d
If x is a poly1d instance, the result is the composition of the two polynomials, i.e., x is “substituted” in p and the simplified result is returned. In addition, the type of x - array_like or poly1d - governs the type of the output: x array_like => values array_like, x a poly1d object => values is also.
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.polyval.html
Related
I work in the field of image processing, and I converted the image into a matrix. The values of this matrix are only two numbers: 0 and 255. I want to know where the value 0 is in which column and in which row it is repeated within this matrix. Please help
I wrote these
array = np.array(binary_img)
print("array",array)
for i in array:
a = np.where(i==0)
print(a)
continue
The == operator on an array returns a boolean array of True/False values for each element. The argwhere function returns the coordinates of the nonzero points in its parameter:
array = np.array(binary_img)
for pt in np.argwhere(array == 0):
print( pt )
I have a matrix X, and the labels to each vector of that matrix as a np array Y. If the value of Y is +1 I want to multiply the vector of matrix X by another vector W. If the value of Y is -1 I want to multiply the vector of matrix X by another vector Z.
I tried the following loop:
for i in X:
if Y[i] == 1:
np.sum(np.multiply(i, np.log(w)) + np.multiply((1-i), np.log(1-w)))
elif Y[i] == -1:
np.sum(np.multiply(i, np.log(z)) + np.multiply((1-i), np.log(1-z)))
IndexError: arrays used as indices must be of integer (or boolean) type
i is the index of X, but I'm not sure how to align the index of X to the value in that index of Y.
How can I do this?
Look into np.where, it is exactly what you need:
res = np.where(Y == +1, np.dot(X, W), np.dot(X, Z))
This assumes that Y can take only value +1 and -1. If that's not the case you can adapt the script above but we need to know what do you expect as result when Y takes a different value.
Also, try to avoid explicit for loops when using numpy, the resulting code will be thousands of times faster.
While it is better to use a no-iteration approach, I think you need a refresher on basic Python iteration.
Make an array:
In [57]: x = np.array(['one','two','three'])
Your style of iteration:
In [58]: for i in x: print(i)
one
two
three
to iterate on indices use range (or arange):
In [59]: for i in range(x.shape[0]): print(i,x[i])
0 one
1 two
2 three
enumerate is a handy way of getting both indices and values:
In [60]: for i,v in enumerate(x): print(i,v)
0 one
1 two
2 three
Your previous question had the same problem (and correction):
How to iterate over a numpy matrix?
this is how I got the two arrays (array 1 and array2) for my function:
x = np.arange(-5, 5,0.01)
prob=stats.norm.pdf(x,0,1)
prob_array=numpy.array(prob).reshape(1000,1) #array1
x_tran=m.transpose()
x_tran_array=array(x_tran)
mu_array=array(mu) # mu is stock return
mu_array1=numpy.array(mu_array).reshape(54966,1)
sigma_array=array(sigma) #sigma is the historical volatility
sigma_array1=numpy.array(sigma_array).reshape(54966,1)
mu1_mat=mat(ones((1,1000))) #for matrix calculation
original_x=mu_array1*mu1_mat+sigma_array1*x_tran_array #array2
I defined a function:
def TK_value(z,p):
if z >= 0:
utility=z**0.88
prob=(p**0.61)/(p**0.61+(1-p)**0.61)**(1/0.61)
else:
utility= -2.25*(-z)**0.88
prob=(p**0.69)/(p**0.69+(1-p)**0.69)**(1/0.69)
return utility*prob
tks=TK_value(original_x,prob_array)
I have two arrays with original_x with shape((54966, 1000)
and prob_array with shape (1000,1). I want to use original_x as z and prob_array as p in this function.
But the error is :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Welcome to SO! The problem seems to be this line: if z >= 0:
If you use the '>'/'<' operator on an array it will return the following:
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> a > 2
array([False, False, True])
This array can't be converted to bool by default, you have to be more specific, for example by using any() to test if atleast one element falls under the given condition.
Numpy arrays can do it like this: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html.
I'm trying to plot some complex functions using numpy. Example of some working code:
import numpy as np
from PIL import Image
size = 1000
w = np.linspace(-10, 10, size)
x, y = np.meshgrid(w, w)
r = x + 1j*y
def f(q):
return np.angle(q)
z = f(r)
normalized = ((255/(np.amax(z) - np.amin(z)))*(z+abs(np.amin(z)))).astype(int)
data = [i for j in normalized for i in j]
img = Image.new('L', (size, size))
img.putdata(data[::-1]) #pixels are done bottom to top
img.show()
However, suppose I want the function f to have a simple comparison in it, like this:
def f(q):
if np.abs(q) < 4:
return 1
else:
return 0
I get the error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
For the np.abs(q) < 4 check.
I did some digging and realized it's because Python is doing the operation on the entire r array, and it can't compare an array to an integer. So, I tried looking for ways to do element-wise comparisons.
This page looked promising: it says I can do element-wise comparisons by using np.less(a, b), so I tried
def f(q):
if np.less(np.abs(q), 4):
return 1
else:
return 0
and got the same ValueError. It seems as though both arguments for np.less() need to be arrays of the same size.
What I want is to compare each element of my array to a single, non-array quantity. I suppose I could make a dummy array of the same size filled with identical 4's, but there has to be a more elegant way of doing this.
The key is to return an array value instead of trying to coerce an array into a single bool, which is what if (some_array): keeps trying to do. There being no unambiguous way to decide what single boolean np.array([True, False]) should convert to, it doesn't even try.
So don't even branch:
def f(q):
return abs(q) < 4
gives an array like
>>> f(np.array([1,3,5]))
array([ True, True, False], dtype=bool)
which as numbers will behave like
>>> f(np.array([1,3,5])).astype(int)
array([1, 1, 0])
and give
I defined a function
def softthresh(u, LAMBDA):
if np.fabs(u) <= LAMBDA:
return 0
else:
return ((np.fabs(u) - LAMBDA) * u / np.fabs(u))
u is a numpy array, and np.fabs will check the relations for each array element (np.fabs(u_i)). It gives me the following error:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Follow up Question:
Strange behaviour in simple function.
def softthresh(u,LAMBDA):
for i in u:
if np.fabs(i)<=LAMBDA:
return 0
else:
return ((np.fabs(i)-LAMBDA)*u/np.fabs(i))
ll = 5.0
xx = np.arange(-10,11)
yy = softthresh(xx,ll)
What I get is not what I expect. for u (=xx ) array-elements that are smaller than 5 i should get zero. But i don't. Why?
You are calling return from inside the inner loop. Therefore, your function returns just after it evaluates the first member of u.
Since you are using NumPy, you should take advantage of NumPy's ability to operate on the whole array at once, and also of NumPy's smart indexing.
def softthreshold(u, LAMBDA):
notzero = np.fabs(u) > LAMBDA # find the indeces of elements that need to be scaled
rr = np.zeros_like(u) # an array the same size/type as u, already initialized to 0
rr[notzero] = (np.fabs(u[notzero])-LAMBDA)*u[notzero]/np.fabs(u[notzero]) # scale each of the members that aren't zero
return rr
Your problems depends on the numpy array. If you are working with a list it works.
Otherwise if you need the numpy array you can use code like
def softthresh(u,LAMBDA):
for i in u:
if np.fabs(i)<=LAMBDA:
return 0
else:
return ((np.fabs(u)-LAMBDA)*u/np.fabs(u))
You get the array through the dependency of <= logic and the numpy.array definition.
If u is an array, you need to loop through all its elements in your function.
Alternatively, you can have u be an element of your array and call it with a loop like this :
tbl = np.array([1, 2, 3, 4, 5])
for elt in tbl:
print(softthresh(elt, 3))
Results would be :
0
0
0
1.0
2.0