python中基于日期的线性回归预测

2024-04-20 11:05:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经将日期转换成了数值,但我仍停留在下一步如何准备预测数据,如何在python代码中使用日期进行预测?如何计算eventoccurrent属性请指导我并改进我的代码,因为它没有任何意义。下面是我的代码

#Here is dataset

    date          Eventhappen
    2016-01-14    A
    2016-01-15    C
    2016-01-16    B
    2016-01-17    A
    2016-01-18    C
    2016-02-18    B

#Converting Date into Numerical Value

    df['Dispatch_Date_Time'] = pd.to_datetime(df['Dispatch_Date_Time'])
    df.set_index('Dispatch_Date_Time', inplace=True)
    df.sort_index(inplace=True)
    df['month'] = df.index.month
    df['year'] = df.index.year
    df['day'] = df.index.day
    df['eventhappen'] = 1

#Preparing the data

    X = df[['year']]
    y = df['eventhappen']

#Trainng the Algorithm
    regressor = LinearRegression()
    regressor.fit(X_train, y_train)

#Making the Predictions
    y_pred = regressor.predict(X_test)

#Plotting the Least Square Line
    sns.pairplot(df, x_vars=['year'], y_vars='eventhappen', size=7, aspect=0.7, kind='reg')

Tags: the代码truedfdateindextimetrain