波士顿房价预测
代码
import numpy as np
import matplotlib. pyplot as plt
def load_data ( ) :
datafile = 'D:\Python\PythonProject\sklearn\housing.data'
data = np. fromfile( datafile, sep= ' ' )
feature_names = [ 'CRIM' , 'ZN' , 'INDUS' , 'CHAS' , 'NOX' , 'RM' , 'AGE' , \
'DIS' , 'RAD' , 'TAX' , 'PTRATIO' , 'B' , 'LSTAT' , 'MEDV' ]
feature_num = len ( feature_names)
data = data. reshape( [ data. shape[ 0 ] // feature_num, feature_num] )
ratio = 0.8
offset = int ( data. shape[ 0 ] * ratio)
training_data = data[ : offset]
maximums, minimums, avgs = training_data. max ( axis= 0 ) , training_data. min ( axis= 0 ) , \
training_data. sum ( axis= 0 ) / training_data. shape[ 0 ]
for i in range ( feature_num) :
data[ : , i] = ( data[ : , i] - avgs[ i] ) / ( maximums[ i] - minimums[ i] )
training_data = data[ : offset]
test_data = data[ offset: ]
return training_data, test_data
class Network ( object ) :
def __init__ ( self, num_of_weights) :
self. w = np. random. randn( num_of_weights, 1 )
self. b = 0.
def forward ( self, x) :
z = np. dot( x, self. w) + self. b
return z
def loss ( self, z, y) :
error = z - y
num_samples = error. shape[ 0 ]
cost = error * error
cost = np. sum ( cost) / num_samples
return cost
def gradient ( self, x, y) :
z = self. forward( x)
N = x. shape[ 0 ]
gradient_w = 1. / N * np. sum ( ( z- y) * x, axis= 0 )
gradient_w = gradient_w[ : , np. newaxis]
gradient_b = 1. / N * np. sum ( z- y)
return gradient_w, gradient_b
def update ( self, gradient_w, gradient_b, eta = 0.01 ) :
self. w = self. w - eta * gradient_w
self. b = self. b - eta * gradient_b
def train ( self, training_data, num_epoches, batch_size= 10 , eta= 0.01 ) :
n = len ( training_data)
losses = [ ]
for epoch_id in range ( num_epoches) :
np. random. shuffle( training_data)
mini_batches = [ training_data[ k: k+ batch_size] for k in range ( 0 , n, batch_size) ]
for iter_id, mini_batch in enumerate ( mini_batches) :
x = mini_batch[ : , : - 1 ]
y = mini_batch[ : , - 1 : ]
a = self. forward( x)
loss = self. loss( a, y)
gradient_w, gradient_b = self. gradient( x, y)
self. update( gradient_w, gradient_b, eta)
losses. append( loss)
print ( 'Epoch {:3d} / iter {:3d}, loss = {:.4f}' .
format ( epoch_id, iter_id, loss) )
return losses
train_data, test_data = load_data( )
net = Network( 13 )
losses = net. train( train_data, num_epoches= 50 , batch_size= 100 , eta= 0.1 )
plot_x = np. arange( len ( losses) )
plot_y = np. array( losses)
plt. plot( plot_x, plot_y)
plt. show( )
x_test = test_data[ : , : - 1 ]
y_test = test_data[ : , - 1 : ]
score = net. loss( net. forward( x_test) , y_test)
运行结果