數據得到的方式多種多樣,經常使用的公開數據集包括:html
1.UCL機器學習知識庫:包括近300個不一樣大小和類型的數據集,可用於分類、迴歸、聚類和推薦系統任務。數據集列表位於:http://archive.ics.uci.edu/ml/python
2.Amazon AWS公開數據集:包含的一般是大型數據集,可經過Amazon S3訪問。這些數據集包括人類基因組項目、Common Crawl網頁語料庫、維基百科數據和Google Books Ngrams。相關信息可參見:http://aws.amazon.com/publicdatasets/bash
3.Kaggle:這裏集合了Kaggle舉行的各類機器學習競賽所用的數據集。它們覆蓋分類、迴歸、排名、推薦系統以及圖像分析領域,可從Competitions區域下載: http://www.kaggle.com/competitions機器學習
4.KDnuggets:這裏包含一個詳細的公開數據集列表,其中一些上面提到過的。該列表位於:http://www.kdnuggets.com/datasets/index.htmloop
下面採用的數據集是MovieLens 100k數據集,MovieLens 100k數據集包含表示多個用戶對多部電影的10萬次評級數據,也包含電影元數據和用戶屬性信息。學習
在目錄下,能夠查看文件中的前5行的數據spa
head -5 u.user 1|24|M|technician|85711 2|53|F|other|94043 3|23|M|writer|32067 4|24|M|technician|43537 5|33|F|other|15213
如今使用Spark交互式終端來對數據進行可視化的操做,以直觀的瞭解數據的狀況orm
1.安裝ipythonhtm
IPython是針對Python的一個高級交互式殼程序,包含內置一系列實用功能的pylab,其中有NumPy和SciPy用於數值計算,以及matplotlib用於交互式繪圖和可視化blog
sudo apt-get install ipython
2.安裝anaconda,安裝的文件是Anaconda2-4.3.1-Linux-x86_64.sh,能夠在清華的開源軟件鏡像站下載
一個預編譯的科學Python套件
bash Anaconda2-4.3.1-Linux-x86_64.sh #一路回車 #文件講會安裝在~目錄下 #在詢問是否把anaconda的bin添加到用戶的環境變量中,選擇yes source ~/.bashrc
在/etc/profile中添加
export PATH=/home/lintong/anaconda2/bin:$PATH
3.啓動Hadoop,在Hadoop的安裝目錄的sbin目錄下啓動start-all.sh
4.啓動pyspark,注意使用的spark的版本是2.1.0,因此參數和低版本的會有不一樣,下圖是啓動後的界面
PYSPARK_DRIVER_PYTHON=/usr/bin/ipython PYSPARK_DRIVER_PYTHON_OPTS="--pylab" pyspark
5.把訓練數據集文件放在Hadoop文件系統中
hadoop fs -put /XXXtinput/ml-100k /user/XXX
6.代碼
user_data = sc.textFile("/user/common/ml-100k/u.user") user_data.first()
user_fields = user_data.map(lambda line: line.split("|"));\ ages = user_fields.map(lambda x: int(x[1])).collect();\ hist(ages, bins=20, color='lightblue', normed=True);\ fig = matplotlib.pyplot.gcf();\ fig.set_size_inches(16, 10)
count_by_occupation = user_fields.map(lambda fields: (fields[3], 1)).reduceByKey(lambda x, y: x + y).collect() #或者 count_by_occupation = user_fields.map(lambda fields: fields[3]).countByValue() x_axis1 = np.array([c[0] for c in count_by_occupation]) y_axis1 = np.array([c[1] for c in count_by_occupation]) #升序排序 x_axis = x_axis1[np.argsort(y_axis1)] y_axis = y_axis1[np.argsort(y_axis1)] pos = np.arange(len(x_axis)) width = 1.0 ax = plt.axes() ax.set_xticks(pos + (width / 2)) ax.set_xticklabels(x_axis) plt.bar(pos, y_axis, width, color='lightblue') plt.xticks(rotation=30) fig = matplotlib.pyplot.gcf() fig.set_size_inches(16, 10)