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()
机器学习之线性回归例子
最新推荐文章于 2024-05-21 10:00:23 发布