在这个实验中,我们使用了 Kaggle 的在线服务来实现线性回归与时间序列分析。我们首先导入所需的库和数据集。然后,我们使用线性回归模型来拟合并预测销售数据。最后,我们使用 seaborn 和 matplotlib 库来绘制销售数据和预测结果的时间序列图。

本实验的目的是探索使用线性回归与时间序列。实验室的范围在这个链接中: https://www.kaggle.com/code/ryanholbrook/linear-regression-with-time系列

然后,您被要求在近 1800 个产品类别中为 Corporation acion Fau lita (一家位于厄瓜多尔的 大型杂货零售商) 实现销售预测: https://www.kaggle.com/code/scratchpad/notebook240dd34254

Setup feedback system

from learntools.core import binder binder.bind(globals()) from learntools.time_series.ex1 import *

Setup notebook

from pathlib import Path from learntools.time_series.style import * # plot style settings

import pandas as pd import matplotlib.pyplot as plt import numpy as np import seaborn as sns from sklearn.linear_model import LinearRegression

data_dir = Path('../input/ts-course-data/') comp_dir = Path('../input/store-sales-time-series-forecasting')

book_sales = pd.read_csv( data_dir / 'book_sales.csv', index_col='Date', parse_dates=['Date'], ).drop('Paperback', axis=1) book_sales['Time'] = np.arange(len(book_sales.index)) book_sales['Lag_1'] = book_sales['Hardcover'].shift(1) book_sales = book_sales.reindex(columns=['Hardcover', 'Time', 'Lag_1'])

ar = pd.read_csv(data_dir / 'ar.csv')

dtype = { 'store_nbr': 'category', 'family': 'category', 'sales': 'float32', 'onpromotion': 'uint64', } store_sales = pd.read_csv( comp_dir / 'train.csv', dtype=dtype, parse_dates=['date'], infer_datetime_format=True, ) store_sales = store_sales.set_index('date').to_period('D') store_sales = store_sales.set_index(['store_nbr', 'family'], append=True) average_sales = store_sales.groupby('date').mean()['sales'] fig, ax = plt.subplots() ax.plot('Time', 'Hardcover', data=book_sales, color='0.75') ax = sns.regplot(x='Time', y='Hardcover', data=book_sales, ci=None, scatter_kws=dict(color='0.25')) ax.set_title('Time Plot of Hardcover Sales');

View the solution (Run this line to receive credit!)

q_1.check()

线性回归时间序列分析实验 - Kaggle 实践

原文地址: https://www.cveoy.top/t/topic/bM4A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录