机器学习之线性回归例子

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
import pandas as pd
'''
    房子大小与price的关系
'''


import random


house_size = [random.randrange(70,200) for i in range(10000)]

distance_from_citycneter = [random.randrange(1,30) for i in range(10000)]

floor = [random.randrange(1,20) for i in range(10000)]

house_price = []

for i in range(10000):
    price = house_size[i]*random.randrange(5e4,10e4) + distance_from_citycneter[i] * -1e5 + floor[i]* 1e4 + random.randrange(1,1e6)
    house_price.append(price)


X = [[1,house_size[i],distance_from_citycneter[i],floor[i]] for i in range(10000)]
#print(X)

X_matrix = np.array(X)
print(X_matrix.shape)

y_matrix = np.array(house_price)

y_matrix = y_matrix.reshape(len(y_matrix),1) #10000 * 1
#print(y_matrix)


theta = [0 for i in range(4)]

theta_matrix = np.array(theta)
theta_matrix = theta_matrix.reshape(len(theta_matrix),1)
theta_matrix = theta_matrix.astype('float64')
#print(theta_matrix)
'''
def cost():
    cost = 0
    for i in range(10000):
        price = 0
        for j in range(4):
            price +=X[i][j]*theta[j]
        cost+=(price - house_price[i])**2
    return cost
#print(house_size)
print(cost())

def cost():
    diff = X_matrix.dot(theta_matrix) - y_matrix
    cost = np.sum(np.power(diff,2))
    return cost

print(cost())
'''

#损失函数
def cost_function(X_,thrta_,y_):
    y_pred = X_.dot(thrta_) # 10000 * 1
    #print(y_pred.shape)
    diff = y_pred - y_
    squard_error = np.power(diff,2)
    return np.sum(squard_error)/(2*len(y_))

#print(cost_function(X_matrix,theta_matrix,y_matrix))

#梯度
def gradident(X_,thrta_,y_):
    y_pred = X_.dot(thrta_)  # 10000 * 1
    #print(y_pred.shape)
    diff = y_pred - y_
    #print(diff.shape)
    gradient = (1/len(y_))*X_.T.dot(diff)   # 4*10000  10000*4
    return gradient

#print(gradident(X_matrix,theta_matrix,y_matrix))
#print(theta_matrix.shape)

max_iter = 20000 #迭代次数
learing_rate = 0.0000003
#[0.003,0.01,0.03,0.1,0.3]

for i in range(max_iter):
    theta_matrix -=gradident(X_matrix,theta_matrix,y_matrix) * learing_rate
    if (i+1) % 20 == 0:
        print(cost_function(X_matrix,theta_matrix,y_matrix))

#print(X_matrix.dot(theta_matrix))

plt.scatter(X_matrix[:,1],y_matrix ,  color='black')#散点输出
plt.plot(X_matrix[:,1], X_matrix.dot(theta_matrix), color='blue', linewidth=0.3)#预测输出
plt.show()



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值