【问题标题】:ValueError: invalid literal for float(): 2017-03-18 19:22:51-07:00ValueError:float() 的无效文字:2017-03-18 19:22:51-07:00
【发布时间】:2018-06-08 09:14:16
【问题描述】:

我对 Python 编程比较陌生。当我尝试标准化主成分分析的数据时,我收到以下错误消息。

Python 版本:2.7.4。

df = pd.read_csv('E:/Downloads/Datasets/PCA_data.csv')
df.head()



  number_people                       date  timestamp  day_of_week  \
0             37  2015-08-14 17:00:11-07:00      61211            4   
1             45  2015-08-14 17:20:14-07:00      62414            4   
2             40  2015-08-14 17:30:15-07:00      63015            4   
3             44  2015-08-14 17:40:16-07:00      63616            4   
4             45  2015-08-14 17:50:17-07:00      64217            4   

   is_weekend  is_holiday  temperature  is_start_of_semester  \
0           0           0        71.76                     0   
1           0           0        71.76                     0   
2           0           0        71.76                     0   
3           0           0        71.76                     0   
4           0           0        71.76                     0   

   is_during_semester  month  hour  
0                   0      8    17  
1                   0      8    17  
2                   0      8    17  
3                   0      8    17  
4                   0      8    17 

x = df.iloc[:,1:8]  # all rows, all the features and no labels
y = df.iloc[:, 0]  # all rows, label only

# Scale the data to be between -1 and 1
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X = scaler.fit_transform(x)
X

X = scaler.fit_transform(x)
Traceback (most recent call last):

  File "<ipython-input-28-ce4e52c57a0a>", line 1, in <module>
    X = scaler.fit_transform(x)

  File "C:\Users\owner\Anaconda2\lib\site-packages\sklearn\base.py", line 494, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\owner\Anaconda2\lib\site-packages\sklearn\preprocessing\data.py", line 560, in fit
    return self.partial_fit(X, y)

  File "C:\Users\owner\Anaconda2\lib\site-packages\sklearn\preprocessing\data.py", line 583, in partial_fit
    estimator=self, dtype=FLOAT_DTYPES)

  File "C:\Users\owner\Anaconda2\lib\site-packages\sklearn\utils\validation.py", line 382, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)

ValueError: invalid literal for float(): 2017-03-18 19:22:51-07:00

请有人调查一下。非常感谢。

【问题讨论】:

  • 您在其中有一个日期列。你想用它做什么?
  • 谢谢..我要删除“日期”和“时间戳”列..

标签: python python-2.7 pandas scikit-learn


【解决方案1】:

date 列是一个字符串,没有数值,因此您不应对其进行缩放。此外,您也不应该考虑 timestamp 列,因为它是一个不断增加的值,您不会从 PCA 中获得任何有意义的信息。

因此我建议这样做:

x = df.iloc[:,3:8]  # all rows, except label, date and timestamp

【讨论】:

    【解决方案2】:

    StandardScaler 只能适应/转换浮点类型,如在 StandardScaler.partial_fit 中使用 dtype=FLOAT_DTYPES 调用 check_array 所指出的那样。

    如果您打算将日期列包含在分析中,则需要排除日期列或将其转换为浮点格式(unix 时间戳)。虽然我建议不要将时间完全作为一项功能。

    注意您还拥有timestamp 列,虽然它能够被StandardScaler 转换,但也应该从您的数据中排除。

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2022-07-28
      • 1970-01-01
      • 2013-01-15
      • 2018-02-19
      • 2018-12-30
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      相关资源
      最近更新 更多