浩晨众云网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Numpy
通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包。
NumPy 是一个非常优秀的提供矩阵操作的包。NumPy的主要目标,就是提供多维数组,从而实现矩阵操作。
NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes.
基本操作
####################################### # 创建矩阵 ####################################### from numpy import array as matrix, arange # 创建矩阵 a = arange(15).reshape(3,5) a # Out[10]: # array([[0., 0., 0., 0., 0.], # [0., 0., 0., 0., 0.], # [0., 0., 0., 0., 0.]]) b = matrix([2,2]) b # Out[33]: array([2, 2]) c = matrix([[1,2,3,4,5,6],[7,8,9,10,11,12]], dtype=int) c # Out[40]: # array([[ 1, 2, 3, 4, 5, 6], # [ 7, 8, 9, 10, 11, 12]])