ML File
ML File
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.
Supervised Learning
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
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,
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.
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).
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.
Less
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
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
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
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.
In [31 :
.min()) / .max()
2 e. o. .050505 ø.ø
566667 1SØ943
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] :
37 .oooooo 51000.000000
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',
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]]
In [1]:
In [74]:
In [75]:
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] :
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]
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]:
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] ]
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)
Wine Class
Wine Class
3.0
25
2.5
20
2.0
15
1.5
10
160
3.5
140
3.0
1202.5
1002.0
1.5
80
1.0
0.6
0.5
0.4
0.3
0.2
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
Alcalit•uty
Total
In
In [25]:
[16] :
[Link](10)
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
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
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)
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()
Principal component-I
Practical -7
Data set
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
In [8] [Link][:,l:].describe()
:
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
2.5
2.0
1.5
160
140
120
100
80
1 2 3 1 2 3
2 3
Wine Class
Wine Class
Hue
0.61.5
1
2 3 1 2 3
Proline
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
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)
** 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.
' 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 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]:
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
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]:
S 0830
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
nsimu = 21
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)
Number of trees
In nsimu = 21
[1671:
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.
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...
3 4 1 1 Futrelie. Mrs. Jacques Heath (Lity female 35.0 1 113802 52.1000 C123 s
May Peel)
In [901:
[92]:
[Link]
Is there
a It looks like morefemalesurvivedthan males!
pattern
for the
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 :
In [97]:
sns. ' whitegrid ' ) sns. countplot(x= ' Survived ' hue= ' SibSp
• data=train, palette= rainboa' )
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>
Pclass
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:
In [1Ø31:
Out :
o
4 male
8.0500 s
sex pd . C 'Sex i J
embark = pd . get_dummies '(train C '
Embarked
In
: 71.282
2 a ze.a
3 .35.a a
a asa
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 =
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)
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( )
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
R»orn Board
In t 71 :
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
Gad Rate
College
In [141:
out[14]
In [151:
10
Rate too
Now compare these cluster centers (for all dimensionsffeatures) to the known means of labeled data
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
Out [43]: Apps Accept Enroll Topt Operc Top25perc [Link] [Link] Outstate Room-Board Books Personal PhD o 10263.1288" 6550.808148 25".722222
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
out[1Ø3]: K-means cluster centroid-distance Mean of corresponding entity (private) Mean of corresponding entity (public)