上一章节知识点通道:ndarray多维数组知识点
import numpy as np
arr = np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[3]
3
arr[-1]
9
arr[2]=123
arr
array([ 0, 1, 123, 3, 4, 5, 6, 7, 8, 9])
arr[3]=88
arr
array([ 0, 1, 123, 88, 4, 5, 6, 7, 8, 9])
arr1 = arr[-3:-1] #左闭右开
arr1
array([7, 8])
arr1[:] = 77 #将上面得到的[7,8]改成[77,77]
arr
array([ 0, 1, 123, 88, 4, 5, 6, 77, 77, 9])
arr
array([ 0, 1, 123, 88, 4, 5, 6, 77, 77, 9])
arr1 = arr[1].copy()
arr1
1
arr1 =34
arr1
34
arr
array([ 0, 1, 123, 88, 4, 5, 6, 77, 77, 9])
arr = np.arange(15).reshape(3,5)
arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
arr[0]
array([0, 1, 2, 3, 4])
arr[2]
array([10, 11, 12, 13, 14])
arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
arr[0][3]
3
arr[2,3] #俩种办法等价
13
arr = np.arange(12).reshape(2,2,3)
arr
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
arr[0]
array([[0, 1, 2],
[3, 4, 5]])
old = arr[0].copy()
arr[0]= 12
arr
array([[[12, 12, 12],
[12, 12, 12]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
arr[0] = old
arr
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
arr
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
arr[1,1]
array([ 9, 10, 11])
arr[0,1,2]
5
arr = np.arange(6)
arr
array([0, 1, 2, 3, 4, 5])
arr[2:5]
array([2, 3, 4])
arr = np.arange(12).reshape(4,3)
arr
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
arr[2:]
array([[ 6, 7, 8],
[ 9, 10, 11]])
arr
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
arr[:,1] #参考数据合并块知识点
array([ 1, 4, 7, 10])
arr[:,1:2] #首先0,1,2,3四行逗号隔开2列重合部分(左闭右开)
array([[ 1],
[ 4],
[ 7],
[10]])
arr[2:,1:] #首先2,3俩行接着逗号隔开1,2俩行重合部分
array([[ 7, 8],
[10, 11]])
fruits = np.array(['apple','banana','pear','banana','pear','apple','pear'])
datas = np.random.randint(-1,1,size=(7,5))
fruits
array(['apple', 'banana', 'pear', 'banana', 'pear', 'apple', 'pear'],
dtype='
datas
array([[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1],
[ 0, 0, 0, -1, 0],
[-1, -1, -1, 0, 0],
[ 0, 0, -1, -1, -1],
[-1, 0, -1, 0, 0],
[ 0, -1, -1, -1, -1]])
fruits == 'pear'
array([False, False, True, False, True, False, True])
datas[False, False, True, False, True, False, True]
array([], shape=(0, 7, 5), dtype=int32)
datas[[False, False, True, False, True, False, True]] #根据布尔值,取出布尔值为1的行,取1 4 7行。
array([[ 0, 0, 0, -1, 0],
[ 0, 0, -1, -1, -1],
[ 0, -1, -1, -1, -1]])
datas[[0, 0, 1, 0, 1, 0, 1]] #无法识别0,1
array([[ 0, 0, -1, 0, 0],
[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1],
[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1],
[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1]])
datas[fruits == 'pear'] #datas取出值为真的行数
array([[ 0, 0, 0, -1, 0],
[ 0, 0, -1, -1, -1],
[ 0, -1, -1, -1, -1]])
datas[fruits != 'pear']
array([[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1],
[-1, -1, -1, 0, 0],
[-1, 0, -1, 0, 0]])
datas[(fruits == 'apple')|(fruits == 'banana')]
array([[ 0, 0, -1, 0, 0],
[ 0, -1, 0, -1, -1],
[-1, -1, -1, 0, 0],
[-1, 0, -1, 0, 0]])
datas[fruits == 'pear',2:]
array([[ 0, -1, 0],
[-1, -1, -1],
[-1, -1, -1]])
datas[fruits == 'pear',2]
array([ 0, -1, -1])
datas[datas == 0] = 1
datas
array([[ 1, 1, -1, 1, 1],
[ 1, -1, 1, -1, -1],
[ 1, 1, 1, -1, 1],
[-1, -1, -1, 1, 1],
[ 1, 1, -1, -1, -1],
[-1, 1, -1, 1, 1],
[ 1, -1, -1, -1, -1]])
arr[[1,3,2]]
array([[ 3, 4, 5],
[ 9, 10, 11],
[ 6, 7, 8]])
arr[[3,2]][:,[2,1]]
array([[11, 10],
[ 8, 7]])
arr[np.ix_([3,2],[2,1])]
array([[11, 10],
[ 8, 7]])