Spaces:
Sleeping
Sleeping
File size: 4,245 Bytes
6064c9d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2020 Erik Härkönen. All rights reserved.\n",
"# This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License. You may obtain a copy\n",
"# of the License at http://www.apache.org/licenses/LICENSE-2.0\n",
"\n",
"# Unless required by applicable law or agreed to in writing, software distributed under\n",
"# the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n",
"# OF ANY KIND, either express or implied. See the License for the specific language\n",
"# governing permissions and limitations under the License.\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.decomposition import PCA"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def rotMax(degrees):\n",
" theta = np.radians(degrees)\n",
" c, s = np.cos(theta), np.sin(theta)\n",
" return np.array(((c, -s), (s, c)))\n",
"\n",
"pointSize = 6\n",
"colormap = 'spring'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(0)\n",
"z = np.random.normal(size=(2,1000))\n",
"\n",
"plt.scatter(z[0,:],z[1,:],c='black', s=pointSize)\n",
"plt.gca().set_aspect('equal', adjustable='box')\n",
"plt.axis('off')\n",
"\n",
"plt.savefig('zplot.pdf')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"R = rotMax(60)\n",
"lam = np.diag([4,1])\n",
"A = R.dot(lam)\n",
"w = A[:,0]\n",
"y = A.dot(z)\n",
"\n",
"plt.scatter(y[0,:],y[1,:],c=z[0,:],s=pointSize)\n",
"plt.gca().set_aspect('equal', adjustable='datalim')\n",
"plt.arrow(0,0,2*w[0],2*w[1], width = 0.1, head_width = 1)\n",
"plt.axis('off')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# perturb z coordinates\n",
"zp = rotMax(-15).dot(z)\n",
"zp = zp + np.random.normal(size=z.shape)\n",
"\n",
"# sigmoid offset\n",
"lam = np.diag([2,1])\n",
"z2 = np.array(lam.dot(zp))\n",
"z2[1,:] = z2[1,:]+6*np.tanh(z2[0,:]*0.5)\n",
"\n",
"# rotate data\n",
"A = rotMax(15).dot(lam)\n",
"y = A.dot(z2)\n",
"\n",
"# PCA \n",
"yt = y.transpose()\n",
"pca = PCA(n_components = 1)\n",
"x = pca.fit_transform(yt)\n",
"w = pca.components_[0]\n",
"if w[0] < 0:\n",
" w = -w\n",
"\n",
"arrow_scale = 10\n",
"\n",
"plt.scatter(y[0,:],y[1,:],s=pointSize,c=x[:,0],cmap=colormap) \n",
"plt.gca().set_aspect('equal', adjustable='datalim')\n",
"plt.arrow(0,0,arrow_scale*w[0],arrow_scale*w[1], width = 0.1, head_width = 1)\n",
"plt.axis('off')\n",
"\n",
"plt.savefig('yplot.pdf')\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"u = np.linalg.pinv(x).dot(z.transpose())[0]\n",
"if u[0] < 0:\n",
" u = -u\n",
"\n",
"arrow_scale = 20\n",
"\n",
"plt.scatter(z[0,:],z[1,:],c=x[:,0]/np.max(np.abs(x)*0.91) , s=pointSize, cmap=colormap)\n",
"plt.gca().set_aspect('equal', adjustable='box')\n",
"plt.arrow(0,0,arrow_scale*u[0],arrow_scale*u[1], width = 0.1, head_width = 0.3)\n",
"plt.axis('off')\n",
"plt.colorbar()\n",
"\n",
"plt.savefig('uplot.pdf')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|