-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week6_assi3_sol1.R
178 lines (144 loc) · 4.38 KB
/
Week6_assi3_sol1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# # -*- coding: utf-8 -*-
# """Week6 Assi3 Sol1.ipynb
#
# Automatically generated by Colaboratory.
#
# Original file is located at
# https://colab.research.google.com/drive/1ewQwyRSZvPfGahiLcEwm1oUjFk3-zrCO
# """
###########################################################################
## Week-6, Homework-3, Sol-1
## Sreya Dhar
## Created: Mar 15, 2021
## Edited: Mar 29, 2021
###########################################################################
rm(list=ls())
setwd("C:/File E/EAS 507 Statistical Mining II/Week-6/HW-3")
## installing all the libaries in R kernel
# install.packages("arules")
# install.packages("Hmisc")
# install.packages("funModeling")
# install.packages("PerformanceAnalytics")
# install.packages("corrplot")
# install.packages("MASS")
# install.packages("ggplot2")
# install.packages("dplyr")
# install.packages("tidyverse")
# install.packages("tidyr")
# install.packages("repr")
# # install.packages("ggstatsplot")
# install.packages("psych")
# install.packages("gplots")
# install.packages("rsample")
# install.packages("rpart")
# install.packages("rpart.plot")
# install.packages("rattle")
# install.packages("RColorBrewer")
# install.packages("partykit")
# install.packages("party")
## importing the libraries in R kernel
library(rpart)
library(rpart.plot)
library(rattle)
library(RColorBrewer)
library(partykit)
library(party)
library(arules)
library(MASS)
library(Hmisc)
library(ggplot2)
library(dplyr)
library(funModeling)
library(tidyverse)
library(tidyr)
library(PerformanceAnalytics)
library(corrplot)
library(repr)
# library(ggstatsplot)
library(psych)
library("gplots")
library(rsample)
library("multtest")
library("fpc")
library("cluster")
# library("bootcluster")
library("fossil")
library(lsa)
library(philentropy)
rm(list=ls())
setwd("C:/File E/EAS 507 Statistical Mining II/Week-6/HW-3")
UserA<-c(5, 4, 0, 5, 2, 0, 3, 2)
UserB<-c(0, 3, 4, 4, 2, 2, 1, 0)
UserC<-c(3, 0, 1, 4, 0, 4, 5, 3)
dats <- data.frame(UserA, UserB, UserC)
### (a) converting the dataframe into boolean matrix for jaccard similarity::
#
jaccA <- c('a', 'b', 'd', 'e', 'g', 'h')
jaccB <- c('b', 'c', 'd', 'e', 'f', 'g')
jaccC <- c('a', 'c', 'd', 'f', 'g', 'h')
# #define Jaccard Similarity function
jaccard_similarity <- function(a, b) {
set_intersect = length(intersect(a, b))
set_union = length(a) + length(b) - set_intersect
return (set_intersect/set_union)
}
# #define Jaccard distance function
jaccard_distance <- function(a, b) {1-jaccard_similarity(a, b)}
#find Jaccard Similarity and distance between the two sets
jaccard_similarity(jaccA, jaccB)
jaccard_distance(jaccA, jaccB)
jaccard_similarity(jaccA, jaccC)
jaccard_distance(jaccA, jaccC)
jaccard_similarity(jaccB, jaccC)
jaccard_distance(jaccB, jaccC)
# distance(AB, method = "jaccard")
#### (b) Cosine Distance
#define vectors
# cosA <- c(5, 4, 0, 5, 2, 0, 3, 2)
# cosB <- c(0, 3, 4, 4, 2, 2, 1, 0)
# cosC <- c(3, 0, 1, 4, 0, 4, 5, 3)
cosA <- c(1, 1, 0, 1, 1, 0, 1, 1)
cosB <- c(0, 1, 1, 1, 1, 1, 1, 0)
cosC <- c(1, 0, 1, 1, 0, 1, 1, 1)
#calculate Cosine similarity
cosine(cosA, cosB)
cosine(cosA, cosC)
cosine(cosB, cosC)
## Cosine Distance as data-frame
cos_data <- cbind(cosA, cosB, cosC)
#calculate Cosine Similarity
cosine(cos_data)
### #calculate Cosine Distance from 'lsa' package
AB <- rbind(cosA,cosB)
distance(AB, method = "cosine")
# (c) Utility matrix as binary, to determine Jaccard distance
jacA <- c('a', 'b', 'd', 'g')
jacB <- c('b', 'c', 'd')
jacC <- c('a', 'd', 'f', 'g', 'h')
#find Jaccard Similarity and distance between the two sets
jaccard_similarity(jacA, jacB)
jaccard_distance(jacA, jacB)
jaccard_similarity(jacA, jacC)
jaccard_distance(jacA, jacC)
jaccard_similarity(jacB, jacC)
jaccard_distance(jacB, jacC)
# (d) Cosine Distance
#define vectors
cossA <- c(1, 1, 0, 1, 0, 0, 1, 0)
cossB <- c(0, 1, 1, 1, 0, 0, 0, 0)
cossC <- c(1, 0, 0, 1, 0, 1, 1, 1)
cosine(cossA, cossB)
cosine(cossA, cossC)
cosine(cossB, cossC)
coss_data <- cbind(cossA, cossB, cossC)
#calculate Cosine Similarity
cosine(coss_data)
# (e) Normalize the matrix
#define vectors
mean_A = 3.5
mean_B = 8/3
mean_C = 10/3
normA <- c(1.5, 0.5, 0, 1.5, -1.5, 0, -0.5, -1.5)
normB <- c(0, 1/3, 4/3, 4/3, -2/3, -2/3, -5/3, 0)
normC <- c(-1/3, 0, -7/3, 2/3, 0, 2/3, 5/3, -1/3)
## end ##