要求的是极值点,不是极值;
开始写代码:
(1)定义一个初始化函数:
1.对数据进行预处理操作;
2.先得到所有的特征个数;
3.初始化参数矩阵
import numpy as np
from utils.features import prepare_for_training #导入预处理的代码
class LinearRegression: #定义一个类
#初始化,需要用到的哪些参数,传参;
def __init__(self, data, labels, polynomial_degree=0, sinusoid_degree=0, normalize_data=True):
"""
1.对数据进行预处理操作
2.先得到所有的特征个数
3.初始化参数矩阵
"""
(data_processed,
features_mean,
features_deviation) = prepare_for_training(data, polynomial_degree, sinusoid_degree, normalize_data=True)
self.data = data_processed
self.labels = labels
self.features_mean = features_mean
self.features_deviation = features_deviation
self.polynomial_degree = polynomial_degree
self.sinusoid_degree = sinusoid_degree
self.normalize_data = normalize_data
num_features = self.data.shape[1]
self.theta = np.zeros((num_features, 1))
"""未完待续"""
(2) 编写梯度下降算法代码:
每一次迭代都需要对参数theta进行修改,因此在梯度下降方法里面嵌套了参数更新方法;
#定义训练函数,设置迭代次数为500次
def train(self, alpha, num_iterations=500):
#定义梯度下降方法
def gradient_descent(self, alpha, num_iterations):
"""
实际迭代模块,会迭代num_iterations次
"""
for _ in range(num_iterations):
self.gradient_step(alpha) #每一次更新都要对参数theta进行修改
#梯度下降参数更新计算方法
def gradient_step(self, alpha):
"""
梯度下降参数更新计算方法,注意是矩阵运算
"""
num_examples = self.data.shape[0] #数据个数
prediction = LinearRegression.hypothesis(self.data, self.theta) #预测值
delta = prediction - self.labels #差值=预测值减去真实值
theta = self.theta #原始的theta
theta = theta - alpha * (1 / num_examples) * (np.dot(delta.T, self.data)).T
self.theta = theta #更新过的theta
@staticmethod
#定义成静态方法,方便直接调用prediction = LinearRegression.hypothesis(self.data,self.theta)
def hypothesis(data, theta):
predictions = np.dot(data, theta) 预测值等于theta乘以数据值x;
return predictions
(3)加入可视化,求损失值
import numpy as np
from utils.features import prepare_for_training #导入预处理的代码
class LinearRegression: #定义一个类
#初始化,需要用到的哪些参数,传参;
def __init__(self, data, labels, polynomial_degree=0, sinusoid_degree=0, normalize_data=True):
"""
1.对数据进行预处理操作
2.先得到所有的特征个数
3.初始化参数矩阵
"""
(data_processed,
features_mean,
features_deviation) = prepare_for_training(data, polynomial_degree, sinusoid_degree, normalize_data=True)
self.data = data_processed
self.labels = labels
self.features_mean = features_mean
self.features_deviation = features_deviation
self.polynomial_degree = polynomial_degree
self.sinusoid_degree = sinusoid_degree
self.normalize_data = normalize_data
num_features = self.data.shape[1]
self.theta = np.zeros((num_features, 1))
def train(self, alpha, num_iterations=500):
"""
训练模块,执行梯度下降
"""
cost_history = self.gradient_descent(alpha, num_iterations)
return self.theta, cost_history
def gradient_descent(self, alpha, num_iterations):
"""
实际迭代模块,会迭代num_iterations次
"""
cost_history = []
for _ in range(num_iterations):
self.gradient_step(alpha)
cost_history.append(self.cost_function(self.data, self.labels))
return cost_history
def gradient_step(self, alpha):
"""
梯度下降参数更新计算方法,注意是矩阵运算
"""
num_examples = self.data.shape[0]
prediction = LinearRegression.hypothesis(self.data, self.theta)
delta = prediction - self.labels
theta = self.theta
theta = theta - alpha * (1 / num_examples) * (np.dot(delta.T, self.data)).T
self.theta = theta
def cost_function(self, data, labels):
"""
损失计算方法
"""
num_examples = data.shape[0]
delta = LinearRegression.hypothesis(self.data, self.theta) - labels
cost = (1 / 2) * np.dot(delta.T, delta) / num_examples
return cost[0][0]
@staticmethod
def hypothesis(data, theta):
predictions = np.dot(data, theta)
return predictions
def get_cost(self, data, labels):
data_processed = prepare_for_training(data,
self.polynomial_degree,
self.sinusoid_degree,
self.normalize_data
)[0]
return self.cost_function(data_processed, labels)
def predict(self, data):
"""
用训练的参数模型,与预测得到回归值结果
"""
data_processed = prepare_for_training(data,
self.polynomial_degree,
self.sinusoid_degree,
self.normalize_data
)[0]
predictions = LinearRegression.hypothesis(data_processed, self.theta)
return predictions
(4)单变量线性回归;
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from linear_regression import LinearRegression
data = pd.read_csv('../data/world-happiness-report-2017.csv')
# 得到训练和测试数据,80%和20%的比例;
train_data = data.sample(frac = 0.8)
test_data = data.drop(train_data.index)
# 输入和输出参数变量名称;
input_param_name = 'Economy..GDP.per.Capita.'
output_param_name = 'Happiness.Score'
# 训练数据的x和y传值;
x_train = train_data[[input_param_name]].values
y_train = train_data[[output_param_name]].values
# 测试数据的x和y传值;
x_test = test_data[input_param_name].values
y_test = test_data[output_param_name].values
# 开始画图;
plt.scatter(x_train,y_train,label='Train data')
plt.scatter(x_test,y_test,label='test data')
plt.xlabel(input_param_name)
plt.ylabel(output_param_name)
plt.title('Happy')
plt.legend()
plt.show()
运行结果为:
(5)训练线性回归模型
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from linear_regression import LinearRegression
data = pd.read_csv('../data/world-happiness-report-2017.csv')
# 得到训练和测试数据
train_data = data.sample(frac = 0.8)
test_data = data.drop(train_data.index)
input_param_name = 'Economy..GDP.per.Capita.'
output_param_name = 'Happiness.Score'
x_train = train_data[[input_param_name]].values
y_train = train_data[[output_param_name]].values
x_test = test_data[input_param_name].values
y_test = test_data[output_param_name].values
plt.scatter(x_train,y_train,label='Train data')
plt.scatter(x_test,y_test,label='test data')
plt.xlabel(input_param_name)
plt.ylabel(output_param_name)
plt.title('Happy')
plt.legend()
plt.show()
"""以下为新加入的代码"""
#定义迭代次数和学习率值
num_iterations = 500
learning_rate = 0.01
#引用之前写的线性回归的类,并进行实例化;
#得到训练后的theta值和历史迭代中的损失值;
linear_regression = LinearRegression(x_train,y_train)
(theta,cost_history) = linear_regression.train(learning_rate,num_iterations)
#打印初始损失值和训练后的损失值,看模型是否收敛了;
print ('开始时的损失:',cost_history[0])
print ('训练后的损失:',cost_history[-1])
#画图,展示每次迭代后的损失值大小,表明模型在不断收敛;
plt.plot(range(num_iterations),cost_history)
plt.xlabel('Iter')
plt.ylabel('cost')
plt.title('GD')
plt.show()
(6) 进行测试,得到线性回归方程;
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from linear_regression import LinearRegression
data = pd.read_csv('../data/world-happiness-report-2017.csv')
# 得到训练和测试数据
train_data = data.sample(frac = 0.8)
test_data = data.drop(train_data.index)
input_param_name = 'Economy..GDP.per.Capita.'
output_param_name = 'Happiness.Score'
x_train = train_data[[input_param_name]].values
y_train = train_data[[output_param_name]].values
x_test = test_data[input_param_name].values
y_test = test_data[output_param_name].values
plt.scatter(x_train,y_train,label='Train data')
plt.scatter(x_test,y_test,label='test data')
plt.xlabel(input_param_name)
plt.ylabel(output_param_name)
plt.title('Happy')
plt.legend()
plt.show()
num_iterations = 500
learning_rate = 0.01
linear_regression = LinearRegression(x_train,y_train)
(theta,cost_history) = linear_regression.train(learning_rate,num_iterations)
print ('开始时的损失:',cost_history[0])
print ('训练后的损失:',cost_history[-1])
plt.plot(range(num_iterations),cost_history)
plt.xlabel('Iter')
plt.ylabel('cost')
plt.title('GD')
plt.show()
"""新加入的代码"""
#定义预测的次数;需要预测的x值,运用训练好的模型对这组新的x值进行预测;
predictions_num = 100
x_predictions = np.linspace(x_train.min(),x_train.max(),predictions_num).reshape(predictions_num,1)
y_predictions = linear_regression.predict(x_predictions)
#画图
plt.scatter(x_train,y_train,label='Train data')
plt.scatter(x_test,y_test,label='test data')
#展示预测的曲线图;
plt.plot(x_predictions,y_predictions,'r',label = 'Prediction')
plt.xlabel(input_param_name)
plt.ylabel(output_param_name)
plt.title('Happy')
plt.legend()
plt.show()
(7)解读标准化的代码:
(x—均值)/ 标准差
修改损失函数: