May-25-2020, 10:14 PM
import numpy as np x = np.zeros(10) np.add.at(x, i, 1) print(x)output
Output:[0. 1. 1. 0. 1. 0. 0. 0. 1. 0.]
I just don't get how this at()
method works. X becomes array that contains all zeroes. Then happens what?
add.at() method
|
May-25-2020, 10:14 PM
import numpy as np x = np.zeros(10) np.add.at(x, i, 1) print(x)output I just don't get how this at() method works. X becomes array that contains all zeroes. Then happens what?
May-26-2020, 04:53 AM
This is how at() method works in the above case
np.add.at(x,i,1) Here x will be array with all zeros as known, Then i will be indexing to the array x, suppose you have given i = range(0,5), then this considers x[0],x[1],x[2],x[3],x[4].. so i is used to provide index for the array you need to change. i can be tuple also. As you have given 1 as third attribute ,it adds one to the above indexes.. suppose your code was like this import numpy as np x = np.zeros(10) i = range(0,5) np.add.at(x, i, 1) print(x) output: [1. 1. 1. 1. 1. 0. 0. 0. 0. 0.] there fore adding one to all the indexes you have defined #np.add.at(x, [0,2,3,4], 1) giving i as tuple is also fine output for this will be as [1. 0. 1. 1. 1. 0. 0. 0. 0. 0.]
May-26-2020, 09:54 AM
@KavyaL please use proper code tags while posting
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs
May-26-2020, 10:56 AM
import numpy as np x = np.zeros(10) i = range(0,5) np.add.at(x, i, 1) print(x) Here x will be array with all zeros as known,Then i will be indexing to the array x, suppose you have given i = range(0,5), then this considers x[0],x[1],x[2],x[3],x[4].. so i is used to provide index for the array you need to change. i can be tuple also. As you have given 1 as third attribute ,it adds one to the above indexes..
May-26-2020, 09:18 PM
KavyaL, thank you for your explanation. I understand it perfectly. In my example, i is not defined. That's the mistake.
|
|