Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历numpy数组并消除异常?_Python_Arrays_Numpy - Fatal编程技术网

Python 如何遍历numpy数组并消除异常?

Python 如何遍历numpy数组并消除异常?,python,arrays,numpy,Python,Arrays,Numpy,我是Python和编程的初学者。我正试图编写一个程序,迭代一个特定的numpy数组,并检测数据集中的异常(异常的定义是任何一个点,它大于没有数据点的平均值的标准偏差的3倍)。每次删除异常数据点时,我都需要重新计算平均值和标准偏差 我已经写了下面的代码,但是注意到了几个问题。循环迭代一次后,它表示160的值已被删除,但当我打印新的_数组时,仍然在数组中看到160 此外,如何重新计算每次删除数据点时的新平均值?我觉得有些东西在for循环中的位置不正确。最后,我对continue的使用是正确的还是应该

我是Python和编程的初学者。我正试图编写一个程序,迭代一个特定的numpy数组,并检测数据集中的异常(异常的定义是任何一个点,它大于没有数据点的平均值的标准偏差的3倍)。每次删除异常数据点时,我都需要重新计算平均值和标准偏差

我已经写了下面的代码,但是注意到了几个问题。循环迭代一次后,它表示160的值已被删除,但当我打印新的_数组时,仍然在数组中看到160

此外,如何重新计算每次删除数据点时的新平均值?我觉得有些东西在for循环中的位置不正确。最后,我对continue的使用是正确的还是应该放在其他地方

import numpy as np

data_array = np.array([
    99.5697438 ,  94.47019021,  55., 106.86672855,
   102.78730151, 131.85777845,  88.25376895,  96.94439838,
    83.67782174, 115.57993209, 118.97651966,  94.40479467,
    79.63342207,  77.88602065,  96.59145004,  99.50145353,
    97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
   110.0687946 , 104.71504012,  89.34719772, 160.,
   110.61519268, 112.94716398, 104.41867586])

for cell in data_array:
    mean = np.mean(data_array, axis=0)
    sd = np.std(data_array, axis=0)
    lower_anomaly_point = mean - (3 * sd)
    upper_anomaly_point = mean + (3 * sd)
    if cell > upper_anomaly_point or cell < lower_anomaly_point:
        print(str(cell) + 'has been removed.')
        new_array = np.delete(data_array, cell)
        continue 
将numpy导入为np
数据_数组=np.array([
99.5697438 ,  94.47019021,  55., 106.86672855,
102.78730151, 131.85777845,  88.25376895,  96.94439838,
83.67782174, 115.57993209, 118.97651966,  94.40479467,
79.63342207,  77.88602065,  96.59145004,  99.50145353,
97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
110.0687946 , 104.71504012,  89.34719772, 160.,
110.61519268, 112.94716398, 104.41867586])
对于数据数组中的单元格:
平均值=np.平均值(数据数组,轴=0)
sd=np.std(数据数组,轴=0)
下异常点=平均值-(3*sd)
上异常点=平均值+(3*sd)
如果单元>上异常点或单元<下异常点:
打印(str(单元格)+“已删除”。)
新建数组=np.delete(数据数组,单元格)
继续

正如@damagedcodda所说,您的主要错误是您应该使用索引而不是值,但如果您在循环内重新计算下限异常点和上限异常点,您将遇到新问题。因此,我建议您尝试np。在何处解决您的任务:

import numpy as np

data_array = np.array([
    99.5697438 ,  94.47019021,  55., 106.86672855,
   102.78730151, 131.85777845,  88.25376895,  96.94439838,
    83.67782174, 115.57993209, 118.97651966,  94.40479467,
    79.63342207,  77.88602065,  96.59145004,  99.50145353,
    97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
   110.0687946 , 104.71504012,  89.34719772, 160.,
   110.61519268, 112.94716398, 104.41867586])

mean = np.mean(data_array, axis=0)
sd = np.std(data_array, axis=0)
lower_anomaly_point = mean - (3 * sd)
upper_anomaly_point = mean + (3 * sd)

data_array = data_array[
    np.where(
        (upper_anomaly_point > data_array) & (data_array > lower_anomaly_point)
    )]
结果是:

array([ 99.5697438 ,  94.47019021,  55.        , 106.86672855,
       102.78730151, 131.85777845,  88.25376895,  96.94439838,
        83.67782174, 115.57993209, 118.97651966,  94.40479467,
        79.63342207,  77.88602065,  96.59145004,  99.50145353,
        97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
       110.0687946 , 104.71504012,  89.34719772, 110.61519268,
       112.94716398, 104.41867586])
