0% found this document useful (0 votes)
36 views1 page

Sample

The document contains Python code that analyzes the Iris dataset using pandas and manual calculations for mean, median, and mode for each species. It groups the data by species and computes statistics for each feature. The results are printed for each species with the calculated values formatted to two decimal places.

Uploaded by

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

Sample

The document contains Python code that analyzes the Iris dataset using pandas and manual calculations for mean, median, and mode for each species. It groups the data by species and computes statistics for each feature. The results are printed for each species with the calculated values formatted to two decimal places.

Uploaded by

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

# import numpy as np

# import pandas as pd
# from sklearn.datasets import load_iris

# iris = load_iris()
# df = pd.DataFrame(iris.data, columns=iris.feature_names)
# df['species'] = iris.target

# df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})

# grouped = df.groupby('species')

# for species, group in grouped:


# print(f"Statistics for {species}:\n")

# for column in df.columns[:-1]: # Skip the 'species' column


# # Manual calculation of mean
# total_sum = 0
# count = len(group[column])
# for value in group[column]:
# total_sum += value
# mean_value = total_sum / count

# # Manual calculation of median


# sorted_values = sorted(group[column])
# if count % 2 == 1: # Odd number of elements
# median_value = sorted_values[count // 2]
# else: # Even number of elements
# median_value = (sorted_values[count // 2 - 1] +
sorted_values[count // 2]) / 2

# # Manual calculation of mode


# value_counts = {}
# for value in group[column]:
# value_counts[value] = value_counts.get(value, 0) + 1
# mode_value = max(value_counts, key=value_counts.get)

# # Print the results


# print(f"{column} - Mean: {mean_value:.2f}, Median: {median_value:.2f},
Mode: {mode_value:.2f}")
# print("\n")

You might also like