Challenge

Project 2

Implement Clustering and then predict the class of car from “Car dataset:

Importing Libraries:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

plt.rcParams["patch.force_edgecolor"] = True

%matplotlib inline

import seaborn as sns

sns.set_style('whitegrid')

import warnings

warnings.filterwarnings('ignore')

from collections import defaultdict

from scipy.sparse import hstack

from sklearn.preprocessing import StandardScalar, OneHotEncoder

from sklearn.model_selection import train_test_split

from sklearns.cluster import kmeans, Agglomerativeclustering

from sklearn.manifold import TSNE

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

Reading the data and separating the target variables:

Reading the cleaned data

data = pd.read_csv('Cleaned_Data.csv')

data.head()

#Storing target data

target_data=pd.DataFrame(data[['price','mpg']])

data.drop(['price','mpg'], axis=1, inplace=True)

target_data.head()

Performing Standardization and One hot encoding and combining the data

#Collecting All feature names

feature_names = []

OHE on Categorical features

ohe = onehotencoder()

#Applying the function on data

car_data = ohe.fit_transform(data[data.select_dtypes(exclude='number').columns].values)

feature_names.extend(ohe.get_feature_names())

standardizing the numerical features

scalar = StandardScalar()

#Applying the function on data

num_data=scalar.fit_transform(data[data.select_dtypes(include='number').columns].values)

feature_names.extend(['size','force'])

Combining the data

data_new = hstack((car_data, num_data))

car_data = pd.DataFrame(data_new.toarray(), columns=feature_names)

Checking the converted data:

car_data.head()

Clustering using Kmeans:

#no of clusters

number_of_clusters= [2,5,10,12,15,20,25,30,35]

sse=defaultdict(int)

for clusters in number_of_clusters:

cluster_algo = Kmeans(n_clusters=clusters, random_state=101)

cluster_algo.fit(car_data.values)

sse[clusters]=cluster_algo.inertia_

plt.figure(figsize=(8,5))

sns.lineplot(line(sse.keys()), list(sse.values()))

plt.title("cluster vs Sum of Squares")

plt.xlabel("Number of Cluster")

plt.ylabel("sse")

plt.show()

Defining the best clusters

best_cluster=10

cluster_algo = KMeans(n_clusters=best_cluster, random_state=0)

cluster_algo.fit(car_data.values)

labels_array= cluster_algo.labels_

car_data['cluster'] = labels_array

Visualizing the best cluster:

transform = TSNE

trans = transform(n_components=2)

data = trans.fit_transform(car_data.values)

label_map= {l:i for i, l in enumerate(np.unique(labels_array))}

node_colours = [label_map[target] for target in labels_array]

plt.figure(figsize=(10,8))

plt.axes().set(aspect="equal")

plt.scatter(data[:,0],

data[:,1],

c=node_colours, alpha=0.3)

plt.title('{} visualization of Car Price Data'.format(transform._name_))

plt.show()

1.Price

building Linear regression on each cluster seperately

y_true = defaultdict (int)

y_pred = defaultdict (int)

for cluster in range (best_cluster):

lin_reg LinearRegression ()

#Getting the indices of data points belong to the cluster
index = car_data.index [car_data['cluster'] == cluster].tolist()

#fitting the model on points in that cluster
lin_reg.fit (car_data.loc[index], target_data['price'][index])

#predicting the train points on that cluster

pred = lin_reg.predict (car_data.loc[index])

#storing true and predicted values in their respeactive dictionaries y_true [cluster] = target_data['price'][index]

y_pred[cluster] = pred

Checking the error on each cluster

for cluster in range (best_cluster):

true = y_true [cluster]

pred= y_pred [ cluster]

print ("Mean Square Error for cluster ) (}".format (cluster, mean squared error (true, pred)))

2. MPG:

building Linear regression on each cluster seperately

y_true defaultdict (int)

y_pred defaultdict (int)

for cluster in range (best_cluster):

lin_reg = LinearRegression ()

#Getting the indices of data points belong to the cluster
index = = car_data.index [car_data['cluster'] = cluster].tolist()

#fitting the model on points in that cluster

lin_reg.fit (car_data.loc[index], target_data['mpg'][index])

#predicting the train points on that cluster

pred = lin_reg.predict (car_data.loc[index])

#storing true and predicted values in their respeactive dictionaries y_true [cluster] = target_data['mpg'] [index]

y_pred [cluster] = pred

Checking the error on each cluster

for cluster in range (best_cluster):

true = y_true [cluster]

pred= y_pred (cluster]

print ("Mean Square Error for cluster () {}".format (cluster, mean_squared_error (true, pred)))

Clustering using Agglomerative

#cluster map on all features

sns.clustermap(car_data)

Defining the Clusters:

cluster_algo = AgglomerativeClustering (n_clusters=5)

cluster_algo.fit (car_data.values)

car_data['cluster'] = cluster_algo.labels_

Visualizing the clusters:

transform = TSNE #PCA

trans = transform (n_components=2)

data = trans.fit_transform (car_data.values)

label_map = { 1: i for i, 1 in enumerate (np. unique (labels_array))} node_colours = [ label_map [target] for target in labels_array]

plt.figure (figsize=(10, 8))

plt.axes().set (aspect="equal")

plt.scatter (data[:,0],

data[:,1],

c=node_colours, alpha=0.3)

plt.title('{} visualization of Car Price Data'.format (transform._name_))

plt.show()

1.Price

y_true = defaultdict (int)

y_pred = defaultdict (int)

for cluster in range (5):

lin_reg = LinearRegression ()

#Getting the indices of data points belong to the cluster

index = car_data.index [car_data['cluster'] == cluster].tolist()

#fitting the model on points in that cluster

lin_reg.fit (car_data.loc[index], target_data['price'][index])

#predicting the train points on that cluster

pred = lin_reg.predict (car_data.loc[index])

#storing true and predicted values in their respeactive dictionaries y_true [cluster] = target_data['price'][index]

y_pred [cluster] = pred

Checking the error on each cluster

for cluster in range (5):

true = y_true (cluster]

pred = y_pred [cluster]

print ("Mean Square Error for cluster () {}".format(cluster, mean squared error (true, pred)))

2.MPG

y_true = defaultdict (int)

y_pred = defaultdict (int)

for cluster in range (5):

lin_reg = Linear Regression ()

#Getting the indices of data points belong to the cluster
index= car_data.index [car_data['cluster'] == cluster].tolist()

#fitting the model on points in that cluster lin_reg.fit (car_data.loc[index], target_data['mpg'][index])

#predicting the train points on that cluster
pred = lin_reg.predict (car_data.loc[index])

#storing true and predicted values in their respeactive dictionaries y_true (cluster] = target_data['mpg'] [index]

y_pred[cluster] = pred

Check the error on each cluster

for cluster in range (5):

true = y true [cluster]

pred = y_pred [cluster]

print ("Mean Square Error for cluster () )". format (cluster, mean squared error (true, pred)))

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.