資料科學:入門/像資料工程師一樣思考

首先,請您在華夏公益教科書註冊並把自己列在下方,以便我們瞭解共同貢獻者。另外,請遵守華夏公益教科書的編輯指南、風格指南和政策與指南。感謝您的配合!
其次,我們只需要每個章節中簡潔、清晰、直接的資訊。我們並不追求面面俱到或完整——本書的價值在於跨學科的簡單整合。對於某個特定主題的深入和複雜性,還有其他地方可以進行更詳細的闡述。在進行貢獻時,請保持“初學者的心態”。另外,請確保每個章節的內容可以在一個小時的課堂時間內講完。如果章節需要超過一個小時才能講完,可能就過於詳細了。
- 在儘可能的情況下,請使用維基百科和維基詞典中定義的術語和概念。這樣,學生可以參考相應的維基百科/維基詞典頁面,更深入地理解這些概念。
第三,這是一本跨學科的書籍。我們希望幫助人們將資料科學應用於所有領域。因此,我們需要各種簡單易懂的示例和練習。
第四,請遵守每個章節的簡單結構:要點概括、討論、擴充套件閱讀、練習和參考文獻。我們希望“擴充套件閱讀”部分連結到線上資源。參考文獻部分可能包含離線資源。要建立新頁面,您應該使用此原型頁面中的維基標記。
第五,如同任何華夏公益教科書,請隨時進行必要的更正、擴充套件解釋和補充,即使它不是“你的”章節。請使用討論頁面解釋可能引起爭議的更改。
第六,一些語法規則
- 請將學生應該學習的關鍵術語和短語用粗體標出。
- 使用 'code' 標籤顯示函式名和程式碼片段:
<code>lm()</code> - 使用行內連結
[[ ]]連線到維基百科、維基詞典、維基共享資源、華夏公益教科書和其他維基媒體基金會屬性。 - 使用參考文獻 (<ref> </ref>) 連線到“外部”資源——包括線上和離線資源。
- 使用引用模板建立引用:模板:Cite book、模板:Cite web、模板:Cite journal
- 如果您想新增圖片或圖表,應該將其載入到維基共享資源中,而不是上傳到華夏公益教科書中。
- 如果合適,在上傳圖表時新增標籤
{{Created with R}})。
- 如果合適,在上傳圖表時新增標籤
- 如果使用的是R標準軟體包以外的軟體包,請在每個函式後用粗體括號括起軟體包名稱:<code>MCMCprobit()</code> ('''MCMCpack''')
- 您可以使用第三章資料定義作為如何構建章節的示例。
最後,非常感謝您自願加入我們的團隊!
當資料科學家以資料工程師的思維方式思考時,他們會以表格的形式進行思考。他們的任務是定義表格的行、列和單元格;將表格相互關聯;以及建立用於攝取、儲存和檢索表格的系統。
(在接下來的討論中,我們需要更多關於表格思維(行和列)以及多個表格如何相互關聯(模式)的內容。也許可以簡單地介紹一下正規化和索引。還可以談談不同的資料管理方案,如平面 CSV 檔案、關係型資料庫管理系統、NoSQL 等。)
資料工程是資料科學中與資料相關的部分。根據維基百科,資料工程包括獲取、攝取、轉換、儲存和檢索資料。資料工程與資料收集、資訊工程、知識工程、資訊管理和知識管理密切相關。
資料工程始於對要解決的問題的總體性質的理解。必須制定一個資料獲取和管理計劃,其中指定資料來源(RSS 源、感測器網路、現有資料儲存庫)、傳入資料的格式(文字、數字、影像、影片)以及資料將如何儲存和檢索(檔案系統、資料庫管理系統)。原始資料是“髒”的。原始資料中會存在不符合已商定的資料定義的記錄。例如,在一家醫院的資料集中,一些 7 到 11 歲的男孩生下了嬰兒。[1] 顯然,這些資料中存在錯誤。資料獲取和管理計劃的一部分是要決定如何處理髒資料(保留、刪除、推斷更正)。
大多數情況下,原始資料的格式與分析工具所需的格式不同。實際上,每個工具都希望以自己的特定方式檢視資料。因此,資料工程的一項任務是轉換資料,使其可以被資料科學團隊將要使用的分析工具使用。例如,一個團隊可能會收到以下格式的產蛋資料,每個觀察結果都在單獨的行中:
| 母雞 | 日期 | 雞蛋數量 |
|---|---|---|
| A | 1 | 3 |
| A | 2 | 4 |
| A | 3 | 2 |
| B | 1 | 1 |
| B | 2 | 0 |
| B | 3 | 2 |
但是,團隊想要進行的分析需要將關於每隻母雞的所有觀察結果放在一行中,如下所示:
| 母雞 | 第 1 天 | 第 2 天 | 第 3 天 |
|---|---|---|---|
| A | 3 | 4 | 2 |
| B | 1 | 0 | 2 |
好的資料工程需要具備操作資料的能力,以及對將要使用資料的分析目的的理解。
在上面的產蛋示例中,第一個表格處於規範化形式,這有利於進一步分析,第二個表格則以使用者可以理解的格式呈現資料。通常,這種格式對要向資料提出的問題會做出隱式假設,例如“母雞產蛋量隨時間的趨勢如何?”。而對於“一隻母雞在多少次情況下沒有產蛋?”等其他問題,則更容易用規範化形式的資料來回答。
分析的來源通常是另一個系統的輸出,因此,例如,一個產蛋資料庫可能在內部以 3 列格式儲存資料,但以“多列”格式匯出報表。資料工程師的任務之一是轉換捕獲的資料,這可能包括重新規範化來自輸出報表的資料。
維基百科將資料庫規範化定義為組織關係型資料庫的欄位和表格的過程,以最大限度地減少冗餘和依賴性,通常是透過將較大的表格拆分為較小的(且冗餘較少的)表格,並定義它們之間的關係來實現。規範化的主要目標是:
- 避免更新和刪除異常
- 在擴充套件資料庫結構時最小化重新設計
- 支援通用查詢,包括在設計時未預料到的未來查詢
假設產蛋資料被擴充套件為儲存每隻母雞的年齡和顏色。這可以在一個表格中表示,如下所示:
| 母雞 | 年齡 | 顏色 | 日期 | 雞蛋數量 |
|---|---|---|---|---|
| A | 2 | 棕色 | 1 | 3 |
| A | 2 | 棕色 | 2 | 4 |
| A | 2 | 棕色 | 3 | 2 |
| B | 1 | 白色 | 1 | 1 |
| B | 1 | 白色 | 2 | 0 |
| B | 1 | 白色 | 3 | 2 |
此表格現在包含冗餘資訊,因為我們重複儲存了每隻母雞的年齡和顏色 3 次。如果我們要儲存每隻母雞數百天的資料,就會變得效率低下。此外,如果母雞 B 變成 2 歲,我們必須同步更新記錄 4、5 和 6 中的更改,以更新年齡資料。規範化的解決方案是為與產蛋表格透過唯一識別符號或鍵關聯的雞相關事實建立一個單獨的“母雞”表格。
維基百科將主鍵定義為關係型資料庫關係型資料庫中表格中記錄的唯一識別符號。一些資料集具有天然的唯一鍵(例如員工表格中的 employee_id),而在其他情況下,則需要透過系統生成唯一鍵,這可以透過內部“遞增”計數器或透過組合多個屬性來建立一個唯一鍵(例如上面的示例中的 Chicken_Day)。其他表格可以透過使用主鍵來交叉引用此表格。例如,“專案”表格可以包含一個包含專案關聯的每個團隊成員的 employee_id 列。此“交叉引用”列稱為外部索引鍵。
實體關係圖(也稱為邏輯資料模型)用於設計關係型資料庫,並且可以很好地理解資料集中的結構。實體關係模型的三個構建塊是實體、屬性和關係。實體是離散且可識別的“事物”,可以是物理物件,例如汽車(或雞),也可以是概念,例如銀行交易或電話呼叫。每個實體可以物理地表示為一個表,其中表的每一列都是實體的屬性(例如 employee_id、forename、surname、date of joining)。關係是連線兩個或多個實體的動詞。例如,一隻雞“下”蛋,或者一名員工“屬於”某個部門。重要的是,關係也有一個基數,可以是“一對一”、“多對一”、“一對多”或“多對多”。例如,一隻雞可以下很多蛋,但每個蛋只由一隻雞下,所以“下”關係是一對多。多對多關係通常表明設計需要進一步闡述。例如,大學教師和學生之間的“教學”關係將是多對多,需要引入諸如班級和日期之類的實體來完全理解這種關係。下面顯示了一個實體關係圖示例。
-
實體關係圖示例,顯示學生和講師之間的關係。
更高階的資料工程還需要了解計算機程式設計和結構化查詢語言,以及關係型和非關係型資料庫管理系統。出於本書的目的,我們將使用R程式語言來完成簡單的資料工程任務。
此作業是關於將資料集讀入 R 資料框。組成 3 或 4 名學生的組。每個學生必須完成本練習的每個部分。組建團隊的目的是互相幫助理解正在發生的事情。一些作業需要一些試錯。不同的學生會進行不同的嘗試和錯誤,因此所有學生都將從彼此的嘗試和錯誤中學習。
3 個部分中的第 1 部分:在 R 中建立 4 個變數,每個變數有 12 個觀測值。
#Create data frame
#
#This work is licensed under a
#Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#D. Calvin Andrus, Ph.D.
#30 August 2012
#Remove Objects in workspace
rm(list=ls())
#Create four variables with 12 oberservations each
#Weather data for Sterling, VA from http://www.weather.com/weather/wxclimatology/monthly/USVA0735
#Retrieved 30 August 2012
#Average Temperature (Farenheit)
#Average Precipitation (inches)
Num <- 1:12
Month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
AveTemp <-c(32, 35, 43, 53, 62, 72, 76, 75, 67, 55, 46, 36)
AvePrcp <-c(2.85, 2.86, 3.60, 3.62, 4.72, 3.92, 3.70, 3.49, 4.00, 3.59, 3.58, 3.09)
#List out the objects that exist in the R environment
ls()
#Verify each variable
Num
Month
AveTemp
AvePrcp
#Link these four variables together into a dataset where each of the 12 observations correspond to each other
#Give the dataset a name (Wthr) using the dataframe command
Wthr <- data.frame(Num, Month, AveTemp, AvePrcp)
#List out the objects that exist in the R environment
ls()
#Notice that the 4 variables are still part of the R environment in addition to the dataframe
#The variables are now also part of the data frame
#Verify the contents of the dataset
Wthr
#Verify the formats within the data frame using the "structure" (str) command
str(Wthr)
#Notice that as part of the data frame the variables have a dollar sign ($) as a prefix
#Compare the Month variable inside and outside the data frame
str(Month)
str(Wthr$Month)
#Whoops! What happened? When we inserted the character variable Month into the data frame, it was converted to a factor variable.
#We call the values of a Factor variable "levels"
#Factor variables are nominal variables, which means the default is that order does not matter, which is called an "unordered" factor.
#Therefore R does two things as a default:
# 1) R prints out the levels in alphabetical order
# 2) R associates an random integer to each level, in this case 5, 4, 8, 1, 9, etc.
#For this particular problem the order of the months does matter.
#We can force an order on a factor by using the factor() function
#This is called an "ordered" factor
levels(Wthr$Month)
Wthr$Month <- factor(Wthr$Month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
#Note we could have also specified, levels=Month, can you explain why?
#Verify that the factor levels are now ordered properly, with the assigned integers in order
levels(Wthr$Month)
str(Wthr$Month)
Wthr
#We can now remove the redundant variables from the R workspace
rm("AvePrcp", "AveTemp", "Month", "Num")
ls()
#The dataframe is the only object left
#Now let's do some plots
plot(x=Wthr$Month, y=Wthr$AveTemp)
lines(Wthr$Month,fitted(loess(Wthr$AveTemp~Wthr$Num)))
plot(x=Wthr$Month, y=Wthr$AvePrcp)
plot(x=Wthr$AveTemp, y=Wthr$AvePrcp, pch=16, cex=1.5)
abline(lm(Wthr$AvePrcp~Wthr$AveTemp))
3 個部分中的第 2 部分。將一個示例資料集載入到資料框中。
#Put Example Data into Data Frame
#
#This work is licensed under a
#Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#D. Calvin Andrus, Ph.D.
#31 August 2012
#Remove Objects in workspace
rm(list=ls())
#Find out the available datasets
data()
#Pick a dataset and get the help file
?state
#Load the dataset into the R workspace
data(state)
#Find out what got loaded
ls()
#Examine the objects that were loaded
str(state.abb)
str(state.area)
str(state.x77)
#Notice that the last object was not a simple variable with a single set of observations, but
#it is a matrix that is 50 rows long and 8 columns wide
#Inspect a summary of these data
summary(state.abb)
summary(state.x77)
#Print out the contents of these objects
state.abb
state.x77
#Now let's put these objects into a data frame called "state" and inspect it
state <- data.frame(state.abb, state.area, state.center, state.division, state.name, state.region, state.x77)
ls()
str(state)
#Remove the old objects, now that we have put the data set into a data frame
rm(state.abb, state.area, state.center, state.division, state.name, state.region, state.x77)
ls()
#Print out the data frame
state
#Examine the relationships among the variables using table() and plot(), then
#Try about 10 different variations on both the table() and the plot() functions
table(state$state.region,state$state.division)
plot(state$Illiteracy,state$Murder)
3 個部分中的第 3 部分 - 匯入外部資料集。
- 在維基百科中找到費雪的鳶尾花資料集。
- 複製資料表並將其貼上到 Microsoft Excel、Apple Numbers 或 Google Docs 電子表格中。
- 將資料集以逗號分隔值 (CSV) 格式儲存在您的桌面上,檔名“iris.csv”。
- 將資料集讀入 R。
- 檢查資料,確保所有資料都在,然後使用
summary()、table()和plot()函式檢視資料。
#Read External Data into Data Frame
#
#This work is licensed under a
#Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#D. Calvin Andrus, Ph.D.
#30 August 2012
#Remove Objects in workspace
rm(list=ls())
#Findout what our default working directory is
getwd()
#Set your working directory to the "desktop" and verify
#You will need to use your own directory structure
setwd("/Users/Calvin/Desktop/")
getwd()
#Read the iris.csv into a dataframe -- and verify
# The first line of the file should be the variable names, hence header=TRUE
# Tell R that the separator is a comma
# If there are other line on top of the variable names, then you will need to skip them
iris <- read.table("iris.csv", header=TRUE, sep=",", skip=0)
str(iris)
iris
#You should have gotten 150 observations on 5 variables
#Explore the data using summary(), table(), and plot()
summary(iris)
table(iris$Species)
plot(iris$Sepal.length,iris$Sepal.width)
#Create a character variable to match a color to the factor variable Species
#Note how the R code implements the follow English statement
# If the variable "iris$species" has the value "I.setosa" then set the "iris$plotcolor" variable to the value "blue"
iris$plotcolor <- as.character("black")
iris$plotcolor [iris$Species == "I. setosa"] <- "blue"
iris$plotcolor [iris$Species == "I. versicolor"] <- "green"
iris$plotcolor [iris$Species == "I. virginica"] <- "red"
plot(
main="Plot of Sepal Size for Three Iris Species",
x=iris$Sepal.width, xlim=c(1,5), xlab="Sepal Width",
y=iris$Sepal.length, ylim=c(3,8), ylab="Sepal Length",
pch=16,
col=iris$plotcolor
)
legend(1.5, 3.5,"Setosa=Blue, Versicolor=Green, Virginica=Red")
#Now, plot the Petal Length and Width
#Compare Sepal Width with Petal Width
#Compare Sepal Length with Petal Length
- ↑ "Heritage Provider Network Heath Prize". Heritage Provider Network, Inc. Retrieved 13 July 2012.
您可以自由
- 共享 - 複製、分發、展示和表演作品(此維基百科中的頁面)。
- 混音 - 改編或製作衍生作品。
在以下條件下
- 署名 - 您必須將此作品歸功於華夏公益教科書。您不得暗示華夏公益教科書以任何方式認可您或您對本作品的使用。
- 相同方式共享 - 如果你更改、轉換或構建此作品,你只能在與本許可證相同或相似的許可證下分發生成的著作。
- 放棄 - 如果您獲得版權持有者的許可,則可以放棄上述任何條件。
- 公有領域 - 如果該作品或其任何部分在適用法律下屬於公有領域,則該狀態不受許可證的影響。
- 其他權利 - 許可證不會影響以下任何權利。
- 您的公平交易或合理使用權,或其他適用的版權例外和限制;
- 作者的道德權利;
- 其他人可能在作品本身或作品的使用方式中擁有的權利,例如宣傳權或隱私權。
- 注意 - 對於任何重複使用或分發,您必須向其他人明確說明此作品的許可條款。執行此操作的最佳方法是連結到以下網頁。
