Python: quickly generate new arrays from known arrays-numpy

Description of Requirement

When using numpy for data analysis, a common requirement is to generate a new array based on a known array. This problem can be divided into two categories:

  1. Generate sub-arrays based on filter conditions;
  2. Generate a new array according to the transformation conditions (the shape of the new array is the same as the original array)

The following is a brief summary.

Generate subarray

Situation 1

Knowing the array a and several filter conditions conds, it is required to generate a sub-array b from the array a.

Solution:b=a[conds] . Such as b=a[a>0], b=a[(a>=1)|(a<=-2)],b=a[(a>=1)&(a<=3)]

Examples: as follows

# 实例1.1:已知数组a,要求找出所有a>0的元素,然后生成一个新数组。
a = np.arange(-5,5,1)
print('原数组a:',a)
b = a[a>0]
print('实例1结果:',b)

# 实例1.2:已知数组a,要求找出所有a>=1或a<=-2的元素,然后生成一个新数组。
b = a[(a>=1) | (a<=-2)]
print('实例2结果:',b)

# 实例1.3:已知数组a,要求找出所有a>=1并且a<=3的元素,然后生成一个新数组。
b = a[(a>=1) & (a<=3)]
print('实例3结果:',b)

operation result:

原数组a: [-5 -4 -3 -2 -1  0  1  2  3  4]
实例1结果: [1 2 3 4]
实例2结果: [-5 -4 -3 -2  1  2  3  4]
实例3结果: [1 2 3]

Situation 2

It is known that array a and array b (same shape), and the filter condition conds_a for array a. It is required to generate a sub-array c from the array b, and the element ids in the one-to-one correspondence with the element ids of the array a satisfying the filtering conditions.

Solution:

c = b[conds_a],such asc = b[(a>=1) & (a<=3)]

Examples: as follows

a = np.arange(-5,5,1)
b = np.arange(-50,50,10)
print('数组a:',a)
print('数组b:',b)

c =  b[(a>=1) & (a<=3)]
print('新数组c:', c)

operation result:

数组a: [-5 -4 -3 -2 -1  0  1  2  3  4]
数组b: [-50 -40 -30 -20 -10   0  10  20  30  40]
新数组c: [10 20 30]

Transform into new array

Knowing the array a and several transform conditions conds, it is required to generate a new array b (same shape as the original array). Solution:

  • Method 1: np.where(where(condition, [x, y]))
    Use scenario: When there are only two or less transformation conditions, such as Example 2.1. This method is equivalent to: if condition x else y

  • Method 2: np.select(condlist, choicelist, default=0)
    Use scenario: When there are any number of transformation conditions, such as example 2.2. This method is equivalent to:

if condlist[0]:
    choicelist[0]
elif condilist[1]:
    choicelist[1]
...
else:
    default
  • Method 3: np.piecewise(x, condlist, funclist, *args, **kw)
    Use scenario: The same as Method 2, except that the transformation conditions are more complex and cannot be written directly. It must be expressed by a function.

Examples: as follows

# 实例2.1:已知数组a,要求对所有a<0的元素取绝对值,对其他元素设为0,然后生成一个新数组
a = np.arange(-5,5,1)
print(a)
b = np.where(a<0, abs(a),0)
print('实例2.1结果:',b)

# 实例2.2:已知数组a,要求对所有a<0的元素取绝对值,对a=0的元素+100,对a>0的元素平方,然后生成一个新数组
b = np.select([a<0, a==0, a>0], [abs(a), a+100, a**2])
print('实例2.2结果:',b)

operation result:

[-5 -4 -3 -2 -1  0  1  2  3  4]
实例2.1结果: [5 4 3 2 1 0 0 0 0 0]
实例2.2结果: [5 4 3 2 1 100 1 4 916]
Published 47 original articles · Like 33 · Visit 310,000+

Guess you like

Origin blog.csdn.net/kaever/article/details/105362005