0% found this document useful (0 votes)
4 views69 pages

ML File

The document outlines various machine learning programs and concepts, including supervised, unsupervised, and reinforcement learning, as well as essential libraries like NumPy, SciPy, and TensorFlow. It discusses the importance of data collection, performance measurement metrics such as accuracy, precision, recall, and F-scores, and provides practical examples of dataset creation and feature scaling in Python. Overall, it serves as a comprehensive guide to understanding and implementing machine learning techniques.

Uploaded by

gs83713
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views69 pages

ML File

The document outlines various machine learning programs and concepts, including supervised, unsupervised, and reinforcement learning, as well as essential libraries like NumPy, SciPy, and TensorFlow. It discusses the importance of data collection, performance measurement metrics such as accuracy, precision, recall, and F-scores, and provides practical examples of dataset creation and feature scaling in Python. Overall, it serves as a comprehensive guide to understanding and implementing machine learning techniques.

Uploaded by

gs83713
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Sr. Name of program Date Remark


no
1 Introduction to machine learning

2 Creation and representation of dataset in machine


learning

3 Feature scaling in machine learning in python.

4 Data pre-processing in machine learning in python.

5 Implement K-NN classifier in machine learning in


python.
6 Python implementation of principle component
nalysis in machine learning.

7 Python Implementation of Naive Bayes classifier in


machine learning.

8 Implementation of decision tree and random forest


classifier in machine learning.

9 Implement logistic regression classification


algorithm in python.

10 Implement K-means clustering in python.

Practical -1
AIM: Introduction to machine learning.
Machine learnin
Machine learning is a branch of artificial intelligence (Al) and computer science which
focuses on the use of data and algorithms to imitate the way that humans learn, gradually
improving its accuracy.
Machine learning is an important component of the growing field of data science.
Through the use of statistical methods, algorithms are trained to make classifications or
predictions, uncovering key insights within data mining projects. These insights
subsequently drive decision making within applications and businesses, ideally impacting
key growth metrics. As big data continues to expand and grow, the market demand for
data scientists will increase, requiring them to assist in the identification of the most
relevant business questions and subsequently the data to answer them.

Classfication of Machine learnin

Supervised Learning

Supervised learning refers to learning by training a model on labeled data. It is a very


common approach for predicting an outcome. For example, let's say we want to predict
who is likely to open an email we send. We can use the data from past sends along with the
"label" telling us if the recipient opened the email or not.

From there, we can build a training data set with data points about the recipient
(location, demographics, past email engagement behavior) along with the label. Our
model trains by trying many different ways to predict the label based on the other data
points until it finds the best one. Now that model can be used to predict who will open
the next email campaign we send.

Unsupervised Learning
Unlike supervised learning, unsupervised learning does not require labeled data.
Instead, it aims to find hidden relationships and patterns in the data. This is perfect
for when we don't know exactly what we're looking for.
The most common example of unsupervised learning, clustering algorithms take a
large set of data points and finds groups within them. For instance, say we want to
segment our customers into groups, but we don't know the best way to define the
groups. Clustering algorithms can identify them.
Reinforcement Learning
Reinforcement learning involves a feedback loop. The algorithm first decides on an
action and then observes data from the outside world to see its effect. As this happens
over and over, the model learns the best way to react. This is very similar to how we
learn by trial and error. For example, when learning to walk, we might start by acting
on our legs while receiving feedback from the environment and adapting our actions
to maximize the rewards

Machine learnin libraries

1 Numpy
NumPy is a very popular python library for large multi-dimensional array and matrix
processing, with the help of a large collection of high-level mathematical functions. It is
very useful for fundamental scientific computations in Machine Learning. It is particularly
useful for linear algebra, Fourier transform, and random number capabilities. High-end
libraries like TensorFlow uses NumPy internally for manipulation of Tensors.
2 Scipy
SciPy is a very popular library among Machine Learning enthusiasts as it contains different
modules for optimization, linear algebra, integration and statistics. There is a difference
between the SciPy library and the SciPy stack. The SciPy is one of the core packages that
make up the SciPy stack. SciPy is also very useful for image manipulation.
3 Scikit-learn
Scikit-learn is one of the most popular ML libraries for classical ML algorithms. It is built
on top of two basic Python libraries, viz., NumPy and SciPy. Scikit-learn supports most of
the supervised and unsupervised learning algorithms. Scikit-learn can also be used for data-
mining and data-analysis, which makes it a great tool who is starting out with ML.
4 TensorFlow
TensorFIow is a very popular open-source library for high performance numerical
computation developed by the Google Brain team in Google. As the name suggests,
Tensorflow is a framework that involves defining and running computations involving
tensors. It can train and run deep neural networks that can be used to develop several Al
applications. TensorFlow is widely used in the field of deep learning research and
application.
5 Pandas
Pandas is a popular Python library for data analysis. It is not directly related to Machine
Learning. As we know that the dataset must be prepared before training. In this case,
Pandas comes handy as it was developed specifically for data extraction and preparation.
It provides high-level data structures and wide variety tools for data analysis. It provides
many inbuilt methods for grouping, combining and filtering data.
1. Matplotlib
Matplotlib is a very popular Python library for data visualization. Like Pandas, it is not
directly related to Machine Learning. It particularly comes in handy when a programmer
wants to visualize the patterns in the data. It is a 2D plotting library used for creating 2D
graphs and plots. A module named pyplot makes it easy for programmers for plotting as
it provides features to control line styles, font properties, formatting axes, etc. It provides
various kinds of graphs and plots for data visualization, viz., histogram, error charts, bar
chats, etc,

Machine learninq Dataset collection


Collecting data for training the ML model is the basic step in the machine learning
pipeline. The predictions made by ML systems can only be as good as the data on which
they have been trained. Following are some of the problems that can arise in data
collection:
Inaccurate data. The collected data could be unrelated to the problem statement.
Missing data. Sub-data could be missing. That could take the form of
empty values in columns or missing images for some class of prediction.
Data imbalance. Some classes or categories in the data may have a
disproportionately high or low number of corresponding samples. As a
result, they risk being under-represented in the model.
Data bias. Depending on how the data, subjects and labels themselves are
chosen, the model could propagate inherent biases on gender, politics, age
or region, for example. Data bias is difficult to detect and remove. Several
techniques can be applied to address those problems:
Pre-cleaned, freely available datasets. If the problem statement (for example,
image classification, object recognition) aligns with a clean, pre-existing,
properly formulated dataset, then take advantage of existing, open-source

Web crawling and scraping. Automated tools, bots and headless browsers
can crawl and scrape websites for data.
Private data. ML engineers can create their own data. This is helpful when the
amount of data required to train the model is small and the problem statement
Performance Measurement of Model in terms Of
Accurac Precision Recall Confusion Matrix& Fl Score :

In a classification problem, the category or classes of data is identified based on training data. The model learns
from the given dataset and then classifies the new data into classes or groups based on the training. It predicts
class labels as the output, such as Yes or No, 0 or 1, Spam or Not Spam, etc. To evaluate the performance of a
classification model, different metrics are used, and some of them are as follows:

Accuracy
O Confusion Matrix
o Precision o Recall
O F-Score

I. Accuracy
The accuracy metric is one of the simplest Classification metrics to implement, and it can be determined as the
number of correct predictions to the total number of predictions.

It can be formulated as:

Number of Correct Predictions


Accuracy
Total number of predictions

When to Use Accuracy?


It is good to use the Accuracy metric when the target variable classes in data are approximately balanced. For
example, if 60% of classes in a fruit image dataset are of Apple, 40% are Mango. In this case, if the model is
asked to predict whether the image is of Apple or Mango, it will give a prediction with 97% of accuracy.

When not to use Accuracy?


It is recommended not to use the Accuracy measure when the target variable majorly belongs to one class. For
example, Suppose there is a model for a disease prediction in which, out of 100 people, only five people have a
disease, and 95 people don't have one. In this case, if our model predicts every person with no disease (which
means a bad prediction), the Accuracy measure will be 95%, which is not correct.

I l . Confusion Matrix
A confusion matrix is a tabular representation of prediction outcomes of any binary classifier, which is used to
describe the performance of the classification model on a set of test data when true values are known.
The confusion matrix is simple to implement, but the terminologies used in this matrix might be confusing for
beginners.

A typical confusion matrix for a binary classifier looks like the below image(However, it can be extended to use
for classifiers with more than two classes).
We can determine the following from the above matrix:

o In the matrix, columns are for the prediction values, and rows specify the Actual values. Here Actual and
prediction give two possible classes, Yes or No. So, if we are predicting the presence of a disease in a
patient, the Prediction column with Yes means, Patient has the disease, and for NO, the Patient doesn't
have the disease.
o In this example, the total number of predictions are 165, out of which 1 10 time predicted yes, whereas
55 times predicted No.
o However, in reality, 60 cases in which patients don't have the disease, whereas 105 cases in which patients
have the disease.
In general, the table is divided into four terminologies, which are as follows:

1 . True Positive(TP): In this case, the prediction outcome is true, and it is true in reality, also.
2. True Negative(TN): in this case, the prediction outcome is false, and it is false in reality, also.
3. False Positive(FP): In this case, prediction outcomes are true, but they are false in actuality.
4. False Negative(FN): In this case, predictions are false, and they are true in actuality.

Ill. Precision
The precision metric is used to overcome the limitation of Accuracy. The precision determines the proportion of
positive prediction that was actually correct. It can be calculated as the True Positive or predictions that are
actually true to the total positive predictions (True Positive and False Positive).

IV. Recall or Sensitivity


It is also similar to the Precision metric; however, it aims to calculate the proportion of actual positive that was
identified incorrectly. It can be calculated as True Positive or predictions that are actually true to the total number
of positives, either correctly predicted as positive or incorrectly predicted as negative (true Positive and false
negative).

The formula for calculating Recall is given below:


Recall:
TP+FN

When to use Precision and Recall?


From the above definitions of Precision and Recall, we can say that recall determines the performance of a
classifier with respect to a false negative, whereas precision gives information about the performance of a
classifier with respect to a false positive.
So, if we want to minimize the false negative, then, Recall should be as near to 100%, and if we want to minimize
the false positive, then precision should be close to 100% as possible.
In simple words, if we maximize precision, it will minimize the FP errors, and if we maximize recall, it will
minimize the FN error.

V. F-Scores
F-score or Fl Score is a metric to evaluate a binary classification model on the basis of predictions that are made
for the positive class. It is calculated with the help of Precision and Recall. It is a type of single score that
represents both Precision and Recall. So, the Fl Score can be calculated as the harmonic mean of both precision
and Recall, assigning equal weight to each of them.

The formula for calculating the Fl score is given below:


precision * recall
Fl — score — 2 *
precision -I- recall
When to use F-Score?
As F-score make use of both precision and recall, so it should be used if both of them are important for
evaluation, but one (precision or recall) is slightly more important to consider than the other. For example, when
False negatives are comparatively more important than false positives, or vice versa.
Practical -2
AIM: Creation and representation of dataset in machine learning.

In [1] : import pandas as pd

dataset = pd. read_csv("[Link]")


In [3] : dataset. head ( )
out[3]: type_school school_accreditation gender interest residence parent_age parent_salary house area average_grades parent was in college in_college
Less
O Academic Interested Urban 56 6950000 83.0 84.09 False True

Less

I Academic A Male Interested Urban 57 4410000 76.8 86.91 False True


Very
2 Academic B Female Interested Urban 50 6500000 80.6 87.43 False True

Very
3 Vocational Interested Rural 49 6600000 78.2 82.12 True True
Very
4 Academic A Female Interested Urban 57 5250000 75.1 86.79 False False

In dataset. tail ( )
Out[a] : type_school school accreditation gender interest residence parent_age parent_salary house area average_grades parent_was in_college in_college
Very
995 Vocational A Female Interested Rural 49 7420000 63.6 85.99 True True

Less
996 Academic B Female Interested Rural 51 7480000 84.3 89.72 True True

997 Vocational A Male Interested Urban 49 5550000 75.2 79.56 False True 998 Academic B Male Uncertain Rural 53 5840000 105.8 87.18 True True

000 Quiet

In [5] :
dataset

out[5]: type_school school_accreditation gender interest residence parent age parent_salary house area average_grades parent_was in college in_college
O Academic A Male Less Urban 56 6950000 83.0 84.09 False True
Interested
Less

Interested
False

I
Urban 57 4410000 76.8 86.91 True
Academic

Very
2 Academic B Female Interested Urban 50 6500000 80.6 8743 False True

Very 49
3 Vocational B Male Interested Rural 6600000 78.2 82.12 True True

4 Academic A Female Urban 57 5250000 75.1 86.79 False


Interested

49 True
995 Vocational A Female Interested Rural 7420000 63.6 85.99 True
996
Academic B Female
Less 84.3 89.72
Rural 51 7480000 True True
Interested
997 Vocational A Male Less 5550000 75.2 79.56

Interested Urban 49 False True

998 Academic B Male Uncertain Rural 53 5840000 105.8 87.18 True True

999 Academic B Male Quiet Urban 50 2940000 69.1 86.13 True False
Interested

1000 rows x 11 columns

In [ J:
Practical -3
AIM: Feature scaling in machine learning in python.
Feature Scaling is a technique to standardize the independent features present in the data in a
fixed range. It is performed during the data pre-processing to handle highly varying magnitudes
or values or units. If feature scaling is not done, then a machine learning algorithm tends to weigh
greater values, higher and consider smaller values as the lower values, regardless of the unit of
the values.
Techniques to perform Feature Scaling
Consider the two most important ones:
Min-Max Normalization: This technique re-scales a feature or observation value with
distribution value between 0 and 1.
Standardization: It is a very effective technique which re-scales a feature value so that it
has distribution with 0 mean value and variance equals to 1.

import matplotlib. pyplot as


pit df. plot (kind = 'bar')
In

Col A Col B Col C Col O

o 180000 110 18.9 1400


330000 205 23.4 1800

2 230000 230 14.0 1300

3 60000 450 13.5 1500


In

<matplotlib. axes ._subplots. AxesSubp10t at Øx91fbbbØ>


350000
col
A
300000 Col
B
250000 Col
c
Col
D
isoooo
100000

In [31 :

# copy the data


df_min_max_scaled =
[Link]()
# apply normal ization techniques for
column in df min max
scaled. colunns:

.min()) / .max()

col A col B col Ccol


D
e .4øøøøø o. 0.545455 0.2
øøøøeø

1 . øøeøøø 1. øøøøøø 1. 1.0


1 øøøoøø

2 e. o. .050505 ø.ø
566667 1SØ943

3 e. 0.427673 ø .øøøøøø 0.4


øøeøøø

In [41 : import matplotlib. pyplot as pit


df_min_max_scaled.plot(kind = 'bar' )

Out(4] : <matplotlib. axes._subplots .AxesSubp10t at Øx99f29dØ>


Practical -4

AIM: Data pre-processing in machine learning in python.


Data preprocessing is a process of preparing the raw data and making it suitable for a
machine learning model. It is the first and crucial step while creating a machine learning
model.

When creating a machine learning project, it is not always a case that we come across the
clean and formatted data. And while doing any operation with data, it is mandatory to
clean it and put in a formatted way. So for this, we use data preprocessing task.

In [3] :

out [3] : Age Salary

count 27.000000 28.000000


mean 36.925926 53642.857143
std 8.757089 19216.532785
min 18.000000 15000.000000
25% 30.000000 44750.000000
50% 37.000000 53000.000000
44. O OO
75% 67000.000000
000
max 50.000000 83000.000000
[14] : # dropping dupL icate values dataset =
dataset . dro p_duplicates() print
(dataset)
Country Age Salary Purchased
France aa.ø 72000 . No 1 Spain
27.0
2 Germany 30.0 54000 . No
3 Spain 38.0 61000 . No
5 Germany 40 . Nam
Yes4 Nigeria
6 France 35.0 58000 . o Yes18.0 15000 .
7 Spain Nan 52000 . No No
8 France 48 • 79000 . Yes
9 Germany 50.0 83000. No
10 France 37.ø 67øøø .
In [5] : dataset. describe()
Out[5] : Age Salary

count 23.000000 24.000000

mean 36.782609 51541.666667

std 8.852101 19352.517344

min 18.000000 15000.000000

25% 30.000000 43750.000000

37 .oooooo 51000.000000

75% 44.000000 62500.000000

max 50.000000 83000.000000


In [6]:

Out[6] : Country Age Salary Purchased

FalFa Fal F
se lse se a
l
s
e
F
a
FalFa Fal
l
se lse se
s
e
FalFa Fal F
2
se lse se a
l
s
e
F
a
FalFa Fal l
se lse se
s
e
4 F
a
FalFa Fal l
se lse se
s
e
F
a
FalFa Tr
l
se lse ue
s
e
F
a
FalFa Fal l
se lse se
s
e
F
Tr Fal a
Fal
u l
se e se
s
e
FalFa Fal F
se lse se a
l
s
e
Fal Fal F
se se a
l
s
e
F
a
1 FalFa Fal l
0 se lse se
s
e
1 FalFa Fal F
1 se lse se a
l
s
e
F
a
1 FalFa Fal l
2 se lse se
s
e
F
a
1 Tr Fa Fal l
3 uelse se
s
e
1 FalFa Fal Tr
4 se lse se ue
F
a
1 FalFa Fal l
5 se lse se
s
e
F
a
1 FalFa Fal
l
6 se lse se
s
e
F
a
1 FalFa Fal
l
8 se lse se
s
e
F
a
1 FalFa Fal l
9 se lse se
s
e
2 Tr F
0 u a
e l
Fal Fal s
se se e
In [7] :#

check
ing
the
num
ber
of
missi
ng
data
datas
et.
isnull
() .
sum()
Out[7] :
Country 1
A
g
e

2
S
a
l
a
r
y

1
p
u
r
c
h
a
s
e
d

1
d
t
y
p
e
:
i
n
t
6
4

In [8] : # Dropping categorical data rows with missing values dataset. ' any',

# Splitting dataset into independent & dependent variable


X = dataset [ ['country' , Age , 'salary' ] ] . values y
= dataset [ 'purchased' ] .values print(x)
subset=[ 'Country' , Purchased ' ] , inp1ace=True)
In [10] :

[ [ 'France' 44.0 72000.0]


[ 'Spain' 27.0 48000.0]
[ 'Germany' 30.0 54000.0]
[ 'Spain' 38.0 61000.0]
[ 'Nigeria' 18.0 15000.0]
[ 'Germany' 40.0 nan]
[ 'France' 35.0 58000.0]
[ 'Spain' nan 52øøø.e]
[ 'France' 48.0 79000.0]
[ 'Germany' 50.0 83000.0]
[ 'France' 37.0 67000.0]
[ 'Nigeria' 50.0 6øøoø.Ø]
[ 'France' 22.0 30000.0]
[ 'Nigeria' 35.0 43000.0]
[ 'Spain' 34.0 aaoøø.ø]
[ 'Spain' 33.0 480ØØ.ø]
[ 'Nigeria' 29.0 77000.0]

In [11] : print(y)

['No' 'Yes' 'No' 'No' 'No' 'Yes' 'Yes' 'No' 'Yes' 'No' 'Yes' 'Yes' 'No'
'Yes' 'Yes' 'Yes' 'Yes' 'Yes' 'Yes' 'Yes' 'No' 'Yes' 'No']
In [12]:

[ [44.0 72000.0]
[27.0 48000.0]
[30.0 54000.0]
[38.0 61000.0]
[18.0 15000.0]
[40.0 nan]
[35.0 58000.0]
[nan 52000.0]
[48. 79000.0]
[50. 83000.0]
[37. 67000.0]
[50. 60000. o]
[22. 30000.0]
[35. 43000.0]
[34. 44000.0]
48000.0]
[29. 77000.0]
[nan 57000.0]
[44.48000.0]
[37.23000.0]
[45.50000.0]
[30.30000.0]
[29.15000.0]]

Ιη [13] : strategy= ' mean ' )


1: 3] )
1:3] )
1:3] )
[ [44.ο 72000.0]
[27.0 48000.0]
[30.0 54000.0]
[38.0 61000.0]
[18.0 15000.0]
[40.0 50636.36363636364]
[35.0 58000.0]
[35.95238095238095 52000.0]
[48.0 79000.0]
[50.0 83000.0]
[37.0 67000.0]
[50.0 60000.0]
[22.0 30000.0]
[35.0 43000.0]
[ 34.ο 44000.0]
[33.0 48000.0]
[29.0 77000.0]
[35.95238095238095 57000.0]
[44.0 48000.0]
[37.ο 23000. ο]
[45.ο 50000. ο]
[30.ο 30000. ο]
ο 15000.
ο] ]
Practical -5
AIM: Implement K-NN classifier in machine learning in python.
K-nearest neighbors (KNN) algorithm is a type of supervised ML algorithm which can be used
for both classification as well as regression predictive problems. However, it is mainly used for
classification predictive problems in industry. The following two properties would define KNN
well -
Lazy learning algorithm - KNN is a lazy learning algorithm because it does
not have a specialized training phase and uses all the data for training while
classification.
Non-parametric learning algorithm - KNN is also a non-parametric learning
algorithm because it doesn't assume anything about the underlying data.

In [1]:

In [74]:

In [75]:

out[75]: WTT pT EQW SB LQ QW FD pJF HQE NXJ TARGETCLASS

0.913917 1.162073 0.567946 0.755464 0.780862 0.352608 0759697 0.643798 0.879422 1.231409 1

1 0.635632 1.003722 0.535342 0.825645 0.924109 0.648450 0.675334 1.013546 0.621552 1.492702

2 0.721360 1.201493 0.921990 0.855595 1.526629 0.720781 1.626351 1.154483 0.957877 1.285597

3 1.234204 1.386726 0.653046 0.825624 1.142504 0.875128 1.409708 1.380003 1.522692 1.153093

4 1.279491 0.949750 0 627280 0.668976 1.232537 0.703727 1.115596 0.646691 1.463812 1.419167

In
In [78]:

[79] :

In [80] : [Link]([Link]( 'TARGET CLASS' , axis-I))


out[80]: StandardSca1er(copy=True, with_mean=True, with_std=True)

In [81] : scaled features = [Link]([Link]( 'TARGET CLASS ' , axis-I))

In [82] : feat = df dfZfepd columns=df


.columns[: head ( )
from sklearn . model selection import train_test_split
out[82] : WTT p Tl EQW SBI LOE QWG FDJ pJF HOE NXJ

o -o. 123542 0.185907 -0.913431 0.319629 -1.033637 -2.308375 -0.798951 -1 482368 -0.949719 -0 643314

1 -1.084836 -0.430348 -1.025313 0.625388 -0.444847 -1.152706 -1.129797 -0.202240 -1.828051 0.636759 2 -0.788702 0.339318
0.301511 0.755873 2.031693 -0.870156 2.599818 0.285707 -0.682494 -0.377850

3 0.982841 1.060193 -0.621399 0.625299 0.452820 -0.267220 1.750208 1.066491 1.241325 -1.026987

4 1.139275 -0.640392 -0.709819 -0.057175 0.822886 -0.936773 0.596782 -1.472352 1.040772 0.276510
In [83] :
In [84]:

In [85]:

out[87]:
In [86]

metric_params=None, n_jobs=l, n_neighbors=l, p=2,


weights= uniform ' )

pred = [Link](X test)

from sklearn . metrics import classification_report,confusion_matrix

In [90] :

[ [125 18]
[ 13 144]]
[91] :

precision
0.91
1 e. 89
0.92 o. 90 157
avg / total e. 90 recall f I-

In [98]

scoresupport e. 90 o. 90 300
87 0.89 143
In [99]:

[Link]( ' Error Rate' )


out[99] : <[Link] at Ox11ca82ba8>
Error Rate vs. K Value
011

Here we can see that that after arouns K>23 the


precision - score support
error rate just tends to recall hover around 0.06-0.05 Let's retrain the model

with that and check th e 0.91 • 87 o. 89 143 classification reoort!

e. 89 0.92 o. 90 157
avg / total ø . 90 o. 90 300
o. 90
In [100] : # FIRST A QUICK COMPARISON TO OUR
ORIGINAL knn =
KNeighborsc1assifier(n_neighbors=1)
knn . fit (x_train, y_train)
pred = knn . predict (X_test)
print( 'WITH K=I' ) print( ' \n') print (
confusion_matrix (y_test , pred ) )
print( ' \n') print(classification
report (y_test, pred) )
WITH

[ [125 18]
[ 13 144]]
In [101]

WITH K=23

[ [132 11]
[ 5 152] ]

precision recall fl-score support


0.96 0.92 .94 143
1 0.93 97 0.95 157
avg / total 0.95 0.95 0.95 300
Type Markdown and LaTeX: a2
Practical -6
AIM: Python implementation of principle component Analysis in
machine learning.

Principal Component Analysis, or PCA, is a dimensionality-reduction


method that is often used to reduce the dimensionality of large data sets,
by transforming a large set of variables into a smaller one that still
contains most of the information in the large set.

Reducing the number of variables of a data set naturally comes at the


expense of accuracy, but the trick in dimensionality reduction is to trade a
little accuracy for simplicity. Because smaller data sets are easier to explore
and visualize and make analyzing data much easier and faster for machine
learning algorithms without extraneous variables to process.

So to sum up, the idea of PCA is simple — reduce the number of variables of a
data set, while preserving as much information as possible.
In [1]:

In [2]:

out[2] Class Alcohol Malic Ash Alcalinity Magnesium Total Nonflavanoid Proanthocyanins Color Hue 00280/00315of Proline
acid of ash phenols Flavanoids phenols intensity diluted wines

o 1 14.23 1.71 2.43 15.6 127 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065 1 I 13.20 1.78 2.14 11.2 100 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050
2 1 13.16 2.36 2.67 18.6 101 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185 3 1 14.37 1.95 2.50 16.8 113 3.85 3.49 0.24 2.18 7.80 0.86 3.45 1480

4 13.24 259 2.87 21.0 118 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735

5 1 14.20 1.76 2.45 15.2 112 3.27 3.39 0.34 1.97 6.75 1.05 285 1450 6 1 14.39 1.87 2.45 14.6 96 2.50 2.52 0.30 1.98 5.25 1.02 3.58 1290

7 14.06 2_15 2.61 17.6 121 2.60 2.51 0.31 1.25 5.05 1.06 3.58 1295 8 14.83 1.64 2.17 14.0 97 2.80 2.98 0.29 1.98 5.20 1.08 2.85 1045

9 1 1386 1.35 2.27 16.0 98 2.98 3.15 0.22 1.85 7.22 1.01 3.55 1045 In [3]:

out[3] :
df. i ] .describe()
loc
OD280/OD
Alcohol Malic acid Ash Alcalinityof ash MagnesiumphenolsTotal Flavanoids Nonflavanoidphenols ProanthocyaninsintensityColor Hue of dilu

count 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000
mean 13.000618 2.336348 2.366517 99.741573 2.295112 2.029270 0.361854 1.590899 5.058090 0.957449 2.611

std 0.811827 1.117146 3.339564 14.282484 0.625851 0.998859 o. 124453 0.572359 2.318286 0.228572 0.709
min 11.030000 0.740000 1.360000 10.600000 70.000000 0.980000 0.340000 o. 130000 0.410000 1.280000 0.480000 1.270
25% 12.362500 1.602500 2.210000 17.200000 88 000000 1.742500 1.205000 0.270000 1.250000 3.220000 0_782500 1_937

13.050000 1.865000 2.360000 19.500000 98.000000 2.355000 2.135000 0.340000 1.555000 4.690000 0.965000 2.780
75% 13.677500 3.082500 2.557500 21.500000 107.000000 2.800000 2.875000 0.437500 1.950000 6.200000 1.120000 3.170
max 14.830000 5.800000 3.230000 30.000000 162.000000 3.880000 5.080000 0.660000 3.580000 13.000000 1.710000 4.000
In for c in [Link][l:] :
df. boxplot(c , by: ' Class ' , , fontsize=14)
[Link]( format(c) , fontsize=16) plt.x1abel("Wine
Class", fontsize=16)

Alcohol Malic acid


Boxplot grouped by Class Boxplot grouped by Class
15

Wine Class
Wine Class

Ash Class Boxplot Alcalinity grouped of by ashClass


Boxplot grouped by
30

3.0
25

2.5
20

2.0
15

1.5
10

Wine Class Wine Class

Magnesium Total phenols


Boxplot grouped by Class Boxplot grouped by Class
4.0

160
3.5
140
3.0

1202.5

1002.0
1.5
80
1.0

Wine Class Wine Class


Flavanoids
Boxplot grouped by Class Nonflavanoid Boxplot grouped ey henolsClass

0.6
0.5
0.4
0.3
0.2

Wine Class Wine Class


Proanthocvanins
Boxplot grouped by Class Boxplot Color grouped intensitdby ass

10

12
Wine Class Wine Class
Hue
Boxplot grouped by Class Proline
Boxplot grouped by Class
1750
1.6
1500
1.4
1250
1.2
1000
1.0
750
0.8
500
0.6
250

Wine Class Wine Class

plt . figure 10, 6) )


PIt. 'OD28Ø/OD315 of diluted wines ' ' Flavanoids •class k' , alpha=ø. 75, s=150)
PIt . grid (True) plot of two features showing the \ncorrelation and class
seperation" , fontsize=15) plt.x1abe1("OD28Ø/OD315 of diluted wines" , fontsize=15)
PIt. y label ( " Flavanoids " , fontsize=15)
pit. show( )
Scatter plot of two features showing the
correlation and class seperation

OD280/OD315 of diluted wines

Wine data set features correlation


10

Alcalit•uty

