Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find mean of array after removing some elements in Python
Suppose we have an array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements.
So, if the input is like nums = [2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8], then the output will be 4.0 because after removing smallest and largest values, all remaining elements are the same.
Algorithm
To solve this problem, we will follow these steps ?
Sort the array
numsn:= size ofnumsper:= quotient of(n * 5 / 100)trimmed_array:= subarray ofnumsfrom indexperto(n - per - 1)mean:= average of all elements intrimmed_arrayReturn
mean
Example
Let us see the following implementation to get better understanding ?
def solve(nums):
nums.sort()
n = len(nums)
per = int(n * 5 / 100)
trimmed_array = nums[per : n - per]
mean = sum(trimmed_array) / len(trimmed_array)
return mean
nums = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8]
print("Original array:", nums)
print("Mean after removing 5% from each end:", solve(nums))
Original array: [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8] Mean after removing 5% from each end: 4.0
How It Works
Let's trace through the example step by step ?
Original array:
[2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8](20 elements)After sorting: Already sorted in this case
Calculate 5%:
per = int(20 * 5 / 100) = 1Remove elements: Remove first 1 element (2) and last 1 element (8)
Trimmed array:
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4](18 elements)Calculate mean:
sum(18 fours) / 18 = 72 / 18 = 4.0
Alternative Implementation
Here's a more concise version using Python's statistics module ?
import statistics
def solve_alternative(nums):
nums.sort()
n = len(nums)
per = int(n * 5 / 100)
trimmed_array = nums[per : n - per]
return statistics.mean(trimmed_array)
nums = [6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0]
print("Mean after trimming:", solve_alternative(nums))
Mean after trimming: 4.0
Conclusion
This trimmed mean approach removes outliers by discarding the extreme 5% values from both ends. It's useful for getting a more robust average that's less affected by outliers than the regular arithmetic mean.
