0% found this document useful (0 votes)
9 views6 pages

ML Lab Programs

The document outlines Python programs for computing central tendency measures (mean, median, mode) and measures of dispersion (variance, standard deviation) using a dataset. It also discusses basic Python libraries such as statistics, math, numpy, and scipy, providing examples of their usage. Additionally, it covers the Pandas and Matplotlib libraries for data manipulation and visualization, demonstrating how to create a dataset and plot scores of students in Math and Science.

Uploaded by

sandysri321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

ML Lab Programs

The document outlines Python programs for computing central tendency measures (mean, median, mode) and measures of dispersion (variance, standard deviation) using a dataset. It also discusses basic Python libraries such as statistics, math, numpy, and scipy, providing examples of their usage. Additionally, it covers the Pandas and Matplotlib libraries for data manipulation and visualization, demonstrating how to create a dataset and plot scores of students in Math and Science.

Uploaded by

sandysri321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write a Python program to compute Central Tendency Measures (Mean, Median, Mode) and
Measure of Dispersion (Variance, Standard Deviation) for a given dataset.

Explanation:
Sum of all values
Mean: The average of all numbers. Formula: Mean = Number of values

Median: The middle value when data is sorted. If even number of values, median = average of two
middle values.

Mode: The most frequently occurring value(s).


∑(𝑥𝑖 −Mean)2
Variance: Measures how far data points spread out from the mean. Formula: Variance = 𝑛

Standard Deviation (SD): Square root of variance. It shows how much data deviates from the mean.

Example Dataset:
data = [10, 15, 20, 20, 25, 30, 35]

Mean = (10+15+20+20+25+30+35)/7 = 22.14

Median = 20 (middle value)

Mode = 20 (appears twice)

Variance ≈ 62.86

Standard Deviation ≈ 7.93

Program:
# Program to compute Central Tendency and Dispersion Measures

import statistics as stats

# Example dataset

data = [10, 15, 20, 20, 25, 30, 35]

# Central Tendency Measures

mean_value = [Link](data) # Mean

median_value = [Link](data) # Median

mode_value = [Link](data) # Mode

# Measure of Dispersion

variance_value = [Link](data) # Variance


std_dev_value = [Link](data) # Standard Deviation

# Output results

print("Dataset:", data)

print("Mean:", mean_value)

print("Median:", median_value)

print("Mode:", mode_value)

print("Variance:", variance_value)

print("Standard Deviation:", std_dev_value)

Output:
Dataset: [10, 15, 20, 20, 25, 30, 35]
Mean: 22.142857142857142

Median: 20
Mode: 20

Variance: 62.857142857142854
Standard Deviation: 7.928141129387872
2. Study of Python Basic Libraries such as Statistics, Math, Numpy and Scipy
Explanation:

• statistics library: Provides functions for mean, median, mode, variance, and standard
deviation. Example: [Link]([1,2,3]) → 2

• math library: Provides mathematical functions like square root, power, factorial,
trigonometric functions. Example: [Link](16) → 4

• numpy library: Powerful library for numerical computing, arrays, and vectorized operations.
Example: [Link]([1,2,3]) → array([1,2,3])

• scipy library: Builds on numpy, provides advanced mathematical, scientific, and statistical
functions. Example: [Link].ttest_ind([1,2,3],[4,5,6]) → t-test result

Example Dataset:

data = [10, 15, 20, 20, 25, 30, 35]

Program:
# Program to study Python basic libraries: statistics, math, numpy, scipy

import statistics as stats

import math

import numpy as np

from scipy import stats as scipy_stats

# Example dataset

data = [10, 20, 30, 40, 50]

# --- Statistics library ---

print("Mean (statistics):", [Link](data))

# --- Math library ---

print("Square root of 16 (math):", [Link](16))

# --- Numpy library ---

arr = [Link](data)

print("Numpy mean:", [Link](arr))


print("Numpy sum:", [Link](arr))

# --- Scipy library ---

# Compare two datasets with t-test

data2 = [12, 22, 32, 42, 52]

t_stat, p_val = scipy_stats.ttest_ind(data, data2)

print("Scipy t-test statistic:", t_stat)

print("Scipy p-value:", p_val)

Output:

Mean (statistics): 30

Square root of 16 (math): 4.0

Numpy mean: 30.0

Numpy sum: 150

Scipy t-test statistic: -0.4472135954999579

Scipy p-value: 0.667551


3. Study of Python Libraries for ML application such as Pandas and Matplotlib

Explanation with Example

• Pandas: Provides powerful data structures like DataFrame for handling tabular data.
Example: [Link]() computes column-wise mean values.

• Matplotlib: A plotting library used to visualize data. Example: [Link](x, y) creates a line
graph.

Example Dataset:

Creating a small dataset of students’ scores in Math and Science using Pandas, then visualize it with
Matplotlib

Program:

# Program to study Pandas and Matplotlib libraries

import pandas as pd

import [Link] as plt

# Create a small dataset using Pandas

data = {

'Student': ['A', 'B', 'C', 'D', 'E'],

'Math': [85, 90, 78, 92, 88],

'Science': [80, 85, 82, 95, 89]

df = [Link](data)

# Display basic statistics

print("Dataset:\n", df)

print("\nMean Scores:\n", [Link](numeric_only=True))

# Plot using Matplotlib

[Link](df['Student'], df['Math'], color='blue', label='Math')

[Link](df['Student'], df['Science'], color='green', alpha=0.6, label='Science')

[Link]('Students')

[Link]('Scores')
[Link]('Students Performance in Math & Science')

[Link]()

[Link]()

Output:
Dataset:

Student Math Science

0 A 85 80

1 B 90 85

2 C 78 82

3 D 92 95

4 E 88 89

Mean Scores:

Math 86.6

Science 86.2

dtype: float64

You might also like