用 岭回归 预测交通流量

1、数据介绍

数据为某路口的交通流量监测数据,记录全年小时级别的车流量。

2、实验目的

根据已有的数据创建多项式特征,使用岭回归模型代替一般的线性模型,对 车流量的信息进行多项式回归。

这里写图片描述

3、代码

import numpy as np
import pandas as pd
from sklearn import cross_validation
from sklearn.linear_model import Ridge
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures

data = pd.read_csv('ridge.csv')
a = np.array(data)
plt.plot(a[:, 5])
plt.show()
x = a[:, 1:5]
y = a[:, 5]
poly = PolynomialFeatures(6)
x = poly.fit_transform(x)
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size = 0.2,random_state = 0)
clf = Ridge(alpha = 1.0, fit_intercept = True)
clf.fit(x_train, y_train)
clf.score(x_test, y_test)
start = 200
end = 300
time = np.arange(start, end)
time
y_pre = clf.predict(x)
plt.plot(time, y[start:end], 'b', label = 'real')
plt.plot(time, y_pre[start:end], 'r', label = 'predict')
plt.legend(loc = 'upper left')
plt.show()

输出:

这里写图片描述

这里写图片描述

  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值