You are on page 1of 2

import pandas as pd

import numpy as np

df=pd.read_csv("Pizza.csv")
df.head()

#Creare un array con variabili numeriche


X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values

#Suddivisione del set di dati originale in un set di addestramento e un set di test


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25,
random_state = 0)

#Applicare il ridimensionamento delle caratteristiche


from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

#Addestrare il modello dell'albero decisionale


from sklearn.tree import DecisionTreeClassifier
Dec_Tree_model = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
Dec_Tree_model.fit(X_train, y_train)

#Addestrare il modello K-NN


from sklearn.neighbors import KNeighborsClassifier
K_NNghr_model = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
K_NNghr_model.fit(X_train, y_train)

#Addestare il modello SVM con rbf kernel


from sklearn.svm import SVC
SVC_rbf_model = SVC(kernel = 'rbf', random_state = 0)
SVC_rbf_model.fit(X_train, y_train)

#Addestrare il modello Logistic Regression


from sklearn.linear_model import LogisticRegression
Log_Reg_model = LogisticRegression(random_state = 0)
Log_Reg_model .fit(X_train, y_train)

#Addestrare il modello Naive Bayes


from sklearn.naive_bayes import GaussianNB
N_Bayes_model = GaussianNB()
N_Bayes_model.fit(X_train, y_train)

#Addestrare il modello Random Forest


from sklearn.ensemble import RandomForestClassifier
Ran_Forest_model = RandomForestClassifier(n_estimators = 10, criterion = 'entropy',
random_state = 0)
Ran_Forest_model.fit(X_train, y_train)

#Addestrare il modello SVM con linear kernel


from sklearn.svm import SVC
SVC_lin_model = SVC(kernel = 'linear', random_state = 0)
SVC_lin_model.fit(X_train, y_train)

#Confronto
from sklearn.metrics import accuracy_score
# Decision Tree model prediction
y_pred_DT = Dec_Tree_model.predict(X_test)
Dec_Tree_model_acc = accuracy_score(y_test, y_pred_DT)

# K-NN model prediction


y_pred_KNN = K_NNghr_model.predict(X_test)
KNN_model_acc = accuracy_score(y_test, y_pred_KNN)

# SVM model with rbf kernel prediction


y_pred_KSVM = SVC_rbf_model.predict(X_test)
SVM_rbf_model_acc = accuracy_score(y_test, y_pred_KSVM)

# Logistic Regression model prediction


y_pred_LR = Log_Reg_model.predict(X_test)
Log_Reg_model_acc = accuracy_score(y_test, y_pred_LR)

# Naive Bayes model prediction


y_pred_NB = N_Bayes_model.predict(X_test)
N_Bayes_model_acc = accuracy_score(y_test, y_pred_NB)

# Random Forest model prediction


y_pred_RF = Ran_Forest_model.predict(X_test)
Ran_Forest_model_acc = accuracy_score(y_test, y_pred_RF)

# SVM model with linear kernel prediction


y_pred_SVC_lin = SVC_lin_model.predict(X_test)
SVC_lin_model_acc = accuracy_score(y_test, y_pred_SVC_lin)

print("The accuracy of the Decision Tree model is


{:.2f}".format(Dec_Tree_model_acc))
print("The accuracy of the K-NN model is {:.2f}".format(KNN_model_acc))
print("The accuracy of the SVM model with rbf kernel is
{:.2f}".format(SVM_rbf_model_acc))
print("The accuracy of the Logistic Regression model is
{:.2f}".format(Log_Reg_model_acc))
print("The accuracy of the Naive Bayes model is {:.2f}".format(N_Bayes_model_acc))
print("The accuracy of the Random Forest model is
{:.2f}".format(Ran_Forest_model_acc ))
print("The accuracy of the SVM model with linear kernel is
{:.2f}".format(SVC_lin_model_acc))

You might also like