lhxxhl 2020-01-14
学习 Linear Regression in Python – Real Python,前面几篇文章分别讲了“regression怎么理解“,”线性回归怎么理解“,现在该是实现的时候了。
Numpy
:数据源scikit-learn
:MLstatsmodels
: 比 scikit-learn
功能更强大以最简单的线性回归为例,代码参考的是原文。
重点是掌握基本思路,以及关键的几个函数。影响拟合度的因素很多,数据源首当其冲,模型的选择也是关键,这些在实际应用中具体讨论,这里就简单的对应前面的基本思路将 sample 代码及运行结果贴一下,稍加解释。
根据自己的需要导入
pip install scikit-learn pip install numpy pip install statsmodels from sklearn.preprocessing import PolynomialFeatures import numpy as np from sklearn.linear_model import LinearRegression import statsmodels.api as sm
""" prepare data
x: regressor
y: predictor
reshape: make it two dimentional - one column and many rows
y can also be 2 dimensional
"""
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1)) """ [[ 5] [15] [25] [35] [45] [55]] """ y = np.array([5, 20, 14, 32, 22, 38]) print(x, y) # [ 5 20 14 32 22 38]
'''create a model and fit it''' model = LinearRegression() model = model.fit(x, y) print(model) # LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
'''get result y = b0 + b1x ''' r_sq = model.score(x, y) print('coefficient of determination(??2) :', r_sq) # coefficient of determination(??2) : 0.715875613747954 print('intercept:', model.intercept_) # (标量) 系数b0 intercept: 5.633333333333329 -------this will be an array when y is also 2-dimensional print('slope:', model.coef_) # (数组)斜率b1 slope: [0.54] ---------this will be 2-d array when y is also 2-dimensional
'''predict response given x, get y from the model y = b0+b1x ''' y_pred = model.predict(x) print('predicted response:', y_pred, sep='\n') #predicted response: #[8.33333333 13.73333333 19.13333333 24.53333333 29.93333333 35.33333333] '''forecast''' z = np.arange(5).reshape((-1, 1)) y = model.predict(z) print(y) #[5.63333333 6.17333333 6.71333333 7.25333333 7.79333333]
2020-01-14 init
本文由博客一文多发平台 OpenWrite 发布!