一、索引
一维数组的索引
import numpy as np
heros=np.array(["韩信","赵云","孙尚香","孙悟空","阿珂"],dtype="U10")
print(heros[2])
二维数组的索引
heros2=np.array([["狄仁杰","廉颇","埃希"],
["苏列","程咬金","后羿"]],dtype="U5")
print(heros2.shape)
#获取“廉颇”
print(heros2[0][1])
一维数组的切片
print(heros[:2])
print(heros[3:]) #['孙悟空' '阿珂']
print(heros[-2:]) #['孙悟空' '阿珂']
print(heros[-4:-1])#['赵云' '孙尚香' '孙悟空']
print(heros[-2:-5])#[] 不等价
二维数组的切片
print(heros2[0])#获取第一行数据
print(heros2[1])
print(heros2[:,1])#['廉颇' '程咬金'] #“,”后面的表示取列的位置
print(heros2[:,:2])#[['狄仁杰' '廉颇'] #后面的表示取列区间
# ['苏列' '程咬金']]
三维数组的索引及切片
import numpy as np
d3=np.array([[
["苏烈","程咬金","廉颇","亚瑟"],
["后羿","公孙离","狄仁杰","鲁班"]
],
[
["王昭君","安其拉","貂蝉","小乔"],
["孙膑","大乔","鬼谷子","蔡文姬"]
],
[
["凯","刘邦","孙悟空","刘备"],
["项羽","刘禅","庄周","东皇太一"]
]
])
print(d3.shape)
print(d3)
print(d3[2][1][2])
print(d3[2,1,2])
print(d3[0])
print(d3[1])
print(d3[2])
print(d3[:,1,1])
print(d3[0,:,:])
print(d3[0,1,::2])
print(d3[0,::-1,-1])
print(d3[0,:,-1])
print(d3[::-1])
二、数组维度转换
数组拓展纬度
import numpy as np
line=np.arange(24)
result=line.reshape(2,3,4)
print(line)
print(result)
line.shape=(2,3,4)
print(line)
line.resize(2,4,3)
print(line)
print(line.resize(24,))
置换行列
import numpy as np
arr=np.arange(24).reshape(4,6)
print(arr)
Tarr=arr.transpose()
print(Tarr)
三、数组组合的方式
import numpy as np
vstack 按垂直方向拼接数组
hstack 按水平方向拼接数组
column_stack 按列方向进行合并=hstack
row_stack 按行进行拼接=vstack
concatnate
dstack
arr1=np.arange(9).reshape(3,3)
print(arr1)
arr2=arr1*2
print(arr2)
3.1vstack按垂直方向拼接数组
# vstack 按垂直方向拼接数组 vertical
result=np.vstack(
(arr1,arr2)
)
print(result)
'''
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
3.2hstack按水平方向拼接数组
hstack=np.hstack((arr1,arr2))
print(hstack)
'''结果果如下
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
3.3column_stack按列方向进行合并
# column_stack 按列方向进行合并 ==按水平拼接hstack
column_stack=np.column_stack((arr1,arr2))
print(column_stack)
'''
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
3.4row_stack行进行拼接
row_stack=np.row_stack((arr1,arr2))
print(row_stack)
concatnate
m1=np.arange(9).reshape(3,3)
print(m1)
m2=m1*2
print(m2)
concatenate=np.concatenate((m1,m2),axis=0)#上下连接
#行合并 行竖向叠加
row_stack=np.row_stack((m1,m2))
vstack=np.vstack((m1,m2))
print("-------------------")
print("axis=0:\n:",concatenate)
'''
axis=0:
: [[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
print("-------------------")
print("vstack:\n",vstack)
'''
vstack:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
print("-------------------")
print("row_stack:\n",row_stack)
'''
row_stack:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
print("********************")
#列合并 行紧跟其后
hstack()
column_stack()
concatenate()
concatenate2=np.concatenate((m1,m2),axis=1)
print("axis=1:\n",concatenate2)
'''
axis=1:
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
print("-------------------")
hstack2=np.hstack((m1,m2))
column_stack=np.column_stack((m1,m2))
print("hstack:\n",hstack2)
'''
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
print("-------------------")
print("column_stack:\n",column_stack)
'''
column_stack:
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
dstack
#深度合并 dstack
print("------------------------------------------")
dstack=np.dstack((m1,m2))
'''
[[[ 0 0]
[ 1 2]
[ 2 4]]
[[ 3 6]
[ 4 8]
[ 5 10]]
[[ 6 12]
[ 7 14]
[ 8 16]]]
shape----------> (3, 3, 2)
'''
print(dstack)
print("shape---------->",dstack.shape) #(3, 3, 2) 加一个元素就是3,3,3
#shape----------> (3, 3, 2)