多元线性回归

#DATASET: https://archive.ics.uci.edu/ml/datasets/Computer+Hardware

import
numpy as np import matplotlib.pyplot as plt import pandas as pd import sklearn import sklearn.preprocessing as pre
df=pd.read_csv('data\machinei\machine.data')
df.head()
 vendornameMYCTMMINMMAXCACHCHMINCHMAXPRPERP
0adviser32/60125256600025616128198199
1amdahl470v/72980003200032832269253
2amdahl470v/7a2980003200032832220253
3amdahl470v/7b2980003200032832172253
4amdahl470v/7c2980001600032816132132
#convert string to num
#X includes the first 8 attributes, Y is the target prediction

x1=pre.LabelEncoder().fit_transform(df.iloc[:,0])
x2=pre.LabelEncoder().fit_transform(df.iloc[:,1])
X=np.zeros((df.iloc[:,:-2].shape[0],8))
X[:,0]=x1
X[:,1]=x2
X[:,2:]=df.iloc[:,2:-2]
Y=df.iloc[:,-2] #goal
#convert string to num
#X includes the first 8 attributes, Y is the target prediction

x1=pre.LabelEncoder().fit_transform(df.iloc[:,0])
x2=pre.LabelEncoder().fit_transform(df.iloc[:,1])
X=np.zeros((df.iloc[:,:-2].shape[0],8))
X[:,0]=x1
X[:,1]=x2
X[:,2:]=df.iloc[:,2:-2]
Y=df.iloc[:,-2] #goal
#compute loss, train parameters

count=0
while True:
    count+=1
    predict_y=np.dot(X,W.T)+b
    rloss=np.dot((Y-predict_y).T,Y-predict_y)/(2*m)
    w_gradient=np.dot((Y-predict_y).T,X)*(-1.0/m)
    b_gradient=np.dot((Y-predict_y).T,np.ones(shape=[m,]))*(-1.0/m)
    #gradient descent
    W-=w_gradient*alpha
    b-=b_gradient*alpha
    if count%10000==0:
        print(rloss)
    if rloss<2500:
        print('MSEloss={}'.format(rloss))
        break
MSEloss=2456.133718645051
W
array([-0.02520069, -0.17961688, -0.00338619,  0.01356295,  0.00726707,
        0.16543731,  0.01494539,  0.13264917])

转载于:https://www.cnblogs.com/Aurora-Borealis/p/11542151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值