#布尔值索引
names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Bob']) #7个数据的数组
data = np.random.random((7,4)) #随机生成7*4的数组
print(data)
print('----------------------------------------------------------1------')
names == 'Bob'
print(data[names == 'Bob']) #将names数组映射到data多维数组中,输出'Bob'所在的数组(第1、4、7行)
print('----------------------------------------------------------2------')
print(data[names == 'Bob',2:])
print('----------------------------------------------------------3------')
names != 'Bob'
print(data[~(names == 'Bob')]) #将names数组映射到data多维数组中,输出不是'Bob'所在的数组(第2、3、5、6行)
print('----------------------------------------------------------4------')
mask = (names == 'Bob') | (names == 'Will') #将names数组映射到data多维数组中,输出是'Bob'和'Will'所在的数组(第1、3、4、5、7行)
print(data[mask])
[[ 0.05764287 0.5674934 0.25030112 0.23331812]
[ 0.46116589 0.42389381 0.99367302 0.3413934 ]
[ 0.62409006 0.53980935 0.20271535 0.93853548]
[ 0.26932701 0.96266169 0.26549336 0.2710549 ]
[ 0.83258971 0.85671927 0.66177204 0.17250801]
[ 0.2554702 0.73446975 0.27736334 0.58976844]
[ 0.17684312 0.28060143 0.33985248 0.08767378]]
----------------------------------------------------------1------
[[ 0.05764287 0.5674934 0.25030112 0.23331812]
[ 0.26932701 0.96266169 0.26549336 0.2710549 ]
[ 0.17684312 0.28060143 0.33985248 0.08767378]]
----------------------------------------------------------2------
[[ 0.25030112 0.23331812]
[ 0.26549336 0.2710549 ]
[ 0.33985248 0.08767378]]
----------------------------------------------------------3------
[[ 0.46116589 0.42389381 0.99367302 0.3413934 ]
[ 0.62409006 0.53980935 0.20271535 0.93853548]
[ 0.83258971 0.85671927 0.66177204 0.17250801]
[ 0.2554702 0.73446975 0.27736334 0.58976844]]
----------------------------------------------------------4------
[[ 0.05764287 0.5674934 0.25030112 0.23331812]
[ 0.62409006 0.53980935 0.20271535 0.93853548]
[ 0.26932701 0.96266169 0.26549336 0.2710549 ]
[ 0.83258971 0.85671927 0.66177204 0.17250801]
[ 0.17684312 0.28060143 0.33985248 0.08767378]]
data=data.astype(np.string_)
data[names == 'Bob'] = 'Bob' #将names中'Bob'映射到data数组中的数据改为'Bob'
data[names == 'Joe'] = 'Joe'
data[names == 'Will'] = 'Will'
data
array([[b'Bob', b'Bob', b'Bob', b'Bob'],
[b'Joe', b'Joe', b'Joe', b'Joe'],
[b'Will', b'Will', b'Will', b'Will'],
[b'Bob', b'Bob', b'Bob', b'Bob'],
[b'Will', b'Will', b'Will', b'Will'],
[b'Joe', b'Joe', b'Joe', b'Joe'],
[b'Bob', b'Bob', b'Bob', b'Bob']],
dtype='|S32')
#花式索引----利用整数数组进行索引
arr = np.empty((8,4))
print(arr)
for i in range(8):
arr[i] = i
print('----------------------------------------------------------1------')
print(arr)
print('----------------------------------------------------------2------')
print(arr[[4,3,0,6]]) #输出数据为4,3,0,6的行
print('----------------------------------------------------------3------')
print(arr[[-3,-5,-7]]) #输出倒数第3行,倒数第5行,倒数第7行
[[ 2.05833592e-312 2.05833592e-312 2.12199579e-312 2.46151512e-312]
[ 9.76118064e-313 2.44029516e-312 2.56761491e-312 2.14321575e-312]
[ 2.33419537e-312 9.76118064e-313 2.46151512e-312 2.22809558e-312]
[ 2.18565567e-312 8.70018275e-313 2.12199579e-312 2.46151512e-312]
[ 1.93101617e-312 2.05833592e-312 2.14321575e-312 6.79038654e-313]
[ 1.29441743e-312 8.27578359e-313 2.35541533e-312 8.27578359e-313]
[ 6.79038654e-313 6.79038653e-313 1.40051722e-312 2.07955588e-312]
[ 2.12199579e-313 2.12199579e-312 2.46151512e-312 4.71294404e+257]]
----------------------------------------------------------1------
[[ 0. 0. 0. 0.]
[ 1. 1. 1. 1.]
[ 2. 2. 2. 2.]
[ 3. 3. 3. 3.]
[ 4. 4. 4. 4.]
[ 5. 5. 5. 5.]
[ 6. 6. 6. 6.]
[ 7. 7. 7. 7.]]
----------------------------------------------------------2------
[[ 4. 4. 4. 4.]
[ 3. 3. 3. 3.]
[ 0. 0. 0. 0.]
[ 6. 6. 6. 6.]]
----------------------------------------------------------3------
[[ 5. 5. 5. 5.]
[ 3. 3. 3. 3.]
[ 1. 1. 1. 1.]]
arr1 = np.arange(32).reshape((8,4))
print(arr1)
print('----------------------------------------------------------1------')
print(arr1[[1,5,7,2],[0,3,1,2]]) #输出下标为(1,0),(5,3),(7,1),(2,2)的数据
print('----------------------------------------------------------2------')
print(arr1[[1,5,7,2]][:, [0,3,1,2]]) #输出下标为1,5,7,2的行。下标为1的行,对应下标为0的列。下标为5的行,对应下标为3的列。
print('----------------------------------------------------------3------')
print(arr1[np.ix_([1,5,7,2],[0,3,1,2])]) #效果同上
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]
----------------------------------------------------------1------
[ 4 23 29 10]
----------------------------------------------------------2------
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
----------------------------------------------------------3------
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
#数组转置 -> 数组.T
arr = np.arange(15).reshape(3,5)
print(arr)
print('----------------------------------------------------------1------')
print(arr.T)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
----------------------------------------------------------1------
[[ 0 5 10]
[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]]
#改变数据的维度
b = np.arange(24).reshape(2,3,4) #reshape改变维度:一维变多维。
print(b)
print('----------------------------------------------------------1------')
print(b.ravel()) #ravel改变维度:多维变一维。(flatten()也一样)
print('----------------------------------------------------------2------')
b.shape = (6,4) #设置shape也可以直接设置数组的维度。
print(b)
print('----------------------------------------------------------2------')
print(b.resize((12,2))) #resize也可以向shape那样改变维度,但是resize只是临时的。
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
----------------------------------------------------------1------
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
----------------------------------------------------------2------
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]