Sitemap

R vs Python???

4 min readJul 2, 2020

--

สวัสดีครับ บทความที่สองของผมในวันนี้เราจะใช้ R เพื่อหาคำตอบกับคำถามที่ว่า ถ้าจะเริ่มทำงานด้าน Data ควรจะเริ่มศึกษาการเขียนโปรแกรมภาษาไหนก่อน

Press enter or click to view image in full size
Image source : https://medium.com/@jacob.w.bedard/r-vs-python-is-it-really-the-great-debate-in-data-science-55a003ef4e44

Dataset ที่นำมาใช้ในการวิเคราะห์จะมาจาก Kaggle Data Science Survey ซึ่งได้รวบรวมความเห็น 10000 คนจากหลายๆ Industry ที่ใช้ Programming language ในการวิเคราะห์ข้อมูลสำหรับงานในแต่ละวัน สามารถดาวน์โหลด CSV file ได้จากลิ้งค์ด้านล่างครับ (ข้อมูลเมื่อปี 2017)

https://drive.google.com/file/d/1dfNEBsZEn_J81Jz9dkQXa_fj49Ecu-3q/view?usp=sharing

Getting Data

เริ่มจากโหลด Package tidyverse (จะใช้ dplyr, ggplot2, readr, tidyr เป็นหลัก)และข้อมูลจากไฟล์ CSV ในการวิเคราะห์ข้อมูล

# Load necessary packages
library(tidyverse)

#Load Dataset
response <- read_csv(“kagglesurvey.csv”)

Press enter or click to view image in full size
แสดงผลลัพธ์หลังจาก Import CSV File มาที่ R

จากคอลัมน์ใน Kaggle Dataset

WorkToolsSelect <- บอกเกี่ยวกับโปรแกรมที่ผู้แสดงความเห็นใช้งานในแต่ละวัน

LanguageRecommendationSelect <- ภาษาที่ผู้แสดงความเห็นแนะนำให้เรียน

EmployerIndustry <- บอกประเภทธุรกิจที่ผู้แสดงความเห็นทำงานอยู่

WorkAlgorithmsSelect <- บอกอัลกอริทึมที่ใช้กับงานของผู้แสดงความเห็น

เนื่องจากมี NA values อยู่ใน Dataset เราจึงต้องกำจัด NA values ออกก่อนที่จะเริ่มทำการวิเคราะห์ข้อมูล โดยใช้ฟังก์ชัน drop_na()

#Clean Dataset and Check
clean_response <- drop_na(response)
head(clean_response,10)
summary(clean_response)
glimpse(clean_response)

Press enter or click to view image in full size
หลังจากจัดการกับ NA values Dataset จะออกมาประมาณนี้

ทีนี้เราก็จะเข้าสู่กระบวนการ Explore Dataset ต่อนะครับ : D

  1. งาน Data Science ใช้ Programming language ไหนเยอะที่สุด

เราจะใช้ฟังก์ชัน str_split() เพื่อที่จะตัด “,” ในคอลัมน์ WorkToolsSelect และใช้ unnest() เพื่อแยก row แต่ละ tool ของงานนั้นๆ

#Split the tools
tools <- clean_response %>%
mutate(work_tools = str_split(WorkToolsSelect, “,”)) %>%
unnest(work_tools)

head(tools)

Press enter or click to view image in full size
ผลลัพธ์ที่ได้จากการ Run code

จากนั้นเราจะจัดกลุ่ม tool ที่ใช้ และหาว่ามีผู้ใช้ tool ทั้งหมดกี่คน โดยใช้ group_by(), summarise() และ arrange () และ plot bar chart โดยใช้ ggplot() เพื่อ Visualize ผลที่ได้ให้เข้าใจโดยง่าย

#Counting and ploting
tool_count <- tools %>% group_by(work_tools) %>%
summarise(total = n()) %>% arrange(desc(total))

head(tool_count)