Total

In

In [25]:

[16] :

[Link](10)

Out [26]: 00280/00315


Alcohol Ash
Malic Alcalinityof ash MagnesiumphenolsTotal Flavanoids Nonflavanoidphenols ProanthocyaninsintensityColor Hue ot
dilutedwines Proline
acid

o 1.518613 -0.562250 0.232053 -1.169593 1.913905 0.808997 1.034819 -0.659563 1.224884 0.251717 0.362177 1.847920 1.013009

1 0.246290 0499413 -0.827996 -2.490847 0.018145 0.568648 o. 733629 -0.820719 -0.544721 -0.293321 0.406051 0.965242

2 0.196879 0.021231 1.109334 -0.268738 0.088358 0.808997 1.215533 -0.498407 2.135968 0.269020 0.318304 0.788587 1.395148

3 1.691550 -0.346811 0.487926 -0.809251 0.930918 1.466525 -0.981875 1.032155 1.186068 —"0.427544 1.184071 2.334574

4 0.295700 0.227694 1.840403 0451946 1.281985 0.808997 0.663351 0.226796 0.401404 -0.319276 0.362177 0.449601 -0.037874

5 1.481555 -0.517367 0.305159 -1.289707 0.860705 1.562093 1.366128 -0.176095 0.664217 0.731870 0.406051 0.336606 2.239039
6 1.716255 -0.418624 0.305159 -1.469878 -0.262708 0.328298 0.492677 -0.498407 0.681738 0.083015 0.274431 1.367689 1.729520

7 1.308617 -0.167278 0.890014 -0.569023 1.492625 0.488531 0.482637 -0.417829 -0.597284 -0.003499 0.449924 1.367689 1.745442

8 2.259772 -0.625086 -0.718336 -1.650049 -o. 192495 0.808997 o. 954502 -0.578985 0.681738 0.061386 0.537671 0.336606 0.949319

s 1.061565 -0.885409 -0.352802 -1.049479 -0.122282 1.097417 1.125176 -1.143031 0.453967 0.935177 0.230557 1.325316 0.949319

In [27] : dfx. describe()


Out [27]:
Color ash phenols intensity
Alcohol Malic acid Ash Alcalinity of Magnesium Total phenols Flavanoids Nonflavanoid Proanthocyanins

count 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1.780000e+02 1780000e+02
mean -8.619821e-16 -8.357859e-17 -8.657245e-16 -1.160121e-16 -1.995907e-17 -2.972030e-16 -4.016762e-16 4.079134e-16 -1.699639e-16 -1.247442+18
std 1.002821e+OO 1.002821e+OO 1.002821e+00 1002821e+OO 1.002821e+OO 1.002821e+OO 1.002821e+00 1.002821e+OO 1.002821e+00
1.002821e+OO
min -2.434235e+00 -1.432983e+00 -3.679162e+00 -2.671018e+00 -2.088255e+00 -2.107246e+00 -1.695971e+00 -1.868234e+00 -2.069034e+00 -1.634288e+00
25% -7.882448e-01 -6.587486e-01 -5.721225e-01 -6.891372e-01 -8.244151e-01 -8.854682e-01 -8.275393e-01 -7.401412e-01 -5.972835e-01-7.951025e-
01
50% 6.099988e-02 -4.231120e-01 -2.382132e-02 1.518295e-03 -1.222817+01 9.595986e-02 1.061497e-01 -1.760948e01 4289785e-02 -1.592246e-
01
75% 8.361286e-01 6.697929+01 6.981085+01 6.020883e-01 5.096384e-01 8.089974+01 8.490851e-01 6.095413e-01 6.291754+01 4.939560e-01
max 2.259772e+00 3.109192e+00 3.156325e+OO 3.154511e+OO 4871372e+00 2.539515e+00 3.062832e+00 2.402403e+00 3.485073e+OO 3.435432e+00

PIt . figure 10, 6) )


PIt. for i in range(len(dfx_pca.explained_variance_ratio_))], y=dfx_pca . explained_variance_ratio
s=2ØO, alpha=ø. 75, c: ' orange ' , edgecolor= 'k' ) PIt. grid (True) [Link]("ExpIained variance ratio of
the \nfitted principal component vector\n" , fontsize=25) p It. xlabel ("Principal components" ,
fontsize=15) [Link]([i+l for i in range(1en(dfx_pca.exp1ained_variance_ratio_))],fontsize=15)
[Link] (fontsize=15) plt.y1abe1("Exp1ained variance ratio" p It. show( )
Explained variance ratio of the
fitted principal component vector
0.40
0.35
.e 0.30

0.25
•z
o
o
0.20
0.15
0 o
0.10 Oo O O
0.05
0.00

1 2 3 4 5 6 7 8 9 10 11 12 13
Principal components
In [50] : dfx trans = [Link](dfx)

In [60] : dfx trans : pd. DataFrame(data=dfx_trans) dfx_trans. head


(10)
o 3.316751 -l .443463 -0.165739 -0.215631 0.693043 -0.223880 0.596427 0.065139 641443 1.020956 -0.451563 0.540810 -0.066239
Out [60] :
1 2.209465 0.333393 -2.026457 -0.291358 -0257655 -0.927120 0.053776 1.024416 -0.308847 0.159701 -0.142657 0.388238 0.003637

2 2.516740 -1.031151 0.982819 0.724902 -0.251033 0.549276 0.424205 -0.344216 -1.177834 0.113361 -0.286673 0.000584 0.021717

3 3.757066 -2.756372 -0.176192 0.567983 -0.311842 0.114431 -0.383337 0.643593 0.052544 0.239413 0.759584 -0.242020 -0.369484

4 1.008908 -0.869831 2.026688 -0.409766 0.298458 -0.406520 0.444074 0.416700 0.326819 -0.078366 -0.525945 -0.216664 -0.079364

5 3.050254 -2.122401 -0.629396 -0.515637 -0.632019 0.123431 0.401654 0.394893 -0.152146 -0.101996 0.405585 -0.379433 0.145155

6 2.449090 -1.174850 -0.977095 -0.065831 -1.027762 -0.620121 0.052891 -0.371934 -0.457016 1.016563 -0.442433 0.141230 -0.271778

7 2.059437 -1.608963 0.146282 -1.192608 0.076903 -1.439806 0.032376 0.232979 o. 123370 0.735600 0.293555 0.379663 -0.110164

8 2.510874 -0.918071 -1.770969 0.056270 -0.892257 -0.129181 0.125285 -0499578 0.606589 0.174107 -0508933 -0.635249 0.142084

9 2.753628 0789438 -0.984247 0.349382 -0.468553 0.163392 -0.874352 0.150580 0.230489 0.179420 0.012478 0.550327 0042455

Plot the first two columns of this transformed data set with the color set to original ground truth class label
In [59] : [Link](figsize=(10,6)) plt. ' Class ' ] ,
edgecolors= k ' , alpha:e. 75, s=15Ø) plt. grid (True) plt.tit1e("C1ass separation using first
two principal components\n" , fontsize:20) plt.x1abe1("Principa1 component-I" ,
fontsize=15) component-2" , fontsize=15) plt. show()

Class separation using first two principal components

Principal component-I

Practical -7

AIM: Python Implementation of Naive Bayes classifier in machine


learning.
Naive Bayes algorithm is a supervised learning algorithm, which is based on Bayes theorem and used
for solving classification problems.
o It is mainly used in text classification that includes a high-dimensional training dataset.
Naive Bayes Classifier is one of the simple and most effective Classification algorithms which helps in
building the fast machine learning models that can make quick predictions.
It is a probabilistic classifier, which means it predicts on the basis of the probability of an object.
Naive: It is called Naive because it assumes that the occurrence of a certain feature is independent
of the occurrence of other features. Such as if the fruit is identified on the bases of color, shape, and
taste, then red, spherical, and sweet fruit is recognized as an apple. Hence each feature individually
contributes to identify that it is an apple without depending on each other.
o Bayes: It is called Bayes because it depends on the principle of Bayes l Theorem.

In [1] : import numpy as np import


pandas as pd import
[Link] as plt
%natplotlib inline

Data set

In [2] : df = pd. df. ./Datasets/[Link]')


head (10)

out[2]: Malicacid Alcalinityof ash phenolsTotal Flavanoids Nonflavanoidphenols Proanthocyanins intensityColor Hue 00280/00315 diluted winesof Proline Class Alcohol Ash Magnesium

14.23 1.71 2.43 15.6 127 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065

1 13.20 1.78 2.14 11.2 100 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050

2 1 13.16 2.36 2.67 18.6 101 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185

3 1 14.37 1.95 2.50 16.8 113 3.85 3.49 0.24 2.18 7.80 086 3.45 1480

4 1 13.24 2.59 2.87 21.0 118 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735

5 14.20 1.76 2.45 15.2 112 3.27 3.39 0.34 1.97 6.75 1.05 2.85 1450

6 1 14.39 1.87 2.45 14.6 96 2.50 2.52 0.30 1.98 5.25 1.02 3.58 1290

7 1 14.06 2.15 2.61 17.6 121 2.60 2.51 0.31 1 95 5.05 1.06 3.58 1295

8 1 14.83 1.64 2.17 14.0 97 2.80 2.98 0.29 1.98 5.20 1.08 2.85 1045

9 1 13.86 1.35 2.27 16.0 98 2.98 3.15 0.22 1.85 7.22 1.01 3.55 1045

Basic statistics of the features

In [8] [Link][:,l:].describe()
:

out[8] : OD280/OD31 Alcalinity Total Nonflavanoid Color


Alcohol Malic acid Ash of ash Magnesium phenols Flavanoids phenols Proanthocyanins intensity Hue of dilute wine

count 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.000000 178.00000
mean 13.000618 2.336348 2.366517 19.494944 99.741573 2.295112 2.029270 0.361854 1.590899 5.058090 0.957449 2.61168

std 0.811827 1.117146 0.274344 3.339564 14.282484 0.625851 0.998859 0.124453 0.572359 2.318286 0.228572 0.70999

