线性回归中的ML |正态方程

正态方程 是一种具有最小二乘成本函数的线性回归分析方法。我们可以直接求出θ的值,而无需使用 梯度下降 。在使用具有小功能的数据集时,采用这种方法是一种有效且省时的选择。 正态方程如下所示:

null

图片[1]-线性回归中的ML |正态方程-yiteyi-C++库

在上面的等式中, θ: 定义它的最佳假设参数。 X: 输入每个实例的特征值。 Y: 每个实例的输出值。

方程式背后的数学——

假设函数

图片[2]-线性回归中的ML |正态方程-yiteyi-C++库

哪里 n: 数据集中要素的数量。 十、 0 : 1(用于向量乘法) 注意,这是θ和x值之间的点积。为了便于求解,我们可以这样写:

图片[3]-线性回归中的ML |正态方程-yiteyi-C++库

线性回归的目的是最小化 成本函数 :

J(Theta)=frac{1}{2m}和{i=1}{m}frac{1}{2}[h{Theta}(x^{(i)})-y^{(i)}]

哪里 十、 : i的输入值 ih 训练范例。 m: 培训次数 n: 数据集功能的数量 Y : 我的预期结果 th 例子 让我们用向量的形式来表示代价函数。

图片[4]-线性回归中的ML |正态方程-yiteyi-C++库

我们在这里忽略了1/2米,因为它不会对工作产生任何影响。在计算梯度下降时,它用于数学上的方便。但这里不再需要它了。

图片[5]-线性回归中的ML |正态方程-yiteyi-C++库

图片[6]-线性回归中的ML |正态方程-yiteyi-C++库

十、 J : j值 ih 我的特色 ih 训练范例。 这可以进一步简化为

X	heta - y

但每个残值都是平方。我们不能简单地将上述表达式平方。因为向量/矩阵的平方不等于其每个值的平方。为了得到平方值,将向量/矩阵与其转置相乘。最后一个方程是

图片[8]-线性回归中的ML |正态方程-yiteyi-C++库

因此,成本函数是

图片[9]-线性回归中的ML |正态方程-yiteyi-C++库

现在用导数得到θ的值

图片[10]-线性回归中的ML |正态方程-yiteyi-C++库

图片[11]-线性回归中的ML |正态方程-yiteyi-C++库

图片[12]-线性回归中的ML |正态方程-yiteyi-C++库

图片[13]-线性回归中的ML |正态方程-yiteyi-C++库

图片[14]-线性回归中的ML |正态方程-yiteyi-C++库

图片[15]-线性回归中的ML |正态方程-yiteyi-C++库

图片[16]-线性回归中的ML |正态方程-yiteyi-C++库

所以,这是最终得出的结果 θ为最小成本值的正态方程。

例子:

Python3

# This code may not run on GFG IDE
# as required modules not found.
# import required modules
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
# Create data set.
x,y = make_regression(n_samples = 100 ,n_features = 1 ,n_informative = 1 ,noise = 10 ,random_state = 10 )
# Plot the generated data set.
plt.scatter(x,y,s = 30 ,marker = 'o' )
plt.xlabel( "Feature_1 --->" )
plt.ylabel( "Target_Variable --->" )
plt.title( 'Simple Linear Regression' )
plt.show()
# Convert  target variable array from 1d to 2d.
y = y.reshape( 100 , 1 )


图片[17]-线性回归中的ML |正态方程-yiteyi-C++库

让我们来实现正常方程:

Python3

# code
# Adding x0=1 to each instance
x_new = np.array([np.ones( len (x)),x.flatten()]).T
# Using Normal Equation.
theta_best_values = np.linalg.inv(x_new.T.dot(x_new)).dot(x_new.T).dot(y)
# Display best values obtained.
print (theta_best_values)


[[ 0.52804151] [30.65896337]]

尝试预测新数据实例:

Python3

# code
# sample data instance.
x_sample = np.array([[ - 2 ],[ 4 ]])
# Adding x0=1 to each instance.
x_sample_new = np.array([np.ones( len (x_sample)),x_sample.flatten()]).T
# Display the sample.
print ( "Before adding x0:" ,x_sample)
print ( "After adding x0:" ,x_sample_new)


Before adding x0: [[-2] [ 4]]After adding x0: [[ 1. -2.] [ 1.  4.]]

Python3

# code
# predict the values for given data instance.
predict_value = x_sample_new.dot(theta_best_values)
print (predict_value)


[[-60.78988524] [123.16389501]]

绘制输出:

Python3

# code
# Plot the output.
plt.scatter(x,y,s = 30 ,marker = 'o' )
plt.plot(x_sample,predict_value,c = 'red' )
plt.plot()
plt.xlabel( "Feature_1 --->" )
plt.ylabel( "Target_Variable --->" )
plt.title( 'Simple Linear Regression' )
plt.show()


图片[18]-线性回归中的ML |正态方程-yiteyi-C++库

使用sklearn LinearRegression类验证上述内容:

Python3

# code
# Verification.
from sklearn.linear_model import LinearRegression
lr = LinearRegression() # Object.
lr.fit(x,y) # fit method.
# Print obtained theta values.
print ( "Best value of theta:" ,lr.intercept_,lr.coef_,sep = '' )
#predict.
print ( "predicted value:" ,lr.predict(x_sample),sep = '' )


Best value of theta:[0.52804151][[30.65896337]]predicted value:[[-60.78988524] [123.16389501]]
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享