-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodice.R
324 lines (212 loc) · 7.54 KB
/
codice.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(modelr)
library(corrplot)
## Set completo dei dati sui Pokemon
set <- read.csv("./sets/Pokedex_Ver3.csv")
## Set con le debolezze dei pokemon
aux_set <- read.csv("./sets/dataset_pokemon.csv")
## Prima Domanda
## Which type is the strongest overall? Which is the weakest?
### Creazione della Weak Table
weak_table <- aux_set %>%
select(type1, type2, contains("against")) %>%
filter(type2 == "") %>%
distinct() %>% ## make only one type for row because are identicaly
select(-type2) %>%
rename(type = type1)
### Somma dei Punti
sum <- weak_table %>%
select(-type) %>%
rowSums()
weak_table %>%
select(-contains("against")) %>%
mutate(sum = sum) %>%
arrange(sum)
## Seconda Domanda
## Which double-type exisitng is the strongest overall? Which is the weakest?
#### Creazione della Double Weak Table
double_weak_table <- aux_set %>%
select(type1, type2, contains("against")) %>%
filter(type2 != "") %>%
distinct()
### Somma dei Punti
sum <- double_weak_table %>%
select(-contains("type")) %>%
rowSums()
double_weak_table %>%
select(-contains("against")) %>%
mutate(sum = sum) %>%
arrange(sum)
## Terza Domanda
## Which type is the most likely to be a legendary Pokemon?
## Ricerca dei pokemon leggendari
legendary <- set %>%
select(NAME, TYPE1, TYPE2, LEGENDARY) %>%
filter(LEGENDARY == TRUE) %>%
distinct() ## necessario poichè alucni pokemon come Deoxsy cambiano forma ma non tipo
### Trovare il Tipo che risulta più frequente
t1 <- as.data.frame(table(legendary$TYPE1))
t2 <- as.data.frame(table(legendary$TYPE2, exclude = ""))
legendary_type_sum <- full_join(t1, t2, by = "Var1") %>%
select(-Var1) %>%
rowSums()
t1 %>%
rename(TYPE = Var1) %>%
select(-Freq) %>%
mutate(SUM = legendary_type_sum) %>%
arrange(SUM)
### Verifica
legendary %>%
filter(TYPE1 == "Psychic" | TYPE2 == "Psychic")
## Quarta Domanda
## How does height and weight of a Pokemon correlate with its various base stats?
ggplot(data = set, mapping = aes(x = HEIGHT, y = HP)) +
geom_point() +
geom_smooth()
height_cor <- as.data.frame(c(cor(set$HEIGHT, set$HP),
cor(set$HEIGHT, set$ATK),
cor(set$HEIGHT, set$DEF),
cor(set$HEIGHT, set$SP_ATK),
cor(set$HEIGHT, set$SP_DEF),
cor(set$HEIGHT, set$SPD)))
weigth_cor <- as.data.frame(c(cor(set$WEIGHT, set$HP),
cor(set$WEIGHT, set$ATK),
cor(set$WEIGHT, set$DEF),
cor(set$WEIGHT, set$SP_ATK),
cor(set$WEIGHT, set$SP_DEF),
cor(set$WEIGHT, set$SPD)))
pairs(set[, c(15, 16, 17, 18, 19, 20, 21)])
# Correlazione tra peso e gli stati
corrplot(cor(set[, c(15, 16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
# Correlazione tra altezza e gli stati
corrplot(cor(set[, c(14, 16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
# Dai grafici capiano che non esiste una correlazione lineare tra l'atezza e il peso verso le statistiche base del pokeomn.
# Proviamo con una quadratica
quad_height <- set[, c(14)] ^ 2
quad_weight <- set[, c(15)] ^ 2
corrplot(cor(quad_height, set[, c(16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
corrplot(cor(quad_weight, set[, c(16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
# Anche con una correlazione quadratica abbiam dei valori molto più piccoli (vicini al 0.2), pertanto non esiste effettivamente una correlazione quadratica.
# Utlima osservazione, proviam con il log
log_height <- set[, c(14)] %>% log()
log_weight <- set[, c(15)] %>% log()
corrplot(cor(log_height, set[, c(16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
corrplot(cor(log_weight, set[, c(16, 17, 18, 19, 20, 21)]),
method = "number",
type = "upper" # show only upper side
)
# In questo caso abbiam che sia per il peso che per l'altezza c'è una correlazione: infatti abbiam che i primi 3 stats, ovvero hp atk e def, hanno una corelazione sopra il 0.5.
ggplot(data = set, mapping = aes(log_height, ATK)) +
geom_point() +
geom_smooth()
# Ultima Domanda
# Can you build a Pokemon dream team?
# A team of 6 Pokemon that inflicts the most damage while remaining relatively impervious to any other team of 6 Pokemon. ---
# Cerchiamo una combinazione di pokemon la quale abbia una ampia "copertura" dai vari attacchi
# Dobbiamo considerare le debolezze e moltiplicarle tra loro
#dream_team <- aux_set[, 2:19] %>%
#rowwise() %>%
#mutate(Product = prod(c_across("against_bug":"against_water"))) %>%
#filter(Product == 0)
cond <- aux_set$against_bug == 0 |
aux_set$against_dragon == 0 |
aux_set$against_dark == 0 |
aux_set$against_electric == 0 |
aux_set$against_fairy == 0 |
aux_set$against_fight == 0 |
aux_set$against_fire == 0 |
aux_set$against_flying == 0 |
aux_set$against_ghost == 0 |
aux_set$against_grass == 0 |
aux_set$against_ground == 0 |
aux_set$against_ice == 0 |
aux_set$against_normal == 0 |
aux_set$against_poison == 0 |
aux_set$against_psychic == 0 |
aux_set$against_rock == 0 |
aux_set$against_steel == 0 |
aux_set$against_water == 0
best_candidates <- aux_set %>%
select(name, contains("against"))
## Selezionare quelli che hanno presente almeno un zero nelle colonne delle debolezze
# y <- aux_set[ which(cond), ]
#[, 2:19]
x <- best_candidates %>%
filter(rowSums(best_candidates == 0) > 0)
## Scegliere quello che ha la somma più bassa
x <- x %>%
rowwise() %>%
mutate(Sum = sum(c_across("against_bug":"against_water"))) %>%
arrange(Sum)
dream_team <- head(x, 1)
## Da lui, cercare un altro pokemon che compensi le debolezze del primo
any(x$against_ground %in% 0)
# Skarmory
new_member <- x %>%
filter(against_ground == 0) %>%
arrange(Sum) %>%
head(1)
dream_team <- rbind(dream_team, new_member)
# --- #
any(x$against_fire %in% 0)
any(x$against_fire %in% 0.5)
# Marill -> Azumarill
new_member <- x %>%
filter(against_fire == 0.5) %>%
arrange(Sum) %>%
head(1)
dream_team <- rbind(dream_team, new_member)
# ---#
any(x$against_electric %in% 0)
# Steelix
new_member <- x %>%
filter(against_electric == 0) %>%
arrange(Sum) %>%
head(1)
dream_team <- rbind(dream_team, new_member)
# ---#
any(x$against_fight %in% 0)
new_member <- x %>%
filter(against_fight == 0) %>%
arrange(Sum) %>%
head(1)
# Honedge --> Aegislash
dream_team <- rbind(dream_team, new_member)
# --- #
any(x$against_dark %in% 0.5)
# ritorna <maehile, che abbiam già in squadra
new_member <- x %>%
filter(against_water == 0.5) %>%
arrange(Sum) %>%
head(2) ## Sarebbe Dialga, però considerare pokemon leggendari nella ricerca non è funzionale al fine di tale, pertanto usaim il secondo che è anche uno starter
dream_team <- rbind(dream_team, new_member[2, ])
final_team <- aux_set %>%
filter(name %in% dream_team$name) %>%
select(name, type1, type2, hp, attack, defense, speed, sp_attack, sp_defense)
head(final_team)
# Usiam le evoluzioni finali
final_team[1,] <-aux_set %>%
select(name, type1, type2, hp, attack, defense, speed, sp_attack, sp_defense) %>%
filter(name == "Azumarill")
final_team[6,] <-aux_set %>%
select(name, type1, type2, hp, attack, defense, speed, sp_attack, sp_defense) %>%
filter(name == "Aegislash")
final_team