min 11.030000 0.740000 1.360000 10.600000 70.000000 0.980000 0.340000 o. 130000 0.410000 1.280000 0.480000 1.27000
25% 12.362500 1.602500 2.210000 17.200000 88.000000 1.742500 1.205000 0.270000 1.250000 3.220000 0.782500 1.93750
50% 13.050000 1.865000 2.360000 19.500000 98.000000 2.355000 2.135000 0.340000 1.555000 4.690000 0.965000 2.78000
75% 13.677500 3.082500 2.557500 21.500000 107.000000 2.800000 2.875000 0.437500 1.950000 6.200000 1.120000 3.17000

max 14.830000 5.800000 3.230000 30.000000 162.000000 3.880000 5.080000 0.660000 3.580000 13.000000 1.710000 4.00000
Boxplotsby output labels/classes
In [20]

Alcohol
Boxplot grouped by Class Malic acid
Boxplot grouped by Class

15

14

13

12

11
1
1 2 3 2 3
Wine Class
Wine Class
Ash
Boxplot grouped by Class Alcalinitv of ash

1 2 3 2 3
Wine Class
1 Wine Class
Magnesium Total phenols
Boxplot grouped by Class 4.0 Boxplot grouped by Class
3
.
5
3
.
0
2
.
5
2
.
0
1
.
5
1
.
0

Boxplot grouéed by Class


3.0

2.5

2.0

1.5

160

140

120

100

80
1 2 3 1 2 3

Wine Class Wine Class


Flavanoids
Boxplot grouped by Class Nonflava grouped noid
ey henols Boxplot Class
5
0.6
4
0.5
3
0.4
2
0.3
2 3 2 3
1 1 Wine Class
1 Wine Class
0. 2
Proanthocyanins Color intensité(
Boxplot grouped by Class Boxplot grouped by ass
3.5 12
3.0 10
2.5 8
2.0 6
1.5 4
1.0 2
0.5
1
2
3
1

2 3
Wine Class
Wine Class
Hue

Boxplot grouped by Class OD280/OD315 of diluted wines


Boxplot grouped by Class
1.64.0
1.43.5
1.23.0
1.02.5
0.82.0

0.61.5
1
2 3 1 2 3

Wine Class Wine Class

Proline

Boxplot grouped by Class 1750

1500
1250
1000
750
500
250
1 2 3
Wine Class
In [82] : PIt . figure (figsize= ( 6) ) pit. 'OD28Ø/OD315 of diluted wines ' ' Flavanoids ' ] , ' Class edgecolors= 'k' , alpha=ø 8,
s=løø) plt . grid ( True) pit. plot of two features showing the \ncorrelation and class seperation" , fontsize=15) pit.
Of diluted wines " , fontsize=15) pit . y label ( " F lavanoids " , fontsize=15 )
Out [82] : <matplotlib . text . Text at øxIfbØ968b748>
Scatter plot of two features showing the

20 30
correlation and class seperation
OD280/OD315 of diluted wines

In [35]: def :
from matplotlib import pyplot as PI t
from matplotlib import cm as cm
fig =
12) )
axl —
fig. cmap = cm.
cax = axl. imshow(df. corr(), interpolation: "nearest"
cmap=cmap) axl . grid (True)
PI t . title( 'Wine data set features correlation\n , fontsize=15)
label s=df. columns axl . set_xticklabels ( labels , fonts ize=9)
axl . set_yticklabels ( labels , fontsize=9)
# Add cotorbar, make sure to specify tick Locations to match desired tickLabeLs
Wine data
fig. colorbar(cax, ticks: [0.1* i for i in 11) J) PI t . show() correlation_mat rix
set
features ( df )
correlation

Alea'inity

Total

Test/train split
In [44]:
from sklearn. model_selection import train
test_split test size=ø.3 # Test-set fraction

In [47]: X = [Link]( 'Class' ,axis=l)


Y = df['C1ass'J
Out
X train, X test, y train, y_test = train test split
[47]:
(X, y, test_size=test_size)
In [48]:
X train. shape
Out
(124, 13)

X_train. head ( )
[48]:AlcoholMalicacid Ash Alcalinity ashof Magnesium phenolsTotal Flavanoids Nonflavanoidphenols ProanthocyaninsintensityColor Hue00280/00315 diluted winesof Proline

33 13.76 1.53 2.70 19.5 132 2.95 2.74 0.50 1.35 5.40 1.25 3.00 1235

15 1363 1.81 2.70 17.2 112 2.85 2.91 0.30 1.46 7.30 1.28 2.88 1310

21 12.93 3.80 2.65 18.6 102 2.41 2.41 0.25 1.98 4.50 1.03 3.52 770

35 13.48 1.81 2.41 20.5 100 2.70 2.98 0.26 1.86 5.10 1.04 3.47 920

79 12.70 3.87 2.40 23.0 101 2.83 2.55 0.43 1.95 2.57 1.19 3.13 463

In [51] :

In [52] :

[Link](X_train,y_train)
Out[53]: GaussianNB(priors=None)

Total number of mislabelled data points from 54 test samples is 2

In [60] : print ("The classification report is as follows. .. In )


print (class ification_report (y_pred, y_test) )
The classification report is as follows. . .
precision recall fI-score support
1 0.93 e. 96 13
2 0.96 0.96 96 23
3 l.øø 94 97 18

avg / total 0.96 0.96 o. 96


In [58]: from [Link] import confusion matrix

cm = (confusion_matrix(y_test,y_pred)) cmdf = Class 'Class I', 'Class 2 • , Class


'Class I ' , 'Class ' print ("The '
confusion matrix looks like following. . .
cmdf )
The confusion matrix looks like following. . .

Out[69J : Classl Class 2 Class 3


Class I 13 1
Class 2 O 22 1
Class 3

** This showed that even in the presence of corrletation among features, the Naive Bayes algorithm performed quite well and could seperate the classes
easily

Practical -8
Aim: Implementation of decision tree and random forest classifier in
machine learning.
Decision Tree is a Supervised learning technique that can be used for both classification and
Regression problems, but mostly it is preferred for solving Classification problems. It is a
treestructured classifier, where internal nodes represent the features of a dataset, branches
represent the decision rules and each leaf node represents the outcome.
In a Decision tree, there are two nodes, which are the Decision Node and Leaf Node. Decision nodes
are used to make any decision and have multiple branches, whereas Leaf nodes are the output of
those decisions and do not contain any further branches.
o The decisions or the test are performed on the basis of features of the given dataset.
Random Forest is a popular machine learning algorithm that belongs to the supervised learning technique.
It can be used for both Classification and Regression problems in ML. It is based on the concept of
ensemble learning, which is a process of combining multiple classifiers to solve a complex problem and to
improve the performance of the model.

df. info( )
(class 'pandas. core. frame. DataFrame ' >
Rangelndex: 9578 entries, to 9577 Data
columns (total 14 columns) :
credit. policy 9578 non-null int64
purpose9578 non-null object
int. rate9578 non-null float64
installment 9578 non-null float64
log. annual. inc 9578 non -null float64
dti 9578 non -null float64 fico9578
non-null int64 days. with. cr. line 9578
non-null float64 revol. bal9578 non-null
int64 revol. util 9578 non -null float64
inq. last. 6mths 9578 non -null int64
delinq. 2yrs 9578 non -null int64
pub. rec9578 non -null int64 not. fully.
paid 9578 non-null int64 dtypes:
float64(6), int64(7), obj ect (1) memory
usage: 1.0+ MB

df. describe()
[Link] installment [Link] dti fico [Link],line [Link] [Link] [Link].6mths delinq.2yrs
count 9578.000000 9578.000000 9578 000000 9578.000000 9578.000000 9578.000000 9578.000000 9.578000e+03 9578.000000 9578.000000 9578.000000
mean 0.804970 0.122640 319.089413 10.932117 12.606679 710 846314 1.691396€+04 46.799236 1.577469 0.163706
std 0.396245 0.026847 207.071301 0.614813 6.883970 37.970537 3.375619e+04 29.014417 2.200245 0.546215
min 0.000000 0.060000 15.670000 7847502 0.000000 612.000000 0.000000e+OO 0.000000 0.000000 0.000000
25% 1.000000 0.103900 163.770000 10.553414 7.212500 682.000000 3.187000e+03 22.600000 0.000000 0.000000
50% 1 .oooooo 0.122100 268 950000 10.928884 12.665000 707.000000 8.596000e+03 46.300000 1.000000 0.000000
75% 1.000000 0.140700 432.762500 11.291293 17850000 737.000000 1.824950e+04 70.900000 2.000000 0.000000
max 1.000000 0.216400 940.140000 14.528354 29.960000 827000000 1.2073S9e+06 119.000000 33.000000 13.000000

4560.767197
2496.930377
178.958333
2820.000000
4139 958333
5730.000000
17639.958330

df.
describe( )
[Link] int- installm [Link]. fi [Link].l revol. revol [Link].6m deli
icy rate ent inc c ine bal .util ths yrs
o
cou 9573.0000 9578.0000 9578.0000 9578.0000 9578.000000 9578.0000 9578.000000 9.578000e+ 9578.0000 9578.0000 9578
nt 00 00 00 00 12.606679 00 4560.767197 03 00 00 0
0.8049 0.1226 319.0894 10.9321 6_883970 710.8463 1.691396e+ 46.7992 1.5774 16
mea 70 2496_9303
40 17 14 04 36 69
13 0.000000 77 0.
n 0.3962 0.0268 0_614 37.97053 3.375619e+ 29.0144 2.2002
7.212500 178.9583 5
st 45 47 207_0713 813 7 04 17 45
01 12.665000 33 0.
d 0.0000 0.0600 7.5475 612.0000 0.000000€+ o. 0.0000
15.6700 17.950000 0
00 00 02 00 2820.0000 00 000000 00
mi
29.960000 0.
n 1.0000 0.1039 00 10.5584 682.0000 00 3.187000e+ 22.6000 0.0000
0
00 00 163.7700 14 00 03 00 00
25 4139.9563 0.
1 0.1221 00 10.9288 707.0000 33 8.596000e+ 46.3000 1.0000
% 0
.ooooo 00 84 00 03 00 00
50 266.9500 5730 0.
o 0.1407 11.2912 737.0000 1.8249SOe+ 70.9000 2.0000
% 00 000000 0
75 1.0000 00 93 00 04 00 00
432.7625 17639.958330 13.0
% 00 0.2164 14.5283 827.0000 1.2073S9e+ 119.0000 33.000000
00
ma 1.0000 00 54 00 06 00
940.1400
x 00
00

df. head( )

In

Out

In

Out[6]: credit-policy purpose [Link] installment [Link] dti fico [Link] [Link] [Link] [Link].6mths delinq.2yrs [Link] not.

is a breakup of credit approval status. I means approved credit, ø not approved .


print (df[ ' credit . policy ' ] . value_counts ( ) ) means
[49]
Follwoing is a breakup of credit approval status. I means approved credit, means not
1 771ø ø 1868 approved.
Name: credit . policy, dtype: int64

' credit . policy' [ r fico ' J . plot . hi st (bins=3ø, alpha=ø. 5, color= ' blue ' , label= ' Credit .
Policy=l 'credit .policy' 'fico • J . plot . color= • red' , label= ' Credit
. Policy=ø plt. legend (fontsize=15 ) [Link] ("Histogram of FICO score by approved or disapproved
credit policies", fontsize=16) score", fontsize=14)
out[23J: <matplotlib. text . Text at ex2a5fb828198>
Histogram of FICO score by approved or disapproved credit policies

Credit-
Policy=l
Credit-
Policy=O

FICO score
Income level does not make a big difference in credit approval odds

0
Credit policy
Out [40 : <matplotlib . text . Text at øx2a5fd301048>
Credit-approved users have a slightly higher days With credit line

out øx2a5fdØIa3c8>
Credit policy
In
[621: plt. Figure 7) ) sns. ' int. rate' , ' fico' , data=df, hue=• credit.
policy • , colz • not. fully. paid ' , palette= • SetI • , size=6)
Out162]: <seaborn. [Link] at Ox2a581656e48>
«matplotl±b. figure. Figure at
not paid — O not ftaty paid —

dF_finaI. head ( )

t:
In df_final = 'purpose' ]
[66]:
,
In 167]:

OutC67) : [Link] intrate installment log -annuaLinc dti fico [Link]-line [Link] revol-util inq-Iast.6mths delinq.2yrs pub -rec [Link]-paid purpose_

In from sklearn. model select±on import


X= 'not paid' y df_finaIC
• not . [Link]'
X_test, y_train, y test = y, 30
_

In X. head()
In
OutC94] log dti days. line bal pub

In [95]:

In [189]:

In [1901:
Out [190]

In [191]:predictions = dtree.
In [192]: from sklearn .metr±cs impor-t classification_report, confusion_matrix
In [193]:

precision recali +1 -score support


0.85 0.84 0.85 2394 1
0.24 0.25 e. 25 48e
avg / total 0.75 0.74 e. 75 2874

In [194]: cm=conFus ion_mat rix (y_test predict


ions ) print( cm) print ("Accuracy of
prediction: " ,
L [2016 378)
C 36e 120])
Accuracy of prediction : e. 743
[IQ]: from sklearn .ensemble import RandomForestC1assifier

In [1Ø31: rfc =

In [1041: y_train)
rfc
Out

verbose=Ø, warm_start=Fa1se)

In [108]:
rfc_pred = rfc.
In [112]:
cr = predictions)

In [1131:
print(cr)
precision recall +1 -score support
0.85 0.84 0.84 2394
0.24 0.25 24 480
1
avg / total 0.75 0.74 0.74 2874

cm = rfc_pred) print
(cm)
[[2389 472
In
In [1531: nsimu = 21 accuracy:
*nsimu ntree =

tel*nsimu for i in
range(l,nsimu):
rfc = ' ) rfc. fit (X_train, y_train) rfc_pred = rfc. predict (X_test) cm = rfc_pred) accuracy(i) =
(cm[0, sum()

In [1541: 10, 6) )
: nsimu] , y=accuracytl : nsimu , s=6ø, c: ' red ' ) of trees in the Random Forest vs.
prediction accuracy (criterion: •gini' ) % fontsize=18) of trees % fontsize=1S) accuracy
from confusion matrix", fontsize=15)
Out [154] : cmatpiotlib. text. Text at øx2a58c7Ø363Ø>
Number of trees in the Random Forest vs. prediction accuracy (criterion: 'gini')

OBO

Number of trees

In [155]: nsimu = 21

rfc = RandomForestC1assifier(n_estimators=i*5,min samples depth=None, criterion: 'entropy')


y_train) rfc_pred = rfc.
cm = rfc_pred) accuracy(i) = (cmtø,
sum() ntree [i J

pit. figure (Figs ize= ( 10 6) ) pit . scatter( x=ntree CI : nsimuJ y=accuracy t I nsimuJ
, s=60, • red • ) pit "Number Of trees in the Random Forest vs. prediction
• entropy • ) % fontsize=18)
accuracy (criterion: pit Of trees" fontsize=IS) pit accuracy from confusion
matrix % fontsize=15)

[156]:

Out-[156] : «matplotlib. text. Text at Øx2a58c9ee94Ø>


Number of trees in the Random Forest vs. prediction accuracy (criterion: •entropy')

S 0830

•eos too Number of trees


In
In 11571:
Fixing tree depth

In [1581:
pit . Figure( figsåze= (10, 6) ) pit. : nsimuJ , y=accuracy : nsimuJ , c— • red
• ) pit . title( "Number of trees in the Random Forest vs. prediction accuracy (max depth:
fontsize=18)
None ) " pit . xlabel( " number Of trees % fontsize—IS) pit . accuracy from confusion matrix
% fontsize=15)
out [158] : <matplotlib. text. Text at Øx2a58caS8S18>
Number of trees in the Random Forest vs. prediction accuracy (max depth: None)

