Logistic regression is a powerful statistical method used for binary classification problems. In this tutorial, we will learn how to implement logistic regression using Python.
Installation
Before we start, make sure you have the following packages installed:
- scikit-learn
- pandas
- numpy
You can install them using pip:
pip install scikit-learn pandas numpy
Data Preparation
Let's start by creating a simple dataset using pandas:
import pandas as pd
data = {
'feature1': [1, 2, 3, 4, 5],
'feature2': [2, 3, 4, 5, 6],
'label': [0, 1, 0, 1, 0]
}
df = pd.DataFrame(data)
Logistic Regression Model
Now, we will create a logistic regression model using scikit-learn:
from sklearn.linear_model import LogisticRegression
# Create a logistic regression model
model = LogisticRegression()
# Train the model
model.fit(df[['feature1', 'feature2']], df['label'])
Making Predictions
Once the model is trained, we can use it to make predictions:
# Make a prediction
prediction = model.predict([[3, 4]])
print(prediction)
Evaluate the Model
To evaluate the performance of our logistic regression model, we can use metrics such as accuracy, precision, recall, and F1 score.
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# Evaluate the model
accuracy = accuracy_score(df['label'], prediction)
precision = precision_score(df['label'], prediction)
recall = recall_score(df['label'], prediction)
f1 = f1_score(df['label'], prediction)
print(f"Accuracy: {accuracy}")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
Conclusion
In this tutorial, we learned how to implement logistic regression using Python. Logistic regression is a simple yet effective model for binary classification problems. For more information on logistic regression, you can read this detailed guide.