机器学习线性回归算法——多特征回归模型

多特征回归模型相比于前面的单特征模型只是添加了一个z轴,在表中取两列数据进行回归测试以及预测。

单特征回归见帖子:求助!!!机器学习线性回归问题-CSDN博客

以及:修改后的主函数:解决啦!!!上一个帖子找到错误了-CSDN博客

此多特征回归模型也只是对UnivariteLinearRegression.py文件进行修改即可

具体代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import plotly
import plotly.graph_objs as go

# plotly.offline.init_notebook_mode()
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_name1 = 'Economy..GDP.per.Capita.'
input_param_name2 = 'Freedom'
output_param_name = 'Happiness.Score'

x_train = train_data[[input_param_name1, input_param_name2]].values
y_train = train_data[[output_param_name]].values

x_test = test_data[[input_param_name1, input_param_name2]].values
y_test = test_data[[output_param_name]].values

plotly_training_trace = go.Scatter3d(
    x=x_train[:, 0].flatten(),
    y=x_train[:, 1].flatten(),
    z=y_train.flatten(),
    name='Training Set',
    mode='markers',
    marker={
        'size': 10,
        'opacity': 1,
        'line':{
            'color': 'rgb(255,255,255)',
            'width': 1
        },
    }
)

plotly_test_trace = go.Scatter3d(
    x=x_test[:, 0].flatten(),
    y=x_test[:, 1].flatten(),
    z=y_test.flatten(),
    name='Test Set',
    mode='markers',
    marker={
        'size': 10,
        'opacity': 1,
        'line': {
            'color': 'rgb(255,255,255)',
            'width': 1
        },
    }
)

plot_layout = go.Layout(
    title='Data Sets',
    scene={
        'xaxis': {'title': input_param_name1},
        'yaxis': {'title': input_param_name2},
        'zaxis': {'title': output_param_name},
    },
    margin={'l': 0, 'r': 0, 't': 0, 'b': 0}
)

plot_data = [plotly_training_trace, plotly_test_trace]
plot_figure = go.Figure(data=plot_data, layout=plot_layout)
plotly.offline.plot(plot_figure)

num_iterations = 500
learning_rate = 0.01
polynomial_degree = 0
sinusoid_degree = 0

linear_regression = LinearRegression(x_train, y_train, polynomial_degree, sinusoid_degree)
(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('Iterations')
plt.ylabel('Cost')
plt.title('Gradient Descent Progress')
plt.show()

predictions_num = 10

x_min = x_train[:, 0].min()
x_max = x_train[:, 0].max()

y_min = x_train[:, 1].min()
y_max = x_train[:, 1].max()

x_axis = np.linspace(x_min, x_max, predictions_num)
y_axis = np.linspace(y_min, y_max, predictions_num)

x_predictions = np.zeros((predictions_num * predictions_num, 1))
y_predictions = np.zeros((predictions_num * predictions_num, 1))

x_y_index = 0
for x_index, x_value in enumerate(x_axis):
    for y_index, y_value in enumerate(y_axis):
        x_predictions[x_y_index] = x_value
        y_predictions[x_y_index] = y_value
        x_y_index += 1

z_predictions = linear_regression.predict(np.hstack((x_predictions, y_predictions)))

plotly_predictions_trace = go.Scatter3d(
    x=x_predictions.flatten(),
    y=y_predictions.flatten(),
    z=z_predictions.flatten(),
    name='Prediction Plane',
    mode='markers',
    marker={
        'size': 1,
    },
    opacity=0.8,
    surfaceaxis=2,
)

plot_data = [plotly_training_trace, plotly_test_trace, plotly_predictions_trace]
plot_figure = go.Figure(data=plot_data, layout=plot_layout)
plotly.offline.plot(plot_figure)

此处用了plotly工具包,做出的回归图为3d模型,如下图所示:

由于加入了两个特征值,因此预测的结果也相比但特征来说更加准确,结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值