Project 2
Objective: Implement Clustering and then predict the class of car from “Car dataset”

# Data cleaning of a Automobile dataset
#importing packages
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.mlab as mlab
import matplotlib
import matplotlib.pyplot as plt
#read the data
df = pd.read_csv('D:/MLnAI/Challenges/Project1/auto.csv')
print(df.head())
print(df.info())
#print(df.describe())
print(df.describe(include="all"))
Sample of raw data set

Information of dataset with data type

Description of dataset

# Adding column names to dataframe
df.columns = ["symboling", "normalized-losses", "make", "fuel", "aspiration", "number of doors", "body-style", "drive-wheels", "engine-location",
"wheel-base","length", "width", "height", "curb-weight", "engine-type", "num-of-cylinders", "engine-size", "fuel-system", "bore",
"stroke", "compression-ratio", "horsepower", "peak-rpm", "city-mpg", "highway-mpg", "price"]
#Display the column names
print(list(df.columns))
print(df.head())
print(df.describe())
Column headings added to the dataset

Dataset with new column headers


Sample of dataset :

The sign "?" has to be replaced and it is replaced with NaN
df = df.replace('?', np.NAN)
print(df.head())
Sample of refined dataset

print(df.loc[7:12,['price']])

The NaN value present in the column "price" will be replaced by the mean of the column "price". This opeartion will be followed in other columns were NaN value are present.
df[["price"]]=df[["price"]].astype(str).astype(float)
mean = df[["price"]].mean(axis=0, skipna= True)
print('mean value is: ',mean)
df[["price"]] = df[["price"]].replace(np.NaN, mean)
df[["normalized-losses"]]=df[["normalized-losses"]].astype(str).astype(float) # Convert column from object to float to relace NAN with mean value
mean = df[["normalized-losses"]].mean(axis=0, skipna= True)
#print('mean value is: ',mean)
df[["normalized-losses"]] = df[["normalized-losses"]].replace(np.NaN, mean)
df[["bore"]]=df[["bore"]].astype(str).astype(float)
mean = df[["bore"]].mean(axis=0, skipna= True)
#print('mean value is: ',mean)
df[["bore"]] = df[["bore"]].replace(np.NaN, mean)
df[["stroke"]]=df[["stroke"]].astype(str).astype(float)
mean = df[["stroke"]].mean(axis=0, skipna= True)
#print('mean value is: ',mean)
df[["stroke"]] = df[["stroke"]].replace(np.NaN, mean)
df[["horsepower"]]=df[["horsepower"]].astype(str).astype(float)
mean = df[["horsepower"]].mean(axis=0, skipna= True)
#print('mean value is: ',mean)
df[["horsepower"]] = df[["horsepower"]].replace(np.NaN, mean)
print(df.loc[129:134,['horsepower']])
df[["peak-rpm"]]=df[["peak-rpm"]].astype(str).astype(float)
mean = df[["peak-rpm"]].mean(axis=0, skipna= True)
#print('mean value is: ',mean)
df[["peak-rpm"]] = df[["peak-rpm"]].replace(np.NaN, mean)
print(df.info())
#print(df)
print(df.loc[7:12,['price']])


Binning the data
# Binning the price to low, medium, High
binwidth = int((max(df['price'])-min(df['price']))/3.0)
bins = range(int(min(df['price'])),int(max(df['price'])),binwidth)
df['price-binned'] = pd.cut(df['price'],bins, labels=['Low','Medium', 'High'])
#df["Price_Bin"] = df['price-binned']
#print(df.loc[15:20,['price','price-binned']])
print(df)
# Binning the horsepower to low, medium, High
binwidth = int((max(df['horsepower'])-min(df['horsepower']))/3.0)
bins = range(int(min(df['horsepower'])),int(max(df['horsepower'])),binwidth)
df['horsepower-binned'] = pd.cut(df['horsepower'],bins, labels=['Low','Medium', 'High'])
#print(df.loc[1:20,['horsepower','horsepower-binned']])
#print(df.info())
print(df)
# label encoding the data
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['make-binned']= le.fit_transform(df['make'])
print(df['make-binned'])
print(df)
# Visualization of correlation with seaborn heatmap
f,ax = plt.subplots(figsize = (10,10))
sns.heatmap(df.corr(),annot = True)
plt.show()



Coorelation map

# Numerical variables and their correlation with price
sns.regplot(x="engine-size", y="price", data=df)
plt.show()
print(df[["engine-size", "price"]].corr())
sns.regplot(x="horsepower", y="price", data=df)
plt.show()
print(df[["horsepower", "price"]].corr())
sns.regplot(x="highway-mpg", y="price", data=df)
plt.show()
print(df[["highway-mpg", "price"]].corr())
sns.regplot(x="city-mpg", y="price", data=df)
plt.show()
print(df[["city-mpg", "price"]].corr())





