在此課程中將帶領對資料分析感到陌生卻又充滿興趣的您,完整地學會運用 R 語言從最初的蒐集資料、探索性分析解讀資料,並進行文字探勘,發現那些肉眼看不見、隱藏在資料底下的意義。此課程主要設計給對於 R 語言有基本認識,想要進一步熟悉實作分析的朋友們,希望在課程結束後,您能夠更熟悉 R 語言這個豐富的分析工具。透過蘋果日報慈善捐款的資料集,了解如何從頭解析網頁,撰寫爬蟲自動化收集資訊;取得資料後,能夠靈活處理資料,做清洗、整合及探索;並利用現成的套件進行文字探勘、文本解析;我們將一步步實際走一回資料分析的歷程,處理、觀察、解構資料,試著看看人們在捐款的決策過程中,究竟是什麼因素產生了影響,以及這些結果又是如何從資料中挖掘而出的呢?
在此課程中將帶領對資料分析感到陌生卻又充滿興趣的您,完整地學會運用 R 語言從最初的蒐集資料、探索性分析解讀資料,並進行文字探勘,發現那些肉眼看不見、隱藏在資料底下的意義。此課程主要設計給對於 R 語言有基本認識,想要進一步熟悉實作分析的朋友們,希望在課程結束後,您能夠更熟悉 R 語言這個豐富的分析工具。透過蘋果日報慈善捐款的資料集,了解如何從頭解析網頁,撰寫爬蟲自動化收集資訊;取得資料後,能夠靈活處理資料,做清洗、整合及探索;並利用現成的套件進行文字探勘、文本解析;我們將一步步實際走一回資料分析的歷程,處理、觀察、解構資料,試著看看人們在捐款的決策過程中,究竟是什麼因素產生了影響,以及這些結果又是如何從資料中挖掘而出的呢?
Slide show for the webinar on "Spatial Data Science with R" organized for the GeoDevelopers.org community. The video of the webinar and all the related materials including source code and sample data can be downloaded from this link: http://amsantac.co/blog/en/2016/08/07/spatial-data-science-r.html
In this webinar I talked about Data Science in the context of its application to spatial data and explained how we can use the R language for the analysis of geographic information within the different stages of a data science workflow, from the import and processing of spatial data to visualization and publication of results.
17. 讀取網頁資訊 (pkg xml2)
library(xml2)
# set your target url
doc <- read_html(url)
# set the xpath of info needed
xpath <- “//*[@id=‘inquiry3’]/table//tr[4]/td[1]”
xml_text(xml_find_all(doc, xpath))
資料收集 - 講解 A-01
17
47. Summary Functions in R
Function Description 白話文
names() Functions to get or set the names of an object 看欄位名稱
head(), tail()
Returns the first or last parts of a vector, matrix, table,
data frame or function 看 前/(後) 幾筆資料
str() Compactly display the internal structure of an R object 物件屬性
summary() Produce result summaries 物件的基本數值狀態
dim() Retrieve or set the dimension of an object 矩陣大小
length() Get or set the length of vectors 向量長度
complete.cases()
Return a logical vector indicating which cases are
complete, i.e., have no missing values 回傳各元素 NA 邏輯值
as.Date()
Convert between character representations and
objects of class "Date" representing calendar dates 轉成日期型態
Function name and parameter 的縮寫解釋:
http://jeromyanglim.blogspot.tw/2010/05/abbreviations-of-r-commands-explained.html
48
48. Visualization Functions in R
Function Description 白話文
plot() Generic function for plotting of R objects 畫圖 (散布圖 or R object)
boxplot()
Produce box-and-whisker plot(s) of the given
(grouped) values 盒鬚圖
hist() Computes a histogram of the given data values 分布圖
barplot()
Creates a bar plot with vertical or horizontal
bars 長條圖
arrows() Draw arrows between pairs of points 加箭頭 (x0, y0, x1, y1)
abline()
a, b: the intercept and slope, single values.
y = [A] + [B]x 加一條截距為a, 斜率為b的直線
lines()
Join the corresponding points with line
segments. 折線圖
Function name and parameter 的縮寫解釋:
http://jeromyanglim.blogspot.tw/2010/05/abbreviations-of-r-commands-explained.html
49
49. session_B_eda.R
讀入資料與看一看變數
# load in apple daily article
> d <- read.csv(“df_article.csv”, fileEncoding =
“utf-8”)
# use dim() to know data frame dimension
> dim(d)
[1] 3779 12
# check the column names
> names(d)
[1] "aid" "case.closed" "date.funded" "date.published"
[5] "donation" "donor" "journalist" "n.image" "n.word"
[10] "title" "url.article" "url.detail"
EDA - 講解 B-02
50
62. # use plot to check relationship between numbers of donors
and total donation (and draw a linear line)
> plot(d$donor, d$donation, pch = ‘.’, cex = 2)
> y <- lm(donation ~ donor, data = d)
> abline(y, col = ‘red’, lwd = 1.5)
plot()
EDA - 講解 B-02
63
109. Skip-gram 模型
藉由 current word 推測 context words
Neural network model
Stochastic gradient descend (SGD)
112
|V|:詞庫內所有的字詞數量
d :字詞向量化的維度
1 x |V|
0.2
:
:
0.5
:
0.1
0
0
:
1
:
:
0
0
0.1
:
:
0.6
:
0.1
0
:
:
:
:
|V| x d
W
C
d x |V|
C
d x |V|
1 x d
wt
最熱的
Wt-1
夏天
Wt+1
季節
0
:
:
1
:
0
0
0
:
:
1
:
0
0
-
-
112
110. Continuous Bag of Words 模型
由 context words 推測 current word
113
0.2
0.1
0
:
0.5
:
0
1 x |V|
d x |V|
W
C|V| x d
C|V| x d
:
:
:
:
:
1 x d
0
1
:
0
:
0
0
0
0
:
1
:
0
0
wt
最熱的
Wt-1
夏天
Wt+1
季節
0
0
0
:
1
:
0
-
113
111. 詞向量
訓練結束後,將1 x |V| 字詞轉換成 d 維向量的
矩陣
|V| x d
W
|V| x d
1
2
:
i
:
:
|V|
1 2 … … … (d-2) (d-1) d
第 i 個詞的詞向量
114
112. 淺談 text2vec
Global corpus statistics + local window
Count-based method
Term co-occurrence matrix, X
Xij: 詞-i 和詞-j 共同出現次數
最熱的夏天 季節
Word,wt
Context words
Wt-1 Wt+1
1
2
⁞
i
⁞
|V|
1 … j … k … |V|
最熱的
夏
天
季
節
+1
Term Co-occurrence Matrix
+1
115