With the recent advancement in AI, scikit-learn has become the most popular and essential tools for data scientists and machine learning engineers to develop resilient machine learning models.
Scikit-learn offers a range of algorithms for supervised, unsupervised and reinforcement learning algorithms which include non-linear, linear, ensemble, association, clustering, dimension reduction model and so much more.
It also provides evaluation, scaling, and selection tools to ensure you select the best models for a given objective.
How to install scikit-learn
To install scikit-learn, you can use PyPI or conda
Ps: this installation works for all platform(Windows, MacOS, linux)
Pypi:
To install scikit-learn via PyPI, open your terminal and run the command below
$ pip install -U scikit-learn
Conda:
To install scikit-learn via conda use the command below.
$ conda create -n sklearn-env -c conda-forge scikit-learn
$ Conda activate sklearn-env
Scikit learn example
Let’s create a simple linear regression model with scikit-learn
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import numpy as np
# Generate some data for the example
np.random.seed(0)
X = np.random.rand(1000, 1)
y = 9 + 6 * X + np.random.rand(1000, 1)
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Create a LinearRegression object and fit it to the training data
reg = LinearRegression()
reg.fit(X_train, y_train)
# Make predictions on the test set
y_pred = reg.predict(X_test)
# Print the coefficient and intercept of the fitted model
print("Coefficient: ", reg.coef_)
print("Intercept: ", reg.intercept_)
This code above uses synthetics data, perform cross validation, and fit the model to linear regression then compute for coefficient and the intercept of the model.
Scikit-learn in action: