Uploaded by Pavithra ravi

Prediction Modelling with Neural Network Assignment copy 2

advertisement
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
CASE STUDY 1 PREDICTION MODELLING WITH NEURAL NETWORK
#import library
import pandas as pd
from pandas import DataFrame
import numpy as np
#import datasets
df_train = pd.read_csv("cs1_training.csv")
df_test = pd.read_csv("cs1_testing.csv")
#select predictor and target variables
x_train = df_train.iloc[:, 1:6]
y_train = df_train['TTF']
x_test = df_test.iloc[:,1:6]
y_test = df_test['TTF']
#train the model with neural network
from sklearn.neural_network import MLPRegressor
regr = MLPRegressor(random_state=0)
regr.fit(x_train, y_train)
y_pred=regr.predict(x_test)
print("Max. iterations:",regr.max_iter,"\ Hidden layer
sizes:",regr.hidden_layer_sizes,"\ Learning
rate:",regr.learning_rate)
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
#plot the results
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
y=df_test['TTF']
x=df_test.iloc[:,0]
z=y_pred
ax = plt.subplot()
ax.xaxis.set_major_locator(ticker.MaxNLocator(30))
ax.plot(x, z, color='red')
ax.plot(x, y, color='green')
ax.set(xlabel='Date-Time', ylabel='TTF (minutes)')
plt.xticks(rotation=90)
plt.legend(['Pred', 'Act'])
plt.show()
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
# evaluate the model
from sklearn import metrics
res=pd.DataFrame(df_test)
res['Predict']=y_pred
res.to_csv('res_cs1pred.csv')
#calulate RMSE
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#calculate errors
err=sum(abs(y_test-y_pred))
print('Error:',err)
#calculate accuracy
acc=100-(err/sum(y_test)*100)
print('Accuracy:',acc)
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
max_iter
hidden_layer_sizes
learning_rate
Accuracy (%)
*1 200
100
constant
68.599
2
500
200
constant
68.287
3
800
300
constant
68.576
4
1000
500
constant
68.549
*default setting
The default setting has proved to produce regression with the best accuracy. The same code is applied
for every hyper parameter setting and plots of results is provided below.
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
#testing different hyperparameter setting
regr = MLPRegressor(random_state=1,max_iter=500,\
hidden_layer_sizes=200)
regr.fit(x_train, y_train)
y_pred=regr.predict(x_test)
print("Max. iterations:",regr.max_iter,"\ Hidden layer
sizes:",regr.hidden_layer_sizes,"\ Learning
rate:",regr.learning_rate)
#plot the results
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
y=df_test['TTF']
x=df_test.iloc[:,0]
z=y_pred
ax = plt.subplot()
ax.xaxis.set_major_locator(ticker.MaxNLocator(30))
ax.plot(x, z, color='red')
ax.plot(x, y, color='green')
ax.set(xlabel='Date-Time', ylabel='TTF (minutes)')
plt.xticks(rotation=90)
plt.legend(['Pred', 'Act'])
plt.show()
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
# evaluate the model
from sklearn import metrics
res=pd.DataFrame(df_test)
res['Predict']=y_pred
res.to_csv('res_cs1pred.csv')
#calulate RMSE
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#calculate errors
err=sum(abs(y_test-y_pred))
print('Error:',err)
#calculate accuracy
acc=100-(err/sum(y_test)*100)
print('Accuracy:',acc)
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
max_iter: 800, hidden_layer_sizes:300, learning_rate: constant
max_iter: 1000, hidden_layer_sizes:500, learning_rate: constant
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
CASE STUDY 2 PREDICTION MODELLING WITH NEURAL NETWORK
#import library
import pandas as pd
from pandas import DataFrame
import numpy as np
#import datasets
df_train = pd.read_csv("cs2_training.csv")
df_test = pd.read_csv("cs2_testing.csv")
#select predictor and target variables
x_train = df_train.iloc[:, 1:6]
y_train = df_train['TTF']
x_test = df_test.iloc[:,1:6]
y_test = df_test['TTF']
#train the model with neural network
from sklearn.neural_network import MLPRegressor
nn = MLPRegressor(random_state=0)
nn.fit(x_train, y_train)
y_pred=nn.predict(x_test)
#evaluate the model
from sklearn import metrics
res=pd.DataFrame(df_test)
res['Predict']=y_pred
res.to_csv('res_cs2pred_assignment.csv')
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
#calulate RMSE
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#calculate errors
err=sum(abs(y_test-y_pred))
print('Error:',err)
#calculate accuracy
acc=100-(err/sum(y_test)*100)
print('Accuracy:',acc)
print("Max. iterations:",nn.max_iter,"\ Hidden layer
sizes:",nn.hidden_layer_sizes,"\ Learning rate:",nn.learning_rate)
#plot the results
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
y=df_test['TTF']
x=df_test.iloc[:,0]
z=y_pred
ax = plt.subplot()
ax.xaxis.set_major_locator(ticker.MaxNLocator(30))
ax.plot(x, z, color='red')
ax.plot(x, y, color='green')
ax.set(xlabel='Date-Time', ylabel='TTF (minutes)')
plt.xticks(rotation=90)
plt.legend(['Pred', 'Act'])
plt.show()
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
Different hyperparameter setting
max_iter
hidden_layer_sizes
learning_rate
Accuracy (%)
*1 200
100
constant
67.577
2
150
constant
68.160
500
3
constant
4
constant
*1 is default setting
#testing different hyperparameter setting
nn =MLPRegressor(random_state=0,max_iter=500,hidden_layer_sizes=150)
nn.fit(x_train, y_train)
y_pred=nn.predict(x_test)
print("Max. iterations:",nn.max_iter,"\ Hidden layer
sizes:",nn.hidden_layer_sizes,"\ Learning rate:",nn.learning_rate)
# evaluate the model
from sklearn import metrics
res=pd.DataFrame(df_test)
res['Predict']=y_pred
res.to_csv('res_cs2pred_assignment_hyperparameter1.csv')
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
#calulate RMSE
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
#calculate errors
err=sum(abs(y_test-y_pred))
print('Error:',err)
#calculate accuracy
acc=100-(err/sum(y_test)*100)
print('Accuracy:',acc)
#Plot the result
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
y=df_test['TTF']
x=df_test.iloc[:,0]
z=y_pred
ax = plt.subplot()
ax.xaxis.set_major_locator(ticker.MaxNLocator(30))
ax.plot(x, z, color='red')
ax.plot(x, y, color='green')
ax.set(xlabel='Date-Time', ylabel='TTF (minutes)')
plt.xticks(rotation=90)
plt.legend(['Pred', 'Act'])
plt.show()
IZYAN LIYANA OMAR 20001951_MUHAMMAD IMRAN 20001986_MUHAMMAD LUQMAN
Quiz/Assignment Predictive Analytics with Python
Download