Machine learning exercise 1
Install keras
#!pip install keras-tuner
#!pip install keras
Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras import layers
from kerastuner.tuners import RandomSearch
from sklearn import metrics
from sklearn.metrics import confusion_matrix
Getting the data
df = pd.read_csv("D:/car data.csv")
df.head()
Let's gather some information
df.info()
df.isnull().sum()
Label encoding
df.replace({"Fuel_Type":{"Petrol":2,"Diesel":1,"CNG":0}},inplace=True)
df.replace({"Seller_Type":{"Dealer":1,"Individual":0}},inplace=True)
df.replace({"Transmission":{"Manual":1,"Automatic":0}},inplace=True)
df.drop(columns=["Car_Name"], axis=1, inplace=True) df.head()
Scaling the data
independent_features=df.drop(columns=["Selling_Price"], axis=1)
scaler= StandardScaler()
transform_scale_data=scaler.fit_transform(independent_features)
transform_scale_data
Separating the data and label
X=transform_scale_data
Y=df["Selling_Price"]
Splitting data into train and test data
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2, random_state=1)
Creating ANN model
Initializing the model
ann=Sequential()
Adding the input layer and first hidden layer
ann.add(Dense(units=4, input_dim=7, kernel_initializer='glorot_uniform',activation="relu", ))
ann.add(Dropout(rate = 0.1))
Adding the Second Hidden layer
ann.add(Dense(units=6, kernel_initializer='he_normal', activation="relu"))
ann.add(Dropout(rate = 0.1))
'''
If you want to add more layers then do the same thing agin. You can change the value of the parameters if you
want like change the number of neurons etc.
'''
Adding the third Hidden layer
ann.add(Dense(units=8, kernel_initializer='glorot_normal', activation="relu"))
ann.add(Dropout(rate = 0.1))
Adding the output layer
ann.add(Dense(units=1,kernel_initializer = 'uniform', activation="linear"))
Compiling ANN
ann.compile(optimizer="adam",loss="mean_absolute_error",metrics=['mean_absolute_error'])
Fitting the the model
ann.fit(X_train,Y_train,validation_split=0.33,batch_size=12,epochs = 100)
R squared error training data
training_data_prediction=ann.predict(X_train)
score_using_r2_score=metrics.r2_score(Y_train,training_data_prediction)
score_using_r2_score
Visualization of the actual value and training data predicted value
plt.scatter(Y_train,training_data_prediction)
plt.xlabel("actual value")
plt.ylabel("Predicted value")
plt.show()
R squared error testing data
testing_data_prediction=ann.predict(X_test)
score_using_r2_score=metrics.r2_score(Y_test,testing_data_prediction)
score_using_r2_score
Visualization of the actual value and testing data predicted value
plt.scatter(Y_test,testing_data_prediction)
plt.xlabel("actual value")
plt.ylabel("Predicted value")
plt.show()
Make Prediction
test_predictions = ann.predict(X_test).flatten()
results_df = pd.DataFrame({'Predicted Price': test_predictions,'Actual Price':Y_test.values.flatten()})
results_df.head(10)