Skip to content

Commit

Permalink
exercise-classfication: sort eigenvectors and eigenvalues and select …
Browse files Browse the repository at this point in the history
…the largest ones (#83)

Co-authored-by: Lucas Weber <[email protected]>
  • Loading branch information
Lucew and Lucas Weber authored May 28, 2024
1 parent 3b8d476 commit a731038
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@
"\n",
"**Task 13:**\n",
" \n",
"Calculate the associated eigenvalues and eigenvectors. (Help: [NumPy documentation](https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html)) \n",
"Calculate the associated eigenvalues, eigenvectors and sort them. (Help: [NumPy documentation](https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html)) \n",
"</div>"
]
},
Expand All @@ -1062,7 +1062,8 @@
},
"outputs": [],
"source": [
"# Calculate the associated eigenvalues and eigenvectors"
"# Calculate the associated eigenvalues and eigenvectors\n",
"# then sort both in increasing order of the eigenvalues"
]
},
{
Expand All @@ -1078,6 +1079,11 @@
"# Calculate the associated eigenvalues and eigenvectors\n",
"eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix)\n",
"\n",
"# sort the eigenvalues and eigenvectors (so they are in increasing order)\n",
"idx = np.argsort(eigenvalues)\n",
"eigenvalues = eigenvalues[idx]\n",
"eigenvectors = eigenvectors[:, idx]\n",
"\n",
"# Display the eigenvalues and eigenvectors\n",
"print(\"Eigenvalues:\")\n",
"print(eigenvalues)\n",
Expand Down Expand Up @@ -1185,8 +1191,8 @@
"outputs": [],
"source": [
"# Select the feature matrix\n",
"# The first eigenvector contains nearly all information => select only that one\n",
"feature_matrix = eigenvectors[:, 0]\n",
"# The largest eigenvector contains nearly all information => select only that one\n",
"feature_matrix = eigenvectors[:, 1]\n",
"\n",
"# Print the feature_matrix\n",
"feature_matrix"
Expand Down

0 comments on commit a731038

Please sign in to comment.