-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoGPT_ds.qmd
341 lines (252 loc) · 8.29 KB
/
autoGPT_ds.qmd
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
---
title: "chatGPT"
subtitle: "데이터 과학"
description: |
데이터 사이언스를 챗GPT로 풀어보자.
author:
- name: 이광춘
url: https://www.linkedin.com/in/kwangchunlee/
affiliation: 한국 R 사용자회
affiliation-url: https://github.com/bit2r
title-block-banner: true
#title-block-banner: "#562457"
format:
html:
css: css/quarto.css
theme: flatly
code-fold: true
code-overflow: wrap
toc: true
toc-depth: 3
toc-title: 목차
number-sections: true
highlight-style: github
self-contained: false
filters:
- lightbox
lightbox: auto
link-citations: true
knitr:
opts_chunk:
message: false
warning: false
collapse: false
editor_options:
chunk_output_type: console
editor:
markdown:
wrap: 72
---
# autoEDA
## `Hmisc::describe()`
[`Hmisc`](https://cran.r-project.org/web/packages/Hmisc/index.html) 패키지를 통해 과거 20년전 데이터 분석방법을 음미합니다.
```{r}
library(tidyverse)
penguins <- palmerpenguins::penguins %>%
# 영어 변수명 한글 변환
set_names(c("종명칭", "섬이름", "부리_길이", "부리_깊이", "물갈퀴_길이",
"체중", "성별", "연도")) %>%
# 결측값 제거
# drop_na() %>%
# 영어 값 한글 값으로 변환
mutate(성별 = ifelse(성별 == "male", "수컷", "암컷"),
섬이름 = case_when( str_detect(섬이름, "Biscoe") ~ "비스코",
str_detect(섬이름, "Dream") ~ "드림",
str_detect(섬이름, "Torgersen") ~ "토르거센"),
종명칭 = case_when( str_detect(종명칭, "Adelie") ~ "아델리",
str_detect(종명칭, "Chinstrap") ~ "턱끈",
str_detect(종명칭, "Gentoo") ~ "젠투")
) %>%
# 자료형 변환
mutate(성별 = factor(성별, levels = c("수컷", "암컷")),
섬이름 = factor(섬이름, levels = c("비스코", "드림", "토르거센")),
종명칭 = factor(종명칭, levels = c("아델리", "턱끈", "젠투")),
연도 = ordered(연도, levels = c(2007, 2008, 2009)))
Hmisc::describe(penguins)
```
## `skimr`
`skimr` 패키지를 사용하여 분석할 데이터와 친숙해진다.
```{r}
penguins %>%
skimr::skim()
```
## `dataxray`
[`dataxray`](https://github.com/agstn/dataxray) 패키지를 사용해서 데이터에 대한 이해를 더욱 높일 수 있다.
:::{.column-page}
```{r}
library(dataxray)
penguins %>%
make_xray() %>%
view_xray()
```
:::
## `dlookr`
[`dlookr`](https://cran.r-project.org/web/packages/dlookr/index.html) 패키지를 사용하여 분석할 데이터와 친숙해진다.
```{r}
library(kableExtra)
penguins %>%
dlookr::describe() %>%
kable(caption = "요약통계량") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = F)
```
## `DataExplorer `
[`DataExplorer `](https://cran.r-project.org/web/packages/DataExplorer/) 패키지를 사용하여 분석할 데이터와 친숙해진다.
```r
DataExplorer::create_report(penguins)
```
:::{.panel-tabset}
### 구조
```{r}
DataExplorer::plot_str(penguins)
```
### DF 요약
```{r}
DataExplorer::introduce(penguins)
```
### DF 요약 시각화
```{r}
DataExplorer::plot_intro(penguins)
```
### 결측값
```{r}
DataExplorer::plot_missing(penguins)
```
### 분포(범주형)
```{r}
DataExplorer::plot_bar(penguins)
```
### 분포(연속형)
```{r}
DataExplorer::plot_histogram(penguins)
```
### 상관관계
```{r}
penguins %>% select_if(is.numeric) %>%
# drop_na() %>%
DataExplorer::plot_correlation(cor_args = list("use" = "pairwise.complete.obs"))
```
### PCA
```{r}
penguins_pca <- penguins %>% select_if(is.numeric) %>%
drop_na() %>% prcomp(scale = TRUE)
summary(penguins_pca)$importance %>% as.data.frame() %>%
kable(caption = "PCA 요약") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = F)
```
:::
# 챗GPT EDA
[[Model: GPT-4, autoEDA and autoML](https://sharegpt.com/c/1lHtBnV)]{.aside}
챗GPT GPT-4 모형을 사용해서 생산성을 크게 향상시킬 수 있다. [@chatGPT2023ds]
먼저 역할을 지정하고 데이터셋이 크지 않은 경우 이를 프롬프트에 넣어 GPT-4모형이 작업할 수 있는 환경을 조성한다.
:::{.panel-tabset}
## 역할부여와 데이터 {.unnumbered}
:::{.callout-note collapse="false"}
### I want you to act as a data scientist using R programming language and code for me. I have a dataset of penguins as follows. If you understand what I instructed. print "OK" and wait for another instruction.
> species,island,culmen_length_mm,culmen_depth_mm,flipper_length_mm,body_mass_g,sex
> Adelie,Torgersen,39.1,18.7,181,3750,MALE
> Adelie,Torgersen,39.5,17.4,186,3800,FEMALE
> Adelie,Torgersen,40.3,18,195,3250,FEMALE
> Adelie,Torgersen,NA,NA,NA,NA,NA
> Adelie,Torgersen,36.7,19.3,193,3450,FEMALE
> Adelie,Torgersen,39.3,20.6,190,3650,MALE
> Adelie,Torgersen,38.9,17.8,181,3625,FEMALE
> Adelie,Torgersen,39.2,19.6,195,4675,MALE <br>
> ...
GPT-4 답변: **OK**
:::
## EDA 작업지시 {.unnumbered}
본격적으로 EDA 작업을 챗GPT에 부여하고 작업을 지시한다.
:::{.callout-note collapse="false"}
### Please write code for the exploratory data analysis (EDA) task and target variable is sex.
Here's an example of an R script that performs exploratory data analysis (EDA) on a dataset, with the target variable being 'sex'. The code uses the same dataset ('penguins.csv') as the previous answer.
```
# Load libraries
library(tidyverse)
library(tidymodels)
library(ggplot2)
library(readr)
# Import dataset
data <- read_csv("penguins.csv")
# Data preprocessing and cleaning
data_clean <- data %>%
drop_na() %>%
mutate(sex = as.factor(sex))
# Examine the structure of the dataset
print(str(data_clean))
# Summary statistics
print(summary(data_clean))
# Target variable distribution
print(table(data_clean$sex))
# Visualize target variable distribution
ggplot(data_clean, aes(x = sex)) +
geom_bar() +
ggtitle("Sex Distribution")
# Pairwise scatterplot of continuous variables
pairs_plot <- data_clean %>%
select_if(is.numeric) %>%
ggpairs()
print(pairs_plot)
# Boxplots for continuous variables by sex
data_clean %>%
gather("variable", "value", -sex) %>%
ggplot(aes(x = sex, y = value)) +
geom_boxplot() +
facet_wrap(~ variable, scales = "free") +
ggtitle("Boxplots of Continuous Variables by Sex")
# Visualize categorical variables by sex
data_clean %>%
ggplot(aes(x = island, fill = sex)) +
geom_bar(position = "dodge") +
ggtitle("Island Distribution by Sex")
# Correlation matrix
cor_matrix <- cor(data_clean %>% select_if(is.numeric))
print(cor_matrix)
```
This script includes several EDA techniques such as examining the dataset's structure, summarizing the dataset, visualizing the target variable distribution, creating pairwise scatterplots, boxplots, and bar charts, and calculating the correlation matrix for the continuous variables.
Make sure to replace the filename and adapt the code to your specific needs.
:::
## EDA 수행 {.unnumbered}
```{r}
# Load libraries
library(tidyverse)
library(palmerpenguins)
library(GGally)
# Import dataset
penguins <- palmerpenguins::penguins
# Data preprocessing and cleaning
penguins <- penguins %>%
drop_na() %>%
mutate(sex = as.factor(sex))
# Examine the structure of the dataset
print(str(penguins))
# Summary statistics
print(summary(penguins))
# Target variable distribution
print(table(penguins$sex))
# Visualize target variable distribution
ggplot(penguins, aes(x = sex)) +
geom_bar() +
ggtitle("Sex Distribution")
# Pairwise scatterplot of continuous variables
pairs_plot <- penguins %>%
select_if(is.numeric) %>%
ggpairs()
print(pairs_plot)
# Boxplots for continuous variables by sex
penguins %>%
select(-species, -island) %>%
pivot_longer(cols = -sex, names_to = "variable", values_to = "value") %>%
ggplot(aes(x = sex, y = value)) +
geom_boxplot() +
facet_wrap(~ variable, scales = "free") +
ggtitle("Boxplots of Continuous Variables by Sex")
# Visualize categorical variables by sex
penguins %>%
ggplot(aes(x = island, fill = sex)) +
geom_bar(position = "dodge") +
ggtitle("Island Distribution by Sex")
# Correlation matrix
cor_matrix <- cor(penguins %>% select_if(is.numeric))
print(cor_matrix)
```
:::