2017-03-08 345 views

回答

5

你想astype

NA = NA.astype(float) 
+0

這是最有幫助的,謝謝 –

0

這就是:

import numpy as np 

A = ["33.33", "33.33", "33.33", "33.37"] 
for i in range(0,len(A)): 
    n = A[i] 
    n=float(n) 
    A[i] = n 

NA = np.asarray(A) 

AVG = np.mean(NA, axis=0) 
maxx = max(A) 
minn = min(A) 

print (AVG) 
print (maxx) 
print (minn) 

希望這有助於! 基本上,你沒有爲最小值和最大值,並在列表中的字符串函數必須是整數

+1

A碼轉儲,無解釋沒有用。 – TigerhawkT3

+0

如果列表無法更改,並且您不得不將其保留爲字符串,該怎麼辦?儘量不要做出假設。 – rayryeng

+0

這不起作用,因爲您並未轉換值,但未將它們放入列表中! – chthonicdaemon

2
import numpy as np 
A = ['33.33', '33.33', '33.33', '33.37'] 
# convert to float 
arr = np.array(map(float, A)) 
# calc values 
print np.mean(arr), np.max(arr), np.min(arr) 

輸出: 33.34 33.37 33.33

+1

請注意:這在Python 3中不起作用,因爲'map'不返回列表。 – chthonicdaemon

3

你有串

您的列表創建了一串字符串

您需要一個浮點數組進行後期處理;所以當你創建數組指定的數據類型,它會轉換成字符串在創建時浮

import numpy as np 

#list of strings 
A = ['33.33', '33.33', '33.33', '33.37'] 
print A 

#numpy of strings 
arr = np.array(A) 
print arr 

#numpy of float32's 
arr = np.array(A, dtype=np.float32) 
print arr 

#post process 
print np.mean(arr), np.max(arr), np.min(arr) 

>>> 
['33.33', '33.33', '33.33', '33.37'] 
['33.33' '33.33' '33.33' '33.37'] 
[ 33.33000183 33.33000183 33.33000183 33.36999893] 
33.34 33.37 33.33 

https://docs.scipy.org/doc/numpy/user/basics.types.html

2

A = [float(v) for v in ['33.33', '33.33', '33.33', '33.37']]

A = np.array(['33.33', '33.33', '33.33', '33.37'], dtype=float)

2

要將字符串轉換爲漂浮物,最簡單的方法是列表理解:

A = ['33.33', '33.33', '33.33', '33.37'] 
floats = [float(e) for e in A] 

現在你可以轉換成數組:

array_A = np.array(floats) 

剩下大概知道你:

mean, min, max = np.mean(array_A), np.min(array_A), np.max(array_A) 
相關問題