【问题标题】:Pandas : TypeError: float() argument must be a string or a numberPandas:TypeError:float() 参数必须是字符串或数字
【发布时间】:2017-05-06 12:20:00
【问题描述】:

我有一个包含

的数据框
user_id    date       browser  conversion  test  sex  age  country
   1    2015-12-03       IE        1         0    M   32.0   US

这是我迄今为止的全部代码!

data["country"].fillna("missing")
data["age"].fillna(-10000, inplace=True)
data["ads_channel"].fillna("missing")
data["sex"].fillna("missing")
data['date'] = pd.to_datetime(data.date)

columns = data.columns.tolist()
columns = [c for c in columns if c not in ["test"]]
from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=2, min_samples_leaf = (len(data)/100) )
clf = clf.fit(data[columns],data["test"])

我收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-560-95a8a54aa939> in <module>()
      4 from sklearn import tree
      5 clf = tree.DecisionTreeClassifier(max_depth=2, min_samples_leaf = (len(data)/100) )
----> 6 clf = clf.fit(data[columns],data["test"])

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\sklearn\tree\tree.pyc in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    152         random_state = check_random_state(self.random_state)
    153         if check_input:
--> 154             X = check_array(X, dtype=DTYPE, accept_sparse="csc")
    155             if issparse(X):
    156                 X.sort_indices()

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    371                                       force_all_finite)
    372     else:
--> 373         array = np.array(array, dtype=dtype, order=order, copy=copy)
    374 
    375         if ensure_2d:

TypeError: float() argument must be a string or a number

我仍在学习编码,我想知道如何克服这个错误。 任何帮助将不胜感激!

【问题讨论】:

  • 您似乎忘记了data["country"].fillna("missing") data["ads_channel"].fillna("missing") 和data["sex"].fillna("missing") 中的inplace=True
  • 所以使用data["country"].fillna("missing", inplace=True) data["ads_channel"].fillna("missing", inplace=True) data["sex"].fillna("missing", inplace=True)
  • 另一种可能的解决方案是data[["country", "ads_channel", "sex"]] = data[["country", "ads_channel", "sex"]].fillna("missing")
  • 这个错误与 pandas 或 sciky-learn 有关吗?我看到错误跟踪是在它的库上而不是在 pandas 上。

标签: python pandas


【解决方案1】:

IIUC 您还需要排除列date:

columns = [c for c in columns if c not in ["test", 'date']]

因为错误:

TypeError: float() 参数必须是字符串或数字,而不是 'Timestamp'

【讨论】:

  • 如果日期/时间是特征数据的一部分,应该怎么做?
  • 有人对此有答案吗?
  • @jezrael 您是否专门指向“转换为数字 unix 日期时间”?
  • @GregHilston - 是的,这是一种可能的解决方案
  • @jezrael 明白了。谢谢!我会看看什么对我有用,但非常感谢!
【解决方案2】:

保留日期(时间)列的解决方案:

data['date'] = pd.to_numeric(pd.to_datetime(data['date']))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2019-11-04
    • 2020-09-11
    • 1970-01-01
    • 2020-10-27
    • 2018-08-31
    • 2019-05-14
    • 2022-01-09
    相关资源
    最近更新 更多