Project 2
AIM: Implement Clustering and then predict the class of car from “Car dataset”.
Attributes imformation:
1.symboling: -3, -2, -1, 0, 1, 2, 3
2.normalized-losses: continuous from 65 to 256
3.make: alfa-romero, audi, bmw, chevrolet, dodge, honda, isuzu, jaguar, mazda, mercedes-benz, mercury, mitsubishi, nissan, peugot, plymouth, porsche, renault, saab, subaru, toyota, volkswagen, volvo.
4.fuel-type: diesel, gas
5.aspiration: std, turbo
6.num-of-doors: four, two
7.body-style: hardtop, wagon, sedan, hatchback, convertible
8.drive-wheels: 4wd, fwd, rwd
9.engine-location: front, rear
10.wheel-base: continuous from 86.6 120.9
11.length: continuous from 141.1 to 208.1
12.width: continuous from 60.3 to 72.3
13.height: continuous from 47.8 to 59.8
14.curb-weight: continuous from 1488 to 4066
15.engine-type: dohc, dohcv, l, ohc, ohcf, ohcv, rotor
16.num-of-cylinders: eight, five, four, six, three, twelve, two
17.engine-size: continuous from 61 to 326
18.fuel-system: 1bbl, 2bbl, 4bbl, idi, mfi, mpfi, spdi, spfi
19.bore: continuous from 2.54 to 3.94
20.stroke: continuous from 2.07 to 4.17
21.compression-ratio: continuous from 7 to 23
22.horsepower: continuous from 48 to 288
23.peak-rpm: continuous from 4150 to 6600
24.city-mpg: continuous from 13 to 49
25.highway-mpg: continuous from 16 to 54
26.price: continuous from 5118 to 45400.
Importing neccessary libraries
Reading data
In above dataframe the column names are not meaningful and dataset contains special characters.
Data Cleaning
Reading the data with proper column name and replacing special characters with NaN values
Handling missing values
#check missing values
Correcting datatypes
- 1.If we check in above Dtypes of all attribues,Some of attributes such as num-of- doors,num-of cylinder.
- they should be 'int'or 'float' format instead of 'object'
- 2.So let's first convert the str to number using replace() and then convert the object to int or float uaing astype().
#Checking unique values of num-of-doors
Binning:
Binning is a process of transforming continuous numerical variables into discrete categorical 'bins', for grouped analysis.
Histogram of horsepower
- 3 bins of equal size bandwidth so we use numpy's linspace(start_value, end_value, numbers_generated function.
- Since we are building 3 bins of equal length, there should be 4 dividers, so numbers_generated=4.
- Till now we have cleaned data, by removing NaN values and special characters and proper naminng to columns
Exploring data with visvulization
Countplots
- 1.four doors are higher than the two doors cars.
- 2.maximum numbers of cars having four number of cylinders in them followed by 6 number of cylinders and then 5.
- 3.Most of the cars at working on gas.
- 4.most of the cars are front wheel drive.
- 5.Almost cars have engine located at front.
Barplot
Pairplot between price and all other attributes
Boxplots
- Categorical attributes and their correlation with price
- Above we can see,boxes overlap and their median lines are inside the overlap range.
- As we mentioned above distributions of price between the different body-style categories have a significant overlap,and so body-style would not be a good predictor of price.
- two boxes are not overlaped and they are enough distinct, we can say engine location is the predictor of price
Correlation and Heatmap
- Compression-ratio has very strong negative correlation with fuel-type
- Price has very strong positive relationship with curb-weight and engine size
- Engine size has positive relationship with num-of cylider,leght,width,height
Converting Categorical columns into Numerical column using labelEncoder
Standardizing data
After standardization next step is to reduce some dimension using PCA technique
Implementing The Principal component analysis
Finding optimun value of K usinng elbow curve
Applying KMean algorithm
By using KMean we have created class,we can see in graph
Cluster Visualization
- Using Kmean wee have grouped data into 5 classes(clusturs).
- Next step is to predict class(clutures) using classification algorithms.
Predicting class using calssification model
Seperating independent and dependent feature
Splitting data into train and test
- logistic regression
- Ke-fold cross validation for logistic regression
- Support Vector Machine
- K-fold cross_validation for SVM
- Decision Tree
- K-fold cross_validation for decision tree
- Random forest
- K-fold cross validation random forest
- KNN Classifier
- K-fold cross validation for Knn
Accuracy comparision table
from above table,
- For Knn classifier, the gap between train and test score is very less as compare to all other model.so Knn is performing very well.
- For logistic regression & random forest, gap between train and test is slightly more as compare to Knn.