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
Data Science Dojo
tutorials
Commits
bf6747a8
Commit
bf6747a8
authored
May 04, 2022
by
Sanghoon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
bb485387
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
0 deletions
+115
-0
iHealth_R_code_notebook.ipynb
...ion/Product Recommendations/iHealth_R_code_notebook.ipynb
+115
-0
No files found.
Crash Course on Naive Bayes Classification/Product Recommendations/iHealth_R_code_notebook.ipynb
0 → 100644
View file @
bf6747a8
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Can Naïve Bayes models be used to provide new product recommendations for FitBits?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#Prepare a clean R environment in work space.\n",
"rm(list=ls())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Import our csv file data\n",
"data=read.csv(\"iHealthdata.csv\")\n",
"names(data)=c(\"Main Interest\",\"Current Exercise Level\", \"How Motivated\", \"Comfortable with tech Devices?\", \"Model #\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"\n",
"#Use the NB classifier on test Data\n",
"iHealth_classifier=function(interest,exercise,motivation,comfortlevel){\n",
" #interest='health'\n",
" #exercise='moderate'\n",
" #motivation='moderate'\n",
" #comfortlevel='yes'\n",
" \n",
" vars=c(interest,exercise,motivation,comfortlevel)\n",
" #data=read.table(\"i-01.txt\")\n",
" data=read.csv(\"iHealthdata.csv\")\n",
" names(data)=c(\"Main Interest\",\"Current Exercise Level\", \"How Motivated\", \"Comfortable with tech Devices?\", \"Model #\")\n",
" \n",
" uniquevec=unique(data$'Model #') #Unique Model Choices\n",
" val=length(uniquevec)\n",
" \n",
" ProbMat=matrix(0,(dim(data)[2]),val) \n",
" \n",
" #compute probabilities\n",
" for (i in 1:(dim(ProbMat)[1])){\n",
" for (j in 1:val){\n",
" data_cond=subset(data,data$`Model #`==uniquevec[j]) #Model type\n",
" \n",
" if (i<dim(ProbMat)[1]){\n",
" probval=dim(subset(data_cond,data_cond[,i]==vars[i]))[1]/dim(data_cond)[1]} #Compute probability of model being chosen\n",
" \n",
" else\n",
" \n",
" {probval=dim(data_cond)[1]/dim(data)[1]} #Compute marginal probabilities\n",
" \n",
" ProbMat[i,j]=probval\n",
" }\n",
" \n",
" }\n",
" \n",
" finalvec=apply(ProbMat,2,prod)\n",
" finalval=which.max(finalvec)\n",
" choice=uniquevec[finalval]\n",
" return(list(ProbMat=ProbMat,finalvec=finalvec,choice=choice))\n",
"}\n",
"\n",
"#}\n",
"#data_i100=subset(data,data$`Model #`=='i100')\n",
"\n",
"\n",
"#data_i500=subset(data,data$`Model #`=='i500')}\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#Executing function in a sample data set to provide new product recommendations\n",
"Ex1<-iHealth_classifier(interest=\"both\",exercise=\"active\",motivation =\"aggressive\",comfortlevel = \"yes\")\n",
"Ex2<-iHealth_classifier(interest=\"appearance\",exercise=\"sedentary\",motivation =\"moderate\",comfortlevel = \"yes\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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