TOP 3大開源Python數(shù)據(jù)分析工具!
在大數(shù)據(jù)庫領域,Python是最常被使用的編程語言,因此了解與其相關的數(shù)據(jù)分析工具是很有必要的。如果你正在使用virtualenv、pyenv或其他變體在自己的環(huán)境中運行Python,那么,可以嘗試本文推薦的三大開源工具。
(注:本文示例使用IPython,如果感興趣,請確保已經(jīng)安裝)
- $ mkdir python-big-data
- $ cd python-big-data
- $ virtualenv ../venvs/python-big-data
- $ source ../venvs/python-big-data/bin/activate
- $ pip install ipython
- $ pip install pandas
- $ pip install pyspark
- $ pip install scikit-learn
- $ pip install scipy
本文選取的示例數(shù)據(jù)是最近幾天從某網(wǎng)站獲取的實際生產(chǎn)日志數(shù)據(jù),從技術層面來看,這些數(shù)據(jù)并不能算作是大數(shù)據(jù),因為它的大小只有大約2Mb,但就演示來說已經(jīng)足夠了。
如果你想獲取這些示例數(shù)據(jù),可以使用git從作者的公共GitHub存儲庫中下載:admintome / access-log-data
- $ git clone https://github.com/admintome/access-log-data.git
數(shù)據(jù)是一個簡單的CSV文件,因此每行代表一個單獨的日志,字段用逗號分隔:
- 2018-08-01 17:10,'www2','www_access','172.68.133.49 - - [01/Aug/2018:17:10:15 +0000] "GET /wp-content/uploads/2018/07/spark-mesos-job-complete-1024x634.png HTTP/1.0" 200 151587 "https://dzone.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"'
以下是日志行架構:

由于對數(shù)據(jù)可執(zhí)行的操作的復雜性不確定,因此本文重點選取加載數(shù)據(jù)和獲取數(shù)據(jù)樣本兩個操作來講解三個工具。
1、Python Pandas
我們討論的***個工具是Python Pandas。正如它的網(wǎng)站所述,Pandas是一個開源的Python數(shù)據(jù)分析庫。它最初由AQR Capital Management于2008年4月開發(fā),并于2009年底開源,目前由專注于Python數(shù)據(jù)包開發(fā)的PyData開發(fā)團隊繼續(xù)開發(fā)和維護,屬于PyData項目的一部分。Pandas最初被作為金融數(shù)據(jù)分析工具而開發(fā)出來,因此,pandas為時間序列分析提供了很好的支持。
首先,啟動IPython并對示例數(shù)據(jù)進行一些操作。(因為pandas是python的第三方庫所以使用前需要安裝一下,直接使用pip install pandas 就會自動安裝pandas以及相關組件)

