Simple question, confusing output:
np.array([-1, 0, 1]).any() < 0
out: False (why??)
but:
np.any(np.array([-1, 0, 1]))
out: True (as expected)
Acoording to documentation both commands should be equivalent, but they aren't.
Numpy is used to provide a method and a function that do the exact same thing.
assert my_array.any() == numpy.any(my_array)
Here my_array.any() is the method and numpy.any(my_array) is the function.
It both return a Boolean. Here you ask why np.array([-1, 0, 1]).any() < 0 returns False because np.array([-1, 0, 1]).any() is True which is equal to the value 1 and you ask if it is < 0 which is False.
import numpy as np
my_array = np.array([-1, 0, 1])
assert my_array.any() == True
assert my_array.any() == 1
assert my_array.any() > 0
assert np.any(my_array) == True
assert my_array.any() == np.any(my_array)
Related
def assignGroup(row):
if row["E114"]=="Very good":
return 1
elif row['E114']=="Fairly good":
return 2
elif row['E114'] =="Bad":
return 3
elif row['E114'] =="Very bad":
return 4
else:
return np.nan
outcome["leader"]=outcome.apply(assignGroup,axis=1)
outcome["leader"] = outcome["E114"].map({
"Very good" : 1,
"Fairly good": 2,
"Bad": 3,
"Very bad": 4
})
Use numpy's where:
import numpy as np
outcome["leader"] = np.where(outcome["E114"] == "Very good", 1, outcome["leader"])
outcome["leader"] = np.where(outcome["E114"] == "Fairly goo", 2, outcome["leader"])
outcome["leader"] = np.where(outcome["E114"] == "Bad", 3, outcome["leader"])
outcome["leader"] = np.where(outcome["E114"] == "Very bad", 4, outcome["leader"])
In Python loops should be last resource
RuntimeWarning: invalid value encountered in multiply
I have a code:
a = Y_list * np.log(Y_list/E_Y)
print(a)
My Y_list contains 0 values, I'm wondering how to do when Y_list = 0 , np.log(0) = 0?
You can use np.where It lets you define a condition for true and false and assign different values.
np.where((Y_list/E_Y)!= 0, np.log(Y_list/E_Y),0)
Alternatively, we can run np.log with a where parameter:
import numpy as np
a = np.arange(0, 5000, 1000)
np.log(a, where=a != 0)
# array([0. , 6.90775528, 7.60090246, 8.00636757, 8.29404964])
I am having problem validating the user input (I am asking user if they wish to continue with the program of calculating factorial). The code is as follows: (User input validation is towards the end of the main function and I have not included the factorial function)
def main():
valid_inp = False
usr_continue = True
while usr_continue:
while valid_inp == False:
usr_inp = int(input('Please ENTER a number: '))
if usr_inp < 0:
print('ERROR, INVALID INPUT')
else:
valid_inp = True
continue
result = factorial(usr_inp)
print(str(result) + '\n')
con_inp = str(input('Would you like to continue ? '))
if con_inp == 'Y' or con_inp == 'y':
usr_continue
elif con_inp == 'N' or con_inp == 'n':
print('Goodbye...')
break
main()
Make a function that only returns on valid input. Use an exception handler to deal with bad integer input, then validate the integer is the range you want:
from math import factorial
def get_nonnegative_integer(prompt):
while True:
try:
val = int(input(prompt)) # bad input for int such as "abc" will raise ValueError
if val >= 0: # good input will be range-checked
return val
else:
print('enter a number >= 0')
except ValueError:
print('invalid input for integer')
def main():
while True:
usr_inp = get_nonnegative_integer('Please enter a number: ')
result = factorial(usr_inp)
print(result)
con_inp = input('Would you like to continue(Y/n)? ').upper() # default Yes
if con_inp.startswith('N'):
print('Goodbye...')
break
main()
I'm having the following error using NumPy:
>>> distance = 0.9014179933248182
>>> min_distance = np.array([0.71341723, 0.07322284])
>>> distance < min_distance
array([False, False])
which is right, but when I try:
>>> distance < min_distance.any()
True
which is obviously wrong, since there is no number in 'min_distance' smaller than 'distance'
What is going on here? I'm using NumPy on Google Colab, on version '1.17.3'.
Whilst numpy bugs are common, this is not one. Note that min_distance.any() returns a boolean result. So in this expression:
distance < min_distance.any()
you are comparing a float with a boolean, which unfortunately works, because of a comedy of errors:
bool is a subclass of int
True is equal to 1
floats are comparable with integers.
E.g.
>>> 0.9 < True
True
>>> 1.1 < True
False
What you wanted instead:
>>> (distance < min_distance).any()
False
try (distance < min_distance).any()
I have two vectors (or two one dimensional numpy arrays with the same number of elements) a and b where I want to find the number of cases I have that:
a < 0 and b >0
But when I type the above (or something similar) into IPython I get:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
How am I supposed to do the above operation?
Thank you
I'm not certain that I understand what you're trying to do, but you might want ((a < 0) & (b > 0)).sum()
>>> a
array([-1, 0, 2, 0])
>>> b
array([4, 0, 5, 3])
>>> a < 0
array([ True, False, False, False], dtype=bool)
>>> b > 0
array([ True, False, True, True], dtype=bool)
>>> ((a < 0) & (b > 0)).sum()
1