【问题标题】:replacing value with median in python在python中用中位数替换值
【发布时间】:2017-02-16 13:02:10
【问题描述】:
lat
50.63757782
50.6375742
50.6375742
50.6374077762
50.63757782
50.6374077762
50.63757782
50.63757782

我用这些纬度值绘制了一个图表,并注意到图表中出现了突然的峰值(异常值)。我想用最后三个值的中值替换每个 lat 值,以便我可以看到有意义的结果

输出可能是

lat               lat_med
50.63757782 50.63757782
50.6375742  50.6375742
50.6375742  50.6375742
50.63740778 50.6375742
50.63757782 50.6375742
50.63740778 50.6375742
50.63757782 50.6375742
50.63757782 50.6375742

我有数千个这样的 lat 值,需要使用 for 循环来解决这个问题。我知道下面的代码有错误,由于我是 python 的初学者,感谢您帮助解决这个问题。

for i in range(0,len(df['lat'])):
    df['lat_med'][i]=numpy.median(numpy.array(df['lat'][i],df['lat'][i-2]))

我刚刚意识到三点的中位数计算不符合我的目的,我需要考虑五个值。有没有办法改变我想要的值的中值函数。谢谢你的帮助

def median(a, b, c):
    if a > b and a > c:
        return b if b > c else c

    if a < b and a < c:
        return b if b < c else c

    return a

【问题讨论】:

  • 您想用median(lat[i-2], lat[i-1], lat[i]) 替换lat[i]?为什么不median(lat[i-1], lat[i], lat[i+1])?无论如何,使用任一公式,您的数组边界都会出现问题:如果i = 0,则lat[i-1] 不存在。您可以在循环中做一些聪明的事情来处理这些情况,或者在循环之外做一些简单的事情来处理它们。

标签: python numpy replace median imputation


【解决方案1】:

只需考虑倒数第二个元素,然后将中间值保存在这个、上一个和下一个元素中。请注意,第一个和最后一个元素保持原样。

试试这个:

lat = [50.63757782, 50.6375742, 50.6375742, 50.6374077762, 50.63757782, 50.6374077762, 50.63757782, 50.63757782]

# returns median value out of the three values
def median(a, b, c):
    if a > b and a > c:
        return b if b > c else c

    if a < b and a < c:
        return b if b < c else c

    return a


# add the first element
filtered = [lat[0]]

for i in range(1, len(lat) - 1):
    filtered += [median(lat[i - 1], lat[i], lat[i + 1])]

# add the last element
filtered += [lat[-1]]

print(filtered)

你正在做的是一个非常基本的Median filter

【讨论】:

  • 嗨马丁,我刚刚意识到三点的中位数计算不符合我的目的,我需要考虑五个值。有没有办法改变我想要的值的中值函数。感谢您的帮助。
【解决方案2】:

您似乎在使用pandas'Dataframe 结构,所以:

import pandas as pd
import numpy as np

df = pd.DataFrame({'lat' : [50.63757782,
                            50.6375742,
                            50.6375742,
                            50.6374077762,
                            50.63757782,
                            50.6374077762,
                            50.63757782,
                            50.63757782]})

def replace_values_with_medians(array):
    last = array.shape[0]-2
    index = 0
    result = np.zeros(last)
    while index < last:
        result[index] = np.median(array[index:index+3])
        index += 1
    return result

lat_med_df = pd.DataFrame({'lat_med':replace_values_with_medians(df['lat'])})
df = pd.concat([df,lat_med_df], axis = 1)
del lat_med_df

结果:

>>> df
         lat    lat_med
0  50.637578  50.637574
1  50.637574  50.637574
2  50.637574  50.637574
3  50.637408  50.637408
4  50.637578  50.637578
5  50.637408  50.637578
6  50.637578        NaN
7  50.637578        NaN

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2021-02-27
    • 2019-11-08
    • 2019-07-19
    • 2019-04-22
    • 2019-01-11
    • 2018-04-21
    • 2021-10-12
    • 2019-05-03
    相关资源
    最近更新 更多