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

扫描二维码关注公众号,回复: 2437930 查看本文章



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.

Returns:

paths : PathCollection


猜你喜欢

转载自blog.csdn.net/u010103202/article/details/75064394
今日推荐