{ "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 }