1、创建全部为1的方法
import numpy as np
t = np.ones(2,3)
print(t)
结果如下:
[[1. 1. 1.]
[1. 1. 1.]]
2、创建全部为0的方法
t1 = np.zeros((3,4))
print(t1)
结果如下:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
3、创建一个对角线为1的正方形数组(方阵)
t2 = np.eye(10)
print(t2)
结果如下:
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
4、获得数组中最值的方法
t3 = np.eye(4)
print(t3)
print(np.argmax(t3,axis=0))
结果是在这样:获得到每组最大值所在的索引位置
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[0 1 2 3]
然后我们将该数组中的1都重新赋值为-1
赋值和修改操作
t3[t3==1] = -1
print(t3)
结果:
[[-1. 0. 0. 0.]
[ 0. -1. 0. 0.]
[ 0. 0. -1. 0.]
[ 0. 0. 0. -1.]]
然后取每组最小值的索引位置
print(np.argmin(t3,axis=1))
结果如下:
[0 1 2 3]