Tuesday, August 26, 2025

🐍Loading built-in datasets (Iris, Digits) in skitlearn

In this section, you will learn how to load and explore built-in datasets in Scikit-learn. We'll focus on the popular Iris and Digits datasets.

🔹 Loading Iris Dataset

The Iris dataset is a classic dataset in machine learning. It contains 150 samples of iris flowers, with 4 features (sepal length, sepal width, petal length, petal width) and a target (species).


from sklearn.datasets import load_iris
import pandas as pd

# Load the dataset
iris = load_iris()

# Features
X = pd.DataFrame(iris.data, columns=iris.feature_names)

# Target
y = pd.Series(iris.target, name='species')

# Preview data
print(X.head())
print(y.head())
    

👉 This code loads the dataset and converts it into a Pandas DataFrame for easy exploration.

🔹 Loading Digits Dataset

The Digits dataset contains 1797 images of handwritten digits (0–9) represented as 8x8 pixel arrays. This dataset is commonly used for classification tasks.


from sklearn.datasets import load_digits
import pandas as pd

# Load the dataset
digits = load_digits()

# Features
X_digits = pd.DataFrame(digits.data)

# Target
y_digits = pd.Series(digits.target, name='digit')

# Preview data
print(X_digits.head())
print(y_digits.head())
    

👉 This loads the digit images and converts them into a DataFrame where each column represents a pixel value.

🖥️ Practice in Browser

No comments:

Post a Comment

🐍What is scikitlearn??