Numpy
通過觀察Python的自有數(shù)據(jù)類型,我們可以發(fā)現(xiàn)Python原生并不提供多維數(shù)組的操作,那么為了處理矩陣,就需要使用第三方提供的相關(guān)的包。
NumPy 是一個(gè)非常優(yōu)秀的提供矩陣操作的包。NumPy的主要目標(biāo),就是提供多維數(shù)組,從而實(shí)現(xiàn)矩陣操作。
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.
基本操作
####################################### # 創(chuàng)建矩陣 ####################################### from numpy import array as matrix, arange # 創(chuàng)建矩陣 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]])