Introduction
This guide explains linear regression and logistic regression with simple steps. Both models belong to supervised learning. Next, you move from definitions to structure, examples, and exercises.
هاد الشرح كيعطيك linear regression و logistic regression بطريقة سهلة. بجوج داخلين ف supervised learning. غادي تشوف التعاريف، الخدمة، و الأمثلة.
Core Concepts Explained
Linear regression predicts numbers. Logistic regression predicts classes. The first model draws a line. The second model outputs probabilities.
Linear regression كيحسب رقم. Logistic regression كيعطي class. Linear كيخدم بخط. Logistic كيخدم ب probability.
1. What Is Linear Regression
Linear regression predicts continuous values. It builds a line that fits input data.
Goal
Predict a numeric output.
How It Works
- The model receives input features
- Each feature multiplies by a weight
- The model sums all weighted values
- The output becomes a number
Use Cases
- House price prediction
- Sales forecasting
- Temperature prediction
Key Points
- Output is numeric
- Training uses mean squared error
- Fits a straight line for simple cases
2. What Is Logistic Regression
Logistic regression predicts labels. It outputs a probability. It picks the class with the highest score.
Goal
Predict a class label.
How It Works
- The model receives input features
- A score is computed
- A sigmoid or softmax function transforms the score
- The output becomes a probability
Use Cases
- Spam detection
- Disease classification
- Customer churn prediction
Key Points
- Output is a class
- Training uses cross entropy
- Supports binary and multi class tasks
Main Differences
| Aspect | Linear Regression | Logistic Regression |
|---|---|---|
| Output | Number | Class |
| Loss | MSE | Cross entropy |
| Activation | No activation | Sigmoid or softmax |
| Task | Regression | Classification |
Syntax or Model Structure
Linear Regression (Python)
from sklearn.linear_model import LinearRegression
import pandas as pd
data = pd.read_csv("data.csv")
X = data[["feature1", "feature2"]]
y = data["target"]
model = LinearRegression()
model.fit(X, y)
print(model.predict([[3.2, 7.1]]))
Logistic Regression (Python)
from sklearn.linear_model import LogisticRegression
import pandas as pd
data = pd.read_csv("data.csv")
X = data[["feature1", "feature2"]]
y = data["label"]
model = LogisticRegression()
model.fit(X, y)
print(model.predict([[1.5, 4.0]]))
Linear and Logistic Regression in Moroccan Darija
Linear Regression
Linear regression كيعطي رقم. كيحاول يرسم خط بين inputs و output.
Logistic Regression
Logistic regression كيعطي class. كيدير probability و كياخد أحسن اختيار.
Far9 Sarih
- Linear regression كيعطي number
- Logistic regression كيعطي class
- Linear كيستعمل MSE
- Logistic كيستعمل cross entropy
Multiple Practical Examples
1. Linear Regression for Price Prediction
predicted_price = model.predict([[120, 3]])
print(predicted_price)
2. Logistic Regression for Spam Classification
email = [[0.8, 0.3]]
print(model.predict(email))
Explanation of Each Example
In the first example, the model predicts a continuous price. In the second example, the model assigns a label: spam or not spam.
ف المثال الأول الموديل كيخرج رقم. ف الثاني كيعطي class.
Exercises
- Write one sentence explaining linear regression.
- Write one sentence explaining logistic regression.
- Create a simple dataset and train a linear regression model.
- Train a logistic regression model on a binary dataset.
- Explain why logistic regression uses sigmoid.
- Compute MSE for a small set of predictions.
- Compute cross entropy for two classes.
- List two regression tasks and two classification tasks.
- Plot a line from linear regression using matplotlib.
- Test logistic regression with different feature inputs.
Conclusion
Linear regression predicts numbers. Logistic regression predicts classes. Both models help build strong foundations for ML practice.
Linear regression كيحسب قيم رقمية. Logistic regression كيعطي classes. بجوج مهمين باش تفهم ML مزيان.