简体   繁体   English

从 int 到 float 的 Numpy 数组

[英]Numpy array from int to float

I have this code to extract some data from a csv.我有这段代码可以从 csv 中提取一些数据。 I can extract the data with no problem but I would like to change the numpy array from a int to a float.我可以毫无问题地提取数据,但我想将 numpy 数组从 int 更改为 float。 I know that the argument to do that is dtype=np.float32 but I keep having an error message when I do include this argument: This is the code:我知道这样做的参数是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]))

This is the code with the argument:这是带有参数的代码:

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))

And I do get this error message:我确实收到此错误消息:

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'

How can I solve this problem?我怎么解决这个问题?

numpy.median() has no argument dtype . numpy.median()没有参数dtype This is its output behaviour:这是它的输出行为:

RETURNS: median : ndarray回报中位数: ndarray

A new array holding the result (unless out is specified, in which case that array is returned instead).保存结果的新数组(除非指定了 out ,在这种情况下,将返回该数组)。 If the input contains integers, or floats of smaller precision than 64, then the output data-type is float64.如果输入包含整数或精度小于 64 的浮点数,则输出数据类型为 float64。 Otherwise, the output data-type is the same as that of the input.否则,输出数据类型与输入数据类型相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM