How to load Iris Dataset in Python?
We can simply access the Iris dataset using the ‘load_iris’ function from the ‘sklearn.datasets’ module. This function allows us to load the Iris dataset and then we call the load_iris() function and store the returned dataset object in the variable named ‘iris’. The object contains the whole dataset including features and target variable.
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
# Access the features and target variable
X = iris.data # Features (sepal length, sepal width, petal length, petal width)
y = iris.target # Target variable (species: 0 for setosa, 1 for versicolor, 2 for virginica)
# Print the feature names and target names
print("Feature names:", iris.feature_names)
print("Target names:", iris.target_names)
# Print the first few samples in the dataset
print("First 5 samples:")
for i in range(5):
print(f"Sample {i+1}: {X[i]} (Class: {y[i]}, Species: {iris.target_names[y[i]]})")
Output:
Feature names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Target names: ['setosa' 'versicolor' 'virginica']
First 5 samples:
Sample 1: [5.1 3.5 1.4 0.2] (Class: 0, Species: setosa)
Sample 2: [4.9 3. 1.4 0.2] (Class: 0, Species: setosa)
Sample 3: [4.7 3.2 1.3 0.2] (Class: 0, Species: setosa)
Sample 4: [4.6 3.1 1.5 0.2] (Class: 0, Species: setosa)
Sample 5: [5. 3.6 1.4 0.2] (Class: 0, Species: setosa)
Iris Dataset
The Iris dataset is one of the most well-known and commonly used datasets in the field of machine learning and statistics. In this article, we will explore the Iris dataset in deep and learn about its uses and applications.
Contact Us