Numpy科学计算库----进阶知识1

一、索引

一维数组的索引

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)  #(3, 2, 4)
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(1,...) 三个点
print(d3[0,1,::2])    #第一层,第二行,间隔相取 => ['后羿' '狄仁杰']

print(d3[0,::-1,-1])  #['鲁班' '亚瑟']  
                      #[::-1]反着取全部行数的数值   -1是步长
print(d3[0,:,-1])     #['亚瑟' '鲁班']
print(d3[::-1])       #所有层数据倒列

二、数组维度转换

数组拓展纬度

#方法一
import numpy as np
line=np.arange(24)
#将line形状改成2层3行4列
result=line.reshape(2,3,4)  #不修改数组本身,返回新的数组
print(line)
print(result)

#方法二
line.shape=(2,3,4)          #shape 修改了数组本身
print(line)

#方法三
line.resize(2,4,3)          #resize 也是修改数字的形状本身
print(line)

print(line.resize(24,))     #变回

置换行列

import numpy as np
arr=np.arange(24).reshape(4,6)
print(arr)

#将4行6列转成6行4列
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  #所有元素乘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   按水平方向拼接数组
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   按行进行拼接,和列拼接vstack一样
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)

你可能感兴趣的:(Numpy科学计算库----进阶知识1)