1. 程式人生 > >PYTHON--一些函式

PYTHON--一些函式

1. numpy.c_[]

和np.r_[]可視為兄弟函式,兩者的功能為np.r_[]新增行,np.c_[]新增列。

a1 = np.array([[1, 2, 3], [4, 5, 6]])
b1 = np.array([[0, 0, 0]])

print(np.r_[a1, b1]) # >>>[[1 2 3]
                           [4 5 6]
                           [0 0 0]]
a1 = np.array([[1, 2], [3, 4], [5, 6]])
b1 = np.array([[0], [0], [0]])

print(np.c_[a1, b1]) # >>>[[1 2 0]
                           [3 4 0]
                           [5 6 0]]
我實際中遇到的程式碼是這樣的
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

這段程式碼中ravel函式將多維陣列降為一維,仍返回array陣列,元素以列排列。之後呼叫np.c_[]將xx.ravel()得到的列後增加以列yy.ravel()。這時每行元素變為了[[x1,y1];

[x2,y2]

……]

這裡的xx,yy使用np.meshgrid得到的座標軸,所以上面那段程式碼實際上執行了對座標軸上所以位置的[x, y]的預測。

參考:http://blog.csdn.net/guoziqing506/article/details/71078576

2. ravel()

array型別物件的方法,ravel函式將多維陣列降為一維,仍返回array陣列,元素以列排列。

與flatten一起學。兩者所要實現的功能是一致的(將多維陣列降位一維),兩者的區別在於返回拷貝(copy)還是返回檢視(view),numpy.flatten()返回一份拷貝,對拷貝所做的修改不會影響(reflects)原始矩陣,而numpy.ravel()返回的是檢視(view,也頗有幾分C/C++引用reference的意味),會影響(reflects)原始矩陣。

>>> x = np.array([[1, 2], [3, 4]])
>>> x
array([[1, 2],
       [3, 4]])
>>> x.flatten()
array([1, 2, 3, 4])
>>> x.ravel()
array([1, 2, 3, 4])
                    兩者預設均是行序優先
>>> x.flatten('F')
array([1, 3, 2, 4])
>>> x.ravel('F')
array([1, 3, 2, 4])

>>> x.reshape(-1)
array([1, 2, 3, 4])
>>> x.T.reshape(-1)
array([1, 3, 2, 4])
參考:http://blog.csdn.net/lanchunhui/article/details/50354978

3. plt.contour 與 plt.contourf

plt.contourf 與 plt.contour 區別:

  • f:filled,也即對等高線間的填充區域進行填充(使用不同的顏色);
  • contourf:將不會再繪製等高線(顯然不同的顏色分界就表示等高線本身),

4. scatter

matplotlib.pyplot.scatter(xys=Nonec=Nonemarker=Nonecmap=Nonenorm=Nonevmin=Nonevmax=Nonealpha=Nonelinewidths=Noneverts=Noneedgecolors=Nonehold=Nonedata=None**kwargs)

Make a scatter plot of x vs y

Marker size is scaled by s and marker color is mapped to c

Parameters:

x, y : array_like, shape (n, )

Input data

s : scalar or array_like, shape (n, ), optional

size in points^2. Default is rcParams['lines.markersize']** 2.

c : color, sequence, or sequence of color, optional, default: ‘b’

c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped.c can be a 2-D array in which the rows are RGB or RGBA, however, including the case of a single row to specify the same color for all points.

marker : MarkerStyle, optional, default: ‘o’

See markers for more information on the different styles of markers scatter supports. marker can be either an instance of the class or the text shorthand for a particular marker.

cmap : Colormap, optional, default: None

Colormap instance or registered name. cmap is only used if c is an array of floats. If None, defaults to rc image.cmap.

norm : Normalize, optional, default: None

Normalize instance is used to scale luminance data to 0, 1. norm is only used if c is an array of floats. If None, use the default normalize().

vmin, vmax : scalar, optional, default: None

vmin and vmax are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array is used. Note if you pass a norm instance, your settings for vmin and vmax will be ignored.

alpha : scalar, optional, default: None

The alpha blending value, between 0 (transparent) and 1 (opaque)

linewidths : scalar or array_like, optional, default: None

If None, defaults to (lines.linewidth,).

verts : sequence of (x, y), optional

If marker is None, these vertices will be used to construct the marker. The center of the marker is located at (0,0) in normalized units. The overall marker is rescaled by s.

edgecolors : color or sequence of color, optional, default: None

If None, defaults to ‘face’

If ‘face’, the edge color will always be the same as the face color.

If it is ‘none’, the patch boundary will not be drawn.

For non-filled markers, the edgecolors kwarg is ignored and forced to ‘face’ internally.