该代码为利用线性回归预测销量 折线图
#!/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.lo