簡體   English   中英

Numpy:將函數應用於二維矩陣的軸 0

[英]Numpy: applying a function to axis 0 of a 2D matrix

我有函數test_outlier如果(x, y)坐標大於遠離連接兩點的線段的某個threshold值,則返回True ,否則返回False

import numpy as np
def test_outlier(point1: np.ndarray, point2: np.ndarray, point3: np.ndarray, threshold: float) -> bool:
    
    line_connecting_point1_point2 = np.linalg.norm(point1 - point2)
    distance_to_line_from_point3 = (np.cross(point2-point1, point3-point1, axis=0) / np.linalg.norm(point2-point1)).astype(float)

point1 = np.array([[2],[3]])
print(point1.shape)
point1
(2, 1)
array([[2],
       [3]])

point2 = np.array([[5],[7]])
print(point2.shape)
point2
(2, 1)
array([[5],
       [7]])

point3 = np.array([[7],[5]])
print(point3.shape)
point3
(2, 1)
array([[7],
       [5]])

test_outlier(point1, point2, point3, 5)
False

我想將test_outlier()函數傳遞給另一個名為count_outliers()的函數,如果通過(2, N)大小的矩陣傳入可變數量的(x, y)坐標,該函數會計算異常值的數量:

def count_outliers(point1: np.ndarray, point2: np.ndarray, coordinates: np.ndarray, threshold: float) -> int:
    num_outliers = 0
    if np.apply_along_axis(test_outlier(point1, point2, coordinates, threshold), 0, coordinates):
        num_outliers += 1
    return num_outliers

我正在嘗試使用np.apply_along_axis() test_outlier()沿軸 0 應用 test_outlier()。

coordinates定義為:

coordinates = np.array([[7, 3, 9, 30],[5, 17, 10, 500]])
print(coordinates.shape)
coordinates 
(2, 4)
array([[  7,   3,   9,  30],
       [  5,  17,  10, 500]])

調用count_outliers(point1, point2, coordinates, 4)會導致此錯誤:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

在上述情況下,從count_outliers中調用的test_outlier函數將應用於以下坐標對:

(7, 5)
(3, 17)
(9, 10)
(30, 500)

我該怎么做呢?

謝謝!

這是答案:

def count_outliers(point1: np.ndarray, point2: np.ndarray, coordinates: np.ndarray, threshold: float) -> int:
    num_outliers = 0
    for elem in range(coordinates.shape[1]):
        if test_outlier(point1, point2, coordinates[:, elem, None], threshold) is True:
            num_outliers += 1
    return num_outliers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM