<jupyter_start><jupyter_text>CAR SALES     This project is binary classification problem, where the chances of buying a car is Veryhigh(0),high(1),Medium(2) and Low(3).<jupyter_code>#Importing important libraries
import numpy as np   # NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices.
import pandas as pd  # Pandas is mainly used for data analysis. Pandas allows importing data from various file formats such as comma-separated values, JSON, SQL, Microsoft Excel. Pandas allows various data manipulation operations such as merging, reshaping, selecting, as well as data cleaning, and data wrangling features.
from sklearn.metrics import classification_report, f1_score, confusion_matrix, log_loss,jaccard_similarity_score, accuracy_score
import matplotlib.pyplot as plt #Matplotlib - It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
#Importing data set
header=["buying","maintainence","persons","doors","Luggage","Safety","Unacc"]
df=pd.read_csv(r"C:\Users\hp\Desktop\project\car.data",header=None,names=header, na_values="?"  )
df.head()# to print first 5 values<jupyter_output><empty_output><jupyter_text>Check for Missing Data<jupyter_code>print('Missing values in column:' )
df.isnull().sum()
#to check for datatype
df.dtypes<jupyter_output><empty_output><jupyter_text>Handle Categorical Values<jupyter_code>obj_df = df.select_dtypes(include=['object']).copy()
obj_df.head()
df['Code']=pd.factorize(df.buying)[0]
df['Codemaint']=pd.factorize(df.maintainence)[0]
df['Codeperosns']=pd.factorize(df.persons)[0]
df['Codedoors']=pd.factorize(df.doors)[0]
df['Codeluggage']=pd.factorize(df.Luggage)[0]
df['Codesafety']=pd.factorize(df.Safety)[0]

df.head()

df.tail()<jupyter_output><empty_output><jupyter_text>Split Data for Training and Testing<jupyter_code>data = df
X=data.loc[:,[ 'Codemaint', 'Codeperosns', 'Codedoors', 'Codeluggage','Codesafety']]
print(X)


print(X.shape)
Y=data[["Code"]]
print(Y)
print(Y.shape)
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)<jupyter_output>(1382, 5) (346, 5) (1382, 1) (346, 1)<jupyter_text>Apply different Classification Algorithms and tune them  LOGISTIC REGRESSION<jupyter_code>from sklearn.linear_model import LogisticRegression
model1 = LogisticRegression()
model1.fit(X_train, Y_train)

predict = model1.predict(X_test)
print((model1.score(X_test, Y_test)*100), "%")<jupyter_output>19.942196531791907 %<jupyter_text>Evaluation metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict))
print("Precision Score : ",precision_score(Y_test, predict, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict,))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.23      0.24      0.23        85
           1       0.16      0.22      0.19        83
           2       0.21      0.16      0.19        91
           3       0.21      0.18      0.19        87

    accuracy                           0.20       346
   macro avg       0.20      0.20      0.20       346
weighted avg       0.20      0.20      0.20       346

Confusion Matrix
[[20 27 17 21]
 [22 18 20 23]
 [22 36 15 18]
 [22 30 19 16]]
Precision Score :  0.1994219653179191
F-1 score :  0.1994219653179191
Accuracy score :  0.1994219653179191<jupyter_text>Decision Tree classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model2 = DecisionTreeClassifier()
model2.fit(X_train, Y_train)
predict2 = model2.predict(X_test)
print((model2.score(X_test, Y_test)*100), "%")<jupyter_output>0.2890173410404624 %<jupyter_text>Evaluation metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict2))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict2))
print("Precision Score : ",precision_score(Y_test, predict2, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict2, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict2,))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.00      0.01      0.01        85
           1       0.00      0.00      0.00        83
           2       0.00      0.00      0.00        91
           3       0.00      0.00      0.00        87

    accuracy                           0.00       346
   macro avg       0.00      0.00      0.00       346
weighted avg       0.00      0.00      0.00       346

Confusion Matrix
[[ 1 71 10  3]
 [70  0 10  3]
 [78 10  0  3]
 [68 18  1  0]]
Precision Score :  0.002890173410404624
F-1 score :  0.002890173410404624
Accuracy score :  0.002890173410404624<jupyter_text>Support Vector Machines<jupyter_code>from sklearn.svm import SVC
model3 = SVC()
model3.fit(X_train, Y_train)
predict3 = model3.predict(X_test)
print((model3.score(X_test, Y_test)*100), "%")<jupyter_output>10.404624277456648 %<jupyter_text>Evaluation Metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict3))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict3))
print("Precision Score : ",precision_score(Y_test, predict3, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict3, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict3))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.11      0.13      0.12        85
           1       0.09      0.11      0.10        83
           2       0.12      0.04      0.06        91
           3       0.11      0.14      0.12        87

    accuracy                           0.10       346
   macro avg       0.11      0.10      0.10       346
weighted avg       0.11      0.10      0.10       346

Confusion Matrix
[[11 34  8 32]
 [26  9 12 36]
 [29 31  4 27]
 [37 29  9 12]]
Precision Score :  0.10404624277456648
F-1 score :  0.10404624277456648
Accuracy score :  0.10404624277456648<jupyter_text>Random Forest Classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
model4 = RandomForestClassifier()
model4.fit(X_train, Y_train)
predict4 = model4.predict(X_test)
print((model4.score(X_test, Y_test)*100), "%")<jupyter_output>0.2890173410404624 %<jupyter_text>Evaluation Metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict4))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict))
print("Precision Score : ",precision_score(Y_test, predict4, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict4, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict4,))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.01      0.01      0.01        85
           1       0.00      0.00      0.00        83
           2       0.00      0.00      0.00        91
           3       0.00      0.00      0.00        87

    accuracy                           0.00       346
   macro avg       0.00      0.00      0.00       346
weighted avg       0.00      0.00      0.00       346

Confusion Matrix
[[20 27 17 21]
 [22 18 20 23]
 [22 36 15 18]
 [22 30 19 16]]
Precision Score :  0.002890173410404624
F-1 score :  0.002890173410404624
Accuracy score :  0.002890173410404624<jupyter_text>Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
model5 = GaussianNB()
model5.fit(X_train, Y_train)
predict5 = model5.predict(X_test)
print((model5.score(X_test, Y_test)*100), "%")<jupyter_output>20.809248554913296 %<jupyter_text>Evaluation Metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict5))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict5))
print("Precision Score : ",precision_score(Y_test, predict5, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict5, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict5,))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.21      0.21      0.21        85
           1       0.20      0.28      0.23        83
           2       0.19      0.15      0.17        91
           3       0.23      0.20      0.21        87

    accuracy                           0.21       346
   macro avg       0.21      0.21      0.21       346
weighted avg       0.21      0.21      0.21       346

Confusion Matrix
[[18 28 18 21]
 [23 23 20 17]
 [23 35 14 19]
 [23 27 20 17]]
Precision Score :  0.20809248554913296
F-1 score :  0.20809248554913296
Accuracy score :  0.20809248554913296<jupyter_text>K- Nearest Neighbours<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
model6 = KNeighborsClassifier(4)
model6.fit(X_train, Y_train)
predict6 = model6.predict(X_test)
print((model6.score(X_test, Y_test)*100), "%")<jupyter_output>5.491329479768786 %<jupyter_text>Evaluation metrics<jupyter_code>print("Classification Report")
print(classification_report(Y_test, predict6))
print("Confusion Matrix")
print(confusion_matrix(Y_test, predict6))
print("Precision Score : ",precision_score(Y_test, predict6, pos_label='positive',average='micro'))
print("F-1 score : ",f1_score(Y_test, predict6, pos_label='positive',average='micro'))
print("Accuracy score : ",accuracy_score(Y_test, predict6,))<jupyter_output>Classification Report
              precision    recall  f1-score   support

           0       0.11      0.19      0.14        85
           1       0.03      0.02      0.03        83
           2       0.00      0.00      0.00        91
           3       0.01      0.01      0.01        87

    accuracy                           0.05       346
   macro avg       0.04      0.06      0.05       346
weighted avg       0.04      0.05      0.04       346

Confusion Matrix
[[16 23 25 21]
 [39  2 22 20]
 [42 23  0 26]
 [45 21 20  1]]
Precision Score :  0.05491329479768786
F-1 score :  0.05491329479768786
Accuracy score :  0.05491329479768786<jupyter_text>Visually compare the performance of all classifiers<jupyter_code>import seaborn as sns
classifiers = [model1, model2, model3,model4,model5,model6]
result_col = ["Classifiers", "Accuracy"]
result = pd.DataFrame(columns=result_col)
acc_dict = {}
for clf in classifiers:
    name = clf.__class__.__name__
    clf.fit(X_train, Y_train)
    predictions = clf.predict(X_test)
    acc = accuracy_score(Y_test,predictions)
    if name in acc_dict:
        acc_dict[name] += acc
    else:
        acc_dict[name] = acc
        
for clf in acc_dict:
    acc_dict[clf] = acc_dict[clf] / 10.0
    result_entry = pd.DataFrame([[clf, acc_dict[clf]]], columns=result_col)
    result = result.append(result_entry)
    
    
plt.xlabel('Accuracy')
plt.title('Classifier Accuracy')

sns.set_color_codes("muted")
sns.barplot(x='Accuracy', y='Classifiers', data=result, color="b")<jupyter_output>C:\Users\hp\anaconda3\lib\site-packages\sklearn\utils\validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
C:\Users\hp\anaconda3\lib\site-packages\sklearn\utils\validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
C:\Users\hp\anaconda3\lib\site-packages\ipykernel_launcher.py:8: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().
  
C:\Users\hp\anaconda3\lib\site-packages\sklearn\naive_bayes.py:206: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=Tr[...]<jupyter_text>result<jupyter_code>result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10819828408007626, 'fraction_numerical': 0.09787098824277089, 'mean_word_length': 3.234443323242516, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '652622', 'license': 'MIT', 'n_tokens_mistral': 5030, 'n_tokens_neox': 4309, 'n_words': 1003, 'path': 'CAR SALES.ipynb', 'repo_name': 'PRAJJWAL123166/CAR-SALES', 'size': '12588'}