我是靠谱客的博主 醉熏月亮,这篇文章主要介绍机器学习(3)-线性回归,现在分享给大家,希望可以做个参考。

线性回归

线性回归是估计输入数据与连续值输出数据之间关系的过程,数据通常是实数形式,目的是为了 估计满足映射关系的基本函数

  • 创建数据文件: data_singlevar.txt
复制代码
1
2
3
4
5
6
1,2 2,4.3 3,6.2 4,7.8 5,9.9 6,11.7
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
线性回归代码如下: test.py #-*- coding:utf-8 -*- from sklearn import linear_model import sys import numpy as np import matplotlib.pyplot as plt #创建训练模型 filename=sys.argv[1] x=[] y=[] with open(filename,'r') as f: for line in f.readlines(): xt,yt=[float(i) for i in line.split(',')] x.append(xt) y.append(yt) #使用百分之八十的数据作为训练数据集,其余作为测试训练集 num_training=int(0.8*len(x)) num_test=len(x)-num_training #训练数据 x_train=np.array(x[:num_training]).reshape((num_training,1)) y_train=np.array(y[:num_training]) #测试数据 x_test=np.array(x[:num_training]).reshape((num_training,1)) y_train=np.array(y[:num_training]) #创建线性回归对象 linear_regressor=linear_model.LinearRegression() #用训练数据集训练模型 linear_regressor.fit(x_train,y_train) y_train_pred=linear_regressor.predict(x_train) plt.figure() plt.scatter(x_train,y_train,color='green') plt.plot(x_train,y_train_pred,color='black',linewidth=4) plt.title('Test Data') plt.show()
  • 执行程序 python test.py data_singlevar.txt

  • 输出预测图形
    这里写图片描述

最后

以上就是醉熏月亮最近收集整理的关于机器学习(3)-线性回归的全部内容,更多相关机器学习(3)-线性回归内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部