From 6a4ef736cf2410f64385a0972db0a96f44869287 Mon Sep 17 00:00:00 2001 From: Avdhoot Fulsundar Date: Wed, 25 Sep 2024 18:09:42 +0530 Subject: [PATCH 1/3] files added --- .../label-propagation/label-propagation.md | 76 ++++++++++++++++++ media/sklearn-label-propagation.png | Bin 0 -> 10263 bytes 2 files changed, 76 insertions(+) create mode 100644 content/sklearn/concepts/label-propagation/label-propagation.md create mode 100644 media/sklearn-label-propagation.png diff --git a/content/sklearn/concepts/label-propagation/label-propagation.md b/content/sklearn/concepts/label-propagation/label-propagation.md new file mode 100644 index 00000000000..8ca2e2f2497 --- /dev/null +++ b/content/sklearn/concepts/label-propagation/label-propagation.md @@ -0,0 +1,76 @@ +--- +Title: 'Label Propagation' +Description: 'A a semi-supervised learning algorithm that spreads labels from a small set of labeled data points to the unlabeled data using a graph-based approach.' +Subjects: + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Classification' + - 'Scikit-learn' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +**Label Propagation** is a semi-supervised learning algorithm used in classification problems where a small portion of the data is labeled, and the remaining data is unlabeled. The algorithm spreads the label information from the labeled instances to the unlabeled ones by propagating labels across the graph built from the input data. In Scikit-learn, this is implemented as part of the `sklearn.semi_supervised` module. + +Scikit-learn provides two closely related semi-supervised algorithms: + +- **Label Propagation**: Propagates labels through the graph using a probabilistic transition matrix. +- **Label Spreading**: A variant of Label Propagation that uses a normalized graph Laplacian to reduce noise sensitivity. + +## Syntax + +```pseudo +from sklearn.semi_supervised import LabelPropagation + +# Create a LabelPropagation object with desired parameters +model = LabelPropagation(kernel='rbf', gamma=1, max_iter=1000) + +# Fit the model to your data +model.fit(X, y) + +# Predict labels for unlabeled data +y_pred = model.predict(X_unlabeled) +``` + +- `kernel`: The kernel function used for label propagation. Common options are `rbf` (radial basis function), `knn` (k-nearest neighbors), and `poly` (polynomial). +- `gamma`: The kernel coefficient for the `rbf` kernel. +- `max_iter`: The maximum number of iterations for the label propagation algorithm. +- `X`: The input data, where each row is a sample and each column is a feature. +- `y`: The corresponding labels for the labeled samples. +- `X_unlabeled`: The input data for the unlabeled samples. + +## Example + +In the following example, a small set of labeled points (`y = 0, 1`) and unlabeled points (`y = -1`) is given. The Label Propagation algorithm spreads the known labels to the unlabeled data points based on their proximity. + +```python +import numpy as np +from sklearn.semi_supervised import LabelPropagation +import matplotlib.pyplot as plt + +# Create synthetic data +X = np.array([[1, 1], [2, 1], [3, 2], [5, 5], [6, 5], [7, 6]]) +y = np.array([0, 0, 1, -1, -1, -1]) # -1 represents unlabeled data + +# Initialize Label Propagation model +model = LabelPropagation() +model.fit(X, y) + +# Predict labels for unlabeled data +predicted_labels = model.transduction_ +print("Predicted labels:", predicted_labels) + +# Plotting the data +plt.scatter(X[:, 0], X[:, 1], c=predicted_labels, cmap='viridis', s=100) +plt.show() +``` + +The output of the above code will be: + +```shell +Predicted labels: [0 0 1 1 1 1] +``` + +![Label Propagation Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/sklearn-label-propagation.png) \ No newline at end of file diff --git a/media/sklearn-label-propagation.png b/media/sklearn-label-propagation.png new file mode 100644 index 0000000000000000000000000000000000000000..5c23d5fdb19a4269f00c717eaedd19d9a389a096 GIT binary patch literal 10263 zcmeHtXH-*J`|m-c5u^odpdcWEfK&w$BZSaz4y>V z1|gsz#XtZ<2oOYS2%#j9kmT;T=D+T{?p=5N@BJ|E`{jN}&Ph4@?7h!^p66Hgi<^eJ zLVG0kKoBH!{o0jV5VVa1UgEoUfNv6_XI;TT!1I>wC8(_T=p6X)lhZ|mix5;9w|DE_ zcJO((+qFBM5Olzc{}LcSkoJS1Qzh50Tr~BwrH<`xHuFy3pwrqYjpHI(%^xuoAkH=P{fE!568ykRFEM#!z{v2~{pl%4)H z>iFpP44$W<)`Na+8ly>rxe|g^gU@XBHZ*_tzPrRBt#0-D%MnsQhpJ{@+X_IB_x+>- zLHAG83qZ$ZT7@7;d^Z@%$7^nakhacKNeDW70F3kHtBD=ZZ(z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?)d3RH|#EtfA)m;7ZH(f$J-> z@E`|5{heHih?y3kc@0qNN$%)tQ+v^kK|9(&v`}qC*XwJfXLaBhBDK*6xm!m z<}=FIS15dqGKyc48+NtMlStNbpBZgM7eYDIwbS--WZx4PR^ErFtwVid9xKi%9yL3V-^qD6hL zDQfijbeV=hhK6cPzZsy8;&wdNaCdPrE&)sVB#x_^mwHA)+V!1uwJQ!OR9JF&M=cH5 z)tZv0c!(K!2pja;+e`f0u7KV0;BS)HT>Y`%;nyU} zgHKQM<7?6;@=Q*Xd*zo>VBeXdM*DzReS>(}5VzVhe4k(<8Ce&Iu)1n3W|B+B92)T7 zbMTbm?=$!T#?TrKvoT%er{v&1en~MVvew*MLpEcVCrFbCVrFKtWsQfsLAWOBe0=XW zQ@`;?HQ0~Q*$HPihlSPKcM0zNc(aMje?6v<{IbQrtriev6kg$ z^_0jnUAj8y9$m7Cwa}wM%dKT_$D23CW#QD)`XbAq6)QT5n-RDqJ5#j;1heT?&)|uh z>+w?h`1L+5CAAd1Vjhh+#KyUMG;HemiGMs(T|b3}SKG z5Wn3H<&nE#D+h(6Xj)^)TtJyXZjX5&<5}G7T4&w&i?R!QQc{x_@0Ql5vxb zYuT!^-Mgf8$qLAH3q^oQ8+n7kDjy1YRl-;pF1L)L&4UdpiLuA?fh_wTsIpCFk`fX{ z95%B&_LycP3oOoD%r}pA5nWT@=H3dPPB6#slAqke-?MgqKmYyT!wi3;@C0)m38B;~ SOa2x>==xQ|D`l7NKK&od*Cves literal 0 HcmV?d00001 From 062923e4895c178636f8bb4952f1ee5cd9ae12fe Mon Sep 17 00:00:00 2001 From: Savi Dahegaonkar <124272050+SaviDahegaonkar@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:41:45 +0530 Subject: [PATCH 2/3] Update content/sklearn/concepts/label-propagation/label-propagation.md --- content/sklearn/concepts/label-propagation/label-propagation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/sklearn/concepts/label-propagation/label-propagation.md b/content/sklearn/concepts/label-propagation/label-propagation.md index 8ca2e2f2497..d44b0ae651e 100644 --- a/content/sklearn/concepts/label-propagation/label-propagation.md +++ b/content/sklearn/concepts/label-propagation/label-propagation.md @@ -1,6 +1,6 @@ --- Title: 'Label Propagation' -Description: 'A a semi-supervised learning algorithm that spreads labels from a small set of labeled data points to the unlabeled data using a graph-based approach.' +Description: 'A semi-supervised learning algorithm that spreads labels from a small set of labeled data points to the unlabeled data using a graph-based approach.' Subjects: - 'Data Science' - 'Machine Learning' From 041f2e0ecfaea7e930cefd64598841caea59b7a1 Mon Sep 17 00:00:00 2001 From: Savi Dahegaonkar Date: Sun, 20 Oct 2024 16:14:35 +0530 Subject: [PATCH 3/3] Prettier Formatting --- content/sklearn/concepts/label-propagation/label-propagation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/sklearn/concepts/label-propagation/label-propagation.md b/content/sklearn/concepts/label-propagation/label-propagation.md index d44b0ae651e..83779bec16c 100644 --- a/content/sklearn/concepts/label-propagation/label-propagation.md +++ b/content/sklearn/concepts/label-propagation/label-propagation.md @@ -73,4 +73,4 @@ The output of the above code will be: Predicted labels: [0 0 1 1 1 1] ``` -![Label Propagation Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/sklearn-label-propagation.png) \ No newline at end of file +![Label Propagation Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/sklearn-label-propagation.png)