AI recommendations basing on the Auto's physical qualifications.
① Train your model
Training Result Summary
Selected Model:
Not available
Test Split:
Not available
Accuracy Score:
Not available
Weighted F1 Score:
Not available
② Get AI Recommendation
Please Enter Your Car Parameters
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, f1_score
from pyodide.http import open_url
import numpy as np
# import time
#from xgboost import XGBClassifier
def upSampling(data):
from sklearn.utils import resample
# Majority Class Dataframe
df_majority = data[(data['score']==0)] # coz it is amounted 1210, which is the most
samples_in_majority = data[data.score == 0].shape[0]
# Minority Class Dataframe of all the three labels
df_minority_1 = data[(data['score']==1)] # coz it is amounted 384
df_minority_2 = data[(data['score']==2)] # coz it is amounted 69
df_minority_3 = data[(data['score']==3)] # coz it is amounted 65
# upsample minority classes
df_minority_upsampled_1 = resample(df_minority_1,
replace=True,
n_samples= samples_in_majority,
random_state=42)
df_minority_upsampled_2 = resample(df_minority_2,
replace=True,
n_samples= samples_in_majority,
random_state=42)
df_minority_upsampled_3 = resample(df_minority_3,
replace=True,
n_samples= samples_in_majority,
random_state=42)
# Combine majority class with upsampled minority classes
df_upsampled = pd.concat([df_minority_upsampled_1, df_minority_upsampled_2, df_minority_upsampled_3, df_majority])
return df_upsampled
def datasetPreProcessing():
# Reading the content of CSV file.
csv_url_content = open_url("https://raw.githubusercontent.com/entzyeung/car-purchase-ai/main/car.csv")
data = pd.read_csv(csv_url_content)
pyscript.write("headingText", "Pre-Processing the Dataset...") # This is used to send messages to the HTML DOM.
# Removing all the null values
data.isna().sum()
# Removing all the duplicates
data.drop_duplicates()
coloumns = ['buying', 'maint', 'doors', 'people', 'luggaage', 'safety', 'score']
data['buying'] = data['buying'].map({'low':0, 'med':1, 'high':2, 'vhigh':3})
data['maint'] = data['maint'].map({'low':0, 'med':1, 'high':2, 'vhigh':3})
data['doors'] = data['doors'].map({'2':0, '3':1, '4':2, '5more':3})
data['people'] = data['people'].map({'2':0, '4':1, 'more':2})
data['luggaage'] = data['luggaage'].map({'small':0, 'med':1, 'big':2})
data['safety'] = data['safety'].map({'low':0, 'med':1, 'high':2})
data['score'] = data['score'].map({'unacc':0, 'acc':1, 'good':2, 'vgood':3})
upsampled_data = upSampling(data)
return upsampled_data
# this function will sit inside trainModel()
def model_selection():
selectedModel = document.querySelector('input[name="modelSelection"]:checked').value;
if selectedModel == "lr":
document.getElementById("selectedModelContentBox").innerText = "Logistic Regression";
return LogisticRegression()
else:
document.getElementById("selectedModelContentBox").innerText = "Gradient Boosting Classifier";
return GradientBoostingClassifier(n_estimators=100, max_depth=4)
def classifier(model, X_train, X_test, y_train, y_test):
clf = model
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_score = clf.fit(X_train, y_train)
acc_score = accuracy_score(y_test, y_pred)
f1Score = f1_score(y_test, y_pred, average='weighted')
return acc_score, model, f1Score
def trainModel(e=None):
global trained_model # this variable is global, not just inside this function
# Get the data, and process
processed_data = datasetPreProcessing()
# Take the Test Split as an input by the user
test_split = float(document.getElementById("test_split").value)
# If the test split is greater than 0.5 or less than 0.1 then we will throw an error.
if test_split > 0.5 or test_split < 0.1:
pyscript.write("headingText", "Please enter test size between 0.1 to 0.5...")
return
# send the value to testSplitContentBox for display
document.getElementById("testSplitContentBox").innerText = test_split;
##############################################
##############################################
# set X and y from the df
X = processed_data[['buying', 'maint', 'doors', 'people', 'luggaage', 'safety']]
y = processed_data['score']
# Splitting the Dataset into training and testing.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_split, random_state=42)
# Below function can return the classification model chosen by the user
model = model_selection()
pyscript.write("headingText", "Model Training Started...")
# time.sleep(1)
acc_score, trained_model, f1Score = classifier(model, X_train, X_test, y_train, y_test)
# Writing the value of accuracy and f1-score to the DOM
document.getElementById("accuracyContentBox").innerText = f"{round(acc_score*100, 2)}%";
document.getElementById("f1ContentBox").innerText = f"{round(f1Score*100, 2)}%";
# Below code is to enable the Model Training Button when the Model is successfully trained.
document.getElementById("submitBtn").classList.remove("disabled");
document.getElementById("submitBtn").disabled = False;
document.getElementById("trainModelBtn").classList.remove("disabled");
document.getElementById("trainModelBtn").disabled = False;
pyscript.write("headingText", "Model Training Completed.")
if e:
e.preventDefault()
return False
def testModel(e=None):
buying_price = int(document.getElementById("buying_price").value)
maintenance_price = int(document.getElementById("maintenance_price").value)
doors = int(document.getElementById("doors").value)
persons = int(document.getElementById("persons").value)
luggage = int(document.getElementById("luggage").value)
safety = int(document.getElementById("safety").value)
arr = np.array([buying_price, maintenance_price, doors, persons, luggage, safety]).astype('float32')
arr = np.expand_dims(arr, axis=0)
result = trained_model.predict(arr)
condition = ""
if result[0] == 0:
condition = "The deal is unacceptable"
elif result[0] == 1:
condition = "The deal is acceptable"
elif result[0] == 2:
condition = "The deal is good"
else:
condition = "The deal is very good"
pyscript.write("resultText", f"AI: {condition}")
if e:
e.preventDefault()
return False
document.getElementById("submitBtn").onclick = testModel
document.getElementById("trainModelBtn").onclick = trainModel