那个密码对我来说是失败的。 数据_数组不更改,np.delete返回新数组,它不更改旧数组。 在代码的任何地方都不使用新的_数组,您可能希望从新的_数组计算平均值 delete的第二个参数应该是index,“指示要删除的子数组”。您不能使用单元格

import numpy as np

data_array = np.array([
    99.5697438 ,  94.47019021,  55., 106.86672855,
   102.78730151, 131.85777845,  88.25376895,  96.94439838,
    83.67782174, 115.57993209, 118.97651966,  94.40479467,
    79.63342207,  77.88602065,  96.59145004,  99.50145353,
    97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
   110.0687946 , 104.71504012,  89.34719772, 160.,
   110.61519268, 112.94716398, 104.41867586])

mean = np.mean(data_array, axis=0)
sd = np.std(data_array, axis=0)
lower_anomaly_point = mean - (3 * sd)
upper_anomaly_point = mean + (3 * sd)
new_array = data_array.copy()
k = 0

for i, cell in enumerate(data_array):
    if cell > upper_anomaly_point or cell < lower_anomaly_point:
        print(str(cell) + 'has been removed.')
        new_array = np.delete(new_array, i - k)
        k += 1
将numpy导入为np
数据_数组=np.array([
99.5697438 ,  94.47019021,  55., 106.86672855,
102.78730151, 131.85777845,  88.25376895,  96.94439838,
83.67782174, 115.57993209, 118.97651966,  94.40479467,
79.63342207,  77.88602065,  96.59145004,  99.50145353,
97.25980235,  87.72010069, 101.30597215,  87.3110369 ,
110.0687946 , 104.71504012,  89.34719772, 160.,
110.61519268, 112.94716398, 104.41867586])
平均值=np.平均值(数据数组,轴=0)
sd=np.std(数据数组,轴=0)
下异常点=平均值-(3*sd)
上异常点=平均值+(3*sd)
新建数组=数据数组。复制()
k=0
对于i,枚举(数据数组)中的单元格:
如果单元>上异常点或单元<下异常点:
打印(str(单元格)+“已删除”。)
new_array=np.delete(new_array,i-k)
k+=1
新的_数组是不带160的数据_数组。正如您所希望的那样,我认为您应该看到并参考第一行,其中明确指出它返回所有不符合arr[obj]的元素,这意味着
numpy.delete()
以基于索引的方式工作。 我建议您编辑代码,以便获得该单元格的索引,然后将其传递到
np.delete()

以下是编辑后的代码:

import numpy as np

data_array = np.array([99.5697438, 94.47019021, 55.0, 106.86672855, 102.78730151, 131.85777845, 88.25376895, 96.94439838, 83.67782174, 115.57993209, 118.97651966, 94.40479467, 79.63342207, 77.88602065, 96.59145004, 99.50145353, 97.25980235, 87.72010069, 101.30597215, 87.3110369, 110.0687946, 104.71504012, 89.34719772, 160.0, 110.61519268, 112.94716398, 104.41867586])
print(data_array)
for cell in data_array:
    mean = np.mean(data_array, axis=0)
    sd = np.std(data_array, axis=0)
    lower_anomaly_point = mean - (3 * sd)
    upper_anomaly_point = mean + (3 * sd)
    if cell > upper_anomaly_point or cell < lower_anomaly_point:
        print(str(cell) + 'has been removed.')
        index=np.where(data_array==cell)
        new_array = np.delete(data_array, obj=index)
        continue 
将numpy导入为np
数据_数组=np.array([99.5697438, 94.47019021, 55.0, 106.86672855, 102.78730151, 131.85777845, 88.25376895, 96.94439838, 83.67782174, 115.57993209, 118.97651966, 94.40479467, 79.63342207, 77.88602065, 96.59145004, 99.50145353, 97.25980235, 87.72010069, 101.30597215, 87.3110369, 110.0687946, 104.71504012, 89.34719772, 160.0, 110.61519268, 112.94716398, 104.41867586])
打印(数据数组)
对于数据数组中的单元格:
平均值=np.平均值(数据数组,轴=0)
sd=np.std(数据数组,轴=0)
下异常点=平均值-(3*sd)
上异常点=平均值+(3*sd)
如果单元>上异常点或单元<下异常点:
打印(str(单元格)+“已删除”。)
index=np.where(数据数组==单元格)
新建数组=np.delete(数据数组,obj=索引)
继续

当前代码的问题(即使修复了错误)首先检查的值在删除条目后可能位于新边距之外。这意味着您的代码可能不会始终返回正确的结果。我建议您使用while循环执行这些步骤:只要存在异常值,即在
3*std
边距之外:1.查找异常值,这是最远的远离
mean
并将其删除2.计算新的
mean
std