我有这段代码可以从csv中提取一些数据。我可以毫无问题地提取数据,但我想将numpy数组从int更改为float。我知道执行此操作的参数是dtype = np.float32,但是当我包含此参数时,我一直收到错误消息: 这是代码:
import numpy as np
import pandas as pd
rainfall=pd.read_csv('Seattle2014.csv')['PRCP'].values
inches=rainfall / 254
inches.shape
rainy=(inches >0)
summer=(np.arange(365)-172<90) &(np.arange(365)-172>0)
print ("Mediam precipitation on rainy days in 2014 (inches): ",np.median(inches[rainy]))
print ("Mediam precipitation on summer days in 2014 (inches): ",np.median(inches[summer]))
print ("Maximum precipitation on summer days in 2014 (inches): ",np.max(inches[summer]))
print ("Median precipitation on non-summer days in 2014 (inches): ",np.median(inches[rainy & ~summer]))
这是带有参数的代码:
rainy=(inches >0)
summer=(np.arange(365)-172<90) &(np.arange(365)-172>0)
print ("Mediam precipitation on rainy days in 2014 (inches): ",np.median(inches[rainy], dtype=float))
我确实收到此错误消息:
TypeErrorTraceback (most recent call last) <ipython-input-136-29b7a435cc0e> in <module>()
1 rainy=(inches >0)
2 summer=(np.arange(365)-172<90) &(np.arange(365)-172>0)
----> 3 print ("Mediam precipitation on rainy days in 2014 (inches): ",np.median(inches[rainy], dtype=float))
4 print ("Mediam precipitation on summer days in 2014 (inches): ",np.median(inches[summer]))
5 print ("Maximum precipitation on summer days in 2014 (inches): ",np.max(inches[summer]))
TypeError: median() got an unexpected keyword argument 'dtype'
如何解决此问题?
答案 0 :(得分:1)
numpy.median()
没有参数dtype
。这是它的输出行为:
回报:中位数: ndarray
一个保存结果的新数组(除非指定了 out ,否则将返回该数组)。 如果输入包含整数或精度小于64的浮点数,则输出数据类型为float64。否则,输出数据类型与输入数据类型相同。