Importing Scikit-Learn in Jupyter Notebook
Learn how to import scikit-learn, a powerful machine learning library, in Jupyter Notebook and unlock its full potential for data analysis and modeling. …
Updated June 8, 2023
Learn how to import scikit-learn, a powerful machine learning library, in Jupyter Notebook and unlock its full potential for data analysis and modeling.
What is Scikit-Learn?
Scikit-learn is an open-source machine learning library for Python that provides a wide range of algorithms for classification, regression, clustering, dimensionality reduction, model selection, and more. It’s built on top of NumPy, SciPy, and matplotlib, making it a great companion to these libraries.
What is Jupyter Notebook?
Jupyter Notebook is an interactive environment for writing and executing code in Python (and other languages). It provides a web-based interface that allows you to create notebooks with cells containing text, code, equations, and visualizations. This makes it ideal for data analysis, machine learning, and scientific computing.
Importing Scikit-Learn in Jupyter Notebook
To import scikit-learn in your Jupyter Notebook, follow these steps:
Step 1: Install Scikit-Learn
Before importing scikit-learn, you need to install it. You can do this using pip, the Python package manager:
!pip install -U scikit-learn
The !
symbol indicates that this is a shell command.
Step 2: Import Scikit-Learn in Your Notebook
Now that scikit-learn is installed, you can import it in your Jupyter Notebook using the following code:
import sklearn
from sklearn import *
Note: You can also use from sklearn import
followed by a specific module (e.g., datasets
, metrics
) to import only what you need.
Step 3: Verify the Import
To ensure that scikit-learn was imported correctly, run the following code:
print(sklearn.__version__)
This should print the version of scikit-learn that you just installed.
Example Use Case
Here’s a simple example of using scikit-learn to perform classification on the Iris dataset:
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Load the Iris dataset
iris = datasets.load_iris()
# Split the data into features and target
X, y = iris.data, iris.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Logistic Regression model
model = LogisticRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = model.predict(X_test)
# Evaluate the model's accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Model Accuracy:", accuracy)
This example demonstrates how to import scikit-learn, load a dataset (in this case, Iris), split it into training and testing sets, create a model (Logistic Regression), train it on the training data, make predictions on the testing data, and evaluate its accuracy.
Conclusion
Importing scikit-learn in your Jupyter Notebook is a straightforward process that involves installing the library and importing it using Python code. With this powerful machine learning library at your fingertips, you can unlock a wide range of algorithms for classification, regression, clustering, dimensionality reduction, model selection, and more.