python---之flatten()函数的用法与scatter函数的用法

总结:flatten()用在这里scatter的意思就是相当于e,对应于x轴,f对应于y轴,相当于画坐标点,而对mnist数据集的那段代码,

添加flatten函数的用法详见:https://blog.csdn.net/zxyhhjs2017/article/details/81152450

部分内容来自http://blog.csdn.net/maoersong/article/details/23823925

flatten函数可以用在array与matrix上,但是不可用在list上:

1:用在array:

    >>> a = [[1,3],[2,4],[3,5]]  
    >>> a = array(a)  
    >>> a.flatten()  
    array([1, 3, 2, 4, 3, 5])  

2、用在列表:直接使用报错

复制代码

>>> a = [[1,3],[2,4],[3,5]]  
>>> a.flatten()  
  
Traceback (most recent call last):  
  File "<pyshell#10>", line 1, in <module>  
    a.flatten()  
AttributeError: 'list' object has no attribute 'flatten'

复制代码

如下为正确用法:

    >>> a = [[1,3],[2,4],[3,5],["abc","def"]]  
    >>> a1 = [y for x in a for y in x]  
    >>> a1  
    [1, 3, 2, 4, 3, 5, 'abc', 'def']  

3、用在矩阵:

复制代码

    >>> a = [[1,3],[2,4],[3,5]]  
    >>> a = mat(a)  
    >>> y = a.flatten()  
    >>> y  
    matrix([[1, 3, 2, 4, 3, 5]])  
    >>> y = a.flatten().A  
    >>> y  
    array([[1, 3, 2, 4, 3, 5]])  
    >>> shape(y)  
    (1, 6)  
    >>> shape(y[0])  
    (6,)  
    >>> y = a.flatten().A[0]  
    >>> y  
    array([1, 3, 2, 4, 3, 5])  

复制代码

在绘制图像,需要部分数据时,可以首先获取需要的矩阵中的一列数,然后通过调用flatten函数转成单行矩阵,再调用.A,获取二维array形式,获取下标为0的元素。此时得到单维array形式,可以在ax.scatter中作为参数,绘制图形。

得到图形如下:

猜你喜欢

转载自blog.csdn.net/zxyhhjs2017/article/details/81152379