소품집

[Kaggle] BlackFriday 데이터를 활용한 EDA (5) 본문

AI

[Kaggle] BlackFriday 데이터를 활용한 EDA (5)

sodayeong 2020. 9. 4. 10:54

setwd('/Users/dayeong/Desktop/reserch/data')

# Dataloading
dt_loan <- read.csv('BlackFriday.csv')

# Packages loading
library(tidyverse)
library(gridExtra)
library(ggplot2)

# 1. 유저별로 구매수에 따라 시각화(1)

# 구매수가 600개 이상인 고객 카운트. 
dt <- dt_loan %>% 
  group_by(User_ID) %>%
  mutate(buy_n = n()) %>%
  filter(buy_n>=600)

# 중복 제거
dt <- dt[!duplicated(dt$User_ID),] 
dt <- droplevels(dt)

# User_ID와 buy_n을 기준으로 정렬
usr_order = dt$User_ID[order(dt$User_ID, dt$buy_n)]
dt$User_ID <- factor(dt$User_ID, levels = user_order)

dt %>% ggplot(aes(y=User_ID, x=buy_n, fill= Age, col=Gender)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_colour_manual(values = c('F'=='pink', 'M'=='grey50')) +
  scale_fill_brewer(palette = 'Spectral') +
  labs(y='Purchase Amoount', x = 'User_ID', 
       title = 'Number of purchases per user(>600)')

# 2. 유저별로 구매수에 따라 시각화(2)
user_order = dt$User_ID[order(dt$Age, dt$Gender, dt$buy_n)]
dt$User_ID <- factor(dt$User_ID, levels = user_order)

dt %>% ggplot(aes(x=buy_n, y=User_ID)) + 
  geom_segment(aes(yend=User_ID, linetype=Gender), xend=0, size=1) +
  geom_point(aes(col=Age, shape=factor(Marital_Status))) +
  labs(title = 'Number of purchase ', x='Purchase amount') +
  scale_shape_manual(values=c(18,16)) +
  theme(plot.title = element_text(size=30, hjust = 0.5))


# 위의 시각화에서 얻은 인사이트를 활용하여 시각화
# (위 그림) Age(26-35)의 유저가 다른 Age에 비해 많은 비율을 보여줌.
# -> 이 Age 고객들을 분석해보자 

Age_25 <- dt %>% 
  filter( Age == "26-35" )

Age_25 <- droplevels(Age_25) # 동일 레벨 제거
Age_25$User_ID <- factor(Age_25$User_ID) # factor형으로 변환
Age_25$Marital_Status <- factor(Age_25$Marital_Status) # factor형으로 변환 

Age_25 <- Age_25 %>%
  group_by(User_ID) %>% 
  mutate( mean_purchase = mean(Purchase))

Age_25 <- Age_25[!duplicated(Age_25$User_ID) , ]

## plot1
output2 <- Age_25 %>% 
  group_by(Marital_Status) %>% 
  mutate(n=n(), ratio = n/sum(n))

output2 <- Age_25 %>% 
  group_by(Marital_Status) %>% 
  summarise(n=n()) %>% 
  mutate(ratio = n/sum(n), 
         location = ifelse(ratio > min(ratio) , min(ratio) + ratio/2 , ratio/2 ) )

plot1 <- output2 %>% 
  ggplot(aes(x=factor(1), y = ratio, fill = Marital_Status)) + 
  geom_bar(stat="identity") + 
  geom_text(aes(x= factor(1), y= location, label = paste("Marital_Status = " ,Marital_Status," and " , round(ratio*100,2),"%",sep="")), size=5) + 
  labs(x="Marital Status" , y = "Ratio" , title = "[26-35 Age] Martial Status 비율") + 
  theme(axis.text.x = element_blank() , axis.title.y=element_blank()) + guides(fill=FALSE, color=FALSE) +
  theme_bw(base_family = 'AppleGothic')


## plot2
plot2 <- Age_25 %>% 
  ggplot(aes(x= mean_purchase , fill = Gender)) + 
  geom_density(alpha = 0.2) + 
  guides(fill = guide_legend(title='Gender')) + 
  labs(x="Mean Purchase" , title = "[26-35 Age] Gender 별 평균 구매가격의 Density") + 
  theme(axis.title.y=element_blank()) +
  theme_bw(base_family = 'AppleGothic')


## plot3
survey <- Age_25 %>% 
  group_by(Occupation ) %>% 
  mutate(Occupation_n = n())

survey2 <- survey %>% 
  ggplot( aes(x = Occupation , y=Occupation_n , fill = Gender)) 


plot3 <- survey2 + 
  geom_bar(stat="identity" , position = "fill") + 
  scale_x_continuous( breaks =sort(unique(survey$Occupation))) +
  labs(x="Occupation" , y ="Ratio of male and female" , 
       title = "[26-35 Age] Gender에 따른 직업의 비율") +
  theme_bw(base_family = 'AppleGothic')

# 시각화 merge
grid.arrange(arrangeGrob(plot1 , plot2, ncol =2 ) , plot3 , nrow =2 )






 

source : data-newbie.tistory.com/46

 

Kaggle BlackFriday 데이터를 활용한 EDA

LeeSungRyeong 관련 파일 : BlackFriday.csv 필요 패키지 : tidyverse gridExtra Tips 그림 크기 방향 조절 하는 방법 ```{r , fig.align=‘center’ , fig.width= 12 , fig.height= 9} library warning message..

data-newbie.tistory.com

위의 자료를 보고 따라하면서 연습해본 포스팅입니다. 

 

728x90

'AI' 카테고리의 다른 글

[Kaggle] 올림픽 데이터를 활용한 EDA  (0) 2020.09.08
[Kaggle] avocado price EDA  (0) 2020.09.08
[Kaggle] 채무 불이행자  (0) 2020.09.01
[Kaggle] 채무 불이행자 searching - ing (4)  (0) 2020.08.31
[Kaggle] interactive visualization  (0) 2020.08.28
Comments