python3机器学习sklearn之线性回归
https://blog.csdn.net/hubingshabi/article/details/80172608
https://blog.csdn.net/hubingshabi/article/details/80181596
这个主要讲了关于一个披萨 根据尺寸变化 价格产生变化的例子
其中有尺寸对价格的 一元一次
尺寸 材料 对价格的多元一次
后面有多项式回归
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
X_train = [[6], [8], [10], [14], [18]]
y_train = [[7], [9], [13], [17.5], [18]]
X_test = [[6], [8], [11], [16]]
y_test = [[8], [12], [15], [18]]
regressor = LinearRegression()
regressor.fit(X_train, y_train)
xx = np.linspace(0, 26, 100)
yy = regressor.predict(xx.reshape(xx.shape[0], 1))
# plt = runplt(size=(8,8))
plt.plot(X_train, y_train, 'k.', label="train")
plt.plot(xx, yy, label="一元线性回归")
#多项式回归
quadratic_featurizer = PolynomialFeatures(degree=2)####修改这个degree 后面的数据 就可以变为几次的
X_train_quadratic = quadratic_featurizer.fit_transform(X_train)
X_test_quadratic = quadratic_featurizer.transform(X_test)
regressor_quadratic = LinearRegression()
#训练数据集用来fit拟合
regressor_quadratic.fit(X_train_quadratic, y_train)
xx_quadratic = quadratic_featurizer.transform(xx.reshape(xx.shape[0], 1))
#测试数据集用来predict预测
plt.plot(xx, regressor_quadratic.predict(xx_quadratic), 'r-', label="多项式回归")
plt.legend()
plt.show()
print(X_train)
print(X_train_quadratic)
print(X_test)
print(X_test_quadratic)
print('一元线性回归 r-squared', regressor.score(X_test, y_test))
print('二次回归 r-squared', regressor_quadratic.score(X_test_quadratic, y_test))
其中degree 处 degree 是几次就是几次的
https://blog.csdn.net/weixin_40683253/article/details/81109129
另外一个例子