-
Notifications
You must be signed in to change notification settings - Fork 1
/
Machine_Learning_1.Rmd
243 lines (172 loc) · 9.57 KB
/
Machine_Learning_1.Rmd
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
---
title: "Machine_Learning_1"
author: "Equipo ML1"
date: "2/24/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r librerias_R,warning=FALSE,echo=FALSE}
library(dplyr)
library(tidyr)
library(ggplot2)
library(forcats)
library(caret)
library(fastcluster)
library(cluster)
#library(PythonInR)
library(VIM)
library(reticulate)
```
```{python librerias_Python, warning=FALSE,echo=FALSE}
import pandas as pd
```
```{r librerias_Python, warning=FALSE,echo=FALSE}
# Información de las columnas
Info_cols<-read.csv('Info_Columns_Philipines.csv',stringsAsFactors = TRUE)
```
```{python librerias_Python, warning=FALSE,echo=FALSE}
# Este es un chunk de python que debería funcionar si el paquete reticulate se ha instalado correctamente
# Carga de los datos de Training en python
example =pd.read_csv("Training_Philipines.csv")
example.head(5)
```
```{r librerias_R,warning=FALSE,echo=FALSE}
# Carga de los datos de training en R
datos<-read.csv('Training_Philipines.csv',stringsAsFactors = TRUE)
head(datos)
```
# Pasos previos a la implementacion de modelos de ML
## Limpieza de datos
En primer lugar, se decide renombrar y reagrupar los niveles de las variables categóricas, creando nuevas variables "limpias" en el data set. Seguidamente, se procede a eliminar las antiguas versiones.
```{r}
datos <- datos %>% mutate(Roof =
recode(Type.of.Roof,
'Strong material(galvanized,iron,al,tile,concrete,brick,stone,asbestos)' = 'Strong',
'Light material (cogon,nipa,anahaw)' = 'Light',
'Mixed but predominantly strong materials' = 'Strong',
'Mixed but predominantly light materials' = 'Light',
'Salvaged/makeshift materials' = 'Salvaged',
'Mixed but predominantly salvaged materials' = 'Salvaged'))
summary(datos$Roof)
```
```{r}
datos <- datos %>% mutate(TenueStatus =
recode(Tenure.Status,
'Own or owner-like possession of house and lot' = 'Fully_Paid',
'Own house, rent-free lot with consent of owner' = 'Partial_Paid',
'Rent house/room including lot' = 'Fully_Paid',
'Rent-free house and lot without consent of owner' = 'Free',
'Rent-free house and lot with consent of owner' = 'Free',
'Own house, rent lot' = 'Fully_Paid',
'Own house, rent-free lot without consent of owner' = 'Free'))
summary(datos$TenueStatus)
```
```{r}
datos <- datos %>% mutate(TypeHousehold =
recode(Type.of.Household,
'Two or More Nonrelated Persons/Members' = 'Nonrelated'))
summary(datos$TypeHousehold)
```
```{r}
datos <- datos %>% mutate(Worker =
recode(Household.Head.Class.of.Worker,
'Self-employed wihout any employee' = 'Freelance',
'Worked for private establishment' = 'Employee',
'Employer in own family-operated farm or business' = 'Family_Business',
'Worked for government/government corporation'= 'Civil_Servant',
'Worked for private household' = 'Employee',
'Worked without pay in own family-operated farm or business' = 'Family_Business_Unpaid',
'Worked with pay in own family-operated farm or business'='Family_Business'))
summary(datos$Worker)
```
```{r}
datos <- datos %>% mutate(TypeBuilding =
recode(Type.of.Building.House,
'Commercial/industrial/agricultural building' = 'Agricultural'))
summary(datos$TypeBuilding)
```
```{r}
# La variable type of walls y type of roof tiene contenidos muy similares. He decidido utilizar las mismas clasificaciones (strong light salvaged)
# ya que nos estamos refiriendo a materiales
datos <- datos %>% mutate(Walls =
recode(Type.of.Walls,
'Quite Strong' = 'Strong',
'Very Light'= 'Light'))
summary(datos$Walls)
```
```{r}
# Aquí para el level 'none' he indicado como 'poor', asumiendo que 'none' significa que no tiene baño.
# Sin embargo, podría representarse como un 'no sabe, no contesta'
# Podríamos indagar si tenemos tiempo
# Además, hay un nivel 'others' que no se como tratar
datos <- datos %>% mutate(Toilet =
recode(Toilet.Facilities,
'Water-sealed, sewer septic tank, used exclusively by household' = 'Good',
'Open pit' = 'Poor',
'Water-sealed, other depository, used exclusively by household'= 'Good',
'None' = 'Poor',
'Closed pit' = 'Poor',
'Water-sealed, sewer septic tank, shared with other household' = 'Good',
'Water-sealed, other depository, shared with other household' = 'Good'))
summary(datos$Toilet)
```
```{r}
# Vuelve a aparecer un nivel de 'others'
datos <- datos %>% mutate(Water =
recode(Main.Source.of.Water.Supply,
'Own use, faucet, community water system' = 'Good conditions',
'Shared, faucet, community water system'= 'Good conditions',
'Tubed/piped shallow well' = 'Regular conditions',
'Protected spring, river, stream, etc'= 'Good conditions',
'Lake, river, rain and others' = 'Bad conditions',
'Unprotected spring, river, stream, etc'= 'Bad conditions',
'Peddler' = 'Regular conditions',
'Unprotected spring, river, stream, etc' = 'Bad conditions',
'Dug well'= 'Bad conditions',
'Own use, tubed/piped deep well' = 'Good conditions',
'Shared, tubed/piped deep well'= 'Good conditions'))
summary(datos$Water)
```
```{r}
# Eliminamos las variables antiguas sin limpiar de nuestro data set
datos<-datos%>%select(-Toilet.Facilities,-Type.of.Walls,-Type.of.Building.House,-Household.Head.Class.of.Worker, -Type.of.Household, -Tenure.Status, -Type.of.Roof, -Main.Source.of.Water.Supply)
```
```{r}
# Categorización de variables number.of con posibles valores None, Unique y Many
datos$Bedrooms <-as.factor(datos$Bedrooms)
datos$Bedrooms <-fct_recode(datos$Bedrooms,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$TV <-as.factor(datos$TV)
datos$TV <-fct_recode(datos$TV,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$CD.VCD.DVD <-as.factor(datos$CD.VCD.DVD)
datos$CD.VCD.DVD <-fct_recode(datos$CD.VCD.DVD,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$ComponentStereoSet <-as.factor(datos$ComponentStereoSet)
datos$ComponentStereoSet <-fct_recode(datos$ComponentStereoSet,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$RefrigeratorFreezer <-as.factor(datos$RefrigeratorFreezer)
datos$RefrigeratorFreezer <-fct_recode(datos$RefrigeratorFreezer,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$WashingMachine <-as.factor(datos$WashingMachine)
datos$WashingMachine <-fct_recode(datos$WashingMachine,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$Airconditioner <-as.factor(datos$Airconditioner)
datos$Airconditioner <-fct_recode(datos$Airconditioner,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$Motorvehicle <-as.factor(datos$Motorvehicle)
datos$Motorvehicle <-fct_recode(datos$Motorvehicle,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$LandlineWirelessTelephones <-as.factor(datos$LandlineWirelessTelephones)
datos$LandlineWirelessTelephones <-fct_recode(datos$LandlineWirelessTelephones,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$CellularPhone <-as.factor(datos$CellularPhone)
datos$CellularPhone <-fct_recode(datos$CellularPhone,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$PersonalComputers <-as.factor(datos$PersonalComputers)
datos$PersonalComputers <-fct_recode(datos$PersonalComputers,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$StoveWithOvenGasRange <-as.factor(datos$StoveWithOvenGasRange)
datos$StoveWithOvenGasRange <-fct_recode(datos$StoveWithOvenGasRange,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$MotorizedBanca <-as.factor(datos$MotorizedBanca)
datos$MotorizedBanca <-fct_recode(datos$MotorizedBanca,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
datos$MotorcycleTricycle <-as.factor(datos$MotorcycleTricycle)
datos$MotorcycleTricycle <-fct_recode(datos$MotorcycleTricycle,None='0',Unique='1',Many='2',Many='3',Many='4',Many='5',Many='6',Many='7',Many='8',Many='9')
```
```{r}
# Conversion de variable de gasto de tabaco a categórica, con valores Smoker y NoSmoker
datos$Tobacco <- replace(datos$Tobacco,datos$Tobacco>"0",9999)
datos$Tobacco <-as.factor(datos$Tobacco)
datos$Tobacco <-fct_recode(datos$Tobacco,NoSmoker='0',Smoker='9999')
```