0% found this document useful (0 votes)
20 views15 pages

Lab2

The document outlines a machine learning project using TensorFlow and Keras to predict financial balance based on income and housing data. It includes data preprocessing, model training with varying layers, and evaluation metrics such as R2 score and mean squared error. Visualizations of predictions and correlation heatmaps are also presented to analyze the relationships within the dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

Lab2

The document outlines a machine learning project using TensorFlow and Keras to predict financial balance based on income and housing data. It includes data preprocessing, model training with varying layers, and evaluation metrics such as R2 score and mean squared error. Visualizations of predictions and correlation heatmaps are also presented to analyze the relationships within the dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Priyank Asodariya - 202201470

import pandas as pd
import numptarget as np
import seaborn as sns
import matplotlib.ptargetplot as plt
import tensorflow as tf

from tensorflow.keras import Sequential


from tensorflow.keras.latargeters import Dense
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score,
mean_squared_error,mean_absolute_error,mean_absolute_percentage_error

scaler = StandardScaler()

train=pd.read_csv('train - train.csv')
test=pd.read_csv('test - test.csv')

x_train = train['Income'].to_numptarget().reshape(-1,1)
target_train = train['Balance'].to_numptarget().reshape(-1,1)
x_test = test['Income'].to_numptarget().reshape(-1,1)
target_test = test['Balance'].to_numptarget().reshape(-1,1)

for i in [16,32,64,128,256]:
ml_model = Sequential()
ml_model.add(Dense(i,activation='relu'))
ml_model.add(Dense(1))
ml_model.compile(loss='mean_squared_error', optimizer='adam',
metrics=['accuractarget'])
ml_model.fit(x_train, target_train, epochs=500,batch_size =
5,verbose=0)
target_pred = ml_model.predict(x_test)
target_train_pred=ml_model.predict(x_train)
print("Training Error ",r2_score(target_train_pred, target_train))
print("Testing Error ",r2_score(target_test, target_pred))
print(mean_squared_error(target_test, target_pred))
print(mean_absolute_error(target_test, target_pred))
print(mean_absolute_percentage_error(target_test, target_pred))
plt.scatter(x_test, target_test, color='red')
plt.scatter(x_test, target_pred, color='blue')
plt.plot(x_test,target_pred,color='green')
plt.show()
print("-"*50)

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 63ms/step


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 58ms/step
Training Error 0.8517386828721549
Testing Error 0.9508672520501443
0.23169167933915444
0.3807450989852905
0.061814227686856724

--------------------------------------------------
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 62ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 59ms/step
Training Error 0.7321397268315671
Testing Error 0.9300057000160803
0.330066963158541
0.4255550995338441
0.057038070632163165
--------------------------------------------------

WARNING:tensorflow:5 out of the last 5 calls to <function


TensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_dist
ributed at 0x7e44a5f22700> triggered tf.function retracing. Tracing is
expensive and the excessive number of tracings could be due to (1)
creating @tf.function repeatedly in a loop, (2) passing tensors with
different shapes, (3) passing Python objects instead of tensors. For
(1), please define your @tf.function outside of the loop. For (2),
@tf.function has reduce_retracing=True option that can avoid
unnecessary retracing. For (3), please refer to
https://www.tensorflow.org/guide/function#controlling_retracing and
https://www.tensorflow.org/api_docs/python/tf/function for more
details.

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 63ms/step

WARNING:tensorflow:6 out of the last 6 calls to <function


TensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_dist
ributed at 0x7e44a5f22700> triggered tf.function retracing. Tracing is
expensive and the excessive number of tracings could be due to (1)
creating @tf.function repeatedly in a loop, (2) passing tensors with
different shapes, (3) passing Python objects instead of tensors. For
(1), please define your @tf.function outside of the loop. For (2),
@tf.function has reduce_retracing=True option that can avoid
unnecessary retracing. For (3), please refer to
https://www.tensorflow.org/guide/function#controlling_retracing and
https://www.tensorflow.org/api_docs/python/tf/function for more
details.

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 67ms/step


Training Error 0.877537073738705
Testing Error 0.9516632260308955
0.22793816348657653
0.3808596513595581
0.06670906941190224

--------------------------------------------------
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 120ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 92ms/step
Training Error 0.8932075070711005
Testing Error 0.9481998374267331
0.24427020994002174
0.38469796398544315
0.07139332008352603
--------------------------------------------------
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 97ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 88ms/step
Training Error 0.9050547516834059
Testing Error 0.9425062575662583
0.27111900497732205
0.39125106791687014
0.07644055667867675
--------------------------------------------------

Q.2
house = pd.read_csv('housing - housing.csv')

house.head()

{"summary":"{\n \"name\": \"house\",\n \"rows\": 489,\n \"fields\":


[\n {\n \"column\": \"RM\",\n \"properties\": {\n
\"dtype\": \"number\",\n \"std\": 0.6436497627572431,\n
\"min\": 3.561,\n \"max\": 8.398,\n
\"num_unique_values\": 430,\n \"samples\": [\n 5.67,\n
6.232,\n 6.8\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"LSTAT\",\n \"properties\": {\n \"dtype\": \"number\",\n
\"std\": 7.081989789065134,\n \"min\": 1.98,\n \"max\":
37.97,\n \"num_unique_values\": 442,\n \"samples\": [\n
12.73,\n 4.14,\n 5.52\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"PTRATIO\",\n \"properties\":
{\n \"dtype\": \"number\",\n \"std\":
2.1112675026309873,\n \"min\": 12.6,\n \"max\": 22.0,\n
\"num_unique_values\": 44,\n \"samples\": [\n 19.6,\n
16.6,\n 15.6\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"MEDV\",\n \"properties\": {\n \"dtype\": \"number\",\n
\"std\": 165340,\n \"min\": 105000,\n \"max\": 1024800,\
n \"num_unique_values\": 228,\n \"samples\": [\n
296100,\n 472500,\n 396900\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n }\n ]\n}","type":"dataframe","variable_name":"house"}

house.describe()

{"summary":"{\n \"name\": \"house\",\n \"rows\": 8,\n \"fields\":


[\n {\n \"column\": \"RM\",\n \"properties\": {\n
\"dtype\": \"number\",\n \"std\": 171.01026967657967,\n
\"min\": 0.6436497627572431,\n \"max\": 489.0,\n
\"num_unique_values\": 8,\n \"samples\": [\n
6.240288343558283,\n 6.185,\n 489.0\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"LSTAT\",\n \"properties\": {\
n \"dtype\": \"number\",\n \"std\": 168.38221606435016,\
n \"min\": 1.98,\n \"max\": 489.0,\n
\"num_unique_values\": 8,\n \"samples\": [\n
12.93963190184049,\n 11.69,\n 489.0\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"PTRATIO\",\n \"properties\":
{\n \"dtype\": \"number\",\n \"std\":
167.35224110126134,\n \"min\": 2.1112675026309873,\n
\"max\": 489.0,\n \"num_unique_values\": 8,\n
\"samples\": [\n 18.51656441717791,\n 19.1,\n
489.0\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"MEDV\",\n \"properties\": {\n \"dtype\": \"number\",\n
\"std\": 318601.84841506614,\n \"min\": 489.0,\n
\"max\": 1024800.0,\n \"num_unique_values\": 8,\n
\"samples\": [\n 454342.9447852761,\n 438900.0,\n
489.0\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n }\n ]\n}","type":"dataframe"}

Q1 = house.quantile(0.25)
Q3 = house.quantile(0.75)
IQR = Q3 - Q1
upper = Q3 + 1.5 * IQR
lower = Q1 - 1.5 * IQR

house=house[house['RM']>lower['RM']]
house=house[house['RM']<upper['RM']]
house = house[house['LSTAT']>lower['LSTAT']]
house = house[house['LSTAT']<upper['LSTAT']]
house=house[house['PTRATIO']>lower['PTRATIO']]
house=house[house['PTRATIO']<upper['PTRATIO']]

sns.heatmap(house.corr(),annot=True)
<Axes: >

sns.pairplot(house)

<seaborn.axisgrid.PairGrid at 0x7e44a5ada4d0>
x = house[['RM','LSTAT','PTRATIO']]
target=house['MEDV']

from sklearn.ml_model_selection import train_test_split


x_train,x_test,target_train,target_test =
train_test_split(x,target,test_size=0.2,random_state=42)

x_train=scaler.fit_transform(x_train)
x_test=scaler.transform(x_test)
x_train.shape

(360, 3)
latargeter = [1,2,3,5,8,16,32,64,128]
accuractarget=[]
mse=[]
mae=[]
for i in latargeter:
print("For ",i," Latargeters")
ml_model = Sequential()
ml_model.add(Dense(64,input_dim=x_train.shape[1],
activation='relu'))
for j in range(i-1):
ml_model.add(Dense(64,activation='relu'))
ml_model.add(Dense(1,activation='linear'))
ml_model.compile(loss='mse', optimizer='adam', metrics=['mse'])
ml_model.fit(x_train, target_train, epochs=50,batch_size =
4,verbose=0)
target_pred = ml_model.predict(x_test)
target_train_pred=ml_model.predict(x_train)
print("Training R2 ",r2_score(target_train, target_train_pred))
print("Testing R2 ",r2_score(target_test, target_pred))
accuractarget.append(r2_score(target_test, target_pred))
mse.append(mean_squared_error(target_test, target_pred))
mae.append(mean_absolute_error(target_test, target_pred))
print("*"*50)

For 1 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 25ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step
Training R2 -9.73264217376709
Testing R2 -11.27657699584961
**************************************************
For 2 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 27ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step
Training R2 0.48627030849456787
Testing R2 0.3928722143173218
**************************************************
For 3 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 59ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
Training R2 0.7502697706222534
Testing R2 0.7188435792922974
**************************************************
For 5 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step
Training R2 0.7675473690032959
Testing R2 0.7164878845214844
**************************************************
For 8 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 53ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step
Training R2 0.7801346778869629
Testing R2 0.7407781481742859
**************************************************
For 16 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 1s 128ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
Training R2 0.6444627642631531
Testing R2 0.6057525873184204
**************************************************
For 32 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 1s 155ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step
Training R2 0.2487562894821167
Testing R2 0.18815523386001587
**************************************************
For 64 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 1s 413ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step
Training R2 0.7718943357467651
Testing R2 0.7003287672996521
**************************************************
For 128 Layers

/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/
dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim`
argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

3/3 ━━━━━━━━━━━━━━━━━━━━ 3s 956ms/step


12/12 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step
Training R2 0.332729697227478
Testing R2 0.233387291431427
**************************************************
plt.plot(latargeter,accuractarget,color='red')
plt.title('Latargeter vs Accuractarget')
plt.xlabel('Latargeters')
plt.targetlabel('Accuractarget')
plt.show()
plt.plot(latargeter,mse,color='red')
plt.title('Latargeter vs MSE')
plt.xlabel('Latargeters')
plt.targetlabel('MSE')
plt.show()
plt.plot(latargeter,mae,color='red')
plt.title('Latargeter vs MAE')
plt.xlabel('Latargeters')
plt.targetlabel('MAE')
plt.show()
Increasing the number of layers in a Neural Network can cause overfitting. From the previous
output, accuracy increased up to 64 layers, but after that, overfitting occurred because no
regularization techniques were used.

You might also like