python高级应用与数据处理学习笔记 09
1、索引、切片
import numpy as np
# 索引
a = np.random.random((2,3,4))
print(a)
# [[[ 0.24669554 0.41147959 0.58667011 0.16186598]
# [ 0.02656519 0.91044857 0.73566317 0.86165827]
# [ 0.95063107 0.86839605 0.55965123 0.38786474]]
#
# [[ 0.14023812 0.75629064 0.81219621 0.87785726]
# [ 0.57575656 0.24517288 0.02211154 0.16691134]
# [ 0.22878503 0.5652508 0.19264238 0.21291273]]]
# 对单个的索引
print(a[0][1][1]) #0.91044857166
print(a[0,1,1]) #0.91044857166
# 对多个的索引
print(a[0][0][1:3])
print(a[0][1][1:3])
print(a[0][2][1:3])
# 上面的3部操作等同于
print(a[0,:,1:3])
# 上面的3部操作等同于
print(a[0][:].T[1:3].T)
# 布尔值的索引
# 布尔值的索引:利用布尔值型的数组进行数据索引,最终返回的结果是对应索引数组中数据为True位置的值
# 注意:
# 1、True位置的元素取出来形成一个新数组,是一维数组
# 2、索引数组和布尔型数组的shape必须一致
# 3、常用于数据清洗
a = np.random.random((4,4,3))
print('a的值为:=========================')
print(a)
b = a > 0.5
print('b的值为:=========================')
print(b)
c = a[b]
print('c的值为:=========================')
print(c)
# a的值为:=========================
# [[[ 0.82845288 0.98329758 0.43359219]
# [ 0.12132049 0.70090391 0.72885301]
# [ 0.50032925 0.26513678 0.96059203]
# [ 0.09782446 0.51226214 0.26209529]]
#
# [[ 0.16773934 0.68813356 0.43629845]
# [ 0.82266855 0.46399805 0.50791978]
# [ 0.27159864 0.93022272 0.56654779]
# [ 0.01313173 0.11657045 0.47096045]]
#
# [[ 0.06509039 0.91746951 0.39954364]
# [ 0.3960294 0.65175321 0.57412961]
# [ 0.23939818 0.7203695 0.15798556]
# [ 0.23249503 0.10869684 0.65794804]]
#
# [[ 0.97224345 0.5668863 0.24372173]
# [ 0.52432717 0.54046684 0.01262262]
# [ 0.02243452 0.6662223 0.05360318]
# [ 0.56587212 0.20596584 0.6398141 ]]]
# b的值为:=========================
# [[[ True True False]
# [False True True]
# [ True False True]
# [False True False]]
#
# [[False True False]
# [ True False True]
# [False True True]
# [False False False]]
#
# [[False True False]
# [False True True]
# [False True False]
# [False False True]]
#
# [[ True True False]
# [ True True False]
# [False True False]
# [ True False True]]]
# c的值为:=========================
# [ 0.82845288 0.98329758 0.70090391 0.72885301 0.50032925 0.96059203
# 0.51226214 0.68813356 0.82266855 0.50791978 0.93022272 0.56654779
# 0.91746951 0.65175321 0.57412961 0.7203695 0.65794804 0.97224345
# 0.5668863 0.52432717 0.54046684 0.6662223 0.56587212 0.6398141 ]
import numpy as np
# 花式索引:利用整数数组进行索引的方式
a = np.arange(24).reshape((4,-1))
print('a的值为:=======================')
print(a)
print('a[1:3]的值为:=======================')
print(a[1:3]) #切片只能用于连续的数据,如果要切不连续的话,那就要用到花式索引了
print('花式索引的应用:=======================')
print('a[[0,3]]:=======================')
print(a[[0,3]])
print('a[[0,3],[0,3]]:=======================')
print(a[[0,3],[0,3]])
print('a[np.ix_([0,3],[0,3])]:=======================')
print(a[np.ix_([0,3],[0,3])])
print('a[[0,3].T[[0,3]].T]:=======================')
print(a[[0,3]].T[[0,3]].T)
# a的值为:=======================
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
# a[1:3]的值为:=======================
# [[ 6 7 8 9 10 11]
# [12 13 14 15 16 17]]
# 花式索引的应用:=======================
# [[ 0 1 2 3 4 5]
# [18 19 20 21 22 23]]
2、转置
import numpy as np
a = np.arange(24).reshape((2,3,4))
print('a的值为:==================')
print(a)
# 转置的第一种方法
# b = a.T
# 转置的第二种方法
# b = np.transpose(a)
# 转置的第三种方法
b = a.transpose()
print('b,b.shape的值为:==================')
print(b,b.shape)