- import pandas as pd
- headers = ["datetime", "source", "type", "log"]
- df = pd.read_csv('access_logs_parsed.csv', quotechar="'", names=headers)
大約一秒后,我們會收到如下回復:
- [6844 rows x 4 columns]
- In [3]:
如上所見,我們有大約7000行數(shù)據(jù),它從中找到了四個與上述模式匹配的列。
Pandas自動創(chuàng)建了一個表示CSV文件的DataFrame對象,Pandas中的DataFrame數(shù)據(jù)既可以存儲在SQL數(shù)據(jù)庫中,也可以直接存儲在CSV文件中。接下來我們使用head()函數(shù)導入數(shù)據(jù)樣本。
- In [11]: df.head()
- Out[11]:
- datetime source type log
- 2018-08-01 17:10 www2 www_access 172.68.133.49 - - [01/Aug/2018:17:10:15 +0000]...
- 2018-08-01 17:10 www2 www_access 162.158.255.185 - - [01/Aug/2018:17:10:15 +000...
- 2018-08-01 17:10 www2 www_access 108.162.238.234 - - [01/Aug/2018:17:10:22 +000...
- 2018-08-01 17:10 www2 www_access 172.68.47.211 - - [01/Aug/2018:17:10:50 +0000]...
- 2018-08-01 17:11 www2 www_access 141.101.96.28 - - [01/Aug/2018:17:11:11 +0000]...
使用Python Pandas可以做很多事情, 數(shù)據(jù)科學家通常將Python Pandas與IPython一起使用,以交互方式分析大量數(shù)據(jù)集,并從該數(shù)據(jù)中獲取有意義的商業(yè)智能。
2、PySpark
我們討論的第二個工具是PySpark,該工具來自Apache Spark項目的大數(shù)據(jù)分析庫。
PySpark提供了許多用于在Python中分析大數(shù)據(jù)的功能,它自帶shell,用戶可以從命令行運行。
- $ pyspark
這會加載pyspark shell:
- (python-big-data)[email protected]:~/Development/access-log-data$ pyspark Python 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. 2018-08-03 18:13:38 WARN Utils:66 - Your hostname, admintome resolves to a loopback address: 127.0.1.1; using 192.168.1.153 instead (on interface enp0s3) 2018-08-03 18:13:38 WARN Utils:66 - Set SPARK_LOCAL_IP if you need to bind to another address 2018-08-03 18:13:39 WARN NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). Welcome to ____ __ / __/__ ___ _____/ /__ _\ \/ _ \/ _ `/ __/ '_/ /__ / .__/\_,_/_/ /_/\_\ version 2.3.1 /_/ Using Python version 3.6.5 (default, Apr 1 2018 05:46:30) SparkSession available as 'spark'. >>>
當你啟動shell時,你會得到一個Web GUI查看你的工作狀態(tài),只需瀏覽到http:// localhost:4040即可獲得PySpark Web GUI。

讓我們使用PySpark Shell加載示例數(shù)據(jù):
- dataframe = spark.read.format("csv").option("header","false").option("mode","DROPMALFORMED").option("quote","'").load("access_logs.csv")
- dataframe.show()
PySpark提供了已創(chuàng)建的DataFrame示例:
- >>> dataframe2.show()
- +----------------+----+----------+--------------------+
- | _c0| _c1| _c2| _c3|
- +----------------+----+----------+--------------------+
- |2018-08-01 17:10|www2|www_access|172.68.133.49 - -...|
- |2018-08-01 17:10|www2|www_access|162.158.255.185 -...|
- |2018-08-01 17:10|www2|www_access|108.162.238.234 -...|
- |2018-08-01 17:10|www2|www_access|172.68.47.211 - -...|
- |2018-08-01 17:11|www2|www_access|141.101.96.28 - -...|
- |2018-08-01 17:11|www2|www_access|141.101.96.28 - -...|
- |2018-08-01 17:11|www2|www_access|162.158.50.89 - -...|
- |2018-08-01 17:12|www2|www_access|192.168.1.7 - - [...|
- |2018-08-01 17:12|www2|www_access|172.68.47.151 - -...|
- |2018-08-01 17:12|www2|www_access|192.168.1.7 - - [...|
- |2018-08-01 17:12|www2|www_access|141.101.76.83 - -...|
- |2018-08-01 17:14|www2|www_access|172.68.218.41 - -...|
- |2018-08-01 17:14|www2|www_access|172.68.218.47 - -...|
- |2018-08-01 17:14|www2|www_access|172.69.70.72 - - ...|
- |2018-08-01 17:15|www2|www_access|172.68.63.24 - - ...|
- |2018-08-01 17:18|www2|www_access|192.168.1.7 - - [...|
- |2018-08-01 17:18|www2|www_access|141.101.99.138 - ...|
- |2018-08-01 17:19|www2|www_access|192.168.1.7 - - [...|
- |2018-08-01 17:19|www2|www_access|162.158.89.74 - -...|
- |2018-08-01 17:19|www2|www_access|172.68.54.35 - - ...|
- +----------------+----+----------+--------------------+
- only showing top 20 rows
我們再次看到DataFrame中有四列與我們的模式匹配,DataFrame此處可以被視為數(shù)據(jù)庫表或Excel電子表格。
3、Python SciKit-Learn
任何關于大數(shù)據(jù)的討論都會引發(fā)關于機器學習的討論,幸運的是,Python開發(fā)人員有很多選擇來使用機器學習算法。
在沒有詳細介紹機器學習的情況下,我們需要獲得一些執(zhí)行機器學習的數(shù)據(jù),我在本文中提供的示例數(shù)據(jù)不能正常工作,因為它不是數(shù)字類型的數(shù)據(jù)。我們需要操縱數(shù)據(jù)并將其呈現(xiàn)為數(shù)字格式,這超出了本文的范圍,例如,我們可以按時間映射日志以獲得具有兩列的DataFrame:一分鐘內(nèi)的日志數(shù)和當前時間:
- +------------------+---+
- | 2018-08-01 17:10 | 4 |
- +------------------+---+
- | 2018-08-01 17:11 | 1 |
- +------------------+---+
通過這種形式的數(shù)據(jù),我們可以執(zhí)行機器學習算法來預測未來可能獲得的訪客數(shù)量,SciKit-Learn附帶了一些樣本數(shù)據(jù)集,我們可以加載一些示例數(shù)據(jù),來看一下具體如何運作。
- In [1]: from sklearn import datasets
- In [2]: iris = datasets.load_iris()
- In [3]: digits = datasets.load_digits()
- In [4]: print(digits.data)
- [[ 0. 0. 5. ... 0. 0. 0.]
- [ 0. 0. 0. ... 10. 0. 0.]
- [ 0. 0. 0. ... 16. 9. 0.]
- ...
- [ 0. 0. 1. ... 6. 0. 0.]
- [ 0. 0. 2. ... 12. 0. 0.]
- [ 0. 0. 10. ... 12. 1. 0.]]
這將加載兩個用于機器學習分類的算法,用于對數(shù)據(jù)進行分類。
結論
在大數(shù)據(jù)領域,Python、R以及Scala是主要的參與者,開源社區(qū)中有不少針對這三者的工具,國內(nèi)互聯(lián)網(wǎng)企業(yè)一向很喜歡基于開源工具自研,選擇之前不妨做好功課,抽取使用人數(shù)較多且應用場景最接近實際需求的方案。