Python中怎樣使用shape計算矩陣的行和列

2021-03-03 20:27:52 字數 4272 閱讀 3558

1樓:匿名使用者

import numpy

a = numpy.array([[1,2,3],[4,5,6]])print a.shape

矩陣有乙個shape屬性,是乙個(行,列)形式的元組

python中怎樣使用shape計算矩陣的行和列

2樓:匿名使用者

import numpy

a = numpy.array([[1,2,3],[4,5,6]])print a.shape

矩陣有乙個shape屬性,是乙個(行,列)形式的元組

3樓:匿名使用者

你得先安裝numpy庫,矩陣

(ndarray)的shape屬性可以獲取矩陣的形狀(例如二維陣列的行專列),獲取的結果是一屬個元組,因此相關**如下:

import numpy as np

x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])

# 輸出陣列的行和列數

print x.shape # (4, 3)# 只輸出行數

print x.shape[0] # 4

# 只輸出列數

print x.shape[1] # 3

python中怎樣使用shape計算矩陣的行和列

4樓:日time寸

import numpy

a = numpy.array([[1,2,3],[4,5,6]])print a.shape

矩陣有乙個shape屬性,是乙個(行,列)形式的元組

python中怎樣使用shape計算矩陣的行和列

5樓:匿名使用者

屬於numpy庫的方源法

import numpy

a=numpy.zeros((5,5),dtype='int32')a.shape

輸出為(5,5) 可以用

a.shape[0]

看行數bai(也就是

du第乙個維度)

p.s. 可以初始zhi化高維矩陣dao 就是a可以不止二維

python中怎樣使用shape計算矩陣的行和列

6樓:碧血玉葉花

>>> import numpy as np

>>> a = np.arange(1,11).reshape(10,1)

>>> b = a * 1.1

>>> c = a / 1.1

>>> a

array([[ 1],

[ 2],

[ 3],

[ 4],

[ 5],

[ 6],

[ 7],

[ 8],

[ 9],

[10]])

>>> b

array([[ 1.1],

[ 2.2],

[ 3.3],

[ 4.4],

[ 5.5],

[ 6.6],

[ 7.7],

[ 8.8],

[ 9.9],

[ 11. ]])

>>> c

array([[ 0.90909091],

[ 1.81818182],

[ 2.72727273],

[ 3.63636364],

[ 4.54545455],

[ 5.45454545],

[ 6.36363636],

[ 7.27272727],

[ 8.18181818],

[ 9.09090909]])

>>> x = np.array([

... np.reshape(a, len(a)),

... np.reshape(b, len(b)),

... np.reshape(c, len(c))

... ]).transpose()

>>> x

array([[ 1. , 1.1 , 0.90909091],

[ 2. , 2.2 , 1.81818182],

[ 3. , 3.3 , 2.72727273],

[ 4. , 4.4 , 3.63636364],

[ 5. , 5.5 , 4.54545455],

[ 6. , 6.6 , 5.45454545],

[ 7. , 7.7 , 6.36363636],

[ 8. , 8.8 , 7.27272727],

[ 9. , 9.9 , 8.18181818],

[ 10. , 11. , 9.09090909]])

>>>

python中怎樣使用shape計算矩陣的行和列

7樓:匿名使用者

>>> import numpy as np

>>> a = np.arange(1,11).reshape(10,1)

>>> b = a * 1.1

>>> c = a / 1.1

>>> a

array([[ 1],

[ 2],

[ 3],

[ 4],

[ 5],

[ 6],

[ 7],

[ 8],

[ 9],

[10]])

>>> b

array([[ 1.1],

[ 2.2],

[ 3.3],

[ 4.4],

[ 5.5],

[ 6.6],

[ 7.7],

[ 8.8],

[ 9.9],

[ 11. ]])

>>> c

array([[ 0.90909091],

[ 1.81818182],

[ 2.72727273],

[ 3.63636364],

[ 4.54545455],

[ 5.45454545],

[ 6.36363636],

[ 7.27272727],

[ 8.18181818],

[ 9.09090909]])

>>> x = np.array([

... np.reshape(a, len(a)),

... np.reshape(b, len(b)),

... np.reshape(c, len(c))

... ]).transpose()

>>> x

array([[ 1. , 1.1 , 0.90909091],

[ 2. , 2.2 , 1.81818182],

[ 3. , 3.3 , 2.72727273],

[ 4. , 4.4 , 3.63636364],

[ 5. , 5.5 , 4.54545455],

[ 6. , 6.6 , 5.45454545],

[ 7. , 7.7 , 6.36363636],

[ 8. , 8.8 , 7.27272727],

[ 9. , 9.9 , 8.18181818],

[ 10. , 11. , 9.09090909]])

>>>

python中怎樣使用shape計算矩陣的行和列

8樓:匿名使用者

import numpy

a = numpy.array([[1,2,3],[4,5,6]])print a.shape

矩陣有乙個shape屬性,是乙個(行,列)形式的元組

python怎麼使用matplotlib畫出下面這樣的圖

最簡單的柱狀 應該是這樣的 coding utf 8 import matplotlib.pyplot as pltimport numpy as np x np.random.randint 0,10,size 10 y np.random.randint 100,1000,size 10 plt...

python中的冒號怎麼用,python中函式引數列表中使用的冒號是幹什麼用的

函式,迴圈,bai條件,類du定義等後面都是跟著乙個zhiblock,block需要縮排,dao所以這些語句後版面都有乙個冒號。至於為什麼權這樣子,我們又不是研究理論的。死記硬背就可以了。什麼是乙個block。每個語言裡幾乎都有有。就是一組語句。比如函式體,迴圈體,條件分支,類的定義等等都是乙個bl...

python中v和f的區別,python中的rawinput和input的區別

回車符復 r 換行符 n 水平製表符制 t 垂直制bai表符 v 換頁符 f v的效果du的確如你zhi程式裡所顯示那dao樣。f在有些終端下與你現在的效果不同。比如可能會清屏。如果你直接向列印口print,那麼會真的換頁。python 中的raw input 和input的區別 1.這兩個函式均能...