ggplot(tool_count, aes(x = fct_reorder(work_tools, total), total)) +
geom_bar(stat = “identity”) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
labs(x = “Tools”, y = “Count”)

tool_count แสดงจำนวนผู้ใช้ Programming language ที่ผู้แสดงความเห็นใช้บ่อยมากที่สุด
Press enter or click to view image in full size
จะเห็นได้ว่า Python, R และ SQL คือ 3 ภาษาหลักที่ Data Science ต้องใช้เป็น

2. R vs Python

Get Piyachart Chaipimol’s stories in your inbox

Join Medium for free to get updates from this writer.

จากผลลัพธ์ที่ได้จะเห็นว่า Python ถูกนำมาใช้งานมากเป็นอันดับ 1 รองลงมาคือ R ทีนี้เราจะมาวิเคราะห์กันว่าในการทำงานจะใช้แค่ Python, R อย่างใดอย่างหนึ่ง หรือเราต้องใช้มันทั้งสองภาษา โดยเราจะใช้ฟังก์ชัน case_when() เพื่อแบ่งกรณีที่เราใช้ R, Python, หรือใช้ทั้งคู่ (คล้ายๆ case when then else end ของ sql) และใช้คู่กับ str_detect()

#R and Python use
debate_tools <- clean_response %>% mutate(language_preference = case_when(
str_detect(WorkToolsSelect, “R”) & !str_detect(WorkToolsSelect, “Python”) ~ “R”,
!str_detect(WorkToolsSelect, “R”) & str_detect(WorkToolsSelect, “Python”) ~ “Python”,
str_detect(WorkToolsSelect, “R”) & str_detect(WorkToolsSelect, “Python”) ~ “both”,
TRUE ~ “neither”
))

head(debate_tools)

Press enter or click to view image in full size
ผลลัพธ์ที่ออกมาหลังจาก run code

จากนั้นเราจะหาจำนวนผู้ใช้ R, Python, ใช้ทั้ง 2 ภาษา และ plot bar graph ออกมา

#R vs Python plot
R_Python <- debate_tools %>% group_by(language_preference) %>%
filter(language_preference != “neither”) %>% summarise(Count = n())

head(R_Python)

ggplot(R_Python, aes(language_preference, Count)) + geom_bar(stat = “identity”)

จะเห็นได้ว่างานด้าน Data Science ส่วนมากควรรู้ทั้ง Python และ R

3. ภาษาที่แนะนำให้เขียนเป็นอันดับแรก?

เราจะแนะนำ Python หรือ R สำหรับผู้ที่เริ่มต้น?

#language recommendations
lang_recoomend <- debate_tools %>% group_by(language_preference, LanguageRecommendationSelect) %>%
summarise(Count = n()) %>% arrange(language_preference, desc(Count)) %>%
mutate(row = row_number()) %>% filter(row <= 4)

head(lang_recoomend)

#plot
ggplot(lang_recoomend, aes(LanguageRecommendationSelect, Count)) +
geom_bar(stat = “identity”) + facet_wrap(~ language_preference)

Press enter or click to view image in full size
Python คือภาษาที่คนส่วนใหญ่แนะนำสำหรับมือใหม่

เราใช้ facet_wrap() เพื่อแบ่งกราฟวิเคราะห์ตามกลุ่มของผู้ใช้ Python , R, both และ ภาษาใดภาษาหนึ่ง

จะเห็นได้ว่า Python คือภาษาที่คน 3 กลุ่มหลักแนะนำให้ศึกษาก่อนมากที่สุด จะมีแค่ R ที่มีแค่คนที่ใช้ R เป็นหลักแนะนำให้ศึกษาก่อนเป็นภาษาแรก

4. เราสามารถสรุปอะไรได้บ้างจาก Dataset นี้

ถ้าจะเปรียบเทียบว่า R กับ python ควรจะเลือกศึกษาภาษาไหนดี

1) python มี syntax ที่เข้าใจง่าย เหมาะสำหรับคนที่มีพื้นฐาน เป็นโปรแกรมเมอร์ หรือ software engineer จะเรียนเป็นเร็ว

ส่วน R ตัวโครงสร้างภาษา จะเหมาะกับคนที่มีพื้นมาจาก stat

2) python เหมาะกับงาน ที่ต้อง deploy models, เอาไปทำเป็นแอพ หรือใช้โมเดลผ่านช่องทางอื่นๆ เนื่องจาก python จะเชื่อมต่อกับโค๊ดของภาษาอื่นๆได้ง่ายกว่า

R จะเหมาะกับงานทำ data analysis ในภาคการเงินการธนาคาร, การตลาด, การค้าปลีก

3) ถ้า โฟกัส ในด้าน การวิเคราะห์ทางสถิติ และการทำ data visualization, R จะมีลูกเล่นเยอะกว่า และทำได้ดีกว่า python

4) แต่ถ้าโฟกัส เฉพาะ machine learning ตัว scikit-learn ของ python จะเหนือกว่า library ด้าน machine learning ของ R เช่น caret

5. เพิ่มเติม : งานด้าน Data Science ควรศึกษาโมเดลแบบไหนเป็นพิเศษ

พิจารณาคอลัมน์ WorkAlgorithmsSelect ซึ่งบอกเกี่ยวกับโมเดลที่ใช้ในการทำงานในแต่ละวัน เราอยากรู้ว่าถ้าจะทำงาน Data Science ควรจะต้องรู้โมเดลอะไรบ้าง เราจึงต้องหาว่าใน Dataset ผู้ที่ทำงานใช้โมเดลไหนเป็นพิเศษ

#Model
skill <- clean_response %>% mutate(work_skill = str_split(WorkAlgorithmsSelect, “,”)) %>%
unnest(work_skill)

head(skill)

#Best DS Model
skill_count <- skill %>% group_by(work_skill) %>%
summarise(total = n()) %>% arrange(desc(total))
head(skill_count)

#Plotting
ggplot(skill_count, aes(x = fct_reorder(work_skill, total), total)) +
geom_bar(stat = “identity”) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
labs(x = “Skill”, y = “Count”)

โมเดลถดถอย ถูกนำมาใช้มากที่สุด รองลงมาคือ Decision Trees
Press enter or click to view image in full size

และถ้าจะหาว่าในแต่ละ Industry จะเลือกใช้โมเดลไหนวิเคราะห์ข้อมูลเราสามารถหาได้ดังนี้

#Model by Industry
Indus_skill <- skill %>% group_by(EmployerIndustry, work_skill) %>%
summarise(Count = n()) %>% arrange(EmployerIndustry, desc(Count)) %>%
filter(work_skill %in% c(“Regression/Logistic Regression”, “Decision Trees”,”Random Forests”, “Neural Networks”, “Bayesian Techniques”))

head(Indus_skill)

#plot
ggplot(Indus_skill, aes(work_skill, Count)) +
geom_bar(stat = “identity”) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
facet_wrap(~ EmployerIndustry) +
labs(x = “Tecnicque”, y = “Count”)

Press enter or click to view image in full size

จากกราฟจะเห็นได้ว่า งานด้าน Academic, Financial, Technology มีการใช้โมเดลในการวิเคราะห์ข้อมูลมากที่สุด และ Regression model, Decision Trees คือโมเดลหลักที่จำเป็นต้องรู้หากจะทำงานด้าน Data Science เพราะครอบคลุมในหลายๆ Industry

Summary

Dataset นี้ เราใช้ฟังก์ชัน dplyr และ ggplot2 เป็นหลักซึ่งสามารถศึกษาได้ง่ายและใช้เวลาไม่นาน แต่สามารถนำมา Explore ข้อมูลจาก Dataset ต่างๆ ที่เก็บมาจากแหล่งต่างๆได้โดยง่าย แล้วพบกันใหม่นะค้าบบบบ :D

--

--