Skip to content
Snippets Groups Projects
Commit ed8875e5 authored by dolfinsbizou's avatar dolfinsbizou
Browse files

Clustering

parent 53cd4b3a
No related branches found
No related tags found
No related merge requests found
library(MASS)
library(caret)
library(e1071)
# Chargement du jeu de données
clus.data <- read.table("tp3_a18_clas_app.txt")
clus.data$y <- clus.data$y - 1 # On nomme les classes 0 et 1 plutôt que 1 et 2 (plus pratique pour la régression logistique)
clus.data.X <- clus.data[-37]
clus.data.y <- clus.data$y
n <- dim(clus.data)[1]
p <- dim(clus.data)[2]
# TODO Questions à régler
# ACP ? pas ACP
# Régularisation des modèles ? Quand ? Sélection de valeurs ?
# Variables diverses
models.list <- c("qda", "lda", "nbc", "logreg") # Liste des modèles à tester
models.error <- c()
trials <- 10 # Nombre d'essais de cross-validation à faire par modèle
n_folds <- 5 # Nombre de blocs pour la cross-validation
# Test pour chaque modèle
for(model in models.list) # Pour chaque modèle
{
models.error[model] <- 0
for(i in c(1:trials)) # On fait plusieurs essais
{
folds <- sample(rep(1:n_folds, length.out=n))
for(k in 1:n_folds) # Sur chaque segment de la cross-validation
{
clus.train <- clus.data[folds!=k,]
clus.train.X <- clus.train[,-p]
clus.train.y <- clus.train$y
clus.test <- clus.data[folds==k,]
clus.test.X <- clus.test[,-p]
clus.test.y <- clus.test$y
# Apprentissage du modèle
if(model == "qda")
{
clus.model <- qda(clus.train.X, clus.train.y)
}
else if(model == "lda")
{
clus.model <- lda(clus.train.X, clus.train.y)
}
else if(model == "nbc")
{
# FIXME naiveBayes ne fonctionne pas, predict sort un truc vide
#clus.model <- naiveBayes(y~., data=clus.train)
clus.model <- NULL
}
else if(model == "logreg")
{
clus.model <- glm(y~., data=clus.train, family=binomial)
}
else
{
clus.model <- NULL
}
if(!is.null(clus.model))
{
if(model == "logreg")
{
clus.predict <- round(predict(clus.model, clus.test.X, type="response"))
models.error[model] <- models.error[model] + confusionMatrix(as.factor(as.integer(clus.predict)), as.factor(clus.test.y))$overall["Accuracy"]
}
else if(model == "nbc")
{
clus.predict <- predict(clus.model, clus.test.X)
models.error[model] <- models.error[model] + confusionMatrix(clus.predict, as.factor(clus.test.y))$overall["Accuracy"]
}
else
{
clus.predict <- predict(clus.model, clus.test.X)
models.error[model] <- models.error[model] + confusionMatrix(clus.predict$class, as.factor(clus.test.y))$overall["Accuracy"]
}
}
}
}
models.error[model] <- models.error[model]/(n_folds*trials)
}
print(models.error)
\ No newline at end of file
---
title: "R Notebook"
title: "Projet 1 SY19"
subtitle: "CHEVALIER Héloïse, JORANDON Guillaume"
output: html_notebook
---
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment