当前位置:Gxlcms > Python > shape计算矩阵的python代码示例

shape计算矩阵的python代码示例

时间:2021-07-01 10:21:17 帮助过:70人阅读

这篇文章主要介绍了Python中shape计算矩阵的方法,涉及Python数学运算相关实现技巧,需要的朋友可以参考下

本文实例讲述了Python中shape计算矩阵的方法。分享给大家供大家参考,具体如下:

看到机器学习算法时,注意到了shape计算矩阵的方法接下来就讲讲我的理解吧

  1. >>> from numpy import *
  2. >>> import operator
  3. >>> a =mat([[1,2,3],[5,6,9]])
  4. >>> a
  5. matrix([[1, 2, 3],
  6. [5, 6, 9]])
  1. >>> shape(a)
  2. (2, 3)
  3. >>> a.shape[0] #计算行数
  4. 2
  5. >>> a.shape[1] #计算列数
  6. 3

接下来是Python中的解释

  1. Examples
  2. --------
  3. >>> np.shape(np.eye(3))
  4. (3, 3)
  5. >>> np.shape([[1, 2]])
  6. (1, 2)
  7. >>> np.shape([0])
  8. (1,)
  9. >>> np.shape(0)
  10. ()
  11. >>> a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
  12. >>> np.shape(a)
  13. (2,)
  14. >>> a.shape
  15. (2,)

以上就是shape计算矩阵的python代码示例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行