1.
Statistical Analysis of a Normally Distributed Dataset Using Python
Aim
To generate a dataset of 100 observations using a normal distribution with mean 60
and standard deviation 15, and to compute the measures of central tendency and
dispersion using Python.
Algorithm
1. Import required Python libraries
2. Set a random seed for reproducibility
3. Generate 100 observations from a normal distribution with
o Mean (μ) = 60
o Standard deviation (σ) = 15
4. Store the data in a Pandas Series
5. Compute measures of central tendency
o Mean
o Median
o Mode
6. Compute measures of dispersion
o Minimum, Maximum
o Range
o Variance
o Standard Deviation
o Quartiles (Q1, Q3)
o Interquartile Range (IQR)
7. Visualize the data using
o Histogram
o Boxplot
Tools Required
Python 3.x
NumPy
Pandas
Matplotlib
SciPy
Source code:
import numpy as np
import pandas as pd
import [Link] as plt
from scipy import stats
# Step 1: Set seed for reproducibility
[Link](42)
# Step 2: Generate data
data = [Link](loc=60, scale=15, size=100)
# Step 3: Convert to Pandas Series
series = [Link](data)
# Step 4: Measures of Central Tendency
mean = [Link]()
median = [Link]()
mode = [Link](series, keepdims=True)[0][0]
# Step 5: Measures of Dispersion
minimum = [Link]()
maximum = [Link]()
range_val = maximum - minimum
variance = [Link](ddof=1)
std_dev = [Link](ddof=1)
q1 = [Link](0.25)
q3 = [Link](0.75)
iqr = q3 - q1
# Display results
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
print("Minimum:", minimum)
print("Maximum:", maximum)
print("Range:", range_val)
print("Variance:", variance)
print("Standard Deviation:", std_dev)
print("Q1:", q1)
print("Q3:", q3)
print("Interquartile Range (IQR):", iqr)
# Step 6: Histogram
[Link](figsize=(8,5))
[Link](series, bins=15, color='skyblue', edgecolor='black')
[Link]("Histogram of Generated Data")
[Link]("Value")
[Link]("Frequency")
[Link]()
# Step 7: Boxplot
[Link](figsize=(6,4))
[Link](series, vert=False)
[Link]("Boxplot of Generated Data")
[Link]("Value")
[Link]()
Output:
Mean: 58.4423022390886
Median: 58.09565562330431
Mode: 20.703823438653835
Minimum: 20.703823438653835
Maximum: 87.78417276763406
Range: 67.08034932898022
Variance: 185.57322606678608
Standard Deviation: 13.622526420117014
Q1: 50.98641494255321
Q3: 66.08928078018093
Interquartile Range (IQR): 15.102865837627718
Output Screen:
Result:
The generated dataset approximately follows a normal distribution centered
around the mean value of 60.
The mean and median are close to each other, confirming the symmetry of
the normal distribution.
The standard deviation is close to the specified value of 15, indicating
appropriate spread.
The histogram shows a bell-shaped curve, while the boxplot confirms the
absence of extreme skewness.
Minor differences in values may occur if the random seed is changed.
[Link] of Mean, Median, and Data Spread for Datasets with Different
Standard Deviations
Aim
To generate two datasets using a normal distribution with the same mean but
different standard deviations, compare their mean and median, and analyze the
data spread using numerical measures and graphical visualization.
Algorithm
1. Import required Python libraries
2. Set a random seed for reproducibility
3. Generate two datasets:
o Dataset 1 with smaller standard deviation
o Dataset 2 with larger standard deviation
4. Store the datasets as Pandas Series
5. Compute:
o Mean
o Median
o Standard deviation
6. Visualize both datasets using:
o Histograms
o Boxplots
7. Compare the results and comment on data spread
Tools Required
Python 3.x
NumPy
Pandas
Matplotlib
Python Code
# Import required libraries
import numpy as np
import pandas as pd
import [Link] as plt
# Set random seed for reproducibility
[Link](42)
# Generate datasets
data1 = [Link](loc=60, scale=10, size=100) # Smaller spread
data2 = [Link](loc=60, scale=20, size=100) # Larger spread
# Convert to Pandas Series
series1 = [Link](data1)
series2 = [Link](data2)
# Compute statistics
mean1, median1, std1 = [Link](), [Link](), [Link]()
mean2, median2, std2 = [Link](), [Link](), [Link]()
# Display results
print("Dataset 1 (Std Dev = 10)")
print("Mean:", mean1)
print("Median:", median1)
print("Standard Deviation:", std1)
print("\nDataset 2 (Std Dev = 20)")
print("Mean:", mean2)
print("Median:", median2)
print("Standard Deviation:", std2)
# Plot histograms
[Link](figsize=(10,5))
[Link](1,2,1)
[Link](series1, bins=15, color='lightgreen', edgecolor='black')
[Link]("Histogram - Dataset 1")
[Link]("Values")
[Link]("Frequency")
[Link](1,2,2)
[Link](series2, bins=15, color='salmon', edgecolor='black')
[Link]("Histogram - Dataset 2")
[Link]("Values")
[Link]("Frequency")
plt.tight_layout()
[Link]()
# Plot boxplots
[Link](figsize=(6,4))
[Link]([series1, series2], labels=['Dataset 1', 'Dataset 2'])
[Link]("Boxplot Comparison of Data Spread")
[Link]("Values")
[Link]()
Output
Dataset 1 (Std Dev = 10)
Mean: 58.96153482605906
Median: 58.73043708220287
Standard Deviation: 9.081684280078006
Dataset 2 (Std Dev = 20)
Mean: 60.44609174099847
Median: 61.68214339893669
Standard Deviation: 19.07337932366207
/tmp/[Link]: MatplotlibDeprecationWarning: The 'labels'
parameter of boxplot() has been renamed 'tick_labels' since Matplotlib 3.9; support
for the old name will be dropped in 3.11.
[Link]([series1, series2], labels=['Dataset 1', 'Dataset 2'])
Output Screen
Result
Both datasets have approximately the same mean and median, as they
were generated with the same population mean.
Dataset 1 has a smaller standard deviation, indicating data points are more
tightly clustered around the mean.
Dataset 2 has a larger standard deviation, indicating a wider spread and
more variability.
The histograms show Dataset 2 is more spread out, while Dataset 1 is more
concentrated.
The boxplot clearly shows Dataset 2 has a larger interquartile range and
longer whiskers.
[Link] of Bell Curves for Two Normal Distributions Using a Single
Histogram Scale
Aim
To plot two histograms corresponding to different normal distributions on the same
scale and compare their bell curves to understand the effect of standard deviation
on data spread.
Algorithm
1. Import required Python libraries
2. Set a random seed for reproducibility
3. Generate two datasets using normal distributions with:
o Same mean
o Different standard deviations
4. Plot both datasets on the same histogram scale
5. Analyze and compare the shapes of the bell curves
Tools Required
Python 3.x
NumPy
Matplotlib
Python Code
# Import required libraries
import numpy as np
import [Link] as plt
# Set random seed for reproducibility
[Link](42)
# Generate datasets
data1 = [Link](loc=60, scale=10, size=1000) # Smaller SD
data2 = [Link](loc=60, scale=20, size=1000) # Larger SD
# Compute statistics for Dataset 1
mean1 = [Link](data1)
median1 = [Link](data1)
std1 = [Link](data1, ddof=1)
var1 = [Link](data1, ddof=1)
# Compute statistics for Dataset 2
mean2 = [Link](data2)
median2 = [Link](data2)
std2 = [Link](data2, ddof=1)
var2 = [Link](data2, ddof=1)
# Display results
print("Dataset 1 (Standard Deviation = 10)")
print("Mean:", mean1)
print("Median:", median1)
print("Standard Deviation:", std1)
print("Variance:", var1)
print("\nDataset 2 (Standard Deviation = 20)")
print("Mean:", mean2)
print("Median:", median2)
print("Standard Deviation:", std2)
print("Variance:", var2)
# Plot histograms on the same scale
[Link](figsize=(8,5))
[Link](data1, bins=30, alpha=0.6, label='Std Dev = 10', density=True)
[Link](data2, bins=30, alpha=0.6, label='Std Dev = 20', density=True)
[Link]("Comparison of Bell Curves for Two Normal Distributions")
[Link]("Values")
[Link]("Density")
[Link]()
[Link]()
Output
Dataset 1 (Standard Deviation = 10)
Mean: 60.193320558223256
Median: 60.25300612234888
Standard Deviation: 9.792159381796756
Variance: 95.88638535851024
Dataset 2 (Standard Deviation = 20)
Mean: 61.41672474498312
Median: 61.26154264658092
Standard Deviation: 19.94908754454842
Variance: 397.96609386005684
Output Screen
Result
Both histograms are centered around the same mean, showing symmetry
typical of normal distributions.
The distribution with smaller standard deviation (σ = 10) produces a taller
and narrower bell curve, indicating data is closely clustered around the
mean.
The distribution with larger standard deviation (σ = 20) produces a shorter
and wider bell curve, indicating greater variability.
Plotting both histograms on the same scale makes the difference in spread
clearly visible.
[Link] Analysis of a Uniformly Distributed Dataset Using Python
Aim
To generate a dataset using a uniform distribution and compute its mean,
variance, and standard deviation using Python.
Algorithm
1. Import required Python libraries
2. Set a random seed for reproducibility
3. Generate a dataset using a uniform distribution within a specified range
4. Compute:
o Mean
o Variance
o Standard Deviation
5. Visualize the data using a histogram
6. Interpret the results
Tools Required
Python 3.x
NumPy
Matplotlib
Python Code
# Import required libraries
import numpy as np
import [Link] as plt
# Set random seed
[Link](42)
# Generate uniform distribution data
# Values are generated between 20 and 80
data = [Link](low=20, high=80, size=1000)
# Compute statistics
mean = [Link](data)
variance = [Link](data, ddof=1)
std_dev = [Link](data, ddof=1)
# Display results
print("Uniform Distribution Statistics")
print("-------------------------------")
print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", std_dev)
# Plot histogram
[Link](figsize=(8,5))
[Link](data, bins=30, color='cornflowerblue', edgecolor='black')
[Link]("Histogram of Uniformly Distributed Data")
[Link]("Values")
[Link]("Frequency")
[Link]()
Output
Uniform Distribution Statistics
-------------------------------
Mean: 49.41539319920802
Variance: 307.2392576842912
Standard Deviation: 17.52824171684916
Output Screen
Result
The mean is approximately the midpoint of the uniform distribution range.
The variance and standard deviation indicate that data points are evenly
spread across the range.
The histogram shows a nearly flat shape, confirming the uniform distribution.
[Link] of Outliers Using Boxplot
Aim
To draw a boxplot for a given dataset and identify the presence of outliers using
Python.
Algorithm
1. Import required Python libraries
2. Define or load the given dataset
3. Compute quartiles and interquartile range (IQR)
4. Plot the boxplot
5. Identify outliers based on the IQR rule
6. Interpret the result
Tools Required
Python 3.x
NumPy
Matplotlib
Python Code
# Import libraries
import numpy as np
import [Link] as plt
# Given dataset
data = [12, 15, 14, 16, 18, 19, 20, 21, 22, 23, 24, 25, 30, 35, 100]
# Convert to NumPy array
data = [Link](data)
# Calculate quartiles and IQR
Q1 = [Link](data, 25)
Q3 = [Link](data, 75)
IQR = Q3 - Q1
# Outlier boundaries
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# Identify outliers
outliers = data[(data < lower_bound) | (data > upper_bound)]
# Display results
print("Q1:", Q1)
print("Q3:", Q3)
print("IQR:", IQR)
print("Lower Bound:", lower_bound)
print("Upper Bound:", upper_bound)
print("Outliers:", outliers)
# Plot Boxplot
[Link](figsize=(12,5))
[Link](1,2,1)
[Link](data, vert=False, patch_artist=True, boxprops=dict(facecolor='lightblue'))
[Link]("Boxplot of Dataset")
[Link]("Values")
# Plot Histogram
[Link](1,2,2)
[Link](data, bins=10, color='lightgreen', edgecolor='black')
[Link]("Histogram of Dataset")
[Link]("Values")
[Link]("Frequency")
plt.tight_layout()
[Link]()
Output
Q1: 17.0
Q3: 24.5
IQR: 7.5
Lower Bound: 5.75
Upper Bound: 35.75
Outliers: [100]
Output Screen
Result
The boxplot visually displays the median, quartiles, and spread of the
dataset.
Any data points lying outside the whiskers are considered outliers.
From the computation, values such as 100 fall beyond the upper bound and
are identified as outliers.
.