Python Scikit Learn के लिए हिंदी में शुरुआती सीखने वालों के लिए ट्यूटोरियल | स्काइकिट स्क्रैच से सीखें

Posted by


Python Scikit-learn is a powerful machine learning library that is widely used for building predictive models. In this tutorial, we will learn about the basics of Scikit-learn and how to use it to build machine learning models in Python. This tutorial is for beginners in Python and machine learning, so we will start from scratch and cover all the necessary concepts step by step.

Prerequisites:
Before we start, make sure you have the following installed on your system:

  1. Python (preferably Python 3.x)
  2. Anaconda (optional but recommended for managing Python environments and packages)
  3. Jupyter Notebook (optional but recommended for writing and running Python code in an interactive way)

Installing Scikit-learn:
To install Scikit-learn, you can use the following command in your terminal:

pip install scikit-learn

This will install the latest version of Scikit-learn on your system.

Importing Scikit-learn:
To use Scikit-learn in your Python code, you need to import it using the following statement:

import sklearn

You can also import specific modules from Scikit-learn, such as models, datasets, and metrics, using the following syntax:

from sklearn import datasets
from sklearn import model_selection
from sklearn import metrics

Loading a Dataset:
Scikit-learn provides built-in datasets that you can use to practice machine learning. To load a dataset, you can use the following code:

from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.target

In this code snippet, we are loading the Iris dataset, which is a popular dataset used for classification tasks.

Splitting the Dataset:
Before building a machine learning model, it’s essential to split the dataset into training and testing sets. You can use the train_test_split function from the model_selection module for this purpose:

from sklearn.model_selection import train_test_split

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

In this code snippet, we are splitting the dataset into 80% training data and 20% testing data.

Building a Model:
Now that we have our dataset split into training and testing sets, we can build a machine learning model using Scikit-learn. Let’s build a simple logistic regression model for our Iris dataset:

from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X_train, y_train)

In this code snippet, we are building a logistic regression model and fitting it to the training data.

Making Predictions:
Once we have trained our model, we can make predictions on the testing data using the predict method:

predictions = model.predict(X_test)

Evaluating the Model:
To evaluate the performance of our model, we can use various metrics such as accuracy, precision, recall, and F1 score. Scikit-learn provides functions for calculating these metrics:

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

accuracy = accuracy_score(y_test, predictions)
precision = precision_score(y_test, predictions, average='macro')
recall = recall_score(y_test, predictions, average='macro')
f1 = f1_score(y_test, predictions, average='macro')

print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)

In this code snippet, we are calculating various evaluation metrics for our model.

Conclusion:
In this tutorial, we have learned the basics of using Scikit-learn for building machine learning models in Python. We covered how to load a dataset, split it into training and testing sets, build a model, make predictions, and evaluate the model’s performance. Scikit-learn offers a wide range of algorithms and tools for machine learning, making it a powerful library for data scientists and machine learning engineers. I hope this tutorial has helped you understand the fundamentals of Scikit-learn and inspired you to explore more advanced concepts in the world of machine learning. Happy coding!