72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
import numpy as np
|
|
"""
|
|
This script demonstrates various statistical operations on NumPy arrays, including mean, median, variance, and standard deviation calculations for both 1D and 2D arrays.
|
|
Features:
|
|
- Calculates mean and median for samples with odd and even numbers of elements.
|
|
- Sorts arrays for display purposes.
|
|
- Computes sample variance (using Bessel's correction) and standard deviation.
|
|
- Performs mean calculations on 2D arrays, both flattened and along specific axes.
|
|
- Simulates exam scores for two students and computes per-student mean, overall median, variance, and standard deviation.
|
|
Dependencies:
|
|
- numpy
|
|
Usage:
|
|
Run the script to see printed outputs of statistical calculations for various sample arrays and a simulated exam scores dataset.
|
|
"""
|
|
|
|
sample = np.array([10, 25, 15, 30, 20, 10, 2])
|
|
print(f'Original sample: {sample}, Odd # of elements:')
|
|
# Calculating the mean
|
|
sample_mean = np.mean(sample)
|
|
print(f'Sorted sample: {np.sort(sample)}')
|
|
# Calculating the median
|
|
sample_median = np.median(sample)
|
|
print(f'Mean: {sample_mean}, median: {sample_median}')
|
|
|
|
|
|
sample = np.array([1, 2, 8, 10, 15, 20, 25, 30])
|
|
print(f'\nOriginal sample: {sample}, Even # of elements:')
|
|
sample_mean = np.mean(sample)
|
|
sample_median = np.median(sample)
|
|
# Sorting the sample
|
|
print(f'Sorted sample: {np.sort(sample)}')
|
|
print(f'Mean: {sample_mean}, median: {sample_median}')
|
|
|
|
|
|
sample = np.array([10, 25, 15, 30, 20, 10, 2])
|
|
print(f'\nOriginal sample: {sample}, Odd # of elements:')
|
|
# Calculating the variance
|
|
sample_variance = np.var(sample, ddof=1)
|
|
# Calculating the standard deviation
|
|
sample_std = np.std(sample)
|
|
print(f'Variance: {sample_variance}, Standard Deviation: {sample_std}')
|
|
|
|
#Higher dimensional array calculations
|
|
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
|
|
print(f'\n2D Array:\n{array_2d}')
|
|
# Calculating the mean in a flattened array
|
|
print(f'Mean (flattened): {np.mean(array_2d)}')
|
|
# Calculating the mean along axis 0
|
|
print(f'Mean (axis 0): {np.mean(array_2d, axis=0)}')
|
|
# Calculating the mean along axis 1
|
|
print(f'Mean (axis 1): {np.mean(array_2d, axis=1)}')
|
|
|
|
# Task: Simulated test scores of 2 students for five different exams
|
|
exam_scores = np.array([[85, 90, 78, 92, 88], [72, 89, 65, 78, 92]])
|
|
print(f'\nExam Scores:\n{exam_scores}')
|
|
|
|
# Calculate the mean score for each student
|
|
mean_scores = np.mean(exam_scores, axis=1)
|
|
print(f'Mean score for each student: {mean_scores}')
|
|
# Calculate the median score of all scores
|
|
median_score = np.median(exam_scores)
|
|
print(f'Median score of all exams: {median_score}')
|
|
|
|
# Calculate the median score of all scores
|
|
median_score = np.median(exam_scores)
|
|
print(f'Median score for all scores: {median_score}')
|
|
# Calculate the variance of all scores
|
|
scores_variance = np.var(exam_scores)
|
|
print(f'Variance for all scores: {scores_variance}')
|
|
# Calculate the standard deviation of all scores
|
|
scores_std = np.std(exam_scores)
|
|
print(f'Standard deviation for all scores: {scores_std}') |