SVM中用到的Numpy、matplotlib函數

numpy.meshgrid

輸入坐標向量返回對應的矩陣。
Return coordinate matrices from coordinate vectors.

np.meshgrid(x1,x2,...,indexing,sparse=False,copy)

x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xx, yy = meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)
plt.show()

matplotlib.pyplot.contour

contour()畫輪廓線(等高線);
contourf()填充等高線。

numpy.r_

默認按照行合并

>>>a=np.array([[1,2,3],[4,5,6]])
>>>np.r_[a,a]
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])

給出了合并軸則按照指定軸合并

>>>np.r_['-1',a,a]
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

使用‘r’或‘c’生成矩陣

>>>np.r_['r',[1,2,3], [4,5,6]]
matrix([[1, 2, 3, 4, 5, 6]])

numpy.ravel()

返回連續扁平的數組(Return a contiguous flattened array)

numpy.ravel(a,order='C')
a:輸入的數組;
order:包括‘C’、‘F’、‘A’、‘K’;a中的元素將按照索引的順序讀取;

x = np.array([[1, 2, 3], [4, 5, 6]])
x.ravel()
[1 2 3 4 5 6]

在一個二位數組中,
‘C’ :row-major, C-style order,在內存中按行存儲,按行計算比較快;
‘F’ :column-major, Fortran-style order,在內存中按列存儲,按列計算較快;

C contiguous,Fortran contiguous 詳見:
https://stackoverflow.com/questions/26998223/what-is-the-difference-between-contiguous-and-non-contiguous-arrays

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容