python随机数据_python自动化分析提取随机数据

问题背景:

我们在测试性能的时候,一般会提取一组数据,但是这组数据里面会有一些异常数组,需要我们能剔除一些异常值,保证输出的数据尽量可靠,通过一些分析处理方法,可以将这一过程程序化。

如何判断一组数据是否稳定?

数学中使用方差或者标准差来表示一组数据的稳定性。如果方差或者标准差越小,说明数据分布越平均,数据的稳定性越好。

方差,标准差

但是这儿不能采用方差与标准差,因为应用的场景不同:方差与标准差是判断当前的一组数据是否稳定,并不是提取出正常的测试数据,换言之,我们需要提取的测试数据,并不是越稳定越好,我们需要的是一种正常的分布。而且方差和标准差的订多少标准比较合适,这个界限比较模糊,所以我还是建议使用中位数来界定。

下面是我提出了一个提取测试数据的方法,大家可以参考下:

先看看我们的要求:输入的数据至少20位,输出的数据10位。

这样我们一定要剔除一些过大或者过小的数据,判断的标准就是中位数。这个中位数是以平均数标识的。

下面先贴上我的处理代码:

test_camera.py

# -*- coding:UTF-8 -*-

# Author : jeffmony@163.com

# Date : 24/12/18

import sys

import numpy

# 寻找 average 在 list 中的索引位置

def findIndex( list, average ):

for index in range(len(list)):

if( list[index] < average):

continue;

return index;

return -1;

# 退出程序

def sysExit():

try:

sys.exit(0);

finally:

print 'System Exit';

# 完成工作

def sysFinsih():

try:

sys.exit(0);

finally:

print 'System Finished';

def OutputList( list , length ):

average = numpy.mean(list);

print '当前数组长度 =', length,',当前list为 =', list;

index = findIndex(list, average)

print '当前数组平均数 =',average,',当前average.index =',index;

tempList = [];

if length >= 14:

if index <= length/2+2 and index >= length/2-2:

for i in range(index -5,index):

tempList.append(list[i]);

for i in range(index, index+5):

tempList.append(list[i]);

print tempList;

sysFinsih();

elif index < length/2 -2:

list.pop(0);

OutputList(list, len(list));

elif index > length/2 +2:

list.pop();

OutputList(list, len(list));

else:

sysExit();

else:

print '当前数组异常值太多,请重新check input';

argv = sys.argv[1]

list = []

file_object = open(argv, 'rU')

try:

for line in file_object:

list.append(float(line.strip('\n')));

finally:

file_object.close()

length = len(list);

if length <= 14:

print '当前数组大小不超过14,请重新check input';

sysExit();

list.sort();

OutputList(list, length);

将需要处理的数据放在本地文本中:

test_camera.txt

1595

1465

1387

1397

1405

1376

1377

1404

1483

1522

1265

1257

1193

1200

1236

1246

1289

1208

1269

1309

下面是运行实例:

python test_camera.py test_camera.txt

当前数组长度 = 20 ,当前list为 = [1193.0, 1200.0, 1208.0, 1236.0, 1246.0, 1257.0, 1265.0, 1269.0, 1289.0, 1309.0, 1376.0, 1377.0, 1387.0, 1397.0, 1404.0, 1405.0, 1465.0, 1483.0, 1522.0, 1595.0]

当前数组平均数 = 1344.15 ,当前average.index = 10

[1257.0, 1265.0, 1269.0, 1289.0, 1309.0, 1376.0, 1377.0, 1387.0, 1397.0, 1404.0]

System Finished

python test_camera.py test_camera2.txt

当前数组长度 = 20 ,当前list为 = [9.89, 10.09, 10.99, 12.23, 12.33, 14.85, 16.02, 16.8, 17.56, 18.11, 18.24, 21.45, 21.56, 23.14, 26.71, 28.21, 34.11, 52.66, 77.22, 89.11]

当前数组平均数 = 26.564000000000004 ,当前average.index = 14

当前数组长度 = 19 ,当前list为 = [9.89, 10.09, 10.99, 12.23, 12.33, 14.85, 16.02, 16.8, 17.56, 18.11, 18.24, 21.45, 21.56, 23.14, 26.71, 28.21, 34.11, 52.66, 77.22]

当前数组平均数 = 23.272105263157897 ,当前average.index = 14

当前数组长度 = 18 ,当前list为 = [9.89, 10.09, 10.99, 12.23, 12.33, 14.85, 16.02, 16.8, 17.56, 18.11, 18.24, 21.45, 21.56, 23.14, 26.71, 28.21, 34.11, 52.66]

当前数组平均数 = 20.275000000000002 ,当前average.index = 11

[16.02, 16.8, 17.56, 18.11, 18.24, 21.45, 21.56, 23.14, 26.71, 28.21]

System Finished

上面加粗的是最终输出的数据。

这个处理方式虽然比较粗糙,但是提供了一种思路。欢迎指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值