Number of trees

In [1591:

[1601: 10, 6) )
: nsimul , : nsimu), s=6Ø, c= ' red ' ) of trees
in the Random Forest vs. prediction accuracy (max depth: fontsize=18)
of trees", fontsize=15) accuracy from confusion matrix%
fontsize=15)
Out [160]
: cmatplotlib. text. Text at Øx2a58cc14e1Ø>
Number of trees in the Random Forest vs. prediction accuracy (max depth: 5)

Number of trees

o em too
In
Minimum sample split criteria
In

ntree = t0J*nsimu for i in


range(l,nsimu):

rfc = gini') rfc.


In
In rfc_pred = [Link](X_test)
[1651:
accuracy[i]
cm =

nsimu = 21

ntree = [01 *nsimu


for i in
•)

rfc_pred = [Link](X_test) cm =
confusion matrix(v test .rfc ored)
pit.
In [1661:
pit. scatter : nsimul , y=accuracytl : nsimu) , 5=60, c= ' red ' ) plt.tit1e("Number of trees in the Random Forest vs.
prediction accuracy (minimum sample split: fontsize=18) pit of trees", fontsize=15) plt accuracy from confusion
matrix", fontsize=15)

Out [166] : <[Link]. Text at Øx2a58ca68d68>


Number of trees in the Random Forest vs. prediction accuracy (minimum sample split: 2)
0 83

Number of trees

In nsimu = 21
[1671:

[1681: [Link](figs plt. : nsimu) , 5=60, c= ' red ' )


plt.tit1e("Number of trees in the Random Forest vs. prediction 20) % fontsize=18)
accuracy (minimum sample split: of trees", fontsize=15) accuracy from
confusion matrix", fontsize=15)
Out [168] : <matplotlib. text. Text at Øx2a58ce4c9Ø8>
Number of trees in the Random Forest vs. prediction accuracy (minimum sample split: 20)
o gas

S 0840

8 0ß35
In
0830

0825

0820

0815

Number of trees
In

Practical -9
AIM: Implement logistic regression classification algorithm in
python.
Logistic regression is one of the most popular Machine Learning algorithms, which comes under the
Supervised Learning technique. It is used for predicting the categorical dependent variable using
a given set of independent variables.
Logistic regression predicts the output of a categorical dependent variable. Therefore the outcome
must be a categorical or discrete value. It can be either Yes or No, 0 or 1, true or False, etc. but
instead of giving the exact value as 0 and 1, it gives the probabilistic values which lie between 0
and 1.
Logistic Regression is much similar to the Linear Regression except that how they are used. Linear
Regression is used for solving Regression problems, whereas Logistic regression is used for solving
the classification problems.

count u 1.000000 01.000000 821.000000 714.000000 "1.000000 "1.000000 u 1.000000


mean 446.000000 0.383338 2.308342 0.52008 0.3815@4 32.204208
std 257.353842 0.486522 0.836071 14.526427 1.102743 0.806057 49.62422
1.000000 0.000000 t .OOOCOO 0.420000 0.000000 0.000000 0.000000
min
25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000 7.910400
446.000000 0.000000 2.000000 28.000000 [Link] 0.000000 14.454200

75% "8.500000 1.000000 3.000000 38.000000 1.000000 0.000000 31.000000


In [89 1: max [Link] 1.000000 2.000000 80.000000 8.000000 6.000000 512.320200

import pandas as pd import


numpy as np import
[Link] as pit import
seaborn as sns %matplotlib inline

train = pd.read_csv(
'titanic_train.CSV train. head() # Training set is already available
Out [89] :
Passengerld Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
Sraund. Owen Harris male s
2 1 Cumings, Mrs. John Stsdley (Florence female 38.0 1 PC 17599 71.2832 ces
c
I
Briggs Th...

2 3 I 3 Hekkinen, Muss. I—ains female 20.0 o o STOW02. 7.9250 NaN


3101282
s

3 4 1 1 Futrelie. Mrs. Jacques Heath (Lity female 35.0 1 113802 52.1000 C123 s
May Peel)

4 5 o 3 Alben, Mr. Wiliym Henry male 35.0 o o 373450 8.0500 NaN s

In [901:

(Class •pandas. core.


frame. DataFrame'
Rangelndex: 891
entries, to 890 Data
columns (total 12
columns) :
Passengerld 891
non-null int64 Survived
891 non-null
int64
Pc-lass 891 non-null int64
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null
object Embarked 889
non-null object
dtypes: float64(2),
int64(5), object(S)
memory usage: 83.6+
KB
In
In [91]: d=train
.describe() d
Passengerld Survived Pclass Age SibSp Parch Fare

[92]:

Out[92] : <matplotlib. text. Text at ex2acbc65cØfØ>


Bar plot of the count of numeric features

In sns . set_style( • whitegrid ' ) sns . •


[931:
Survived • , data—train, palette= ' Rd3u_r ' )

OutC93J : <matplotlib. axes . _subplots .AxesSubp10t at Øx2acbc63f24Ø>

[Link]

Is there
a It looks like morefemalesurvivedthan males!
pattern
for the

survivability based on sex?

In [94]:
OutC94] :
sns set style( • ) s . • Survived • hue— • Pc s • , data—train, palette— •
rainbow •
OutC9SJ : cmatpiotlib. axes. _subplots . AxesSubpIot at Øx2acbc7Sb2Ø8>

Following
code c i ass survived—train . groupby( • Pc lass • ) • Survived • -mean ( )
extracts class_s ur-vived pd . DataFrame(f )
and plots f class survived . plot . • Survived • ) pit -title( 'V Fraction OF
the passengers survived by class % Fontsize=17)
fraction Of passenger count that survived, by each class
C 961 :

out : <[Link]. Text at Øx2acbc8c99e8>


Fraction of passengers survived by class

In [97]:
sns. ' whitegrid ' ) sns. countplot(x= ' Survived ' hue= ' SibSp
• data=train, palette= rainboa' )

OutC97] : .AxesSubp10t at Øx2acbc8accf8>

How does the overall age distribution look like?

In [98]: pit of the passengers" , fontsize=18) pit. ylabel( "Count " , fontsize=18) plt.
histogram of the train( 'Age ' . hist( bins=3Ø, color= • darkred • , alpha=ø. 7
, 6) )

.AxesSubp10t at Øx2acbc925438>

Age histogram of the passengers


In

Age of the passengers

Pclass

In [IØØJ: . groupby( ' Pc lass • ) [ ' Age • ] . mean ( )


= pd.
f_cias s_Age. plot. bar • Age ' ) pit. title( "Average age of
passengers by class" , fontsize=17) pit. y label( "Age (years) %
fontsize=17) pit class", fontsize=17)
Out : <matplotlib. text. Text at Øx2acbcc993c8>
Average age of passengers by class

Passenger class
In a—list [ ' Age •
])
def :
Age = cols CØJ
[Ell: Pclass cols(1J if
pd. isnuIi(Age):
if Pciass I: return
elif Pc lass 2:
return all]
else :
return at 2]
else :
return Age
In [1ø21:

Out cmatplotlib. Text Øx2acbccf43c8>


Bar plot of the count of numeric features

In [1Ø31:

Out :

Alien. [Link] Henry male o o


In [151: train .drop( [ 'Passengerld' , ' narree ' , 'Ticket' J, axis—I, inp1ace=True)
train . head( )
Out [les]: Survived Pclass Sex Age Sib Sp Parch Fare
Embarked
o
o c
2 1 3 female o
7.@250 s

3 1 I female o 52.1 coo


s

o
4 male
8.0500 s

sex pd . C 'Sex i J
embark = pd . get_dummies '(train C '
Embarked
In

(1101: train . drop ( C "Sex' '


Emba e=True) train pd
Out
. concat( [train, sex * train. head ( )

: 71.282
2 a ze.a
3 .35.a a

a asa

This data set is now ready for logistic regression analysis!

[1871: from sklearn . model_selection import


X tr—ins X—test, y_träin. y_test = 'Survived '
train ' Survived 1 , test
size=øe30 random state-111)
In [1931 :

F I-score vs. regularization parameter


Penalty parameter

F I-score
as a nsimu=1Ø1 class
function report =
*nsimu
of test imu
set size test_fraction for i in
X_train, X test, y_train, y_test =

logmodel max iter=1ØØØ,


nZobs=4)) iogmodei. fit (X_train, y_train) predictions = iogmodel.

predict (X_test)
predictions)
pit . plot (test_fractiontl : -2) , 1 : len
(fl) -2] ) pit . vs. test set size (fraction)%fontsize=2Ø) pit
set size (fraction)" , fontsize=17) pit on test pit .
shova()
(fraction)
In [197 J:
Fl -score vs. test set size (fraction)

Test set size (fraction)


F I-score as
a function
of random nsimu=1Ø1
seed of class_report = *nsimu
test/train for i in range(l,nsimu):
split X_train,
In [2e11:
logmodel iogmodel. fit (X_train, y_train) predictions = iogmodei. predict (X_test)

predictions)
pit . plot ( random_init [1 : len (
random_init ) -2] , fl[l : len(fl) - 2] ) vs.
random initialization seed" , fontsize=2Ø) initialization seed" ,
fontsize=17) plt.y1abe1("F1-score on test data%fontsize=17)
pit. shoea( )

Fl -score vs. random initialization seed

Random initialization seed


Practical -10

AIM: Implement K-means clustering in python.


The K-means algorithm divides a set of N samples X into K disjoint clusters C, each described by the mean pj of
the samples in the cluster. The means are commonly called the cluster "centroids"; note that they are not, in
general, points from X, although they live in the same space. The K-means algorithm aims to choose centroids
that minimise the inertia, or within-cluster sum of squared criterion:
min 1 1 2)
In (11: import pandas as pd import numpy
as np import matplotlib. pyplot as
pit import seaborn as sns
%matpiotiib inline
In [86]: df = pd.read_csv( 'College_Data'

In [31: df. head()


Out(3]•.
Private Apps Accept Enroll TopfOperc Top25perc [Link] [Link] Outstate Room-Board Books Personal PhD Terminal S.F-Ratio

Abilene
Christian Yes 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1
University
Adelphi
Yes 2186 1@24 512 16 20 1227 12280 6450 750 1500 30 12.2
University
Adrian Yes 1428 1097 236 22 50 1036 11250 3750 400 1165 52 12.9
College
Agnes
Scott Yes 417 342 137 60 510 12900 5450 450 875 7.7
College
Alaska
Pacific Yes 148 55 16 249 4120 1500 72 11.0
no
University

In [41:
df. info()
(class 'pandas. [Link]' >
Index: 777 entries, Abilene Christian I-hiversity to York College of Pennsylvania
Data columns (total 18 columns ) :
Private 777 non-null object
Apps 777 non-null int64 Accept
777 non-null int64 Enroll 777 non-
null int64 ToplØperc777 non-null int64
Top25perc 777 non-null int64
F. Undergrad 777 non-null int64
P. Undergrad 777 non-null int64
Outstate 777 non-null int64 Room.
Board 777 non-null int64 Books
777 non-null int64 Personal 777 non-
null int64 PhD 777 non-null int64
Terminal 777 non-null int64 S.F. Ratio
777 non-null float64 perc .alumni
777 non-null int64 Expend 777 non-
null int64 Grad. Rate 777 non-
null int64 dtypes: fioat64(1), int64(16),
object(l) memory usage: 115.3+ KB
In [51: dF. describe( )
Out(S): Apps Accept Enroll Topl Operc Top25perc EUndergrad P. Undergrad Outstate [Link] Books Personal

count 777.000000 777.000000 777.000000 777.000000 777 .oooooo 777.000000 777.000000 777.000000 777.000000 777.000000 777.000000 77
mean 3001.033352 2018.804370 779.972973 27.558559 855.2Æ5&4 10440.069241 4357.526334 549.380952 1340.042214
std 2870.201484 2451.113971 gee. 17atgo 17840264 12.304778 4850.420531 1522.431887 4023.016484 165.105360 677.071454 1
min 81. cooooc 72.000000 35.000000 1 -ococoo 13@.oooooc I-cooooo 2340.000000 treo-cooooc z. ococoo 250.000000
.OOOOOC
25% 776-cooooc 604-000000 242.000000 15.000000 41-000000 092.000000 es_oooooo 7320.000000 25@[Link] 470.000000 850 -cocooo

1558.000000 1110.000000 434.000000 23-000000 54.000000 1707.000000 353-000000 eeeo-oooooc 4200.000000 500. ocoooo 1200. cocooo

75% 2824. cooooc 2424.000000 C02. ocoooo 35.000000 n -000000 4005.000000 067.000000 12925.000000 5050.000000 [Link] 1700. oocooo
8
max 48094.000000 26330.000000 6302.000000 96.000000 100.000000 31043.000000 21836.000000 21700.000000 8124.000000 2340.000000 eco. oocooo to

sns . ' whitegrid' ) sns . Implot( • Cutstate , ' F


Undergrad • hue= ' Private ' palette— ' coolwarm • , size=6,
aspect—I,

sns . • whitegrid' ) sns . Implot( • Room. Board


• Grad . Rete ' , data=df, hue= ' Private • , palette= '
coolwarm ' size=6, aspect—I, )
<seaborn. axisgrid . FacetGrid at Øx1df639b871Ø>

R»orn Board

In t 71 :

Out t 7] : <seaborn. axisgrid . FacetGrid at Øx1df639b84ØØ>


ate

Outt8J:

In [91:
sns . ' Private ' perc. alumni ' , data=df)
',
Out-[9] :
<matplotlib. axes._subplots .AxesSubpIot at ØxIdf63e5d6d8>

In [101:
sns. '
darkgrid ' ) g = sns
.FacetGrid(df, g =
[Link]([Link], 'Outstate• ,
70
to

2500

In [111: sns. set_style( ' darkgrid • ) g


= [Link](df, g =
[Link](plt .hist, 'Grac .Rate'

Gad Rate

In (12]: df[dft • Grad . Rate • ] > løø]


Out [12]: Private Apps Accept Enroll Top 1 Operc Top25perc [Link] P-undergrad Outstate Room -Board Books Personal PhD Terminal [Link]

College

In [871: 'Grad. Rate ' • Cazenovia College' J = 10

C : \Users \Tirtha \Python IAnaconda311 ib\site-packages n_. py : I :


SettingWithCopyUarning: A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: [Link]

sns. ' darkgrid • ) g = sns , palette=• coolwarm• , -2) g =


[Link]([Link], 'Grad. Rate • ,

In [141:

out[14]

In [151:
10

Rate too

In [281 : from sklearn .cluster import KMeans

[411 : kmeans = init=2Ø)


Out [41] : kmeans . fit(df. drop( ' Private ' , axis—I) )
KNeans (algorithm= • auto' , copy_x=True, init= 'k-means++' , max
iter=3øø , n clusters=2, n init=2ø,
'auto' , tol=ø. 001, verbose=ø)
In [421:
clus cent=kmeans. cluster
centers clus cent
Out [421:
1.03631389e+Ø4, 6.55Ø89815e•Ø3, 2.56972222+03,
4.14907407e+ø1, 7. Ø2037037e+Ø1, 1.30619352e+Ø4,
2.46486111+03, 1.07191759e+Ø4, 4.643472222+03,
5.95212963e+Ø2, 1.7142e37øe-ø3, 8.63981481+01,
9.13333333e+Ø1, 1.40277778e+ø1, 2. øø74ø741e+e1,
1.417øsøøøe+ß, 6.75925926e•ø1, 3.14814815e-ø1],
1.81323468e+Ø3, 1.28716592e+Ø3, 4.91044843e+Ø2,
2.5309417øe+ø1, 5.34708520e•ø1, 2.18854858+03,
5.95458894e+Ø2, 1.03957085e-ø4, 4.31136472e+Ø3,
5.41982063e+Ø2, 1.28033632e•ø3, 7. ø4424514e+ø1,
7.78251121e+Ø1, 1.40997010e-ø1, 2.31748879+01,
8.932Ø4634e+Ø3, 6.50926756e+Ø1, 7.93721973e-e1]1)

Now compare these cluster centers (for all dimensionsffeatures) to the known means of labeled data

In [20]: 'Private' Yes '[Link]() # Statistics for private colleges only

Out[20] : Apps Accept Enroll Topl Operc Top25perc F-undergrad P-Undergrad Outstate [Link] Personal
count 565.000000 565.000000 565.000000 565.000000 565.000000 505.000000 565.000000 565.000000 565.000000 565.000000 505. cocooo

mean 1305.702655 456.@45132 20.320972 56.957522 1872.168142 433.966372 11801.603805 547.506105 1214.440708

std 2443.341319 457.520130 17.351201 19.583360 2110.051772 722.270487 2707.470822 174.222303 622.87%47 1

min 81.000000 72.000000 35.000000 1.000000 [Link] 139.000000 [Link] 2340.000000 2370.000000 250.000000 250.000000

25% 610.000000 501.000000 206.000000 17.000000 42.000000 840.000000 63.000000 9100.000000 2738.000000 450.000000 800.000000

50% 1133.000000 350.000000 228.000000 25.000000 55.000000 1274.000000 207.000000 11200.000000 4400.000000 500.000000 1100.000000

75% 2185.000000 1580.000000 520.000000 wococoo 70.000000 2018.000000 541.000000 13270.000000 5400.000000 600.000000 1500.000000 8
max 20192.000000 12007.000000 4615.000000 [Link] 100.000000 27378.000000 10221.000000 21700.000000 8124.000000 2240.000000 [Link] to

In [211: df[dft'Private ' 1--'No' .describe() # Statistics for public colleges only
Out[21J : Apps Accept Enroll Top I Operc Top25perc EUndergrad P-Undergrad Outstate Room. Board Books Personal
count 212.000000 212.000000 212.000000 212.000000 212.000000 212-000000 212.000000 212.000000 212.000000 212.000000 212.000000 21,
mean 1040.872342 22.834906 52.702830 8571.004717 1@78.188679 "13.410377 2748.2405% 554.377358 1676.981132
std 5370.675335 3477.266270 1261.592009 16.180443 20.091058 2321.034az 2145.248389 858.139@28 135.72925 677.515680
min 233000000 233.000000 153.000000 1.000000 12.000000 623.000000 9.000000 2530.000000 1730.000000 06.000000 400.000000
25% 2190.750000 1503.250000 701.750000 12.000000 37 .oooooc 2601.000000 aoo-oooooo 5306.000000 2121.500000 500.000000 1200.000000
50% 4307.000000 292@.500000 1337.500000 19.000000 51 .oooooc 6785.500000 1375.000000 [Link] 2708.000000 550.000000 1649.000000
75% 7722.500000 5234.000000 2243.750000 27.500000 65.000000 12507.000000 2495.250000 7844.000000 4362.000000 012.000000 2051.250000
max 48094.000000 20330.000000 6292.000000 [Link] 100.000000 31043.000000 21836.000000 15732.000000 6540.000000 1125.000000 4238.000000 10

df_desc=pd. DataFrame ( df. describe ( ) ) feat =


kmcius = pd. columns—feat) kmcius
In [43]:

Out [43]: Apps Accept Enroll Topt Operc Top25perc [Link] [Link] Outstate Room-Board Books Personal PhD o 10263.1288" 6550.808148 25".722222

41.4@0741 70.202704 12001.25185 4643.472222 505.212%' 1714.202704 ea-3@8148

1.044842 505.45804
4211.304723 541.2820ö3 1280.330323 70.442451
In [441 : kmeans.
1,
Out [44] .• 1, 1, 1, 1, 1, 1, o,
1,
array( labels 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, e, e, 1, 1, 1, 1, e, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, a,
1, 1,
1,
1, 1, e, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,
1, 1, 1, e, 1, 1, e, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, o, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, e, 1, 1, 1, 1, a, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, e, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, a, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, I, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, e, 1, 1,
1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
1, 1, 1, e, e, 1, 1, 1, 1, 1, 1, 1, 1, 1, e, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, e, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, e, 1, 1, 1, 1, 1, 1, 1, 1, o, e, 1,
1, 1, 1,
e, 1, 1, 1, 1, 1,
1, 1, a, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, o, 1,
1, 1, 1, 1, e, 1, 1, 1, 1, e, 1, 1, 1, 1, 1, e, 1, 1, 1, e, 1,
1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, e,
1
1, 1, 1 1, 1, 1, e, 1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,
1, o, 1, I, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, o, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, e, 1, o, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, o, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1,
1, 1, 1, e, o, 1, 1, 1, 1, 1, 1,
1, e, 1, 1,
1, e,
1, 1, 1, 1,
1, 1, 1, 1, 1, a, e, 1, 1, 1, 1, 1, 1,
1, e, 1, 1, 1, e, e, e, 1, 1, 1, e, 1, e, 1,
1,
1, 1, I, 1, 1, 1, 1, 1, e, 1, e,
1,
1, o, e, 1, 1, 1, 1, 1, ø, e, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, o, 1, 1, 1, 1, 1, 1, 1, o, 1, 1,
1, 1, 1, 1, 1, 1, 1, e, 1, 1, 1, 1, 1, 1, 1, a, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, e, 1, 1, 1, 1, 1, 1,
In [241: def converter(cluster) : if
:
return 1 else:
return

In (25]: dfl=df # Create o copy of data frame so that original data frame does not get corrupted ' with the cluster index df1t'C1uster'J =
dfC ' Private ] . apply(converter)
In [26]: dfl. head( )

Private Apps Accept Enroll ToplOperc Top25perc [Link] PUndergrad Outstate Room .Board Books Personal PhD Terminal [Link]

Abilene
Christian Yes 1600 1222 721 22 2885 537 7440 3300 450 2200 70 18.1
University

Adelphi Yes 21u 1@24 512 16 2083 1227 12280 6450 750 1500 29 30 12.2
University

Adrian Yes 1428 10@7 236 1036 11250 3750 400 1165 53 12.2
College
Agnes
Scott yes 417 349 137 510 5450 450 875 22 7.7
College
Alaska
Pacific Yes 146 55 16 249 see 7580 4120 no 1500 11.9
University

' Private ' 'Yes '


' Private ' ] ' No' ]
In [45 J: from [Link] import confusion_matrix,
classification_report print ( dfl[ ' Cluster • J , kmeans. labels_)
) print • Cluster ' , kmeans. labels_) )
74 138]
34 531]] precision recali fl-score
support
0.69 0.35 0.46 212
0.79 0.94 e.86 565 avg / total 0.76 0.78
ø.75
In [1Ø2J:
In [103]: kmeans =
kmeans. fit(df. drop( ' Private ' , axis-I) ) clus cent=kmeans. cluster centers
DataFrame(df. describe ( ) ) feat = kmcius =
pd. DataFrame (clus_cent, coil-nns=feat) diff() . iloc
centroxd• _diff = centroid_difft 't4ean
of corresponding entity (private) 'K-means cluster
centroid diffl 'toean of corresponding
entity (public)' centroid diff

out[1Ø3]: K-means cluster centroid-distance Mean of corresponding entity (private) Mean of corresponding entity (public)

Accept 5203.732229 1305.702655 3212.287736


2073.677372 456.@45132 1640.872642
ToplOperc 16.181324 22.834006
Top25perc 16.732852 50.957522 52.702830
[Link] 10873.336605 1872.168142 8571.004717
[Link] 1369.402217 433.%6372
Outstate 323.467406 11801.093805 "13.410377
Room. Board 322.1074% 45". t 43362 2748.2405%

Books 53.230000 547.5061@5 554.277358


Personal 423.867381 1214.440708 1676.981132

PhD 15.255697 71.0@3805 76.83406


Terminal 13.508221 78.534512 82.816038
[Link] -0.071923 12.945487 17.139151
perc-alumni -3.100814 25.800205 14.258491
Expend 5228.452682 7458.210038
[Link] 2.499917 68.966372 56.042453

You might also like