-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bc2e5c
commit 537e91c
Showing
1 changed file
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "458048cb-959b-4c1e-a4bf-08f64f0af712", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy\n", | ||
"from pca import pca\n", | ||
"from pcp import pcp\n", | ||
"from spcp import spcp" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c6d850e2-665d-4d2c-b9fc-959351de0b18", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"rank = 5\n", | ||
"n = 1000\n", | ||
"m = 100\n", | ||
"\n", | ||
"# Create low-rank with random bases\n", | ||
"x = (numpy.random.randn(m, rank)).astype(numpy.float32)\n", | ||
"y = (numpy.random.randn(n, rank)).astype(numpy.float32)\n", | ||
"b = y @ x.T" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d11962f0-7815-4b44-a240-b82cf56d6f41", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create uniform noise pattern (m = l + s)\n", | ||
"d = numpy.random.uniform(low=-1, high=1, size=(n, n)).astype(numpy.float32)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3b3071fd-ce85-4b80-8f4f-3416ac6040f0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"a = b + d" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d6ebd91b-8ae7-41bc-8959-915bccf01d4b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Initiate PCA\n", | ||
"pca_object = pca(verbose=False, rank=rank)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7c9fb551-3717-4a07-ad4e-d9ba58a311b7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Run PCA\n", | ||
"pca_object.run(a)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a4a88548-1830-47ae-be75-6d1b268028d7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print('Fit: ', linalg.norm(pca_object.b - b, \"fro\") / linalg.norm(b, \"fro\")*100, ' %')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e701526c-9a8a-4aae-89e4-734e2cc62b7e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Initiate PCP\n", | ||
"pcp_object = pcp(verbose=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e25ee615-dc4c-4a0f-b63c-8c3ca7bf9107", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Run PCP\n", | ||
"pcp_object.run(a)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5126cc98-6359-4f62-8475-fa7f48d04e1b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print('Fit: ', linalg.norm(pcp_object.b - b, \"fro\") / linalg.norm(b, \"fro\")*100, ' %')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "46cdc0df-d382-4348-a83c-b7e17ff250fb", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Initiate SPCP\n", | ||
"spcp_object = spcp(verbose=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "14c6f0ef-077f-4657-8db2-67c0797b8386", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Run SPCP\n", | ||
"spcp_object.run(a)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "96570952-cd7d-4449-9779-d140000324f1", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print('Fit: ', linalg.norm(spcp_object.b - b, \"fro\") / linalg.norm(b, \"fro\")*100, ' %')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |