|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 17, |
| 6 | + "id": "edf5b12f-64d7-43be-8a25-6ed5cea8d0e8", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "Components: [[-0.57735027 -0.57735027 -0.57735027]\n", |
| 14 | + " [ 0. 0. 0. ]]\n", |
| 15 | + "Transformed data: [[ 5.14470537 0. ]\n", |
| 16 | + " [ 0. 0. ]\n", |
| 17 | + " [-5.14470537 0. ]]\n" |
| 18 | + ] |
| 19 | + } |
| 20 | + ], |
| 21 | + "source": [ |
| 22 | + "from sklearn.decomposition import SparsePCA\n", |
| 23 | + "import numpy as np\n", |
| 24 | + "\n", |
| 25 | + "# Generate example data\n", |
| 26 | + "X = np.array([[1, 2, 3],\n", |
| 27 | + " [4, 5, 6],\n", |
| 28 | + " [7, 8, 9]])\n", |
| 29 | + "\n", |
| 30 | + "# Perform Sparse PCA\n", |
| 31 | + "spca = SparsePCA(n_components=2, alpha=1)\n", |
| 32 | + "X_spca = spca.fit_transform(X)\n", |
| 33 | + "\n", |
| 34 | + "# Components\n", |
| 35 | + "print(\"Components:\", spca.components_)\n", |
| 36 | + "\n", |
| 37 | + "# Transformed data\n", |
| 38 | + "print(\"Transformed data:\", X_spca) " |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": 18, |
| 44 | + "id": "eb31b678-a661-4764-99c1-cd16429bb3c5", |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [ |
| 47 | + { |
| 48 | + "data": { |
| 49 | + "text/plain": [ |
| 50 | + "((3, 2), (3, 3))" |
| 51 | + ] |
| 52 | + }, |
| 53 | + "execution_count": 18, |
| 54 | + "metadata": {}, |
| 55 | + "output_type": "execute_result" |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "X_spca.shape, X.shape" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 19, |
| 65 | + "id": "66f4a8e0-68ea-441e-9b61-4eb9e6f0c141", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "data": { |
| 70 | + "text/plain": [ |
| 71 | + "(array([[ 5.14470537, 0. ],\n", |
| 72 | + " [ 0. , 0. ],\n", |
| 73 | + " [-5.14470537, 0. ]]),\n", |
| 74 | + " array([[1, 2, 3],\n", |
| 75 | + " [4, 5, 6],\n", |
| 76 | + " [7, 8, 9]]))" |
| 77 | + ] |
| 78 | + }, |
| 79 | + "execution_count": 19, |
| 80 | + "metadata": {}, |
| 81 | + "output_type": "execute_result" |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "X_spca, X" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "id": "97c79b31-37ae-4f4c-88ae-dcc234346957", |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": "Python 3 (ipykernel)", |
| 100 | + "language": "python", |
| 101 | + "name": "python3" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "codemirror_mode": { |
| 105 | + "name": "ipython", |
| 106 | + "version": 3 |
| 107 | + }, |
| 108 | + "file_extension": ".py", |
| 109 | + "mimetype": "text/x-python", |
| 110 | + "name": "python", |
| 111 | + "nbconvert_exporter": "python", |
| 112 | + "pygments_lexer": "ipython3", |
| 113 | + "version": "3.11.7" |
| 114 | + } |
| 115 | + }, |
| 116 | + "nbformat": 4, |
| 117 | + "nbformat_minor": 5 |
| 118 | +} |
0 commit comments