Demo entry 6662182

1

   

Submitted by anonymous on Nov 25, 2017 at 03:33
Language: Python. Code size: 1.5 kB.

import numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import random as rd
import matplotlib.pyplot as plt

#read data
def read_boston_data():
     boston=load_boston()
     x=np.array(boston.data)
     y=np.array(boston.target)
     return x, y
x,y=read_boston_data()

#Normalizing training features 
normalized_x=preprocessing.normalize(x)

#Append variable X0=1
m=len(normalized_x)
x0=np.ones(m)
normalized_x=np.column_stack((x0,normalized_x))


#Partition the dataset
x_train, x_test, y_train, y_test=train_test_split(normalized_x,y,random_state=1,test_size=0.1)


#Define regression model
def model(B,X):
	y_hat=np.sum(X.dot(B), axis=0)
	return y_hat

#Variable
X=np.array(x_train)
B=np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#Define learning_rate & epochs
learning_rate=[0.00001, 0.0001 , 0.001 , 0.01 , 0.1 , 1] 


#Training & testing the model
for i in range (len(learning_rate)):
	rmse, h=[], []
	max_epochs=10
	for epoch in range(max_epochs):
		for j in range(0, len(X)):
			error=model(B,X[i])-y_train[j]
			B=B-learning_rate[i]*error*X[j]
		for k in range(0, len(x_test)):
			sum_error=0.0
			y_pred=model(B, x_test[k])
			prediction_error=y_pred-y_test[k]
			sum_error += (prediction_error)**2
		mean_error=sum_error/float(len(y_test)) 
		rmse.append(np.sqrt(mean_error))
		h.append(epoch+1)		
		rd.shuffle(X)
		epoch += 1
	plt.plot(h,rmse,color='black', linewidth=1) 
	plt.xticks(h)
	plt.show()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).