天天看點

讀書筆記資料科學入門————可視化資料本章摘要matplotlib的配置各種圖形的繪制

本章摘要

資料可視化是資料科學家的重要部分。建立可視化的目的:探索資料,交流資料。

本章利用一個外置的matplotlib庫的配置進行可視化的初步了解

matplotlib的配置

    許多工具可以可視化資料,目前應用很廣的是matplotlib庫,在windows下配置該子產品稍微比較麻煩,下面會一步一步講解:

Matplotib 是python 的一個繪圖庫,裡頭有各種各樣的繪圖方法,可以用Matplotib 顯示圖像,放大圖像,儲存圖像等等,對于OpenCV處理圖像具有非常大的幫助。

如果是下載下傳的庫帶有exe安裝程式,可以直接進行安裝,同時會定位到Python安裝的位置。

如果下載下傳的庫是py檔案那麼需要用指令行進行安裝:

Python第三方子產品中一般會自帶setup.py檔案,在Windows環境下,我們隻需要使用cmd指令:

      cd c:\python\..

      python setup.py install

那麼所需要下載下傳子產品到底有哪些呢:

Numpy,SciPy,MatplotLib

下載下傳Numpy,SciPy,MatplotLib這三個庫的exe,注意,這裡用的是exe,因為MatplotLib的使用需要以Numpy的支援,是以最好先裝NumPy再裝MatplotLib。下載下傳位址:

NumPy: http://sourceforge.net/projects/numpy/files/NumPy/1.9.2/

SciPy: http://sourceforge.net/projects/scipy/files/scipy/0.15.1/

MatPlotLib: http://matplotlib.org/downloads.html

當出現各種報錯的時候,然後進行測試的時候一般會出現很多問題都是确實某某子產品的問題,這些子產品舉例如下:

1. No module name six

對于報這個錯誤的時候需要下載下傳six子產品

http://www.pythonhosted.org/six/

 解壓之後,進入解壓目錄,指令行執行 python setup.py install 就可以安裝完成。

2. ImportError: matplotlib requires dateutil

https://pypi.python.org/pypi/python-dateutil/1.4.1

同樣到解壓目錄下,執行 python  setup.py install 同樣可以安裝成功,

3. ImportError: matplotlib requires pyparsing

http://sourceforge.net/projects/pyparsing/files/pyparsing/pyparsing-2.0.3/

      下載下傳pyparsing-2.0.3.win32-py2.6.exe,輕按兩下運作,安裝完成====

以上缺少的子產品以及應對各種子產品不同安裝方法如上。

測試程式如下:

>>> from matplotlib import pyplot as plt

>>> years = [1950,1960,1970,1980,1990,2000,2010]

>>> gdp=[300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]

>>> plt.plot(years,gdp,color='green',marker='o',linestyle='solid')

>>> plt.title('GDP')

讀書筆記資料科學入門————可視化資料本章摘要matplotlib的配置各種圖形的繪制

各種圖形的繪制

條形圖

觀察離散的項目集合數量變化: >>> movies = ["Annie","Ben-Hur","Caseblanca","Gandhi","WestSideStory"]

>>> num_oscars=[5,11,3,8,10]

>>> xs = [i+0.1 for i,_ in enumerate(moveies)]

>>> xs = [i+0.1 for i,_ in enumerate(movies)]

>>> from matplotlib import pyplot as plt

>>> plt.bar(xs,num_oscars)//設定條形圖高度和寬度

>>> plt.title("love movies")

<matplotlib.text.Text object at 0x0526A150>

>>> plt.xticks([i+0.5 for i,_ in enumerate(movies)],movies)//設定X軸的變量名字

([<matplotlib.axis.XTick object at 0x050E5D30>, <matplotlib.axis.XTick object at 0x050E5D70>, <matplotlib.axis.XTick object at 0x0527A0F0>, <matplotlib.axis.XTick object at 0x052CD670>, <matplotlib.axis.XTick object at 0x052CDA10>], <a list of 5 Text xticklabel objects>)

>>> plt.show()

讀書筆記資料科學入門————可視化資料本章摘要matplotlib的配置各種圖形的繪制

條形圖主要用來繪制擁有大量數值取值的變量直方圖,探索取值是如何分布的。其中bar函數參數中寬度設定了左移變量是為了使中心在某處。 函數plt.axis([2012.5,2014.5,499,506])顯示的是橫軸2013到2014而縱軸是499到506

線圖

如果為了表示某種事物的趨勢,那麼可以采用線圖。 首先進行資料的讀取: >>> variance = [1,2,4,8,16,32,64,128,256]

>>> bias_squared=[256,128,64,32,16,8,4,2,1]

>>> total_error=[x+y for x,y in zip(variance,bias_squared)]

>>> total_error

[257, 130, 68, 40, 32, 40, 68, 130, 257]

>>> xs = [i for i,_ in enumerate(variance)]

>>> xs

[0, 1, 2, 3, 4, 5, 6, 7, 8]

然後進行繪制偏差方差的權衡圖

同一個圖上顯示多個序列: >>> plt.plot(xs,variance,'g-',label='variance')

[<matplotlib.lines.Line2D object at 0x055190B0>]

>>> plt.plot(xs,bias_squared,'r-',label='bias^2')

>>> plt.plot(xs,total_error,'b:',label='total_error')

[<matplotlib.lines.Line2D object at 0x05519590>]

>>> plt.legend(loc=9)

<matplotlib.legend.Legend object at 0x05519C10>

>>> plt.title('bias variance')

<matplotlib.text.Text object at 0x054FE050>

>>> plt.show()

讀書筆記資料科學入門————可視化資料本章摘要matplotlib的配置各種圖形的繪制

散點圖

散點圖是顯示成對資料集的可視化關系的選擇,比如你的使用者已有的朋友數目和他們每天花在網站上分鐘數 >>> friends=[70,65,72,63,71,64,60,64,67]

>>> minutes=[175,170,205,120,220,130,105,145,190]

>>> labels = ['a','b','c','d','e','f','g','h','i']

>>> plt.scatter(friends,minutes)

<matplotlib.collections.PathCollection object at 0x050FCF10>

>>> plt.title('minutes and friends')

<matplotlib.text.Text object at 0x0510C530>

>>> plt.xlabel('friends')

<matplotlib.text.Text object at 0x053C7A50>

>>> plt.ylabel('minutes')

<matplotlib.text.Text object at 0x052CD9F0>

>>> plt.show()

讀書筆記資料科學入門————可視化資料本章摘要matplotlib的配置各種圖形的繪制

繼續閱讀