Loading
Loading
Linear Regression is the simplest and most important algorithm in ML. Understanding it deeply opens the door to understanding all other ML algorithms.
Linear Regression finds the best straight line describing the relationship between variables:
y_hat = w₁x₁ + w₂x₂ + ... + wₙxₙ + b
How it learns: Gradient Descent minimizes the Mean Squared Error (MSE) by iteratively adjusting weights w and bias b.
| Problem | Solution | |---------|---------| | Non-linear relationship | Polynomial Regression or Decision Trees | | Severe outliers | Data cleaning or Ridge/Lasso | | Correlated features | PCA or Ridge Regression |
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
print("=" * 55)
print("Linear Regression: التنبؤ بأسعار المنازل")
print("=" * 55)
# ─────────────────────────────────────────
# بيانات مصطنعة تحاكي سوق العقارات
# ─────────────────────────────────────────
np.random.seed(42)
n = 500
# ميزات
area = np.random.normal(150, 50, n) # المساحة (م²)
rooms = np.random.randint(2, 7, n) # عدد الغرف
age = np.random.randint(1, 40, n) # عمر البيت
proximity = np.random.uniform(0, 20, n) # المسافة عن المركز (كم)
# السعر = دالة خطية + ضجيج
price = (area * 2000 + rooms * 15000 - age * 500
- proximity * 3000 + np.random.normal(0, 20000, n))
df = pd.DataFrame({
"area": area, "rooms": rooms,
"age": age, "proximity_km": proximity,
"price": price
})
print(f"\nعدد المنازل في البيانات: {len(df)}")
print(f"متوسط السعر: {df['price'].mean():,.0f} ريال")
print(df.head())
# ─────────────────────────────────────────
# تحضير البيانات
# ─────────────────────────────────────────
features = ["area", "rooms", "age", "proximity_km"]
X = df[features]
y = df["price"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Scaling — مهم لـ Gradient Descent
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ─────────────────────────────────────────
# بناء وتدريب النموذج
# ─────────────────────────────────────────
model = LinearRegression()
model.fit(X_train_scaled, y_train)
print("\n" + "=" * 55)
print("معاملات النموذج:")
for feat, coef in zip(features, model.coef_):
print(f" {feat:15s}: {coef:+10,.0f} ريال لكل وحدة (بعد scaling)")
print(f" {'bias':15s}: {model.intercept_:+10,.0f}")
# ─────────────────────────────────────────
# تقييم النموذج
# ─────────────────────────────────────────
y_pred = model.predict(X_test_scaled)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print("\n" + "=" * 55)
print("نتائج التقييم على بيانات الاختبار:")
print(f" MAE = {mae:>12,.0f} ريال (متوسط خطأ مطلق)")
print(f" RMSE = {rmse:>12,.0f} ريال")
print(f" R² = {r2:>12.3f} (1.0 = مثالي)")
# ─────────────────────────────────────────
# تنبؤ ببيت جديد
# ─────────────────────────────────────────
new_house = pd.DataFrame([{
"area": 180, "rooms": 4, "age": 10, "proximity_km": 5
}])
new_house_scaled = scaler.transform(new_house)
predicted = model.predict(new_house_scaled)[0]
print("\n" + "=" * 55)
print("تنبؤ لبيت جديد:")
print(f" المساحة: 180م², الغرف: 4, العمر: 10 سنوات, المسافة: 5كم")
print(f" السعر المتوقع: {predicted:,.0f} ريال")