當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy.compress()用法及代碼示例


numpy.compress(condition,array,axis = None,out = None):沿提到的軸返回滿足條件的軸的選定數組切片。
參數:

condition : [array_like]Condition on the basis of which user extract elements. 
      Applying condition on input_array, if we print condition, it will return an arra
      filled with either True or False. Array elements are extracted from the Indices having 
      True value.
array     : Input array. User apply conditions on input_array elements
axis      : [optional, int]Indicating which slice to select. 
         By Default, work on flattened array[1-D]
out       : [optional, ndarray]Output_array with elements of input_array, 
               that satisfies condition

返回:

Copy of array with elements of input_array, that satisfies condition and along given axis
# Python Program illustrating 
# numpy.compress method 
  
import numpy as geek 
  
array = geek.arange(10).reshape(5, 2) 
print("Original array : \n", array) 
  
a = geek.compress([0, 1], array, axis=0) 
print("\nSliced array : \n", a) 
  
a = geek.compress([False, True], array, axis=0) 
print("\nSliced array : \n", a)

輸出:


Original array : 
 [[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

Sliced array : 
 [[2 3]]

Sliced array : 
 [[2 3]]


相關用法


注:本文由純淨天空篩選整理自 numpy.compress() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。