+ 1

cannot add number in numpy array

I need to work only with arrays without converting m to list #------------------PROGRAM----------------- n = [i/10 for i in range(10)] m = [i for i in range(10)] m = np.array(m) for i, j in enumerate(n): m[i] = m[i] + j print(n) print(m) #------------------OUTPUT-------------------- [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] [0 1 2 3 4 5 6 7 8 9] #------------------what I need---------------- [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] [0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9] thanks for help

23rd Sep 2022, 12:10 PM
Kairav Bhatia
Kairav Bhatia - avatar
5 Respuestas
+ 1
Why don't you just add two numpy arrays? As m an n have the same dimensions, you can do: np.array(m) + np.array(n)
23rd Sep 2022, 12:35 PM
Lisa
Lisa - avatar
+ 1
Then how about an example code that makes this clear? If the arrays have the same number of elements, you can add them, when they have the same shape. You can reshape() numpy arrays. If they have different numbers if elements, please clarify what should happen in this case. You can link your code like this: Go to Code section, click +, select the programming language, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. This way other people can test your code and it is easier to read.
23rd Sep 2022, 1:00 PM
Lisa
Lisa - avatar
+ 1
Lisa , i'm on PC so there is no option here to add code #----------program----------- n = np.array([i/10 for i in range(10)]) weight_a = [i * 0 for i in range(10)] # creating weights weight_b = [i * 0 for i in range(10)] weight_c = [i * 0 for i in range(10)] m2 = np.array([weight_a, weight_b, weight_c]).transpose() print(n) for i, j in enumerate(n): # i = index , j = data m2[i][0] += j print(m2) #---------------------- when I print m2 the answer is still 0, n array elements not adding up in m2 array thankyou so much again
23rd Sep 2022, 1:46 PM
Kairav Bhatia
Kairav Bhatia - avatar
+ 1
Lisa #---------program-------------------- n = np.array([i/10 for i in range(10)]) weight_a = [i * 0 for i in range(10)] # creating weights weight_b = [i * 0 for i in range(10)] weight_c = [i * 0 for i in range(10)] m2 = np.array([weight_a, weight_b, weight_c]).transpose() print(n) m2 = m2.transpose() m2[0] = m2[0] + n print(m2) #-------output--------------------- [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] [[0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]] #---------expected output--------- [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] [[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]] thanks for helping
24th Sep 2022, 8:44 AM
Kairav Bhatia
Kairav Bhatia - avatar
0
Lisa this is example code, i want to add numbers from one 1D array to another 2D array
23rd Sep 2022, 12:55 PM
Kairav Bhatia
Kairav Bhatia - avatar