简体   繁体   English

如何从 numpy 数组中删除两个最小数字并计算中位数 python 3

[英]How do I remove two smallest numbers from a numpy array and calculate median python 3

I am generating a 5x3 random array.我正在生成一个 5x3 随机数组。 I need to identify and remove the smallest two numbers and then calculate the median with rest of the numbers.我需要识别并删除最小的两个数字,然后用数字的 rest 计算中位数。 I am able to identify the smallest number, but unable to proceed further.我能够识别最小的数字,但无法继续进行。

import numpy as Np

a = Np.random.randint(100, size = (5,3))
print(a)
print('\n')

sort1 = Np.concatenate(a, axis=0)
print("Concatenated: ", sort1)
print('\n')
sort2 = Np.sort(sort1)
print(sort2)
print('\n')

c = Np.min(sort2,0) #min
print(c)
print('\n')

med = Np.median(sort2)
print("Median is: ", med)

You can do it like this:你可以这样做:

a = Np.random.randint(100, size = (5,3))
a = Np.delete(a, a.argmin())
a = Np.delete(a, a.argmin())
Np.median(a)

# 48.0

Well, you managed to get the smallest number, now you need to remove it.好吧,您设法获得了最小的数字,现在您需要将其删除。

I would create a list, you don't actually need an array to calculate the median.我会创建一个列表,您实际上并不需要一个数组来计算中位数。 After creating the list it gets easier, you remove the items with list.remove() and the median function stays the same.创建列表后变得更容易,您使用 list.remove() 删除项目,中位数 function 保持不变。

Your code will look like this:您的代码将如下所示:

import numpy as Np

a = Np.random.randint(100, size = (5,3))


sort1 = list(Np.concatenate(a, axis=0))

sort1.remove(min(sort1))

med = Np.median(sort1)

sort1.remove(min(sort1))

med2 = Np.median(sort1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我有多个最小数字并且想要两个索引,如何在python数组中找到最小数字的索引? - How do I find the Index of the smallest number in an array in python if I have multiple smallest numbers and want both indexes? 如何从numpy数组中获取两个最小值 - How to get the two smallest values from a numpy array Python:如何在不使用 median() 调用的情况下读取数字的 .txt 文件以查找所述数字的中位数? - Python: How do I read a .txt file of numbers to find the median of said numbers without using median() call? 如何计算 python 中的 PSD、中值频率和平均频率? - How do I calculate PSD, Median Frequency and Mean Frequency in python? 如何获得数组的 10 个最小数字? - How do I get the 10 smallest numbers of an array? 如何从 numpy 中的数组中删除数组? - How do I remove an array from an array in numpy? 从多个 numpy arrays 中删除最小的 p% 数字 - Remove smallest p% numbers from multiple numpy arrays 如何使用 python/numpy 计算百分位数? - How do I calculate percentiles with python/numpy? Python:如何计算文件中的数字总和? - Python: How do I calculate the sum of numbers from a file? 从numpy数组中删除数字 - Remove numbers from a numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM