Commit 8ca11bfe by Sanghoon

Upload New File

parent 137277c4
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#Develop algorithm to predict whether a person has diabetes given health factors\n",
"\n",
"#Prepare a clean R environment in work space.\n",
"rm(list=ls()) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Use setwd() to navigate the data directory and specify desired folder. Here we are using Rstudio Editor directory.\n",
"setwd(dirname(rstudioapi::getSourceEditorContext()$path))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Import our csv file data\n",
"data=read.csv(\"pima.csv\",header=TRUE) "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#Construct a training data set\n",
"TrainingPct=0.8 #Percent of data to train model on\n",
"TrainingSample=floor(TrainingPct*dim(data)[1]) #Number of observations to train the model on , #dim()[n] = Retrieve or set the n dimension of an object\n",
"TestSample=dim(data)[1]-TrainingSample #Number of observations to test the model on"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"TrainingData=data[1:TrainingSample,] #Get the training data\n",
"Diabetes_categ=unique(TrainingData$diabetes) #Categorize diabetes by taking unique elements of the column diabetes(which is 0 and 1 in this case)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"MeanMat=matrix(0,length(Diabetes_categ),dim(TrainingData)[2]-1) #Initialize matrix for mean values in training sample\n",
"SDMat=matrix(0,length(Diabetes_categ),dim(TrainingData)[2]-1) #Initialize matrix for standard deviations(st dev) in training sample\n",
"MargProb=rep(0,length(Diabetes_categ)) #Initialize vector for marginal probabilities\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"for (i in 1:length(Diabetes_categ)){ #Using for loop, loop through whether or not the person has diabetes\n",
"Data_categ=subset(TrainingData,TrainingData$diabetes==Diabetes_categ[i]) #Subset training sample based on whether or not the person has diabetes\n",
" for (j in 1:(dim(Data_categ)[2]-1)){ #Using for loop, loop through obtain mean, st dev, and marginal probability\n",
" mean_val=mean(Data_categ[,j]) #Calculates mean\n",
" sd_val=sd(Data_categ[,j]) #Calculates st dev\n",
" MeanMat[i,j]=mean_val\n",
" SDMat[i,j]=sd_val\n",
" MargProb[i]=dim(Data_categ)[1]/dim(TrainingData)[1] #Calculates marginal probability\n",
"}\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"ProbList=list(MeanMat=MeanMat,SDMat=SDMat,MargProb=MargProb) #Stores the training data (mean, sd, marg prob in a list)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"#Construct a test sample\n",
"TestData=data[(TrainingSample+1):dim(data)[1],] #Select all except the training sample from the data\n",
"TestVec=TestData[1,]\n",
"AssignedMat=matrix(0,dim(TestData)[1],3)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#Construct a function NB classifier\n",
"pima_fn<-function(TestVec,ProbList){\n",
" \n",
" #Bring in training data as separate matrices and vectors - mean, st dev, and marg prob\n",
" MeanMat=ProbList$MeanMat\n",
" SDMat=ProbList$SDMat\n",
" MargProb=ProbList$MargProb\n",
" ProbTestMat=matrix(0,length(MargProb),length(TestVec))\n",
" \n",
" for (j in 1:length(TestVec)){ #Loop through the different elements of the patient (various variables)\n",
" \n",
" for (k in 1:length(ProbList$MargProb)){ #Loop through the options as to whether or not the patient has diabetes\n",
" \n",
" if (j<length(TestVec))\n",
" {ProbTestMat[k,j]=dnorm(as.numeric(TestVec[j]),MeanMat[k,j],SDMat[k,j])} #Calculate the normal density value\n",
" else\n",
" {ProbTestMat[k,j]=MargProb[k]} #Calculate marg prob\n",
" }\n",
" Probs=apply(ProbTestMat,1,prod) #Calculate the product across probabilities\n",
" ind=which.max(Probs) #Find which probability is higher\n",
" AssignedVec=c(Probs,Diabetes_categ[ind]) \n",
" }\n",
" return(list(AssignedVec=AssignedVec[1:2],AssignedCondition=AssignedVec[3])) #Elements returned as a list.\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#Load NB classifier\n",
"\n",
"for (i in 1:dim(TestData)[1]){\n",
" \n",
" TestVec=TestData[i,1:(dim(TestData)[2]-1)]\n",
" result<-pima_fn(TestVec,ProbList)\n",
" AssignedMat[i,]=c(as.numeric(result$AssignedVec),result$AssignedCondition)\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"CheckMat=data.frame(cbind(TestData$diabetes,AssignedMat[,3]))\n",
"colnames(CheckMat)=c(\"Actual\",\"Assigned\")\n",
"Pct_Accuracy=sum(CheckMat$Actual==CheckMat$Assigned)/dim(TestData)[1] #Computes the percent accuracy\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Classifier Percent Accuracy\"\n",
"[1] 0.721519\n"
]
}
],
"source": [
"print(\"Classifier Percent Accuracy\") #Print our accuracy as percent value.\n",
"print(Pct_Accuracy)\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"#Executing function in a sample data set to predict likelihood of diabetes\n",
"Example=read.csv(file=\"Example_Diabetes.csv\",header=TRUE)\n",
"Ex1<-pima_fn(Example[1,],ProbList)\n",
"Ex2<-pima_fn(Example[2,],ProbList)\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
}
  • عند رفع ملف جديد أو انتظار التحميل، يمكنك الاستفادة من الوقت بمشاهدة البث المباشر عبر Yacine TV والاستمتاع بالمحتوى المفضل لديك بكل سهولة.

  • While uploading a new file or waiting for it to download, you can make the most of the time by editing my videos on VN APK and enjoying content with ease.

    Edited by kinza
  • Super helpful, Carlos! This kind of applied Naive Bayes demo with real-world health data reminds me of how narrative-driven games like Summertime Saga MOD track decision outcomes based on character stats — except here it's actual health variables and probabilities! Love how clean and annotated the R code is. Thanks for sharing this — definitely using it as a reference for my own classification projects.

  • شاهد أحدث مباريات كرة القدم مجانًا تمامًا بمساعدة Yacine Tv

  • How people handles these programmatic based task. how much is is difficult to understand, after seeing this i can say that the development of Editor like VN Video Editor & Maker is not a simple task. Because there are no of options in editor to perform task like video animation, keyframes, cool text effects, splitting, chromakey etc. All of these functions are premium and paid the cost of developers effort. But if you are interesting to download free mod then there are several safe sites to download Mod application like Apkpure, Apkdone and official site VN Video Editor. Visit the website and download the VN Mod Apk - Download Premium Unlocked Apk 2025. If you want to edit video using desktop PC or Laptop then you can download VN Mod Apk For PC and install the emulator to run this application.

  • Katso pörssisähkön spot hinta 2025 reaaliajassa. Säästä sähköä ja rahaa ajoittamalla kulutus edullisille tunneille. https://sahkohintanyt.fi/

  • This Naive Bayes classification example is an excellent demonstration of applying predictive modeling in healthcare. For anyone interested in high-quality AI photo enhancement, check out this tool: https://theremini.org/ — it’s great for restoring and improving old or blurry images.

  • Shop wholesale 7 OHMZ Brand Advanced Kratom Alkaloids Tablets. Potent formula, bulk packs available. Perfect for retailers seeking premium kratom products. https://www.smoketokes.com/products/7-ohmz-advanced-kratom-alkaloids-tablets-14mg-x-3-tablets-20pc-per-box

  • This Naive Bayes classification example for predicting diabetes is a great use of data-driven health insights. 🧠📊 Similarly, tools like Remini Mod AI for iOS use AI to process and enhance image data—especially useful in healthcare for restoring blurry or old diagnostic images. Just as this model improves decision-making, Remini boosts image clarity, making both tools powerful in their own domains through AI.

  • This Naive Bayes classification example clearly highlights the potential of predictive modeling in transforming healthcare outcomes. On a similar note, if you're exploring AI tools for image clarity, try this AI-powered tool to enhance and restore old or blurry photos — it delivers outstanding results with minimal effort.

    Edited by Zeeshan
  • Great crash course on Naive Bayes! The diabetes example is super clear for beginners like me—it really shows how probability-based classification works in real datasets. By the way, speaking of real-world applications and experiments, I recently tried out the summertime saga mod apk and it’s surprisingly well-structured and fun for testing game logic on iOS.

  • Super helpful, Carlos! This kind of applied Naive Bayes demo with real-world health data reminds me of how narrative-driven games like track decision `outcomes based on character stats — except here it's actual health variables and probabilities! Love how clean and annotated the R code is. Thanks for sharing this — definitely using it as a reference for my own classification projects.https://yacinetvpro.com/

  • Super helpful, Carlos! This kind of applied Naive Bayes demo with real-world health data reminds me of how narrative-driven games like track decision `outcomes based on character stats — except here it's actual health variables and probabilities! Love how clean and annotated the R code is. Thanks for sharing this — definitely using it as a reference for my own classification projects.https://yacinetvpro.com/

  • Oh nice, I once tried testing my own build and realized how tricky it gets without a proper aplikasi apk installed 😅. Do you usually test yours on emulators or real devices?

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