Now that you've learned about Scikit-learn's purpose, installed the library, and familiarized yourself with its API structure and data requirements, it's time to ensure everything is set up correctly. This practical exercise will walk you through verifying your Scikit-learn installation and loading one of its sample datasets. Running this code successfully confirms that your environment is ready for building machine learning models.
First, let's confirm that Python can find your Scikit-learn installation and check its version. Open your Python interpreter, Jupyter Notebook, or IDE and run the following code:
import sklearn
# Print the installed version
print(f"Scikit-learn version: {sklearn.__version__}")
You should see the installed version number printed without any ImportError
messages. The exact version might differ, but seeing a version number (e.g., 1.2.2
or higher) indicates success. If you encounter an error, please revisit the "Installation and Environment Setup" section to ensure Scikit-learn and its dependencies (like NumPy and SciPy) were installed correctly in your active Python environment.
Next, let's try loading one of the built-in datasets discussed earlier. The load_iris
function from sklearn.datasets
provides the classic Iris dataset, often used for introductory classification examples.
from sklearn.datasets import load_iris
import numpy as np
# Load the Iris dataset
iris = load_iris()
# The loaded object is a 'Bunch' object, similar to a dictionary
print(f"Keys of iris dataset: {iris.keys()}")
# The data is typically stored in .data and the target labels in .target
# These are usually NumPy arrays
print(f"Shape of data: {iris.data.shape}")
print(f"Shape of target: {iris.target.shape}")
# Let's look at the first 5 samples and their corresponding targets
print("\nFirst 5 samples (features):\n", iris.data[:5])
print("\nFirst 5 targets (labels):\n", iris.target[:5])
# Feature names and target names are also often included
print(f"\nFeature names: {iris.feature_names}")
print(f"\nTarget names: {iris.target_names}")
Executing this code should output:
iris
object (like data
, target
, target_names
, feature_names
, DESCR
).data
array (typically (150, 4), representing 150 samples and 4 features).target
array (typically (150,), representing the class label for each sample).If both code snippets run without errors and produce output similar to that described, your Scikit-learn environment is correctly configured. You have successfully imported the library and loaded data in the format it expects. You are now ready to proceed to the next chapters, where you'll start using Scikit-learn to build and evaluate machine learning models.
© 2025 ApX Machine Learning