import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from sklearn.datasets import load_iris
try: iris = load_iris() df = pd.DataFrame(data=iris.data, columns=[col.strip().replace(" ", "_") for col in iris.feature_names]) df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names) print("Dataset loaded successfully.\n") except Exception as e: print(f"Error loading dataset: {e}")
print("First 5 rows of the dataset:") print(df.head())
print("\nData types:") print(df.dtypes)
print("\nMissing values:") print(df.isnull().sum())
print("\nDescriptive statistics:") print(df.describe())
grouped = df.groupby("species").mean() print("\nAverage values per species:") print(grouped)
sns.set(style="whitegrid")
plt.figure(figsize=(8, 5)) df.groupby("species").mean().T.plot(kind='line', marker='o') plt.title("Mean Feature Values per Species") plt.ylabel("Mean Value") plt.xlabel("Feature") plt.grid(True) plt.legend(title="Species") plt.tight_layout() plt.show()
plt.figure(figsize=(6, 4)) sns.barplot(data=df, x="species", y="petal_length", errorbar=None) plt.title("Average Petal Length per Species") plt.ylabel("Petal Length (cm)") plt.xlabel("Species") plt.show()
plt.figure(figsize=(6, 4)) sns.histplot(df["sepal_length_(cm)"], bins=20, kde=True) plt.title("Distribution of Sepal Length") plt.xlabel("Sepal Length (cm)") plt.ylabel("Frequency") plt.show()
plt.figure(figsize=(6, 4)) sns.scatterplot(data=df, x="sepal_length_(cm)", y="petal_length", hue="species") plt.title("Sepal Length vs Petal Length by Species") plt.xlabel("Sepal Length (cm)") plt.ylabel("Petal Length (cm)") plt.legend(title="Species") plt.show()