深度學習中的Python語言3:SciPy和Matplotlib庫介紹
本文介紹Python中兩個常用庫:SciPy和Matplotlib。
SciPy
Numpy提供了高性能的多維向量和一些用於處理的基礎工具。SciPy在此基礎上提供大量的函數用於科學和工程應用。
熟悉SciPy的最好的方式是瀏覽官方文檔,此文章僅僅拋磚引玉,介紹一些常用功能。
1. 圖片操作(Image operations)
從硬碟讀取圖片、寫入圖片、重新調整圖片大小。見下面例子:
from scipy.misc import imread, imsave, imresizenn# Read an JPEG image into a numpy arraynnimg = imread(assets/cat.jpg)nnprint(img.dtype, img.shape) # Prints "uint8 (400, 248, 3)"nn# We can tint the image by scaling each of the color channelsnn# by a different scalar constant. The image has shape (400, 248, 3);nn# we multiply it by the array [1, 0.95, 0.9] of shape (3,);nn# numpy broadcasting means that this leaves the red channel unchanged,nn# and multiplies the green and blue channels by 0.95 and 0.9nn# respectively.nnimg_tinted = img * [1, 0.95, 0.9]nn# Resize the tinted image to be 300 by 300 pixels.nnimg_tinted = imresize(img_tinted, (300, 300))nn# Write the tinted image back to disknnimsave(assets/cat_tinted.jpg, img_tinted)nnuint8 (400, 248, 3)n
上圖:原圖;
下圖:重新調整大小後的圖片。
2. MATLAB 文件
簡單來說scipy.io.loadmat和scipy.io.savemat用於讀寫matlab文件。更多參見鏈接。
3. 求點之間的距離
scipy.spatial.distance.pdist可以方便的求點之間的各種『距離』,官方資料鏈接。以求歐式距離為例:
import numpy as npnnfrom scipy.spatial.distance import pdist, squareformnn# Create the following array where each row is a point in 2D space:nn# [[0 1]nn# [1 0]nn# [2 0]]nnx = np.array([[0, 1], [1, 0], [2, 0]])nnprint(x)nn# Compute the Euclidean distance between all rows of x.nn# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],nn# and d is the following array:nn# [[ 0. 1.41421356 2.23606798]nn# [ 1.41421356 0. 1. ]nn# [ 2.23606798 1. 0. ]]nnd = squareform(pdist(x, euclidean))nnprint(d)nn[[0 1]nn [1 0]nn [2 0]]nn[[ 0. 1.41421356 2.23606798]nn [ 1.41421356 0. 1. ]nn [ 2.23606798 1. 0. ]]n
另外一個非常實用的求兩個點集之間的距離的函數是scipy.spatial.distance.cdist,文檔點這裡。
Matplotlib
Matplotlib是一個用於作圖的庫,本文介紹的matplotlib.pyplot模型提供了一個作圖系統。下面簡要介紹。
1. 作圖
Matplotlib最終要的功能就是畫圖,你可以繪製2D的數據,下面是一個簡單的例子:
import numpy as npnnimport matplotlib.pyplot as pltnn# Compute the x and y coordinates for points on a sine curvennx = np.arange(0, 3 * np.pi, 0.1)nny = np.sin(x)nn# Plot the points using matplotlibnnplt.plot(x, y)nnplt.show() # You must call plt.show() to make graphics appear.n
我們可以輕鬆的畫出多條線,增加title和legend還有axis labels:
import numpy as npnnimport matplotlib.pyplot as pltnn# Compute the x and y coordinates for points on sine and cosine curvesnnx = np.arange(0, 3 * np.pi, 0.1)nny_sin = np.sin(x)nny_cos = np.cos(x)nn# Plot the points using matplotlibnnplt.plot(x, y_sin)nnplt.plot(x, y_cos)nnplt.xlabel(x axis label)nnplt.ylabel(y axis label)nnplt.title(Sine and Cosine)nnplt.legend([Sine, Cosine])nnplt.show()n
更多的關於plot的資料,看這裡。
2. 如何畫出多圖
在同一張圖片中作出多個不同的圖,可以使用subplot函數。
import numpy as npnnimport matplotlib.pyplot as pltnn# Compute the x and y coordinates for points on sine and cosine curvesnnx = np.arange(0, 3 * np.pi, 0.1)nny_sin = np.sin(x)nny_cos = np.cos(x)nn# Set up a subplot grid that has height 2 and width 1,nn# and set the first such subplot as active.nnplt.subplot(2, 1, 1)nn# Make the first plotnnplt.plot(x, y_sin)nnplt.title(Sine)nn# Set the second subplot as active, and make the second plot.nnplt.subplot(2, 1, 2)nnplt.plot(x, y_cos)nnplt.title(Cosine)nn# Show the figure.nnplt.show()n
官方文檔。
3. 圖片
imshow函數用來顯示圖片。
import numpy as npnnfrom scipy.misc import imread, imresizennimport matplotlib.pyplot as pltnnimg = imread(assets/cat.jpg)nnimg_tinted = img * [1, 0.95, 0.9]nn# Show the original imagennplt.subplot(1, 2, 1)nnplt.imshow(img)nn# Show the tinted imagennplt.subplot(1, 2, 2)nn# A slight gotcha with imshow is that it might give strange resultsnn# if presented with data that is not uint8. To work around this, wenn# explicitly cast the image to uint8 before displaying it.nnplt.imshow(np.uint8(img_tinted))nnplt.show()n
推薦閱讀:
※看看黃哥是怎麼解決問題的,網友答疑對話錄
※《Django By Example》第八章 中文翻譯
※請問安裝完anaconda後在開始的菜單中沒有Anaconda文件夾怎麼辦?
※Python · 神經網路(五)· Cost & Optimizer
※黃哥Python 轉載的霸氣文章"Yes, Python is Slow, and I Don』t Care"
TAG:Python |