Programming for Data Science, Lec 12: Machine Learning in Python using scikit-learn (sklearn)

Published: 08 March 2024
on channel: Dr. Data Science
618
7

#machinelearning #datascience #scikitlearn

How to use sklearn?

The workflow for model fitting and prediction using scikit-learn (sklearn) for supervised learning tasks is standardized and straightforward, making it easy to apply across various models and datasets.

1. Choose a Model
Select a suitable model from scikit-learn based on the type of supervised learning problem at hand (regression or classification). For example, `LinearRegression` for a regression problem, or `LogisticRegression` for a binary classification problem.

2. Import the Model
Import the chosen model from the appropriate module within scikit-learn. For example:
```
from sklearn.linear_model import LinearRegression
```
3. Prepare the Data
Your dataset should be divided into features (independent variables) and the target (dependent variable). The features are usually represented as a 2D array (or DataFrame) `X`, and the target as a 1D array `y`.

4. Split the Data
Use `train_test_split` from `sklearn.model_selection` to split your data into training and testing sets. This step is crucial for evaluating the model's performance on unseen data.
```
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

```
5. Initialize the Model
Instantiate the model with any desired parameters. For example:
```
model = LinearRegression()
```
6. Fit the Model
Train the model on your training data by calling the `fit` method.
```
model.fit(X_train, y_train)

```
7. Make Predictions
Once the model is trained, use it to make predictions. For regression tasks, use the `predict` method. For classification, `predict` gives you the predicted class labels, while `predict_proba` gives you the probabilities for each class.
```
predictions = model.predict(X_test)
```
8. Evaluate the Model
Assess the model's performance using appropriate metrics. For regression, common metrics include Mean Absolute Error (MAE), Mean Squared Error (MSE), or R-squared. For classification, you might use accuracy, precision, recall, F1 score, or AUC-ROC.
```
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, predictions)
```


On this page of the site you can watch the video online Programming for Data Science, Lec 12: Machine Learning in Python using scikit-learn (sklearn) with a duration of hours minute second in good quality, which was uploaded by the user Dr. Data Science 08 March 2024, share the link with friends and acquaintances, this video has already been watched 618 times on youtube and it was liked by 7 viewers. Enjoy your viewing!