Pass numpy array as function argument [closed]

Pass numpy array as function argument [closed]



I want to pass a numpy array arr to what_a_function and print the resulting array, and this should be done without using loop. I know it is ugly and unfriendly, but I have to do so. I tried with vectorize but kept failing. May anyone share some pointers please? Thanks!


arr


what_a_function


vectorize


import numpy as np
def what_a_function(x):
return -np.cos(x.all()) * (0.5 < np.sin(x) < 2) + (np.sin(x) <= 0.5) + (x ** 2) * (np.sin(x) >= 2)

a=1
b=5

vfunc = np.vectorize(what_a_function)
arr = np.arange(a,b+0.1,0.1)

print(arr)
print(vfunc(arr))



And it will complain AttributeError: 'float' object has no attribute 'all'.


AttributeError: 'float' object has no attribute 'all'



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Please show an example input and output. Also, vectorize is basically just a convenience method that isn't going to be faster than a for loop. Since the code fails, it's not clear to me what x.all() was intended to do.
– roganjosh
Aug 31 at 15:38



vectorize


for


x.all()





Why are you using vectorize?
– juanpa.arrivillaga
Aug 31 at 15:39





The point of vectorize is to take a function that only works on one value at a time (e.g., a float) and turn it into a function that takes an array of values and automatically loops over them. That means your code should be expecting x to be a float, and using float methods on it, not NumPy array methods. Or, better, it should be using array methods, so you don’t need to use vectorize in the first place.
– abarnert
Aug 31 at 15:49


vectorize


x


vectorize





vectorize pass scalar elements of arr to what_a_function. Thus what_a_function must work with a simple numeric x.
– hpaulj
Aug 31 at 16:02


vectorize


arr


what_a_function


what_a_function


x





Thanks for all your reponse! I discovered later that it is because I forgot to remove x.all(). Sorry!
– Steven Shi
Sep 2 at 9:00


x.all()




2 Answers
2



That ' super ugly and unfriendly' equation doesn't make sense. What kind of x value is it supposed to evaluate?


x


-np.cos(x.all()) * (0.5 < np.sin(x) < 2) + (np.sin(x) <= 0.5) + (x ** 2) * (np.sin(x) >= 2)



x.all() requires an array (with all method), and returns a boolean (scalar or array), which is a nonsense input for np.cos.


x.all()


all


np.cos



np.sin(x) is ok with a scalar or array, but the 0.5<...<2 only works for a scalar (it's Python that doesn't work for numpy).


np.sin(x)


0.5<...<2



The next np.sin(x)<=.5 will produces a boolean (scalar or array). x**2 will be a numeric value.


np.sin(x)<=.5


x**2



The + and * will sort of work, converting the boolean True/False to 1/0 integers. But logical operators are better.


+


*



If we knew what is was supposed to do, we could probably write it to work directly with a numeric array. np.vectorize is not a good substitute for writing proper array compatible code. As I commented, vectorize passes the array values to the function one by one, as scalars. That's why the all method produces an error (and doesn't make sense). On top of that vectorize is slow.


np.vectorize


vectorize


all


vectorize



A straight forward list comprehension is faster:


np.array([your_function(i) for i in x])





Thanks for your clear and concise answer! It's later discovered that x,all() is redundant.
– Steven Shi
Sep 2 at 9:05


x,all()



The function all() takes list as its argument.
I made a tiny change to the code. I hope it helps!


import numpy as np
def what_a_function(x):
return all([-np.cos(x)]) * (0.5 < np.sin(x) < 2) + (np.sin(x) <= 0.5) + (x ** 2) * (np.sin(x) >= 2)

a=1
b=5

vfunc = np.vectorize(what_a_function)
arr = np.arange(a, b+0.1, 0.1)

print(arr)
print(vfunc(arr))

Output:[1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7
2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4. 4.1 4.2 4.3 4.4 4.5
4.6 4.7 4.8 4.9 5. ]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế