python NumPy library study notes two

python NumPy library study notes two

introduction

Continue to learn numpy library.

The main contents and index sections NumPy

NumPy and slice index

Ndarray content objects may be accessed and modified by an index or slices, as with the slicing operation list in Python.

ndarray array based on 0 - n subscript for the index, the object slice by slice built-in functions, and set the start, stop, and step parameters, a new array cut from the original array.

Slicing through the slice () function

slice () function is a built-in functions, slice () function to achieve the object slices, mainly used in the slicing operation in the function parameter.

import numpy as np
a = np.arange(10)
s = slice(2,7,2)

print(a)
[0 1 2 3 4 5 6 7 8 9]

print(a[s])
[2 4 6]

By colon :and ...sliced

Sections may be separated by a colon parameters start: stop: step for slicing operation:

import numpy as np
 
a = np.arange(10)  
b = a[2:7:2]   # 从索引 2 开始到索引 7 停止,间隔为 2
print(b)
# out [2  4  6]

Colon: explanation: If you place only one parameter, such as [2] , returns a single element corresponding to the index. If [2:] , indicating that all items will be retrieved later from the beginning of the index. If the two parameters, such as : [72] entry, then extracting two indexes (not including the stop index) between.

a = np.arange(10)  # [0 1 2 3 4 5 6 7 8 9]

b = a[5]

print(b)
5

print(a[2:])
[2 3 4 5 6 7 8 9]

print(a[2:5])
[2 3 4]

The same applies multidimensional arrays the index extraction method:

Slice may further include the ellipsis ..., to make the length of the same dimension of the array of selected tuples. If the line position of an ellipsis, it returns ndarray comprising rows of elements.

a = np.array([[1,2,3],[3,4,5],[4,5,6]])

print(a[1:])
[[3 4 5]
 [4 5 6]]

print (a[...,1])   # 第2列元素
[2 4 5]

print (a[1,...])   # 第2行元素
[3 4 5]

print (a[...,1:])  # 第2列及剩下的所有元素
[[2 3]
 [4 5]
 [5 6]]

Usage colon sections, sections were incubated with R somewhat similar language usage data frame.

NumPy advanced indexing

In addition to the above described integer index and slice, the array can be indexed by fancy Boolean and integer index array index.

Integer array index

Integer array the array index of an existing, returns a ndarray object containing the elements taken.

import numpy as np 
# 以下实例获取数组中(0,0),(1,1)和(2,0)位置处的元素。
x = np.array([[1,  2],  [3,  4],  [5,  6]]) 
y = x[[0,1,2],  [0,1,0]]  
print (y)
Out[164]:[1 4 5]
type(y)
Out[165]: numpy.ndarray
    
# 以下实例获取数组4*3数组的四个角的元素。    
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])
rows = np.array([[0,0],[3,3]]) 
cols = np.array([[0,2],[0,2]]) 
y = x[rows,cols]
print (y)
[[ 0  2]
 [ 9 11]]

type(y)
Out[169]: numpy.ndarray
type(rows)
Out[170]: numpy.ndarray

#结合: 和...进行索引
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
b = a[1:3, 1:3]
c = a[1:3,[1,2]]
d = a[...,1:]
print(b)
print(c)
print(d)

Boolean Index

Boolean index by Boolean operators: to get an array of elements that meet the specified conditions (such as comparison operators).

# 以下实例获取大于 5 的元素:
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  
print (x[x >  5])

# 以下实例使用了 ~(取补运算符)来过滤 NaN。
a = np.array([np.nan,  1,2,np.nan,3,4,5])  
print (a[~np.isnan(a)])

# 以下实例演示如何从数组中过滤掉非复数元素。
a = np.array([1,  2+6j,  5,  3.5+5j])  
print (a[np.iscomplex(a)])

Fancy index

Fancy index refers to the use integer index into the array.

Fancy index value based on the value of the array index as a subscript of an axis of the target array. For a one-dimensional array of integers as an index, if the goal is one-dimensional array, then the result is that index element corresponding to the location; if the goal is a two-dimensional array, the corresponding target is the next line.

Fancy indexes do not like a slice, it always copies the data to the new array.

x=np.arange(32).reshape((8,4))
print(x)
[[ 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. 传入顺序索引数组,即取相应的行
print (x[[4,2,1,7]])
[[16 17 18 19]
 [ 8  9 10 11]
 [ 4  5  6  7]
 [28 29 30 31]]

# 2. 传入倒序索引数组,即倒着数取相应的行
print (x[[-4,-2,-1,-7]])
[[16 17 18 19]
 [24 25 26 27]
 [28 29 30 31]
 [ 4  5  6  7]]

# 3. 传入多个索引数组(要使用np.ix_),即第一个数组是对应的行,第二个数组是对应的列
print (x[np.ix_([1,5,7,2],[0,3,1,2])])
[[ 4  7  5  6]
 [20 23 21 22]
 [28 31 29 30]
 [ 8 11  9 10]]

Guess you like

Origin www.cnblogs.com/songbiao/p/12449510.html