python 做回归
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 做回归相关的知识,希望对你有一定的参考价值。
1 一元线性回归
线性回归是一种简单的模型,但受到广泛应用,比如预测商品价格,成本评估等,都可以用一元线性模型。y = f(x) 叫做一元函数,回归意思就是根据已知数据复原某些值,线性回归(regression)就是用线性的模型做回归复原。
基本思想:已知一批(x, y)来复原另外未知的值,例如(1,1),(2,2),(3,3),那么(4,?),大家很容易知道(4,4),这就是一元线性回归求解问题
import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties from sklearn.linear_model import LinearRegression x = [[1], [2],[3], [4], [5], [6]] y = [[1], [2.1], [2.9], [4.2], [5.1], [5.8]] model = LinearRegression() model.fit(x, y) predicted = model.predict([13])[0] x2 = [[0], [2.5],[5.3],[7.6],[9.1]] y2 = model.predict(x2) print model.score(x2, y2) print predicted fonr = FontProperties() plt.figure() plt.title("linearRegression") plt.xlabel(‘x‘) plt.ylabel(‘y‘) plt.axis([0, 10, 0, 10]) plt.grid(True) plt.pause(0.4) plt.plot(x , y, ‘k.‘) plt.plot(x2 , y2, ‘g.‘) plt.show() print np.var(x2, ddof = 0) print model.score(x2, y2)
2 多元线性回归
假设方程为:,求解系数,因为不一定是方阵,所以不能直接求,所以我们对方程两边同乘以,则,由于是方阵,所以两边同时乘以的逆,就可以得到:。
设计二元一次方程:y=1+2x1+3x2
取样本为(1,1,1),(1,1,2),(1,2,1),计算得y=(6,9,8)
注意:这里面常数项1相当于1*x0,只不过这里的x0永远取1
X = [[1,1,1],[1,1,2],[1,2,1]]
y = [[6],[9],[8]]
from numpy.linalg import inv from numpy import dot, transpose from numpy.linalg import lstsq x = [[1,1,1], [1,1,2], [1,2,1]] y = [[6], [9], [8]] print dot(inv(dot(transpose(x),x)),dot(transpose(x), y)) ##===最小二乘法===================== print lstsq(x, y)[0] from sklearn.linear_model import LinearRegression x = [[1,1,1], [1,1,2], [1,2,1]] y = [[6], [9], [8]] model = LinearRegression() model.fit(x, y) x2 = [[1,3,5]] y2 = model.predict(x2) print y2
3 .采用多项式回归
import sys reload(sys) sys.setdefaultencoding(‘utf-8‘) import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression#导入线性回归模型 from sklearn.preprocessing import PolynomialFeatures# 导入多项式回归模型 plt.figure() plt.title(‘single variable‘) plt.xlabel(‘x‘) plt.ylabel(‘y‘) plt.axis([30, 400, 100, 400]) plt.grid(True) x = [[50], [100], [150], [200], [250], [300]] y = [[150], [200], [250], [280], [310], [330]] x_test = [[250], [300]] y_test = [[310], [330]] plt.plot(x, y, ‘g.‘,markersize =20) model = LinearRegression() x2 = [[30], [400]] model.fit(x, y) y2 = model.predict(x2) plt.plot(x2, y2,label = ‘$y = ax + c$‘) plt.legend()
xx = np.linspace(30, 400, 100) quadratic_featurizer = PolynomialFeatures(degree = 2)#实例化一个二次多项式 x_train_quadratic = quadratic_featurizer.fit_transform(x)#用二次多项式多样本x做变换 xx_quadratic = quadratic_featurizer.transform(xx.reshape(xx.shape[0], 1))# 把训练好X值的多项式特征实例应用到一系列点上,形成矩阵 regressor_quadratic = LinearRegression() regressor_quadratic.fit(x_train_quadratic, y) plt.plot(xx, regressor_quadratic.predict(xx_quadratic),label="$y = ax^2 + bx + c$")
plt.legend() cubic_featurizer = PolynomialFeatures(degree = 3) x_train_cubic = cubic_featurizer.fit_transform(x) xx_cubie = cubic_featurizer.transform(xx.reshape(xx.shape[0], 1)) regressor_cubic = LinearRegression() regressor_cubic.fit(x_train_cubic, y) plt.plot(xx, regressor_cubic.predict(xx_cubie),label="$y = a_1x^3 + a_2x^2 + a_3x +c $") plt.legend() print ‘一元线性回归 r-squared‘, model.score(x_test, y_test) x_test_quadratic = quadratic_featurizer.transform(x_test) print ‘二次性回归 r-squared‘, regressor_quadratic.score(x_test_quadratic, y_test) x_test_cubic = cubic_featurizer.transform(x_test) print ‘三次线性回归 r-squared‘, regressor_cubic.score(x_test_cubic, y_test) plt.show()
以上是关于python 做回归的主要内容,如果未能解决你的问题,请参考以下文章