线性回归算法

  

《机器学习》实验报告

实验名称:       线性回归算法           

目录

1.1问题描述

1.2问题求解

2.1问题描述

2.2问题求解


  • 问题

1.1问题描述

考察平面上3个点: (0,1) , (1,1),  (2,3)。

(1)请计算能够完美拟合这3个点的2次多项式。

(2)在线性模型假设 H={h=wx + b: w∈ℝ, b∈ℝ} ,假定损失是例2.2中的平方损失函数,请写出在训练数据集S={(0,1),(1,1),(2,3)}上经验损失最小化算法中的目标函数,并计算出最优的线性模型参数。

1.2问题求解

  1. 计算的公式为: y=x*x -x +1

代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 3, 1)
y = np.array([1,1,3])
z1 = np.polyfit(x, y, 2)#用2次多项式拟合
p1 = np.poly1d(z1)
print(p1) #在屏幕上打印拟合多项式

2. Y = x + 0.6667

代码:

方法1:

这里面的训练集和测试集都是本身。

from sklearn import datasets, linear_model
import matplotlib.pyplot as plt
import numpy as np


diabetes_x_train = [[0],[1],[2]]
diabetes_y_train = [[1],[1],[3]]

diabetes_x_test = [[0],[1],[2]]
diabetes_y_test = [[1],[1],[3]]

# 回归训练及预测
clf = linear_model.LinearRegression()
clf.fit(diabetes_x_train, diabetes_y_train)

print
'Coefficients :\n', clf.coef_
print("Residual sum of square: %.2f" % np.mean((clf.predict(diabetes_x_test) - diabetes_y_test) ** 2))
print("variance score: %.2f" % clf.score(diabetes_x_test, diabetes_y_test))

# 绘图
plt.title(u'LinearRegression Diabetes')  # 标题
plt.xlabel(u'Attributes')  # x轴坐标
plt.ylabel(u'Measure of disease')  # y轴坐标
# 点的准确位置
plt.scatter(diabetes_x_test, diabetes_y_test, color='black')
# 预测结果 直线表示
plt.plot(diabetes_x_test, clf.predict(diabetes_x_test), color='blue', linewidth=3)
plt.show()

方法2:

import matplotlib.pyplot as plt

import numpy as np
import linear_regression as lib

x = np.arange(0, 3, 1)
y = np.array([1,1,3])
z1 = np.polyfit(x, y, 1)
p1 = np.poly1d(z1)
print(p1) #在屏幕上打印拟合多项式
yvals=p1(x)#也可以使用yvals=np.polyval(z1,x)
plot1=plt.plot(x, y, '*',label='original values')
plot2=plt.plot(x, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('polyfitting')
plt.show()
plt.savefig('p1.png')

  • 问题

2.1问题描述

平面上3个点: (-1.0,-1.2) , (0.0,1.0),  (1.0,2.8)。

(1)请写出相应的正规方程。

(2)并通过求解正规方程来计算这3个点的最佳拟合。

2.2问题求解

y = 2 x + 0.8667

代码:

方法1:

from sklearn import datasets, linear_model
import matplotlib.pyplot as plt
import numpy as np
diabetes_x_train = [[-1],[0],[1]]
diabetes_y_train = [[-1.2],[1],[2.8]]
diabetes_x_test =[[-1],[0],[1]]
diabetes_y_test =[[-1.2],[1],[2.8]]
# 回归训练及预测
clf = linear_model.LinearRegression()
clf.fit(diabetes_x_train, diabetes_y_train)  # 注: 训练数据集

# 系数 残差平法和 方差得分
print
'Coefficients :\n', clf.coef_
print("Residual sum of square: %.2f" % np.mean((clf.predict(diabetes_x_test) - diabetes_y_test) ** 2))
print("variance score: %.2f" % clf.score(diabetes_x_test, diabetes_y_test))

# 绘图
plt.title(u'LinearRegression Diabetes')  # 标题
plt.xlabel(u'Attributes')  # x轴坐标
plt.ylabel(u'Measure of disease')  # y轴坐标
# 点的准确位置
plt.scatter(diabetes_x_test, diabetes_y_test, color='black')
# 预测结果 直线表示
plt.plot(diabetes_x_test, clf.predict(diabetes_x_test), color='blue', linewidth=3)
plt.show()

方法2:

import matplotlib.pyplot as plt
import numpy as np
import linear_regression as lib
x = np.array([-1,0,1.0])
y = np.array([-1.2,1,2.8])
z1 = np.polyfit(x, y, 1)
p1 = np.poly1d(z1)
print(p1) #在屏幕上打印拟合多项式
yvals=p1(x)#也可以使用yvals=np.polyval(z1,x)
plot1=plt.plot(x, y, '*',label='original values')
plot2=plt.plot(x, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('polyfitting')
plt.show()
plt.savefig('p1.png')

资料下载:

https://download.csdn.net/download/m0_61504367/85007898

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪程序猿

就当请我吃顿饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值