Commit bf6747a8 by Sanghoon

Upload New File

parent bb485387
{
"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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment