新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中如何基于numpy創(chuàng)建矩陣
python的功能強(qiáng)大依賴于各種庫發(fā)揮的作用,例如numpy庫就提供了矩陣運(yùn)算的功能,如果我們想要進(jìn)行矩陣運(yùn)算,首先要導(dǎo)入numpy的包,創(chuàng)建矩陣。本文介紹python中基于numpy創(chuàng)建矩陣的三種方法:1、手動(dòng)創(chuàng)建;2、利用numpy數(shù)組創(chuàng)建;3、使用numpy.matix()函數(shù)創(chuàng)建矩陣。

創(chuàng)新互聯(lián)公司專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,德陽服務(wù)器托管,德陽服務(wù)器托管,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。
一、導(dǎo)入numpy
>>>from numpy import *;#導(dǎo)入numpy的庫函數(shù) >>>import numpy as np; #這個(gè)方式使用numpy的函數(shù)時(shí),需要以np.開頭。
二、python基于numpy創(chuàng)建矩陣方法
1、手動(dòng)創(chuàng)建
a=np.mat('1 2 3;4 5 6;7 8 9') # 中間打逗號也可以 b=np.mat('1,2,3;4,5,6;7,8,9')
2、利用numpy數(shù)組創(chuàng)建
c=np.mat(np.arange(9)) #一維的矩陣 c=np.mat(np.arange(9).reshape(3,3))
3、使用numpy.matix()函數(shù)創(chuàng)建矩陣
import numpy as np
# create 2x2 matrix
a = np.matrix([[1, 2], [3, 4]]) # using array of array
print('2x2 matrix is:\n', a)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', a.shape)
以上就是python中基于numpy創(chuàng)建矩陣的三種方法,希望能幫助到你進(jìn)行創(chuàng)建矩陣哦~
網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:python中如何基于numpy創(chuàng)建矩陣
當(dāng)前地址:http://fisionsoft.com.cn/article/dhcohgd.html


咨詢
建站咨詢
