numpy中ndarray常见的属性

numpy中ndarray常见的属性

dtype:数据类型
T:转置(注意一维数组问题)
shape:形状
ndim:维度个数
size:总的元素个数
conj:共轭
real:实部
imag:虚部
flat:1-D的iterator

先随机生成3*3的方阵

import  numpy as np
a=np.random.randint(1,9,size=9).reshape((3,3))
print(a)

结果

[[1 3 1]
 [8 2 6]
 [6 4 4]]

显示数据类型

print(a.dtype)
int32

转置

print(a.T)

结果

[[1 8 6]
 [3 2 4]
 [1 6 4]]

矩阵形状

print(a.shape)
print(a.ndim)
(3, 3)
2

矩阵大小

print(a.size)

结果

9

矩阵iterator

print(a.flat)

结果

<numpy.flatiter object at 0x0000023DF50D73D0>

生成一个复矩阵

b=np.random.randint(1,9,size=9).reshape((3,3))
a=a+b*(1j)

输出

array([[1.+7.j, 3.+2.j, 1.+1.j],
       [8.+5.j, 2.+1.j, 6.+7.j],
       [6.+8.j, 4.+2.j, 4.+8.j]])

求实部虚部

print('实部=',a.real,'\n虚部',a.imag)

输出

实部= [[1. 3. 1.]
 [8. 2. 6.]
 [6. 4. 4.]] 
虚部= [[7. 2. 1.]
 [5. 1. 7.]
 [8. 2. 8.]]
发布了19 篇原创文章 · 获赞 0 · 访问量 1150

猜你喜欢

转载自blog.csdn.net/Jinyindao243052/article/details/104030095