Python--对成矩阵的数据利用scatter画散点图以及对flatten函数和.A函数的理解

import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(dataMat[:,0].flatten().A[0],dataMat[:,1].flatten().A[0],marker='^',s=90,c='red')
plt.show()

其中dataMat是一个1000*2的矩阵

Flatten函数

flatten是numpy.ndarray.flatten的一个函数,即返回一个折叠成一维的对象。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的。
其官方文档是这样描述的

Parameters:

ndarray.flatten(order='C')
Return a copy of the array collapsed into one dimension.
order : {‘C’, ‘F’, ‘A’, ‘K’}, optional

‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.

解释:

a.flatten()就是把a降到一维,默认是按横的方向降
那么a.flatten().A又是什么呢? 其实这是因为若此时的a是个矩阵,降维后还是个矩阵,矩阵.A(等效于矩阵.getA())变成了数组

例子:

#当a为array
>>> from numpy import *
>>> a=array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> a.flatten()
array([1, 2, 3, 4, 5, 6])
>>> a.flatten('F')
array([1, 3, 5, 2, 4, 6])  # 按列排序
>>> a.flatten('A')
array([1, 2, 3, 4, 5, 6])
>>> 


#当a为矩阵
>>>a=mat([[1,2,3],[4,5,6]])
>>> a
matrix([[1, 2, 3],
        [4, 5, 6]])
>>> a.flatten()
matrix([[1, 2, 3, 4, 5, 6]])
>>>a=mat([[1,2,3],[4,5,6]])
>>> a
matrix([[1, 2, 3],
        [4, 5, 6]])
>>> a.flatten()
matrix([[1, 2, 3, 4, 5, 6]])
>>> y=a.flatten().A 
>>> shape(y)
(1L, 6L)
>>> shape(y[0]) 
(6L,)
>>> a.flatten().A[0] 
array([1, 2, 3, 4, 5, 6])
>>>  

Python中flatten( ),matrix.A用法_lilong117194的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值