mr.m
mr.m

Reputation: 332

Covert numpy.ndarray to a list

I'm trying to convert this numpy.ndarray to a list

[[105.53518731]
 [106.45317529]
 [107.37373843]
 [108.00632646]
 [108.56373502]
 [109.28813113]
 [109.75593207]
 [110.57458371]
 [111.47960639]]

I'm using this function to convert it.

conver = conver.tolist()

the output is this, I'm not sure whether it's a list and if so, can I access its elements by doing cover[0] , etc

[[105.5351873125], [106.45317529411764], [107.37373843478261], [108.00632645652173], [108.56373502040816], [109.28813113157895], [109.75593206666666], [110.57458370833334], [111.47960639393939]]

finally, after I convert it to a list, I try to multiply the list members by 1.05 and get this error!

TypeError: can't multiply sequence by non-int of type 'float'

Upvotes: 0

Views: 99

Answers (4)

Niel_Eenterm
Niel_Eenterm

Reputation: 128

Try this:

import numpy as np

a = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639]]

b = [elem[0] for elem in a]
b = np.array(b)

print(b*1.05)

Upvotes: 0

hpaulj
hpaulj

Reputation: 231325

You start with a 2d array, with shape (n,1), like this:

In [342]: arr = np.random.rand(5,1)*100                                                                
In [343]: arr                                                                                          
Out[343]: 
array([[95.39049043],
       [19.09502087],
       [85.45215423],
       [94.77657561],
       [32.7869103 ]])

tolist produces a list - but it contains lists; each [] layer denotes a list. Notice that the [] nesting matches the array's:

In [344]: arr.tolist()                                                                                 
Out[344]: 
[[95.39049043424225],
 [19.095020872584335],
 [85.4521542296349],
 [94.77657561477125],
 [32.786910295446425]]

To get a number you have to index through each list layer:

In [345]: arr.tolist()[0]                                                                              
Out[345]: [95.39049043424225]
In [346]: arr.tolist()[0][0]                                                                           
Out[346]: 95.39049043424225
In [347]: arr.tolist()[0][0]*1.05                                                                      
Out[347]: 100.16001495595437

If you first turn the array into a 1d one, the list indexing is simpler:

In [348]: arr.ravel()                                                                                  
Out[348]: array([95.39049043, 19.09502087, 85.45215423, 94.77657561, 32.7869103 ])
In [349]: arr.ravel().tolist()                                                                         
Out[349]: 
[95.39049043424225,
 19.095020872584335,
 85.4521542296349,
 94.77657561477125,
 32.786910295446425]
In [350]: arr.ravel().tolist()[0]                                                                      
Out[350]: 95.39049043424225

But if your primary goal is to multiply the elements, doing with the array is simpler:

In [351]: arr * 1.05                                                                                   
Out[351]: 
array([[100.16001496],
       [ 20.04977192],
       [ 89.72476194],
       [ 99.5154044 ],
       [ 34.42625581]])

You can access elements of the array with:

In [352]: arr[0,0]                                                                                     
Out[352]: 95.39049043424225

But if you do need to iterate, the tolist() option is good to know. Iterating on lists is usually faster than iterating on an array. With an array you should try to use the fast whole-array methods.

Upvotes: 3

Hozayfa El Rifai
Hozayfa El Rifai

Reputation: 1442

yes, it's a list, you can check the type of a variable:

type(a)

to multiply each element with 1.05 then run the code below:

x = [float(i[0]) * 1.05 for i in a]
print(x)

Upvotes: 0

galaxyan
galaxyan

Reputation: 6111

you convert to list of list, so you could not broadcast.

import numpy as np
x = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639],]
x = np.hstack(x)
x * 1.05

array([110.81194668, 111.77583405, 112.74242535, 113.40664278,
       113.99192177, 114.75253769, 115.24372867, 116.1033129 ,
       117.05358671])

Upvotes: 0

Related Questions