scikit-learn是一个开源的、可用于商业的机器学习工具包,此工具包包含本课程中需要使用的许多算法的实现
Goals
In this lab you will utilize scikit-learn to implement linear regression using a close form solution based on the normal equation
Tools
You will utilize functions from scikit-learn as well as matplotlib and NumPy.
import numpy as np
np.set_printoptions(precision=2)
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.preprocessing import StandardScaler
from lab_utils_multi import load_house_data
import matplotlib.pyplot as plt
dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';
plt.style.use('./deeplearning.mplstyle')
Linear Regression, closed-form solution
Scikit-learn 有实现了closed-form 的线性回归模型 linear regression model
用之前lab中的数据
Size (1000 sqft) | Price (1000s of dollars) |
---|---|
1 | 300 |
2 | 500 |
Load the data set
X_train = np.array([1.0, 2.0]) #features
y_train = np.array([300, 500]) #target value
Create and fit the model
下面的代码使用scikit-learn执行回归
第一步是创建一个回归对象,第二步使用与对象关联的方法之一fit
,这将执行回归,将参数与输入数据拟合,该工具包需要一个二维的X矩阵
linear_model = LinearRegression(