在构建模型时使用datetime64特征类型?

我有一个包含大约50个特征的数据框。我的实验中涉及到一个分类问题,因此我想通过“GradientBoostingClassifier”来训练模型。该数据框(mydata)被视为训练集。其中一个特征(feature20)是日期,我需要在训练集中也考虑这个特征,因此我尝试将日期转换为datetime64,如下所示:

  mydata['feature20']=pd.to_datetime(mydata['feature20'])

现在,当我尝试用分类器训练模型时,出现了以下错误:

  float() argument must be a string or a number, not 'Timestamp'

有什么解决这个问题的想法吗?


回答:

你可以轻松地将日期转换为整数:df[“feature20”].astype(“int64”) // 10**9.

注意: 除非你正在处理时间序列,否则最好不要直接使用datetime特征。通常,你会希望从datetime中提取更多的信息——比如星期几、月份的哪一天、年度的第几周、月份编号等


演示:

In [9]: df = pd.DataFrame({'feature20':pd.date_range('2010-01-01', periods=10)})In [10]: df["new"] = df["feature20"].astype("int64") // 10**9In [11]: dfOut[11]:   feature20         new0 2010-01-01  12623040001 2010-01-02  12623904002 2010-01-03  12624768003 2010-01-04  12625632004 2010-01-05  12626496005 2010-01-06  12627360006 2010-01-07  12628224007 2010-01-08  12629088008 2010-01-09  12629952009 2010-01-10  1263081600In [12]: df["date"] = pd.to_datetime(df["new"], unit="s")In [13]: dfOut[13]:   feature20         new       date0 2010-01-01  1262304000 2010-01-011 2010-01-02  1262390400 2010-01-022 2010-01-03  1262476800 2010-01-033 2010-01-04  1262563200 2010-01-044 2010-01-05  1262649600 2010-01-055 2010-01-06  1262736000 2010-01-066 2010-01-07  1262822400 2010-01-077 2010-01-08  1262908800 2010-01-088 2010-01-09  1262995200 2010-01-099 2010-01-10  1263081600 2010-01-10

如果你的日期精确到微秒:

In [28]: df = pd.DataFrame({'feature20':pd.date_range('2010-01-01 01:01:01.123456', freq="123S", periods=10)})In [29]: dfOut[29]:                   feature200 2010-01-01 01:01:01.1234561 2010-01-01 01:03:04.1234562 2010-01-01 01:05:07.1234563 2010-01-01 01:07:10.1234564 2010-01-01 01:09:13.1234565 2010-01-01 01:11:16.1234566 2010-01-01 01:13:19.1234567 2010-01-01 01:15:22.1234568 2010-01-01 01:17:25.1234569 2010-01-01 01:19:28.123456In [30]: df["new"] = df["feature20"].astype("int64") // 10**3In [31]: dfOut[31]:                   feature20               new0 2010-01-01 01:01:01.123456  12623076611234561 2010-01-01 01:03:04.123456  12623077841234562 2010-01-01 01:05:07.123456  12623079071234563 2010-01-01 01:07:10.123456  12623080301234564 2010-01-01 01:09:13.123456  12623081531234565 2010-01-01 01:11:16.123456  12623082761234566 2010-01-01 01:13:19.123456  12623083991234567 2010-01-01 01:15:22.123456  12623085221234568 2010-01-01 01:17:25.123456  12623086451234569 2010-01-01 01:19:28.123456  1262308768123456In [32]: df["date"] = pd.to_datetime(df["new"], unit="us")In [33]: dfOut[33]:                   feature20               new                       date0 2010-01-01 01:01:01.123456  1262307661123456 2010-01-01 01:01:01.1234561 2010-01-01 01:03:04.123456  1262307784123456 2010-01-01 01:03:04.1234562 2010-01-01 01:05:07.123456  1262307907123456 2010-01-01 01:05:07.1234563 2010-01-01 01:07:10.123456  1262308030123456 2010-01-01 01:07:10.1234564 2010-01-01 01:09:13.123456  1262308153123456 2010-01-01 01:09:13.1234565 2010-01-01 01:11:16.123456  1262308276123456 2010-01-01 01:11:16.1234566 2010-01-01 01:13:19.123456  1262308399123456 2010-01-01 01:13:19.1234567 2010-01-01 01:15:22.123456  1262308522123456 2010-01-01 01:15:22.1234568 2010-01-01 01:17:25.123456  1262308645123456 2010-01-01 01:17:25.1234569 2010-01-01 01:19:28.123456  1262308768123456 2010-01-01 01:19:28.123456

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注