How to find the mean and median of given data?
I am currently engaged as a data analyst at a retail company. My team is evaluating the sales data of the different stores in my region to understand their performance. I have sales figures from the ten stores for the last two months:
- Store A - $20,000
- Store B- $18,000
- Store C-$15,000
- Store D-$22,000
- Store E-$21,000
- Store F-$19,000
- Store G-$17,000
- Store H- $25,000
- Store I-$500,000
- Store J-$19,000
Given this data, how can I calculate the mean and median sales for these stores?
In the context of data science, you can calculate the mean and median for the sales data:-
Mean calculations
The mean which is also known as the average can be calculated by summing all the values and dividing by the number of observations:
Sales_data = [20000, 18000, 15000, 22000, 21000, 19000, 17000, 25000, 500000, 19000]
Mean_sales = sum(sales_data) / len(sales_data)
Print(f”Mean sales: {mean_sales}”)
Median Calculations
The median is the middle value in a sorted list of the numbers. If there is an even number of observation, then it is the average of the two middle numbers:
Sorted_sales = sorted(sales_data)
N = len(sorted_sales)
If n % 2 == 1:
Median_sales = sorted_sales[n // 2]
Else:
Median_sales = (sorted_sales[n // 2 – 1] + sorted_sales[n // 2]) / 2
Print(f”Median sales: {median_sales}”)
In this above-given scenario, the mean sales ($81,900) are heavily influenced by the outliers which makes it higher than most of the individual sales. The median sales ($19,500), however, can give a more representative middle value of the sales data which can be unaffected by extreme outliers.
Here is the example given in Java programming language which would demonstrate how you can calculate the mean and median for any given dataset:
Import java.util.Arrays;
Public class SalesStatistics {
Public static void main(String[] args) {
// Sales data for ten stores
Int[] salesData = {20000, 18000, 15000, 22000, 21000, 19000, 17000, 25000, 500000, 19000};
// Calculate mean
Double meanSales = calculateMean(salesData);
System.out.println(“Mean sales: “ + meanSales);
// Calculate median
Double medianSales = calculateMedian(salesData);
System.out.println(“Median sales: “ + medianSales);
}
// Method to calculate mean
Public static double calculateMean(int[] data) {
Int sum = 0;
For (int num : data) {
Sum += num;
}
Return (double) sum / data.length;
}
// Method to calculate median
Public static double calculateMedian(int[] data) {
// Sort the array
Arrays.sort(data);
Int n = data.length;
If (n % 2 == 1) {
// Odd number of elements
Return data[n / 2];
} else {
// Even number of elements
Return (data[n / 2 – 1] + data[n / 2]) / 2.0;
}
}
}