<jupyter_start><jupyter_text>Evaluating Used Cars with Classification IntroductionIn recent years, used car market is getting larger and larger. Many people begin purchasing used cars instead of new cars, since used cars are always cheaper than new cars, and a lot of used cars really have good reliability. However, there are still a bunch of defective used cars in market. For example, my one friend bought a 2000 Toyota. One day, when she was driving, suddenly, her engine broke down. I am also a used-car victim, I purchased a 2001 Nissan six years ago, after just one week, I could not start up my car anymore. Defective used cars not only hurt customers, but also ruin sellers' reputation, so evaluating used cars is very important. Data DescriptionOur data includes 1728 used cars. Our variables are: 1). Buying price, 2). Price of maintenance, 3). Number of doors, 4). Capacity in terms of persons to carry, 5).The size of trunk, and 6). Estimated safety of the car. Both buying price and price of maintenance are categorized into four levels: very high, high, medium, and low. Number of doors includes 2, 3, 4, and 5-more. Capacity in terms of persons to carry has three levels: 2, 4, and more. The size of trunk is categorized into small, medium, and big.  Estimated safety of the car is low, medium, and high. Our classifications for the used cars are unacceptable, acceptable, good, and very good. Our dataset can be downloaded from https://archive.ics.uci.edu/ml/datasets/Car+Evaluation<jupyter_code>import pandas as pd
car = pd.read_csv('C:\Atop Materials\car evaluation.csv', header = 0)
car.head()
def tonum (x):
    if x == "vhigh":
        return 4
    if x == "high":
        return 3
    if x == "med":
        return 2
    if x == "low":
        return 1
    if x == "5more":
        return 5
    if x == "4":
        return 4
    if x == "3":
        return 3
    if x == "2":
        return 2
    if x == "more":
        return 5
    if x == "small":
        return 1
    if x == "big":
        return 3
    if x == "unacc":
        return 1
    if x == "acc":
        return 2
    if x == "good":
        return 3
    if x == "vgood":
        return 4
    
car["buying"] = car["Buying"].apply(tonum)
car["maint"] = car["Maint"].apply(tonum)
car["doors"] = car["Doors"].apply(tonum)
car["persons"] = car["Persons"].apply(tonum)
car["trunk"] = car["Trunk"].apply(tonum)
car["safty"] = car["Safty"].apply(tonum)
car["evaluation"] = car["Evaluation"].apply(tonum)
car = car[["buying", "maint", "doors", "persons", "trunk", "safty", "evaluation"]]<jupyter_output><empty_output><jupyter_text>MethodologyIn this section, we will devide our data into training set and test set, and then we will use support vector machine, k-nearest neighbors, and decision tree to do classification.<jupyter_code>import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier

x = car[["buying", "maint", "doors", "persons", "trunk", "safty"]]
y = car["evaluation"]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)


# svm
clf1 = svm.SVC(kernel = 'linear')
clf1.fit(x_train, y_train)
yhat1 = clf1.predict(x_test)
print("Accuracy of SVM is:", np.round(np.mean(y_test == yhat1), 4))

# KNN
k = 5
clf2 = KNeighborsClassifier(n_neighbors = k)
clf2.fit(x_train, y_train)
yhat2 = clf2.predict(x_test)
print("Accuracy of KNN is:", np.round(np.mean(y_test == yhat2), 4))

# Decision Tree
clf3 = DecisionTreeClassifier(criterion = 'entropy', max_depth = 4)
clf3.fit(x_train, y_train)
yhat3 = clf3.predict(x_test)
print("Accuracy of Decision Tree is:", np.round(np.mean(y_test == yhat3), 4))<jupyter_output>Accuracy of SVM is: 0.8651
Accuracy of KNN is: 0.9518
Accuracy of Decision Tree is: 0.8555
{'fraction_non_alphanumeric': 0.11387163561076605, 'fraction_numerical': 0.021221532091097308, 'mean_word_length': 3.892405063291139, 'pattern_counts': {'":': 15, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '34959', 'license': 'MIT', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1227, 'n_words': 487, 'path': 'python/coursera_python/IBM/Evaluating+Used+Cars+with+Classification.ipynb', 'repo_name': 'SayanGhoshBDA/code-backup', 'size': '3864'}