-
Notifications
You must be signed in to change notification settings - Fork 1
/
CODE_asteroids_analysis_KNN_temp.R
92 lines (70 loc) · 3.03 KB
/
CODE_asteroids_analysis_KNN_temp.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
setwd("~/Github/AsteroidsClassification")
library(plyr)
library(gridExtra)
library(neuralnet)
library(caret)
library(tidyverse)
#t-distribution
confidence_interval <- function(vector, interval) {
# Standard deviation of sample
vec_sd <- sd(vector)
# Sample size
n <- length(vector)
# Mean of sample
vec_mean <- mean(vector, na.rm = TRUE)
# Error according to t distribution
error <- qt((interval + 1)/2, df = n - 1) * vec_sd / sqrt(n)
# Confidence interval as a vector
result <- c("err" = error, "mean" = vec_mean)
return(result)
}
actFun <- list()
actFun$sigmoid <- function(x) {
1 / (1 + exp(-x))
}
actFun$relu <- function(x) {
max(0,x)
}
#load dataset RObject as asteroids_split
load("DATA_asteroids_dataset_split_0.7.RData")
asteroids_split$train$Hazardous.int = as.factor(asteroids_split$train$Hazardous)
#logistic binary or sigm, tanh
#funz loss/obiettivo error quadr, cross entropy
#multiclass relu
act.fun_list = c('logistic',"tanh")
network = neuralnet(Hazardous ~ Orbit.Axis..AU. + Orbit.Eccentricity + Orbit.Inclination..deg. + Perihelion.Argument..deg. + Node.Longitude..deg. + Mean.Anomoly..deg. + Perihelion.Distance..AU. + Aphelion.Distance..AU. + Orbital.Period..yr. + Minimum.Orbit.Intersection.Distance..AU. + Asteroid.Magnitude,
asteroids_split$train[1:500,], hidden=5,
linear.output = FALSE)
net.predict = predict(network, asteroids_split$test[1:100,])
table(asteroids_split$test[1:100,]$Hazardous, net.predict[, 1] > 0.5)
plot(network)
net.prediction = c("a","b","c")[apply(net.predict, 1, which.max)]
# Binary classification
nn <- neuralnet(Species == "setosa" ~ Petal.Length + Petal.Width, iris_train, linear.output = FALSE)
pred <- predict(nn, iris_test)
table(iris_test$Species == "setosa", pred[, 1] > 0.5)
# Multiclass classification
nn <- neuralnet((Species == "setosa") + (Species == "versicolor") + (Species == "virginica")
~ Petal.Length + Petal.Width, iris_train, linear.output = FALSE)
pred <- predict(nn, iris_test)
table(iris_test$Species, apply(pred, 1, which.max))
#pesi generalizzati
#quanto attributo spiega il target - rispetto alla rnn ottenuta
#0 spiegano poco, 1 molto
par(mfrow=c(2,2))
gwplot(network,selected.covariate="Orbit.Axis..AU.")
gwplot(network,selected.covariate="Orbit.Eccentricity")
gwplot(network,selected.covariate="Orbit.Inclination..deg.")
gwplot(network,selected.covariate="Perihelion.Argument..deg.")
par(mfrow=c(2,2))
gwplot(network,selected.covariate="Node.Longitude..deg.")
gwplot(network,selected.covariate="Mean.Anomoly..deg.")
gwplot(network,selected.covariate="Orbit.Inclination..deg.")
gwplot(network,selected.covariate="Perihelion.Distance..AU.")
par(mfrow=c(2,2))
gwplot(network,selected.covariate="Aphelion.Distance..AU.")
gwplot(network,selected.covariate="Orbital.Period..yr.")
gwplot(network,selected.covariate="Orbit.Inclination..deg.")
gwplot(network,selected.covariate="Minimum.Orbit.Intersection.Distance..AU.")
par(mfrow=c(2,2))
gwplot(network,selected.covariate="Asteroid.Magnitude")