该代码为利用线性回归预测销量 折线图
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso, Ridge
from sklearn.model_selection import GridSearchCV
if __name__ == "__main__":
# pandas读入
data = pd.read_csv('Advertising.csv') # TV、Radio、Newspaper、Sales
x = data[['TV', 'Radio', 'Newspaper']]
y = data['Sales']
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1,train_size=0.8) # random_state表示给定一个随机种子,数字任意; train_size=0.8表示训练数据80%,默认75%,若train_size=100表示训练数据100个
# model = Lasso() #Lasso回归使用L1正则
model = Ridge()#Ridge回归使用L2正则 其实需要参数alpha,表示正则项里的 lamda
alpha_can = np.logspace(-3, 2, 10)#从10的-3次方 到10的二次方 等比数列
np.set_printoptions(suppress=True)#numpy的输出以小数点形式输出0.001 不要类似1.000000e-0

这篇博客展示了如何运用线性回归算法预测产品销量,并通过代码实现数据处理和模型训练。文章中包含了线性回归模型的构建过程,以及使用交叉验证来评估模型的性能。内容以Python为编程语言,探讨了数据结构与算法在人工智能预测任务中的应用。
最低0.47元/天 解锁文章
953

被折叠的 条评论
为什么被折叠?