# Categorical variables and their correlation with price
sns.boxplot(x="engine-location", y="price", data=df)
plt.show()
sns.boxplot(x="aspiration", y="price", data=df)
plt.show()
sns.boxplot(x="body-style", y="price", data=df)
plt.show()



Implementing clustering:
# Prediction with Kmeans
df.columns = df.columns.str.replace("-","_")
df1 = pd.DataFrame(columns = ["symboling", "make", "fuel", "aspiration", "number of doors", "body_style", "drive_wheels",
"engine_location", "engine_type", "num_of_cylinders", "fuel_system", "compression_ratio", "price_binned", "horsepower_binned", "make_binned"])
df1 = df[["symboling", "make", "fuel", "aspiration", "number of doors", "body_style", "drive_wheels",
"engine_location", "engine_type", "num_of_cylinders", "fuel_system", "compression_ratio", "price_binned", "horsepower_binned", "make_binned"]]
#print(df1)
df2 = pd.DataFrame(columns = ["normalized_losses","wheel_base","length", "width","height","curb_weight","engine_size","bore","stroke",
"horsepower","peak_rpm","city_mpg", "highway_mpg","price"])
df2 = df[["normalized_losses","wheel_base","length", "width","height","curb_weight","engine_size","bore","stroke",
"horsepower","peak_rpm","city_mpg", "highway_mpg","price"]]
df2_norm = (df2- df2.min()) / (df2.max()- df2.min()) # Scaling
#print(df2_norm)
df3 = pd.concat([df2_norm, df1], axis = 1)
#print(df3)
#print('0 : alfa-romero',"\n" '1 : audi',"\n" '2 : bmw',"\n" '3 : chevrolet',"\n" '4 : dodge',"\n" '5 : honda',"\n" '6 : isuzu',"\n" '7 : jaguar',"\n" '8 : mazda',"\n"
'9 : mercedes-benz',"\n" '10 : mercury',"\n" '11 : mitsubishi',"\n" '12 : nissan',"\n" '13 : peugot',"\n" '14 : plymouth',"\n" '15 : porsche',"\n" '16 : renault',"\n"
'17 : saab',"\n" '18 : subaru',"\n" '19 : toyota',"\n" '20 : volkswagen',"\n" '21 : volvo')
sns.countplot(x = df3.make_binned, label = "Counts")
plt.show()
# Clustering for make and horse power
data = df3[["make_binned", "price"]]
from sklearn.cluster import KMeans
sse = []
# Determine the suitable 'k' value from elbow curve
for k in range (2,10):
kmeans = KMeans(n_clusters = k)
kmeans.fit(data)
sse.append(kmeans.inertia_)
plt.plot(range(2,10), sse)
plt.title("Elbow Curve")
plt.show()
km = KMeans(n_clusters = 7)
Y = km.fit_predict(data)
data["Cluster"] = Y
#print(data)
plt.scatter(data.make_binned[data.Cluster == 0], data.price[data.Cluster == 0],)
plt.scatter(data.make_binned[data.Cluster == 1], data.price[data.Cluster == 1],)
plt.scatter(data.make_binned[data.Cluster == 2], data.price[data.Cluster == 2],)
plt.scatter(data.make_binned[data.Cluster == 3], data.price[data.Cluster == 3],)
plt.scatter(data.make_binned[data.Cluster == 4], data.price[data.Cluster == 4],)
plt.scatter(data.make_binned[data.Cluster == 5], data.price[data.Cluster == 5],)
plt.scatter(data.make_binned[data.Cluster == 6], data.price[data.Cluster == 6],)
plt.scatter(data.make_binned[data.Cluster == 7], data.price[data.Cluster == 7],)
plt.scatter(data.make_binned[data.Cluster == 8], data.price[data.Cluster == 8],)
plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:,1], s = 150,, marker = '*')
plt.title("K-means Data Clustering")
plt.show()
# Prediction with supervised learning
from sklearn import datasets, linear_model
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
X = data[["price"]]
Y = data[["make_binned"]]
#print(X)
#print(Y)
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.40, random_state = 150)
#print ("x_train: ", x_train)
#print ("y_train: ", y_train)
#print("x_test: ", x_test)
#print ("y_test: ", y_test)
import sklearn.linear_model
model = sklearn.linear_model.LinearRegression()
model.fit(X,Y)
plt.show()
#print('The score is : ', model.score(x_test, y_test))
y_predict = model.predict(x_test)
#print(y_predict)

