EXPERIMENT -1:
TITLE: understanding the data
Concept:
1. Import data
2. understand its structure
3. identify the data types
4. identify the dependent and independent variables
5. summary statistics
Example :
We shall consider [Link] for this lab experiment
R syntax:
data=[Link]('[Link]')
> dim(data)
[1] 859 12
> str(data)
'[Link]': 859 obs. of 12 variables:
$ Age : int 52 39 43 39 56 41 51 57 48 34 ...
$ Sex : chr "F" "F" "M" "M" ...
$ ChestPainType : chr "ATA" "NAP" "ATA" "NAP" ...
$ RestingBP : int 120 110 142 160 120 125 130 140 120 150 ...
$ Cholesterol : int 210 182 207 147 85 269 179 260 254 214 ...
$ FastingBS : int 0 0 0 1 0 0 0 1 0 0 ...
$ RestingECG : chr "Normal" "ST" "Normal" "Normal" ...
$ MaxHR : int 148 180 138 160 140 144 100 140 110 168 ...
$ ExerciseAngina: chr "N" "N" "N" "N" ...
$ Oldpeak : num 0 0 0 0 0 0 0 0 0 0 ...
$ ST_Slope : chr "Up" "Up" "Up" "Up" ...
$ HeartDisease : int 0 0 0 0 0 0 0 0 0 0 ...
> # in the data dependent variable is heart disease
> # independent variables : age, sex, chest pain, resting bp , cholestrol, fastingBS, resting ECG,
maxHR, ExerciseAngina, Oldpeak, ST_Slope.
> # this is a a supervised learning problem and it is classification problem
> #4. identify data types
> Filter(is numeric, data)
Error: unexpected symbol in "Filter(is numeric"
> Filter([Link], data)
Age RestingBP Cholesterol FastingBS MaxHR Oldpeak HeartDisease
1 52 120 210 0 148 0.0 0
2 39 110 182 0 180 0.0 0
3 43 142 207 0 138 0.0 0
4 39 160 147 1 160 0.0 0
5 56 120 85 0 140 0.0 0
6 41 125 269 0 144 0.0 0
7 51 130 179 0 100 0.0 0
8 57 140 260 1 140 0.0 0
9 48 120 254 0 110 0.0 0
10 34 150 214 0 168 0.0 0
11 50 140 129 0 135 0.0 0
12 39 190 241 0 106 0.0 0
13 59 130 188 0 124 1.0 0
14 47 140 276 1 125 0.0 0
15 38 140 297 0 150 0.0 0
16 49 130 207 0 135 0.0 0
17 35 120 160 0 185 0.0 0
18 52 125 272 0 139 0.0 0
19 46 110 240 0 140 0.0 0
20 58 130 230 0 150 0.0 0
21 54 120 246 0 110 0.0 0
22 34 130 161 0 190 0.0 0
23 48 108 163 0 175 2.0 0
24 54 120 230 1 140 0.0 0
25 42 120 228 0 152 1.5 0
26 38 145 292 0 130 0.0 0
27 61 130 294 0 120 1.0 0
28 43 120 215 0 175 0.0 0
29 39 120 241 0 146 2.0 0
30 53 140 243 0 155 0.0 0
31 39 110 273 0 132 0.0 0
32 42 120 198 0 155 0.0 0
33 43 120 249 0 176 0.0 0
34 50 120 168 0 160 0.0 0
35 39 130 215 0 120 0.0 0
36 48 100 159 0 100 0.0 0
37 40 130 275 0 150 0.0 0
38 55 120 270 0 140 0.0 0
39 41 120 291 0 160 0.0 0
40 49 140 185 0 130 0.0 0
41 54 160 195 0 130 1.0 0
42 47 160 263 0 174 0.0 0
43 42 120 196 0 150 0.0 0
44 52 140 225 0 140 0.0 0
45 48 140 238 0 118 0.0 0
46 58 135 222 0 100 0.0 0
47 58 140 179 0 160 0.0 0
48 29 120 243 0 160 0.0 0
49 40 140 235 0 188 0.0 0
50 53 140 320 0 162 0.0 0
51 49 140 187 0 172 0.0 0
52 59 140 287 0 150 0.0 0
53 37 130 194 0 150 0.0 0
54 46 130 238 0 90 0.0 0
55 51 130 224 0 150 0.0 0
56 46 110 238 0 140 1.0 0
57 54 160 312 0 130 0.0 0
58 58 130 251 0 110 0.0 0
59 50 120 328 0 110 1.0 0
60 46 180 280 0 120 0.0 0
61 50 170 209 0 116 0.0 0
62 48 130 245 0 160 0.0 0
63 45 135 192 0 110 0.0 0
64 41 125 184 0 180 0.0 0
65 62 160 193 0 116 0.0 0
66 49 120 297 0 132 1.0 0
67 42 150 268 0 136 0.0 0
68 57 130 308 0 98 1.0 0
69 47 110 249 0 150 0.0 0
70 46 120 230 0 150 0.0 0
71 42 160 147 0 146 0.0 0
72 31 100 219 0 150 0.0 0
73 56 130 184 0 100 0.0 0
74 50 150 215 0 140 0.0 0
75 35 120 308 0 180 0.0 0
76 28 130 132 0 185 0.0 0
77 56 130 276 0 128 1.0 0
78 56 130 219 0 164 0.0 0
79 30 170 237 0 170 0.0 0
80 54 120 217 0 137 0.0 0
81 55 140 196 0 150 0.0 0
82 29 140 263 0 170 0.0 0
83 48 120 195 0 125 0.0 0
84 33 120 298 0 185 0.0 0
85 55 120 256 1 137 0.0 0
86 53 120 195 0 140 0.0 0
87 41 120 295 0 170 0.0 0
88 37 130 173 0 184 0.0 0
89 37 130 315 0 158 0.0 0
90 40 130 281 0 167 0.0 0
91 38 120 275 0 129 0.0 0
92 41 112 250 0 142 0.0 0
93 54 140 309 0 140 0.0 0
94 39 120 200 0 160 1.0 0
95 55 145 326 0 155 0.0 0
96 43 120 266 0 118 0.0 0
97 54 120 171 0 137 2.0 0
98 61 125 292 0 115 0.0 0
99 62 140 271 0 152 1.0 0
100 53 120 274 0 130 0.0 0
101 55 130 394 0 150 0.0 0
102 36 150 160 0 172 0.0 0
103 51 150 200 0 120 0.5 0
104 55 122 320 0 155 0.0 0
105 46 140 275 0 165 0.0 0
106 54 120 221 0 138 1.0 0
107 54 160 305 0 175 0.0 0
108 34 98 220 0 150 0.0 0
109 47 130 235 0 145 2.0 0
110 45 120 225 0 140 0.0 0
111 32 105 198 0 165 0.0 0
112 55 120 220 0 134 0.0 0
113 45 180 295 0 180 0.0 0
114 59 180 213 0 100 0.0 0
115 57 180 347 0 126 0.8 0
116 54 130 253 0 155 0.0 0
117 60 120 246 0 135 0.0 0
118 51 130 220 0 160 2.0 0
119 55 110 344 0 160 0.0 0
120 42 140 358 0 170 0.0 0
121 51 110 190 0 120 0.0 0
122 59 140 169 0 140 0.0 0
123 53 120 181 0 132 0.0 0
124 48 133 308 0 156 2.0 0
125 36 120 166 0 180 0.0 0
126 48 110 211 0 138 0.0 0
127 47 140 257 0 135 1.0 0
128 53 130 182 0 148 0.0 0
129 55 140 0 0 150 0.2 0
130 53 130 0 0 120 0.7 0
131 65 155 0 0 154 1.0 0
132 53 80 0 0 141 2.0 0
133 45 110 0 0 138 -0.1 0
134 38 100 0 0 179 -1.1 0
135 72 160 0 0 114 1.6 0
136 56 120 0 0 97 0.0 0
137 44 130 209 0 127 0.0 0
138 66 110 213 1 99 1.3 0
139 66 120 0 0 120 -0.5 0
140 60 180 0 0 140 1.5 0
141 56 126 166 0 140 0.0 0
142 62 120 220 0 86 0.0 0
[ reached 'max' / getOption("[Link]") -- omitted 717 rows ]
> colnames(Filter([Link],data))
[1] "Age" "RestingBP" "Cholesterol" "FastingBS"
[5] "MaxHR" "Oldpeak" "HeartDisease"
> colnames(Filter([Link],data))
[1] "Sex" "ChestPainType" "RestingECG" "ExerciseAngina"
[5] "ST_Slope"
> # in r
> # in the above data on indeoendent variables we have seven numeric variables
> # in over data on independent variables we have 4 character variables
> # in the above data on indeoendent variables we have six numeric variables
> # there are five categorial indeoendent variables
>
>
> #SUMMARY STATISTICS
> # 1. measures of central tendency
> # 2. measures of dispersion
> # 3. shape statistics
> summary(data )
Age Sex ChestPainType RestingBP
Min. :28.00 Length:859 Length:859 Min. : 0.0
1st Qu.:48.00 Class :character Class :character 1st Qu.:120.0
Median :55.00 Mode :character Mode :character Median :130.0
Mean :54.03 Mean :132.6
3rd Qu.:61.00 3rd Qu.:140.0
Max. :77.00 Max. :200.0
Cholesterol FastingBS RestingECG MaxHR
Min. : 0.0 Min. :0.0000 Length:859 Min. : 60.0
1st Qu.:168.5 1st Qu.:0.0000 Class :character 1st Qu.:119.0
Median :222.0 Median :0.0000 Mode :character Median :137.0
Mean :196.0 Mean :0.2468 Mean :136.2
3rd Qu.:267.0 3rd Qu.:0.0000 3rd Qu.:155.0
Max. :603.0 Max. :1.0000 Max. :202.0
ExerciseAngina Oldpeak ST_Slope HeartDisease
Length:859 Min. :-2.6000 Length:859 Min. :0.0000
Class :character 1st Qu.: 0.0000 Class :character 1st Qu.:0.0000
Mode :character Median : 0.8000 Mode :character Median :1.0000
Mean : 0.9326 Mean :0.5914
3rd Qu.: 1.6000 3rd Qu.:1.0000
Max. : 6.2000 Max. :1.0000
> psych::describe(data )
vars n mean sd median trimmed mad min max range skew
Age 1 859 54.03 9.36 55.0 54.27 8.90 28.0 77.0 49.0 -0.25
Sex* 2 859 1.80 0.40 2.0 1.88 0.00 1.0 2.0 1.0 -1.53
ChestPainType* 3 859 1.76 0.97 1.0 1.64 0.00 1.0 4.0 3.0 0.83
RestingBP 4 859 132.61 18.73 130.0 131.72 14.83 0.0 200.0 200.0 0.17
Cholesterol 5 859 195.98 111.40 222.0 201.08 69.68 0.0 603.0 603.0 -0.58
FastingBS 6 859 0.25 0.43 0.0 0.18 0.00 0.0 1.0 1.0 1.17
RestingECG* 7 859 1.98 0.65 2.0 1.97 0.00 1.0 3.0 2.0 0.02
MaxHR 8 859 136.17 25.64 137.0 136.52 26.69 60.0 202.0 142.0 -0.12
ExerciseAngina* 9 859 1.42 0.49 1.0 1.40 0.00 1.0 2.0 1.0 0.31
Oldpeak 10 859 0.93 1.08 0.8 0.79 1.19 -2.6 6.2 8.8 0.95
ST_Slope* 11 859 2.33 0.61 2.0 2.38 0.00 1.0 3.0 2.0 -0.31
HeartDisease 12 859 0.59 0.49 1.0 0.61 0.00 0.0 1.0 1.0 -0.37
kurtosis se
Age -0.33 0.32
Sex* 0.35 0.01
ChestPainType* -0.71 0.03
RestingBP 3.29 0.64
Cholesterol -0.05 3.80
FastingBS -0.63 0.01
RestingECG* -0.60 0.02
MaxHR -0.47 0.87
ExerciseAngina* -1.91 0.02
Oldpeak 1.07 0.04
ST_Slope* -0.66 0.02
HeartDisease -1.86 0.02
> [Link]("stargazer")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and
install the appropriate version of Rtools before proceeding:
[Link]
Installing package into ‘C:/Users/shagufta/Documents/R/win-library/4.1’
(as ‘lib’ is unspecified)
trying URL '[Link]
Content type 'application/zip' length 617690 bytes (603 KB)
downloaded 603 KB
package ‘stargazer’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\shagufta\AppData\Local\Temp\RtmpGkPEMK\downloaded_packages
> stargazer::stargazer(data,type = 'text')
PRACTICAL EXPERMIENT 2 : DATA PRE PROCESSING WITH R
Concept: pre-processing of data
Missing value analysis
Duplicated data analysis
Outliers analysis
Dimension reduction
Example : air quality data set in base R
Procedure :
Load air quality data from base R
Check the structure of the data
Understand the structure of the data
Check missing values
Summary of missing values
Remove/impute missing values
Check and summarise information on duplicated data
Remove duplicated values in the data
Check outliers with boxplots
Remove/impute outliers in the data
R CODE :
Airquality$ozone[[Link](airquality$ozone)]=mean(airquality$ozone,[Link]
=T)
Colsums([Link](airquality))
Airquality$solar.R[[Link](airquality$solar
Practical-6: Supervised Learning Models: Multiple Linear Regression model
CONCEPT: linear regression
EXAMPLE: airquality data set in base R
PROCEDURE:
1. Data processing :
. missing values analysis
Duplicate values analysis
Outlier analysis
Variable identifying
Independent and dependent variables identifying
2. Data partitioning train and test
3. Building model on train data
4. Accuracy of model on train data
5. Building model on test data
6. Accuracy of model on test data
7. Compare accuracy of data on both train and test data
R CODE: # data is air quality
#data pre processing
#we have 44 missing values
sum([Link](airquality))
#imputation of missing values
airquality$Ozone[[Link](airquality$Ozone,),=mean(airquality$Ozone)]
library(zoo)
airquality=zoo::[Link](airquality)
#imputation of outliers
airquality_new=subset(airquality,airquality$Ozone>7.75&airquality$Ozone<108.50)
str(airquality_new)
sum(duplicated(airquality_new))
#identifying IV & DV
View(airquality_new)
#DV is temp, IVS are ozone,solar,r,wind,month,day
#train-test split data 70:30
library(caret)
index=caret::createdatapartition(airquality_new$Temp,p=0.7,list=F)
train_data=airquality_new[index]
test_data=airquality_new[-index,]
#model building
#build model on train data
mod_train=caret::train(temp~.,method='lm',data=train_data)
predict_temp=predict(mod_train)
#calculate RMSE
caret::RMSE(airquality_new$temp,predict_temp)
#build model in test data
mod_test=caret::train(temp~.,method='lm',data=test_data)
predict_temp=predict(mod_test)
#calculate RMSE
caret::RMSE(airquality_new$temp,predict_temp)
#the model is underfitting since the RMSE of train data is 10.45and RMSE of test data is 9.84
==============================================
Statistic N Mean St. Dev. Min Max
----------------------------------------------
Age 859 54.031 9.362 28 77
RestingBP 859 132.608 18.733 0 200
Cholesterol 859 195.985 111.404 0 603
FastingBS 859 0.247 0.431 0 1
MaxHR 859 136.172 25.638 60 202
Oldpeak 859 0.933 1.078 -2.600 6.200
HeartDisease 859 0.591 0.492 0 1
----------------------------------------------
> table(data$Sex)
F M
168 691
> table(data$ChestPainType)
ASY ATA NAP TA
484 140 190 45
> table(data$RestingECG)
LVH Normal ST
188 501 170
> table(data$ExerciseAngina)
N Y
495 364
> table(data$ST_Slope)
Down Flat Up
63 451 345
>ssssssssss
KNN: K-Nearest Neighbours Algorithm:
This is an algorithm which can be used for both regression and classification
(here we’re using it only for classification)
-A powerful classification algorithm used in pattern recognition
-KNN stores all available cases and classifies new cases based on a similarity measure
(eg: distance function)
-one of the top data mining algorithms used today (Recommendations in flipkart etc)
-a non-parametric lazy learning algorithm
Steps in construction of KNN:
1. Load the data
2. Initialize K to your chosen number of neighbours for each example in the data
3. Calculate the distance between the query example and the current example from
the data
4. Add the distance and the index of the example to an ordered collection
5. Sort the ordered collection of the distances and the indices from smallest to largest
(in ascending order ) by the distances
6. Pick the first K entries from the sorted collection
7. Get labels from the sorted K entries
8.
Ex-3: supervised learning :Classification with naïve bayes :
Concept: classification with naïve bayes
Data: iris in R
PROCEDURE:
1. Data processing :
. missing values analysis
Duplicate values analysis
Outlier analysis
Variable identifying
Independent and dependent variables identifying
2. Data partitioning train and test
3. Building model on train data
4. Accuracy of model on train data
5. Accuracy of model on test data
6. Compare accuracy of data on both train and test data
R code:
#############
#naive bayes classification with iris data
############
data("iris")
#examine the data
?iris
#pre-processing of the data
#missing values
#duplicate values
#outliers
#finding missing values
sum([Link](iris))
#there are no missing values
#finding duplicate values
sum(duplicated(iris))
#to find which row is duplicate
which(duplicated(iris))
#removing duplicates from the data
iris=unique(iris)
str(iris)
#before there used to be 150 obs, now duplicates have been [Link],we have 149 obs
#finding outliers in the data
boxplot(iris[,-5])
#we found outliers in [Link] coloumn.
#replace outliers in [Link]
# we use percentile capping method
#it takes only part of the data 5% and 95%.
quantile(iris$[Link],probs = c(0.05,0.95))
#lowest values is 2.34 for 5%
#upper values is 3.80 for 95%
#percentile capping 5%-95%
iris_new=subset(iris,iris$[Link]>2.34&iris$[Link]<3.80)
iris_new
str(iris_new)
#running classification algorithm:NB(naive bayes)
#create data index
index=createDataPartition(iris_new$Species,p=0.7,list=F)
#train and test split of iris_new data
train_data=iris_new[index,]
test_data=iris_new[-index,]
#building naive bayes model on train data
mod=caret::train(Species~.,method='nb',data=train_data)
Experiment-7:
Unsupervised learning: Model clustering
Title: Model clustering
Concept: clustering means grouping the data into homogeneous units called clusters
The principle for clustering is : withon the group homogeneity and between the groups
heterogeneity
Types of clustering methods are popular
1. K-means
2. Hierarchical
3. Density based clustering
Example: aglomerative clustering in R using iris data
Procedure:
1. Acquire data
2. Pre process data( removing duplicates, null values, outliers)
3. Scale data
4. Finding the distance
5. Finding k-value(no. of clusters)
6. Build model
7. Visualize clustering
8. Find membership
9. Find cluster performance
R-code:
###############################################
#K-means aglomarative clustering
#data: Iris
###############################################
# to find the distance , we require only numerical data , hence we remove species
column
iris_new=iris[-5]
iris_new
#pre processing data
#removing null values
sum([Link](iris_new))
# there are no null values
# finding duplicates
sum(duplicated(iris_new))
#no duplicates
# finding outliers
boxplot(iris_new)
iris_new_scaled=scale(iris_new)
# find the distances
dist=dist(iris_new_scaled)
parameters::n_clusters_gap(iris_new_scaled)
#k=3 as give nis data description
#build k-means model
mod=kmeans(iris_new_scaled,centers = 3)
parameters::cluster_performance(mod)
mod2=kmeans(iris_new_scaled,centers = 2)
parameters::cluster_performance(mod2)
hc=hclust(dist)
factoextra::fviz_cluster(mod,iris_new)
mod_hierarchical=hclust(dist)
plot(mod_hierarchical,hang=-1,cex=0.2)
[Link](mod_hierarchical,k=3,border='green')
membership=cutree(mod_hierarchical,3)
table(membership)
newdataframe=[Link](iris_new,membership)
subset(newdataframe,newdataframe$membership==1)
subset(newdataframe,newdataframe$membership==2)
#finding cluster performance
parameters::cluster_performance(mod_hierarchical,iris_new,membership)
Practical-7:
Principle component analysis(PCA)
Concept:
Dimentionality reduction technique
Used as data pre-processing step
Comes under unsupervised learning technique
Example: Do PCA on BOSTON Housing prices data
Procedure :
Load boston data from MASS package
Examine the structure of the data
Use only numeric variables
Find principle components in the data using screeplot and kaiser’s criterion
Find the principle components in the boston data
kaisers criteria and scree plot will tell how many principle components
kaisers criteria : std>=1 (approx)
scree plot : the plot where it becomes flat
R CODE :
##pca
##reducing independent variables , means reducing columns
#focus on independent variables after taking away dependent variable
# load the data
library(MASS)
data=MASS::Boston
str(Boston)
#medv dependent variable ,[median value of housing prices];
#14 columns , 1 is dv and 13 are iv , 506 rows
#we shall do dimension reduction (pca)on 13 ivs
# from variables to principle components
#principle components must be less than variables
data_iv=data[,-14]
str(data_iv)
# do pca on data_iv
pca=princomp(data_iv,cor=T)
# finding eigen vectors
pca$sdev
# std>=1 , approx -- nly 5
#finding pricniple components using scree or elbow
plot(pca,type='lines')
# in scree plot we got 5 principle components
# which means even if we take 5 principle compinentsinstead of 13 variables
our analysis wont change
pca$scores
ourpcs=pca$scores[,1:5]
head(ourpcs)
newdata=[Link](ourpcs,Boston$medv)
5 principle components :-
Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
1 -2.098297 -0.773112750 0.34294273 -0.8917740 0.4230704
2 -1.457252 -0.591985207 -0.69519931 -0.4874585 -0.1958757
3 -2.074598 -0.599639400 0.16712160 -0.7392042 -0.9345341
4 -2.611504 0.006870619 -0.10028413 -0.3437212 -1.1049560
5 -2.458185 -0.097711947 -0.07534821 -0.4279069 -1.0659243
6 -2.214852 0.009487012 -0.67238109 -0.1759102 -0.6271876
Conclusion : 13 ivs have been reduced 5 principle components
PRACTICAL-8: APRIORI ALGORITHM
Concept: association rule mining or market basket analysis
Example: we do association rule mining with data set from arules
package
Procedure:
loading data from arules package , [Link]()
find item frequency using itemFrequency() function
item frequency plot of top itms
Finding rules using confidence and support parameters with apriory()
function
Inspecting the top rules
Visualization of rules with arulesViz package
UNIT-5: TIME SERIES ANALYSIS
A time series data is a set of observations on a variable generated sequentially
in time.
Data having no time dimension is known as cross-sectional data
Data having both time dimension and cross-sectional dimension is called
as panel data
Time series data is again divided into 2 types:
1. Continuous time series
2. Discrete time series
Continuous time series the observations are real time oriented: ex: ECG
Discrete time series: the observations are made at some fixed time
interval
Ex: daily, weekly, monthly, quarterly, yearly
Time series analysis:
A statistical technique that attempts to forecast future values of the time
series by examining past observations of the day
Time series components:
Trend
Seasonality
Cycle
Noise
[Link]: Long term increase or decrease in time series
2. Seasonality: Up and down in time series within one year
3. Cycle: Up and down in time series more than one year
[Link] or random variation or irregular component:
Erratic, non-systematic
Decomposition of time series:
Dividing the time series into 4 components
When to use additive model?
We use additive model when seasonality is constant
Multiplicative model is used when seasonality is increasing or
decreasing