Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
tutorials
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
saeid
tutorials
Commits
bb485387
Commit
bb485387
authored
May 04, 2022
by
Sanghoon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
b398cc59
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
0 deletions
+63
-0
iHealth_Code.R
...yes Classification/Product Recommendations/iHealth_Code.R
+63
-0
No files found.
Crash Course on Naive Bayes Classification/Product Recommendations/iHealth_Code.R
0 → 100644
View file @
bb485387
# Can Naive Bayes models be used to provide new product recommendations for FitBits
#Prepare a clean R environment in work space.
rm
(
list
=
ls
())
#Use setwd() to navigate the data directory and specify desired folder. Here we are using Rstudio Editor directory.
setwd
(
dirname
(
rstudioapi
::
getSourceEditorContext
()
$
path
))
#Import our csv file data
data
=
read.csv
(
"iHealthData.csv"
)
names
(
data
)
=
c
(
"Main Interest"
,
"Current Exercise Level"
,
"How Motivated"
,
"Comfortable with tech Devices?"
,
"Model #"
)
#Use the NB classifier on test Data
iHealth_classifier
=
function
(
interest
,
exercise
,
motivation
,
comfortlevel
){
#interest='health'
#exercise='moderate'
#motivation='moderate'
#comfortlevel='yes'
vars
=
c
(
interest
,
exercise
,
motivation
,
comfortlevel
)
#data=read.table("i-01.txt")
data
=
read.csv
(
"iHealthData.csv"
)
names
(
data
)
=
c
(
"Main Interest"
,
"Current Exercise Level"
,
"How Motivated"
,
"Comfortable with tech Devices?"
,
"Model #"
)
uniquevec
=
unique
(
data
$
'Model #'
)
#Unique Model Choices
val
=
length
(
uniquevec
)
ProbMat
=
matrix
(
0
,(
dim
(
data
)[
2
]),
val
)
#compute probabilities
for
(
i
in
1
:
(
dim
(
ProbMat
)[
1
])){
for
(
j
in
1
:
val
){
data_cond
=
subset
(
data
,
data
$
`Model #`
==
uniquevec
[
j
])
#Model type
if
(
i
<
dim
(
ProbMat
)[
1
]){
probval
=
dim
(
subset
(
data_cond
,
data_cond
[,
i
]
==
vars
[
i
]))[
1
]
/
dim
(
data_cond
)[
1
]}
#Compute probability of model being chosen
else
{
probval
=
dim
(
data_cond
)[
1
]
/
dim
(
data
)[
1
]}
#Compute marginal probabilities
ProbMat
[
i
,
j
]
=
probval
}
}
finalvec
=
apply
(
ProbMat
,
2
,
prod
)
finalval
=
which.max
(
finalvec
)
choice
=
uniquevec
[
finalval
]
return
(
list
(
ProbMat
=
ProbMat
,
finalvec
=
finalvec
,
choice
=
choice
))
}
#}
#data_i100=subset(data,data$`Model #`=='i100')
#data_i500=subset(data,data$`Model #`=='i500')}
#Executing function in a sample data set to provide new product recommendations
Ex1
<-
iHealth_classifier
(
interest
=
"both"
,
exercise
=
"active"
,
motivation
=
"aggressive"
,
comfortlevel
=
"yes"
)
Ex2
<-
iHealth_classifier
(
interest
=
"appearance"
,
exercise
=
"sedentary"
,
motivation
=
"moderate"
,
comfortlevel
=
"yes"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment