{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# numpy_色々" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:47.042622", "start_time": "2016-07-03T13:59:44.594181" }, "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from matplotlib import animation as ani\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:47.765649", "start_time": "2016-07-03T13:59:47.755257" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vstacked = np.vstack(([1, 2], [3, 4]))\n", "vstacked" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:48.436950", "start_time": "2016-07-03T13:59:48.431189" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack(([1, 2], [3, 4]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:49.041174", "start_time": "2016-07-03T13:59:49.034224" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 1, 2],\n", " [3, 4, 3, 4]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack((vstacked, vstacked))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:49.542556", "start_time": "2016-07-03T13:59:49.535299" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.stack([[1, 2], [3, 4]])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:50.183008", "start_time": "2016-07-03T13:59:50.175306" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [2, 4]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.stack([[1, 2], [3, 4]], axis=1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:51.130165", "start_time": "2016-07-03T13:59:51.121434" }, "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "matrix([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat = np.matrix([[1, 2, 3], [4, 5, 6]])\n", "mat" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:51.879258", "start_time": "2016-07-03T13:59:51.873016" }, "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary = np.array(mat)\n", "ary" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:52.417528", "start_time": "2016-07-03T13:59:52.412322" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(numpy.matrixlib.defmatrix.matrix, numpy.ndarray)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(mat), type(ary)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:53.064457", "start_time": "2016-07-03T13:59:53.057243" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 2, 4, 6],\n", " [ 8, 10, 12]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary + mat" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:53.423257", "start_time": "2016-07-03T13:59:53.414470" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 2, 4, 6],\n", " [ 8, 10, 12]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat + ary" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:53.609345", "start_time": "2016-07-03T13:59:53.600107" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 2, 4, 6],\n", " [ 8, 10, 12]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat + mat" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:53.777617", "start_time": "2016-07-03T13:59:53.768611" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[14, 32],\n", " [32, 77]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat * mat.T" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:53.942773", "start_time": "2016-07-03T13:59:53.935194" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[17, 22, 27],\n", " [22, 29, 36],\n", " [27, 36, 45]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat.T * mat" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:54.100252", "start_time": "2016-07-03T13:59:54.089903" }, "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "matrix([[17, 22, 27],\n", " [22, 29, 36],\n", " [27, 36, 45]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mat.T @ mat" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:54.254096", "start_time": "2016-07-03T13:59:54.246686" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[14, 32],\n", " [32, 77]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary @ ary.T" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:54.404527", "start_time": "2016-07-03T13:59:54.397521" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(10)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:54.972593", "start_time": "2016-07-03T13:59:54.968532" }, "collapsed": true }, "outputs": [], "source": [ "from scipy.spatial import distance" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:56.206438", "start_time": "2016-07-03T13:59:56.198756" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distance.euclidean([0, 0], [1, 1])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T13:59:57.823190", "start_time": "2016-07-03T13:59:57.816927" }, "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# http://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy\n", "np.linalg.norm(np.array([0, 0]) - np.array([1, 1]))\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:01:30.254975", "start_time": "2016-07-03T14:01:30.249074" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0],\n", " [1, 1]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[0, 0], [1, 1]])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:02:12.146348", "start_time": "2016-07-03T14:02:12.138367" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 2])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(np.array([[0, 0], [1, 1]]), axis=1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:00:54.703270", "start_time": "2016-07-03T14:00:54.696591" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 1.41421356])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(np.sum(np.array([[0, 0], [1, 1]]), axis=1))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:02:51.803912", "start_time": "2016-07-03T14:02:51.798445" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmin.html\n", "np.argmin([2,3,1]), np.argmax([2,3,1])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:02:54.245361", "start_time": "2016-07-03T14:02:54.238153" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[4, 5, 6],\n", " [1, 2, 3]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reshaped = np.array([4,5,6,1,2,3]).reshape(2, 3)\n", "reshaped" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:02:56.627959", "start_time": "2016-07-03T14:02:56.621914" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3, 2)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.argmin(reshaped), np.argmax(reshaped)\n", "# flattenしたときのIndex" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:06.688809", "start_time": "2016-07-03T14:03:06.677149" }, "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False False False False]\n", "[False False False True True]\n" ] }, { "data": { "text/plain": [ "array(['a', 1, 'b', nan, None], dtype=object)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "print(pd.isnull(np.array(['a', 1, 'b', np.nan, np.nan])))\n", "print(pd.isnull(np.array(['a', 1, 'b', np.nan, None])))\n", "np.array(['a', 1, 'b', np.nan, None])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:07.661098", "start_time": "2016-07-03T14:03:07.618969" }, "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "5.5777999999999999" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(np.linalg.norm(np.array([3.33, 2.67]) - np.array([5, 1])) ) ** 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:12.608598", "start_time": "2016-07-03T14:03:12.605484" }, "collapsed": true }, "outputs": [], "source": [ "# ランダムにクラスタとなるデータをK個選ぶ\n", "# 各データは、K個のデータうち、一番近いクラスタに属させる\n", "# クラスタ内の重心を変更する" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:13.921969", "start_time": "2016-07-03T14:03:13.917401" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(1.16, 1)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:14.853388", "start_time": "2016-07-03T14:03:14.846661" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(array([1, 2]),)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(np.array([1,2,3,4]) == np.array([4,2,3,1]))\n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "ExecuteTime": { "end_time": "2016-07-03T14:03:25.465585", "start_time": "2016-07-03T14:03:25.456020" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True], dtype=bool)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3]) == np.array([1,2,3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python [default]", "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.5.2" }, "toc": { "toc_cell": false, "toc_number_sections": true, "toc_threshold": 6, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 0 }