ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

MachineLearningHomework1: LinearRegresshion (python)

2019-02-28 16:47:31  阅读:222  来源: 互联网

标签:LinearRegresshion plt r2 python clf MachineLearningHomework1 train test validati


算是自己整个完成的,没有参考别人的,留念。

import numpy as np
import pandas as pd
import math
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import pickle

#读取数据
data = pd.read_csv(r'G:\Code\Python\untitled\ML_wu_homework\machine-learning-ex1\ex1\ex1data1.txt', \
                   header=None, names=['Population', 'Profit'])

#数据的预处理
x = data['Population']
x = np.array(x)

#x = preprocessing.scale(x)
y = data['Profit']
y_raw = np.array(y)

#留出test数据集
test_num = int(math.ceil((len(x) * 0.2)))
x_test = x[-test_num:]
y_test = y[-test_num:]
x = x[:-test_num]
y = y[:-test_num]

#划分数据集
x_train, x_validation, y_train, y_validation = train_test_split(x, y, test_size=0.25)

#线性回归部分
clf = LinearRegression(n_jobs=-1)

clf.fit(x_train.reshape(-1, 1), y_train)

#保存结果,以免每次计算
with open('homework1.pickle', 'wb') as wf:
    pickle.dump(clf, wf)
with open('homework1.pickle', 'rb') as rf:
     clf = pickle.load(rf)

#输出拟合的r^2值
r2_train = clf.score(x_train.reshape(-1, 1), y_train)
print('train r2 is: ' + str(r2_train))

r2_validation = clf.score(x_validation.reshape(-1, 1), y_validation)
print('validation r2 is: ' + str(r2_validation))

r2_test = clf.score(x_test.reshape(-1, 1), y_test)
print('test r2 is: ' + str(r2_validation))

#画图,看看拟合结果
plt.figure(num=1)
y_train_pre = clf.predict(x_train.reshape(-1, 1))
plt.plot(x_train, y_train_pre, 'g')

plt.scatter(x_train, y_train)
plt.plot()

plt.figure(num=2)
y_validition_pre = clf.predict(x_validation.reshape(-1, 1))
plt.plot(x_validation, y_validition_pre, 'r')
plt.scatter(x_validation, y_validation)

plt.plot()
plt.show()

#输出拟合出来的系数
print('Estimated coefficients for the linear regression problem is: ')
print(clf.coef_)
print('Independent term in the linear model is: ')
print(clf.intercept_)







i

标签:LinearRegresshion,plt,r2,python,clf,MachineLearningHomework1,train,test,validati
来源: https://blog.csdn.net/qq_41704837/article/details/88034960

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有