|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pandas as pd
- import matplotlib.pyplot as plt
- import numpy as np
- import os
- os.chdir(r'C:\Users\Administrator\Desktop')
- data = pd.read_table('1.txt',sep = '\s*')
- plt.scatter(data['Height'],data['Weight'])
- #进行标准化
- a = data['Height'].values
- a_ = (a - a.mean()) / a.std()
- b = data['Weight'].values
- b_ = (b - b.mean()) / b.std()
- ###Python线性回归的两种实现
- import lines
- theat = np.array([0,0])
- y = lines.function1(data['Height'].values[:,np.newaxis],data['Weight'].values[:,np.newaxis].T)
- h = lines.function2(a_[:,np.newaxis],
- b_[:,np.newaxis].T,
- rate=0.01,theat=theat,num=19,minnum=0.000001)
- ##进行逆标准化
- h[0],h[1] = h[0] * b.std() - h[1] * b.std() / a.std() * a.mean() + b.mean(),\
- h[1] * b.std() / a.std()
- print y , h
- ##和sklearn库进行比较
- from sklearn import linear_model
- linreg = linear_model.LinearRegression()
- linreg.fit(data['Height'].values[:,np.newaxis],data['Weight'].values[:,np.newaxis])
- print linreg.coef_
- print linreg.intercept_
- linre = linear_model.LinearRegression()
- linre.fit(a_[:,np.newaxis],
- b_[:,np.newaxis])
- print linreg.coef_
- print linreg.intercept_
- y_ = linreg.predict(a[:,np.newaxis])
- plt.plot(a,y_)
- plt.subplots()
- plt.scatter(a_,b_)
- y__ = linre.predict(a_[:,np.newaxis])
- plt.plot(a_,y__)
- plt.show()
复制代码
最小二乘[[-143.02691844]
[ 3.89903027]]
梯度下降 [[-142.99965677]
[ 3.89859294]]
sklearn库
[[ 3.89903027]]
[-143.02691844]
[[ 3.89903027]]
[-143.02691844]
数据在附件中
|
-
原始
-
标准化后
-
-
1.txt
818 Bytes, 下载次数: 4
评分
-
查看全部评分
|