Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用scikit学习的时间戳_Python_Python 3.x_Datetime_Pandas_Scikit Learn - Fatal编程技术网

Python 使用scikit学习的时间戳

Python 使用scikit学习的时间戳,python,python-3.x,datetime,pandas,scikit-learn,Python,Python 3.x,Datetime,Pandas,Scikit Learn,sklearn分类器接受pandas的时间戳(=datetime64[ns])作为X中的一列,只要所有X列都是该类型。但是当同时存在TimeStamp和float列时,sklearn拒绝使用TimeStamp 除了使用astype(int)将时间戳转换为int)之外,还有其他解决方法吗?(我仍然需要原始列来访问dt.year等,因此理想情况下,我不希望仅仅为了提供一个要学习的特性而创建一个重复的列。) 将熊猫作为pd导入 从sklearn.linear\u模型导入线性回归 测试=pd.日期范围(

sklearn分类器接受pandas的
时间戳
(=
datetime64[ns]
)作为X中的一列,只要所有X列都是该类型。但是当同时存在
TimeStamp
float
列时,sklearn拒绝使用TimeStamp

除了使用astype(
int
)将时间戳转换为
int
)之外,还有其他解决方法吗?(我仍然需要原始列来访问
dt.year
等,因此理想情况下,我不希望仅仅为了提供一个要学习的特性而创建一个重复的列。)

将熊猫作为pd导入
从sklearn.linear\u模型导入线性回归
测试=pd.日期范围('20000101',周期=100)
test_df=pd.DataFrame({'date':test})
测试_df['a']=1
测试_df['y']=1
lr=线性回归()
lr.fit(test_df['date']],test_df['y'])可以正常工作
lr.fit(test_df['date','date']],test_df['y'])#工作正常
lr.fit(测试测向['date','a']],测试测向['y'])投诉
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 lr.配合(测试测向['date','a']],测试测向['y']
/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/linear\u model/base.py(自身、X、y、样本重量)
434 n\u jobs=self.n\u jobs
435 X,y=check_X_y(X,y,accept_sparse=['csr','csc','coo'],
-->436 y_数值=真,多输出=真)
437
438如果((样品重量不是无)且np至少为1d(
/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/utils/validation.py in check_X_y(X,y,接受稀疏、数据类型、顺序、复制、强制所有有限、确保二维、允许多输出、确保最小样本、确保最小特征、数字、警告数据类型、估计器)
521 X=检查数组(X,接受稀疏,数据类型,顺序,复制,强制所有有限,
522确保2d,允许nd,确保最小样本,
-->523确保功能、警告(数据类型、估计器)
524如果多输出:
525 y=检查数组(y,'csr',强制所有有限=真,确保2d=假,
/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/utils/validation.py in check_数组(数组、接受稀疏、数据类型、顺序、复制、强制所有有限、确保2d、允许nd、确保最小样本、确保最小特征、警告数据类型、估算器)
402#确保我们准确地转换为数字:
403如果dtype_numeric和array.dtype.kind==“O”:
-->404数组=array.astype(np.float64)
405如果不允许\u nd和array.ndim>=3:

406 raise VALUERROR(“找到的数组应该是dim%d.%s您可以将其转换为正确的整数或浮点值

test_df['date'] = test_df['date'].astype(int)

您希望拟合X和y,其中X是特征(2个或更多),y是目标。将datetimeindex用作时间序列,而不是特征。在我的示例中,我拟合mag>7的地震,并计算每次地震之间经过的天数。经过的天数、深度、纬度和经度将输入线性回归分类器

 events=df[df.mag >7]
 events=events.sort_index()

 index=0
 #dates ascending False
 events['previous']=events.index
 for key,item in events.iterrows():
      if index>0:
          events.loc[key,'previous']=events.index.values[index-1]
          events.loc[key,'time_delta']=events.index.values[index]-events.index.values[index-1]
          index+=1

events['elapsed_days']=events['time_delta'].apply(lambda x: np.nan_to_num(x.days))

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

X=events[['latitude','longitude','elapsed_days','depth']]
y=np.nan_to_num(events['mag'])
X_train,X_test,y_train, y_test=train_test_split(X,y,test_size=0.3,random_state=42)

lr = LinearRegression()

lr.fit(X,y)
y_pred=lr.predict(X_test)

fig, ax= plt.subplots()
ax.plot(X_test['elapsed_days'],y_pred)
plt.title('Magnitude Prediction')
plt.show()
fig, ax= plt.subplots()
ax.plot(events.index,np.nan_to_num(events['mag']))
plt.xticks(rotation=90)
plt.legend(['Magnitude'])
twin_ax=ax.twinx()
twin_ax.plot(events.index,events['elapsed_days'],color='red')
plt.legend(['Elapsed Days'],loc=1)
plt.show()

你解决问题了吗?你能分享你的解决方案吗?你必须至少有两个线性回归功能才能工作为什么会有反对票?我支持这个,并将转换为秒,如中所示
 events=df[df.mag >7]
 events=events.sort_index()

 index=0
 #dates ascending False
 events['previous']=events.index
 for key,item in events.iterrows():
      if index>0:
          events.loc[key,'previous']=events.index.values[index-1]
          events.loc[key,'time_delta']=events.index.values[index]-events.index.values[index-1]
          index+=1

events['elapsed_days']=events['time_delta'].apply(lambda x: np.nan_to_num(x.days))

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

X=events[['latitude','longitude','elapsed_days','depth']]
y=np.nan_to_num(events['mag'])
X_train,X_test,y_train, y_test=train_test_split(X,y,test_size=0.3,random_state=42)

lr = LinearRegression()

lr.fit(X,y)
y_pred=lr.predict(X_test)

fig, ax= plt.subplots()
ax.plot(X_test['elapsed_days'],y_pred)
plt.title('Magnitude Prediction')
plt.show()
fig, ax= plt.subplots()
ax.plot(events.index,np.nan_to_num(events['mag']))
plt.xticks(rotation=90)
plt.legend(['Magnitude'])
twin_ax=ax.twinx()
twin_ax.plot(events.index,events['elapsed_days'],color='red')
plt.legend(['Elapsed Days'],loc=1)
plt.show()