代码实现

``````# -*- coding: utf-8 -*-
"""一元线性回归及代码实现.ipynb
"""

import numpy as np
import matplotlib.pyplot as plt

"""# 一、数据集"""

# 读入数据集
data = np.genfromtxt('data.csv', delimiter=',')
'''
np.genfromtxt('data.csv', delimiter=',')

'''
print(data[:5]) #

x = data[:,0]
y = data[:,1]
print(x,y)

# 将数据可视化
plt.scatter(x,y) # 散点图
plt.show()

"""# 二、建模"""

# 最小二乘法
def compute_error(b, k, x_data, y_data):
totalError = 0
for i in range(0, len(x_data)):
totalError += (y_data[i] - (k * x_data[i] + b)) ** 2

lr = 0.0001 # 学习率

# 最大迭代次数
epochs = 50

k = 0 # 斜率
b = 0 # 截距
for epoch in range(epochs):
# m = float(len(x))
# for j in range(0, len(x)):
#   b_grad += (1/m) * (((k * x[j]) + b) - y[j])
#   k_grad += (1/m) * x[j] * (((k * x[j]) + b) - y[j])
# 同步更新k和b，完成一次梯度下降算法
# 每迭代5次，输出一次图像
# if epoch % 5 == 0:
#   print(f'epoch{epoch}')
#   plt.plot(x,y,'b.') # b. 是指以蓝色的点显示
#   plt.plot(x,k*x+b,'r')
#   plt.show()

return k,b

print("Starting b = {0}, k = {1}, error = {2}".format(b, k, compute_error(b, k, x, y)))
print("Running...")
print("After {0} iterations b = {1}, k = {2}, error = {3}".format(epochs, b, k, compute_error(b, k, x, y)))

# 画图
plt.plot(x, y, 'b.')
plt.plot(x, k*x + b, 'r')
plt.show()
``````

sklearn实现

``````"""# 三、skleran"""

from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt

# 载入数据
data = np.genfromtxt("data.csv", delimiter=",")
x_data = data[:,0]
y_data = data[:,1]
plt.scatter(x_data,y_data)
plt.show()
print(x_data.shape)

x_data = data[:,0,np.newaxis] # 因为sklearn要求输入的数据是二维
y_data = data[:,1,np.newaxis]
# 创建并拟合模型
model = LinearRegression()
model.fit(x_data, y_data)

# 画图
plt.plot(x_data, y_data, 'b.')
plt.plot(x_data, model.predict(x_data), 'r')
plt.show()

``````

THE END