{ "cells": [ { "cell_type": "markdown", "id": "6ab85223", "metadata": {}, "source": [ "# Federated logistic regression with scikit-learn\n", "\n", "# انحدار لوجستي اتحادي بـ scikit-learn\n", "\n", "**Educational simulation / محاكاة تعليمية.** Synthetic data only; no real patients or users. Federated learning does not by itself guarantee privacy.\n", "\n", "بيانات اصطناعية فقط؛ لا بيانات حقيقية. التعلم الاتحادي لا يضمن الخصوصية تلقائياً.\n", "\n", "Executed with Python 3.13.18, NumPy 2.5.3, scikit-learn 1.9.1. Outputs below were produced by actually running the cells." ] }, { "cell_type": "code", "id": "96894cea", "metadata": {}, "execution_count": 1, "outputs": [], "source": [ "\"\"\"FedAvg with scikit-learn: average coef_/intercept_ of SGDClassifier models trained on each client's digits.\"\"\"\n", "\n", "import numpy as np\n", "\n", "from sklearn.datasets import load_digits\n", "\n", "from sklearn.linear_model import SGDClassifier\n", "\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "code", "id": "68026465", "metadata": {}, "execution_count": 2, "outputs": [], "source": [ "X, y = load_digits(return_X_y=True); X = X / 16.0" ] }, { "cell_type": "code", "id": "fd173e8c", "metadata": {}, "execution_count": 3, "outputs": [], "source": [] }, { "cell_type": "code", "id": "f77a4a13", "metadata": {}, "execution_count": 4, "outputs": [], "source": [ "Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)" ] }, { "cell_type": "code", "id": "aa1966f7", "metadata": {}, "execution_count": 5, "outputs": [], "source": [ "parts = np.array_split(np.random.default_rng(0).permutation(len(ytr)), 5)" ] }, { "cell_type": "code", "id": "67a1a4fb", "metadata": {}, "execution_count": 6, "outputs": [], "source": [ "coef = np.zeros((10, 64)); intercept = np.zeros(10)" ] }, { "cell_type": "code", "id": "625984a4", "metadata": {}, "execution_count": 7, "outputs": [], "source": [] }, { "cell_type": "code", "id": "0492ce54", "metadata": {}, "execution_count": 8, "outputs": [], "source": [ "for rnd in range(15):\n", " cs, bs, ns = [], [], []\n", " for p in parts:\n", " clf = SGDClassifier(loss=\"log_loss\", learning_rate=\"constant\", eta0=0.05, alpha=1e-4, max_iter=1, tol=None, random_state=rnd)\n", " clf.partial_fit(Xtr[p][:1], ytr[p][:1], classes=np.arange(10)) # initialise shapes\n", " clf.coef_, clf.intercept_ = coef.copy(), intercept.copy()\n", " for _ in range(3):\n", " clf.partial_fit(Xtr[p], ytr[p])\n", " cs.append(clf.coef_); bs.append(clf.intercept_); ns.append(len(p))\n", " w = np.array(ns) / sum(ns)\n", " coef = sum(wi * c for wi, c in zip(w, cs)); intercept = sum(wi * b for wi, b in zip(w, bs))" ] }, { "cell_type": "code", "id": "05430506", "metadata": {}, "execution_count": 9, "outputs": [], "source": [ "pred = np.argmax(Xte @ coef.T + intercept, axis=1)" ] }, { "cell_type": "code", "id": "1cf824f7", "metadata": {}, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "federated test accuracy: 0.961\n" ] } ], "source": [ "print(\"federated test accuracy:\", round(float((pred == yte).mean()), 3))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.13.18" } }, "nbformat": 4, "nbformat_minor": 5 }