Skip to content

Commit ffa742f

Browse files
committed
avoid ties in sorting eigenvalues in PCA sec
1 parent 8e3627b commit ffa742f

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

code/ch05/ch05.ipynb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,13 @@
747747
" for i in range(len(eigen_vals))]\n",
748748
"\n",
749749
"# Sort the (eigenvalue, eigenvector) tuples from high to low\n",
750-
"eigen_pairs.sort(reverse=True)"
750+
"eigen_pairs.sort(key=lambda k: k[0], reverse=True)\n",
751+
"\n",
752+
"# Note: I added the `key=lambda k: k[0]` in the sort call above\n",
753+
"# just like I used it further below in the LDA section.\n",
754+
"# This is to avoid problems if there are ties in the eigenvalue\n",
755+
"# arrays (i.e., the sorting algorithm will only regard the\n",
756+
"# first element of the tuples, now)."
751757
]
752758
},
753759
{
@@ -2318,21 +2324,21 @@
23182324
],
23192325
"metadata": {
23202326
"kernelspec": {
2321-
"display_name": "Python 2",
2327+
"display_name": "Python 3",
23222328
"language": "python",
2323-
"name": "python2"
2329+
"name": "python3"
23242330
},
23252331
"language_info": {
23262332
"codemirror_mode": {
23272333
"name": "ipython",
2328-
"version": 2
2334+
"version": 3
23292335
},
23302336
"file_extension": ".py",
23312337
"mimetype": "text/x-python",
23322338
"name": "python",
23332339
"nbconvert_exporter": "python",
2334-
"pygments_lexer": "ipython2",
2335-
"version": "2.7.11"
2340+
"pygments_lexer": "ipython3",
2341+
"version": "3.5.1"
23362342
}
23372343
},
23382344
"nbformat": 4,

0 commit comments

Comments
 (0)