From b297de6d8e3b10b03cb33a45a145b2cf7c1f4509 Mon Sep 17 00:00:00 2001 From: todoit Date: Mon, 9 May 2016 22:48:21 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=BA=BF=E6=80=A7=E4=BB=A3=E6=95=B0?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/leetCode/__init__.py | 0 src/leetCode/leetcode.py | 14 + .../homework1_circle.cpython-34.pyc | Bin 1885 -> 2120 bytes src/todoit/MatPlot/circle_plot.py | 2 +- src/todoit/MatPlot/homework_sort_fast.py | 49 ++ src/todoit/MatPlot/homework_sort_slow.py | 49 ++ src/todoit/linearAlgebra/__init__.py | 0 src/todoit/linearAlgebra/homework3-1.py | 48 ++ src/todoit/linearAlgebra/lesson22.py | 8 + src/todoit/linearAlgebra/numpy_study_01.ipynb | 758 ++++++++++++++++++ 10 files changed, 927 insertions(+), 1 deletion(-) create mode 100644 src/leetCode/__init__.py create mode 100644 src/leetCode/leetcode.py create mode 100644 src/todoit/MatPlot/homework_sort_fast.py create mode 100644 src/todoit/MatPlot/homework_sort_slow.py create mode 100644 src/todoit/linearAlgebra/__init__.py create mode 100644 src/todoit/linearAlgebra/homework3-1.py create mode 100644 src/todoit/linearAlgebra/lesson22.py create mode 100644 src/todoit/linearAlgebra/numpy_study_01.ipynb diff --git a/src/leetCode/__init__.py b/src/leetCode/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/leetCode/leetcode.py b/src/leetCode/leetcode.py new file mode 100644 index 0000000..c8534fd --- /dev/null +++ b/src/leetCode/leetcode.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +''' +Created on 2016年4月29日 +@author: todoit +''' + + +def reverseString(s): + return s[::-1] + +if __name__ == '__main__': + s = 'adsf asfd' + print(reverseString(s)) \ No newline at end of file diff --git a/src/todoit/MatPlot/__pycache__/homework1_circle.cpython-34.pyc b/src/todoit/MatPlot/__pycache__/homework1_circle.cpython-34.pyc index eee9d6456b54dbbfd017cf56934662678c59aa1e..50c0225a558199ee7a6bde05b42417ac11fe7d86 100644 GIT binary patch delta 962 zcmah`O=}ZT6g~G%GLudwnYO96HPzNu#B7w-MVBqrtzB555R{?vrfF)DrjrS#G9`L<_JbwdS19LD&L>6} zeh!*2E=v~U+QFUyk_?ve2NEwLDUA zAPGLI=PTHVfZ{ygY5P4^xuKQP=yG82R}1)~p?^t`5+oeI0~wze`U%xx-2cu~b%V|Q zUp#^--d6T|AJfH2fx%^&re!+W3Rkjw1#|YMxz=p@4INw%Dia%xc0Hy}Ogn+iOsqPy zvD%p}X9K_0u7zH$+6sFe^Xi=vt1XFz<&a}c87{eMnx<8O3YJMGxin7;{2Nxm4N1y) zUVW|F@Vvgu+vJ8%)WhpB1*M^0Y%b0nxf*MA-}7Qxj?MLIr5*UO!E3CV+(~mTPBm*= kVVr7hY(4H>vt}m7IL$O&a?(!Dv7M5`XFKX56=_oa27ku0t^fc4 delta 702 zcmZ`$O=}ZT6g~I7`R*hWC()KxOiXGa%hrVmLJMw1&@L({jDeSqjdqNaEJS7ql3yUR z@ISZ{!L>_&LqHJRI2#vrsaWqDTaki~d6#p~J?FkR_kC=9s`tM6HE()#buV#%FZgE( z#yh+x(W~Cw@IHnP<^Uyt64>o~Z@>Q{3f>)w3DL!3-&miytmZDbsa+3 nums[i+1]: + nums[i],nums[i+1] = nums[i+1],nums[i] + + yield nums + +def update(data): + for i in xrange(len(lines)): + lines[i].set_ydata([0,data[i]]) + return lines + + +t = np.arange(0.0, 5.0, 0.1) +s = f(t) + +fig = plt.figure(figsize=(12, 6)) +ax = fig.add_subplot(111) + +lines=[] + +for i in xrange(len(t)): + line,=ax.plot([i,i], [0,s[i]] , 'b-o') + lines.append(line) + +ax.set_xlabel('index') +ax.set_ylabel('value') +ax.set_title('BubbleSort') + + +ani = animation.FuncAnimation(fig, update, bubbleSort, interval=10, repeat=False) + +plt.show() \ No newline at end of file diff --git a/src/todoit/MatPlot/homework_sort_slow.py b/src/todoit/MatPlot/homework_sort_slow.py new file mode 100644 index 0000000..86ced72 --- /dev/null +++ b/src/todoit/MatPlot/homework_sort_slow.py @@ -0,0 +1,49 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# CopyRight by heibanke + +import matplotlib.pyplot as plt +import numpy as np +import matplotlib.animation as animation + +def f(t): + s1 = np.sin(2 * np.pi * t) + e1 = np.exp(-t) + return np.absolute((s1 * e1)) + .05 + + +def bubbleSort(): + nums = s + for j in xrange(len(nums),-1,-1): + for i in xrange(0,j-1,1): + if nums[i] > nums[i+1]: + nums[i],nums[i+1] = nums[i+1],nums[i] + + yield nums + +def update(data): + for i in xrange(len(lines)): + lines[i].set_ydata([0,data[i]]) + return lines + + +t = np.arange(0.0, 5.0, 0.1) +s = f(t) + +fig = plt.figure(figsize=(12, 6)) +ax = fig.add_subplot(111) + +lines=[] + +for i in xrange(len(t)): + line,=ax.plot([i,i], [0,s[i]] , 'b-o') + lines.append(line) + +ax.set_xlabel('index') +ax.set_ylabel('value') +ax.set_title('BubbleSort') + + +ani = animation.FuncAnimation(fig, update, bubbleSort, interval=10, repeat=False) + +plt.show() \ No newline at end of file diff --git a/src/todoit/linearAlgebra/__init__.py b/src/todoit/linearAlgebra/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/todoit/linearAlgebra/homework3-1.py b/src/todoit/linearAlgebra/homework3-1.py new file mode 100644 index 0000000..59d0100 --- /dev/null +++ b/src/todoit/linearAlgebra/homework3-1.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +#copyRight by heibanke +#%matplotlib inline + +from matplotlib import pyplot as plt +import numpy as np + +A=np.array([[3],[1]]) +C=np.array([[1],[3]]) + +x=np.linspace(-2,2,10) + +""" +x相当于是一维的点。 +xx=Ax相当于讲解图中的OA这条直线。为了打直线图引入的变量。 +3x是这条直线的横坐标范围,x是这条直线的纵坐标范围 +这里也可以不引入这么多点,引入两个点就行,不过这样可以和后续代码保持一致。 +""" +x.shape=(1,10) +xx=A.dot(x) + + +B=A.T.dot(C) +AA=np.linalg.inv(A.T.dot(A)) + +#P=Ax^=A*AA*B +P=A*AA*B +E=C-P + +fig = plt.figure() #figsize=(10,6) +ax= fig.add_subplot(111) + +ax.plot(xx[0,:],xx[1,:]) +ax.plot(A[0],A[1],'ko') +ax.plot([C[0],P[0]],[C[1],P[1]],'r-o') +ax.plot([0,C[0]],[0,C[1]],'m-o') +ax.plot([0,E[0]],[0,E[1]],'k-o') + +margin=0.1 +ax.text(A[0]+margin, A[1]+margin, r"A",fontsize=20) +ax.text(C[0]+margin, C[1]+margin, r"C",fontsize=20) +ax.text(P[0]+margin, P[1]+margin, r"P",fontsize=20) +ax.text(E[0]+margin, E[1]+margin, r"E",fontsize=20) + + +ax.axis('equal') +plt.show() \ No newline at end of file diff --git a/src/todoit/linearAlgebra/lesson22.py b/src/todoit/linearAlgebra/lesson22.py new file mode 100644 index 0000000..997cdbc --- /dev/null +++ b/src/todoit/linearAlgebra/lesson22.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +''' +Created on 2016年5月9日 +@author: todoit +''' + + diff --git a/src/todoit/linearAlgebra/numpy_study_01.ipynb b/src/todoit/linearAlgebra/numpy_study_01.ipynb new file mode 100644 index 0000000..b4d5280 --- /dev/null +++ b/src/todoit/linearAlgebra/numpy_study_01.ipynb @@ -0,0 +1,758 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# numpy基础入门 ——by 黑板客" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. 矩阵的创建" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4] [1 2 3 4 5] int32 (4,) 4 1\n" + ] + } + ], + "source": [ + "a=np.arange(1,5)\n", + "b=np.array([1,2,3,4,5])\n", + "print(a, b, a.dtype, a.shape, a.size, a.ndim)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " np.arange类似range函数\n", + " np.array用来生成矩阵\n", + " dtype是数据类型,有int64, complex, uint16等\n", + " shape是个元组属性,表示每一维的宽度\n", + " size是所有元素个数\n", + " ndim是维数" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3] int64\n" + ] + } + ], + "source": [ + "b=np.array([1,2,3],dtype='int64') # int64, complex, uint16......\n", + "print(b, b.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 2 3 4 5]\n", + " [0 1 2 3 4 5]\n", + " [0 1 2 3 4 5]] (3, 6) 18\n" + ] + } + ], + "source": [ + "m=np.array([np.arange(6),np.arange(6),np.arange(6)])\n", + "print(m, m.shape, m.size)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3 4]\n", + " [5 6 7 8]] 3 6\n", + "3 6\n", + "[[[ 1 2 3]\n", + " [ 4 5 6]]\n", + "\n", + " [[ 7 8 9]\n", + " [10 11 12]]] (2, 2, 3)\n", + "[7 8 9]\n" + ] + }, + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 每一个[]代表一维,比如\n", + "# [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 代表矩阵的维度是(2,2,3)\n", + "# 其中第一个2,代表最外层的两个[],第二个2代表第二层[],第三个3代表最里层的维度。\n", + "n=np.array([[1,2,3,4],[5,6,7,8]])\n", + "\n", + "#下面这两种用法一样吗??\n", + "print(n, n[0,2], n[1,1])\n", + "print(n[0][2], n[1][1])\n", + "\n", + "m=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])\n", + "print(m, m.shape)\n", + "print(m[1,0])\n", + "type(m)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 1 2 3]\n", + " [ 4 5 6]]\n", + "\n", + " [[ 7 8 9]\n", + " [10 11 12]]]\n", + "[[1 2 3 4]\n", + " [5 6 7 8]]\n", + "[ 1 2 3 4 5 6 7 8 9 10 11 12]\n", + "[1 2 3 4 5 6 7 8]\n" + ] + } + ], + "source": [ + "x=m.ravel()\n", + "y=n.flatten()\n", + "print(m)\n", + "print(n)\n", + "print(x)\n", + "\n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " ravel()和flatten()看起来效果一样,都是把矩阵展平了。它们的区别在于\n", + " ravel()返回的是原有数据的一个映射(view),没有分配新的存储\n", + " flatten()返回的是新的数据\n", + " 因此如果我们改变它们的值,就可以看出区别\n", + " \n", + " numpy还有一些函数有这样的区别,关键在于判断函数返回是原数据的映射还是返回新的数据。" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 1 2 3]\n", + " [10 5 6]]\n", + "\n", + " [[ 7 8 9]\n", + " [10 11 12]]]\n", + "[[1 2 3 4]\n", + " [5 6 7 8]]\n" + ] + } + ], + "source": [ + "x[3]=10;y[3]=10\n", + "print(m)\n", + "print(n)\n", + "# 看看m,n哪个的值改变了" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## reshap 和 resize" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 10]\n", + " [ 5 6 7 8]\n", + " [ 9 10 11 12]] None \n", + " [[1 2 3 4]\n", + " [5 6 7 8]\n", + " [0 0 0 0]]\n" + ] + } + ], + "source": [ + "# reshape返回一个view\n", + "x=m.reshape(3,4)\n", + "# resize直接在当前数据上更改,返回空\n", + "y=n.resize(3,4)\n", + "print(x,y,'\\n',n)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 10]\n", + " [ 5 6 7 8]\n", + " [10 10 10 10]]\n", + "[[[ 1 2 3]\n", + " [10 5 6]]\n", + "\n", + " [[ 7 8 10]\n", + " [10 10 10]]]\n" + ] + } + ], + "source": [ + "x[2]=10\n", + "print(x)\n", + "print(m)\n", + "# 看看m和n哪个改变了,有什么区别" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0 1 2 3]\n", + " [ 4 5 0 1]\n", + " [10 10 10 10]]\n", + "[[0 1 2 3 4 5]\n", + " [0 1 2 3 4 5]]\n" + ] + } + ], + "source": [ + "m=np.array([np.arange(6),np.arange(6)])\n", + "# copy()可以强制返回一个新的数据\n", + "x=m.reshape(3,4).copy()\n", + "x[2]=10;print(x);print(m)\n", + "#看看这次m的值随x改变而改变吗" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]\n" + ] + } + ], + "source": [ + "# linspace返回0,1之间的10个数据,它们之间的间隔自动计算\n", + "x=np.linspace(0.1,1,10)\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 3 5 7 6 9 1 2 8 4]\n", + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n" + ] + } + ], + "source": [ + "a = np.arange(10)\n", + "np.random.shuffle(a) # 随机排第一维\n", + "print(a)\n", + "\n", + "a = np.arange(9).reshape((3, 3)) # 随机排第一维,想一想结果是什么\n", + "np.random.shuffle(a)\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1., 1., 1., 1.])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 全0矩阵\n", + "a=np.zeros((3,3))\n", + "# 全1矩阵\n", + "b=np.ones((5,4))\n", + "# 单位矩阵\n", + "c=np.eye(3)\n", + "# 取对角元素\n", + "np.diag(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. 矩阵的加法" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[ 3 4 5]\n", + " [ 6 7 8]\n", + " [ 9 10 11]]\n", + "[[ 0 2 4]\n", + " [ 3 5 7]\n", + " [ 6 8 10]]\n", + "[[ 0 1 2]\n", + " [ 4 5 6]\n", + " [ 8 9 10]]\n" + ] + } + ], + "source": [ + "a = np.arange(9).reshape((3, 3))\n", + "\n", + "## 每个元素的broadcast\n", + "print(a)\n", + "print(a+3)\n", + "\n", + "## 行broadcast\n", + "print(a+np.arange(3))\n", + "\n", + "## 列broadcast\n", + "print(a+np.arange(3).reshape(3,1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## broadcast广播操作\n", + " 运算不仅仅作用在某个元素,而是作用在整个矩阵,或者整行,或者整列。\n", + " 比如\n", + " n*m + 1*1 就是将1*1的元素作用在n*m的整个矩阵\n", + " n*m + n*1 就是将n*1的元素作用在n*m的每一列\n", + " n*m + 1*m 就是将1*m的元素作用在n*m的每一行\n", + " 乘法类似" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4 6]\n", + "[3 7]\n", + "[ 2. 3.]\n", + "[ 1.5 3.5]\n", + "[ 1. 1.]\n", + "[[ 6 8 10]\n", + " [12 14 16]]\n", + "[[ 3 5 7]\n", + " [15 17 19]]\n", + "[[ 3 12]\n", + " [21 30]]\n" + ] + } + ], + "source": [ + "a = np.array([[1, 2], [3, 4]])\n", + "print(np.sum(a,axis=0))\n", + "print(np.sum(a,axis=1))\n", + "print(np.mean(a,axis=0))\n", + "print(np.mean(a,axis=1))\n", + "print(np.std(a,axis=0))\n", + "\n", + "#axis=0,就是按第一个维度进行计算,行向量[1,2], [3,4]\n", + "#axis=1,就是按第二个维度进行计算,列向量[1,3], [2,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 6 8 10]\n", + " [12 14 16]]\n", + "[[ 3 5 7]\n", + " [15 17 19]]\n", + "[[ 3 12]\n", + " [21 30]]\n" + ] + } + ], + "source": [ + "#再拓展到3维\n", + "a = np.arange(12).reshape((2,2,3))\n", + "print np.sum(a,axis=0) #按第一维度加,结果为2*3矩阵, 可以理解为1*2*3\n", + "print np.sum(a,axis=1) #按第二维度加,结果为2*3矩阵, 可以理解为2*1*3\n", + "print np.sum(a,axis=2) #按第三维度加,结果为2*2矩阵, 可以理解为2*2*1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. 矩阵的转置" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 0]\n", + " [1 1]\n", + " [2 2]\n", + " [3 3]\n", + " [4 4]\n", + " [5 5]]\n", + "[[0 0]\n", + " [1 1]\n", + " [2 2]\n", + " [3 3]\n", + " [4 4]\n", + " [5 5]]\n" + ] + } + ], + "source": [ + "print(m.transpose())\n", + "print(m.T)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4. 矩阵的乘法" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.arange(12).reshape((6, 2))\n", + "b = np.arange(10).reshape((2,5))\n", + "\n", + "# 两种矩阵乘法形式\n", + "print a.dot(b)\n", + "print np.dot(a,b)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "## 每个元素broadcast\n", + "print a*2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# 行broadcast\n", + "print a*[1,2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# 列broadcast\n", + "print a*np.arange(6).reshape(6,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5.矩阵的拼接" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "b = 2 * a\n", + "c = np.hstack((a, b))\n", + "print c" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.concatenate((a, b), axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "c=np.vstack((a, b))\n", + "c=np.concatenate((a, b), axis=0)\n", + "\n", + "print np.hsplit(a, 3)\n", + "print np.vsplit(a,3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. 矩阵的查找" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.arange(12).reshape((3, 4))\n", + "b = a%2==0\n", + "c = a>4\n", + "print b\n", + "print c\n", + "# 这里也用到了broadcast" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.arange(12).reshape((3, 4))\n", + "print a\n", + "print np.argmax(a)\n", + "# 其实是列broadcast,返回每列最大值的idx\n", + "print np.argmax(a, axis=0)\n", + "\n", + "# 行broadcast,返回每行最大值的idx\n", + "print np.argmax(a, axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# np.where支持多个逻辑组合, 得到满足条件的index\n", + "idx=np.where((a>3))\n", + "print a[idx]\n", + "\n", + "idx=np.where((a>3)&(a<7))\n", + "print a[idx]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 7. homework" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. http://www.labri.fr/perso/nrougier/teaching/numpy.100/" + ] + } + ], + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From b542898ff8c93c6894d15e3650353d9d258258da Mon Sep 17 00:00:00 2001 From: todoit Date: Sun, 15 May 2016 23:55:42 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9C=80=E5=B0=8F=E4=BA=8C=E4=B9=98?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/todoit/MatPlot/homework_3_v2.py | 4 +- src/todoit/linearAlgebra/homework3-1.py | 3 ++ src/todoit/linearAlgebra/homework_zuixiao1.py | 32 +++++++++++++++ src/todoit/linearAlgebra/zuixiao1.py | 40 +++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/todoit/linearAlgebra/homework_zuixiao1.py create mode 100644 src/todoit/linearAlgebra/zuixiao1.py diff --git a/src/todoit/MatPlot/homework_3_v2.py b/src/todoit/MatPlot/homework_3_v2.py index 65e2300..6500654 100644 --- a/src/todoit/MatPlot/homework_3_v2.py +++ b/src/todoit/MatPlot/homework_3_v2.py @@ -139,12 +139,12 @@ def updateAll(i): #这里的frames至少应该为data的长度 anims = animation.FuncAnimation(fig, updateAll,init_func=init, blit=False, frames=len(data), interval=500, repeat=True) - #plt.show() + plt.show() # FFwriter = animation.FFMpegWriter() # Set up formatting for the movie files fps = frames per second #anims.save('D:/MovWave.mp4', writer=FFwriter, fps=1,dpi=300) - anims.save('D:/homework_3.gif', fps=2, writer='imagemagick') + #anims.save('D:/homework_3.gif', fps=2, writer='imagemagick') diff --git a/src/todoit/linearAlgebra/homework3-1.py b/src/todoit/linearAlgebra/homework3-1.py index 59d0100..a86d170 100644 --- a/src/todoit/linearAlgebra/homework3-1.py +++ b/src/todoit/linearAlgebra/homework3-1.py @@ -7,6 +7,7 @@ import numpy as np A=np.array([[3],[1]]) + C=np.array([[1],[3]]) x=np.linspace(-2,2,10) @@ -25,7 +26,9 @@ AA=np.linalg.inv(A.T.dot(A)) #P=Ax^=A*AA*B +#得到投影后的向量 P=A*AA*B +#投影在另一个维度上的向量 E=C-P fig = plt.figure() #figsize=(10,6) diff --git a/src/todoit/linearAlgebra/homework_zuixiao1.py b/src/todoit/linearAlgebra/homework_zuixiao1.py new file mode 100644 index 0000000..aa64676 --- /dev/null +++ b/src/todoit/linearAlgebra/homework_zuixiao1.py @@ -0,0 +1,32 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +#copyRight by heibanke + +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(-1,1,100) +y = 2*np.sin(x*2.3)+0.5*x**3 + +y1 = y+0.5*(np.random.rand(len(x))-0.5) + +A = np.vstack((x,np.ones(len(x)))).T +b = y1.reshape(y1.shape[0],1) + +def projection(A,b): + #### + # return A*inv(AT*A)*AT*b + #### + AA = A.T.dot(A) + w=np.linalg.inv(AA).dot(A.T).dot(b) + print w + return A.dot(w) + +yw = projection(A,b) +yw.shape = (yw.shape[0],) + +plt.plot(x,y,color='g',linestyle='-',marker='') +plt.plot(x,y1,color='m',linestyle='',marker='o') +plt.plot(x,yw,color='r',linestyle='',marker='.') + +plt.show() diff --git a/src/todoit/linearAlgebra/zuixiao1.py b/src/todoit/linearAlgebra/zuixiao1.py new file mode 100644 index 0000000..3f66e4b --- /dev/null +++ b/src/todoit/linearAlgebra/zuixiao1.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +#copyRight by heibanke + +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(-1,1,100) +y = 2*np.sin(x*2.3)+0.5*x**3 + +y1 = y+0.5*(np.random.rand(len(x))-0.5) + +c = np.tile([1],100) + +################################## +# 写下你的Code +#Ax^=b,求出x^,即可得到直线y=ax+b +#向量A是一个100维的向量,第一列是x1...x100,第二列是1 +A = np.array([x,c]).T +#A = np.vstack((x,np.ones(len(x)))).T +#b是y1...y100 +b = np.array([y1]).T +#b = y1.reshape(y1.shape[0],1) +#根据公式求x^ +x_bar = (np.linalg.inv(A.T.dot(A))).dot(A.T).dot(b) + +#计算直线 + +y2 = A.dot(x_bar) + +y2.shape = (y2.shape[0],) +################################## +plt.plot(x,y,color='g',linestyle='-',marker='') +plt.plot(x,y1,color='m',linestyle='',marker='o') + +plt.plot(x,y2,color='b',linestyle='-',marker='') + +# 把拟合的曲线在这里画出来 + +plt.show() From 67a511b2ed1cbba1d8f5f63fc4c466d68c1b079d Mon Sep 17 00:00:00 2001 From: todoit Date: Sat, 11 Jun 2016 23:03:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?'=E8=AF=BE=E6=97=B633=E4=BD=9C=E4=B8=9A'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__pycache__/load_MNIST.cpython-34.pyc | Bin 0 -> 2779 bytes .../__pycache__/solutions_3.cpython-34.pyc | Bin 0 -> 1617 bytes src/todoit/linearAlgebra/homework_zuixiao1.py | 2 +- src/todoit/linearAlgebra/homework_zuixiao2.py | 41 +++++++ src/todoit/linearAlgebra/hw_3-1.py | 38 ++++++ src/todoit/linearAlgebra/hw_3-2.py | 27 +++++ src/todoit/linearAlgebra/hw_3-3.py | 14 +++ src/todoit/linearAlgebra/hw_3-4.py | 72 +++++++++++ src/todoit/linearAlgebra/hw_3-5.py | 29 +++++ src/todoit/linearAlgebra/hw_3-6_pca.py | 28 +++++ src/todoit/linearAlgebra/leastsqTest.py | 45 +++++++ src/todoit/linearAlgebra/load_MNIST.py | 113 ++++++++++++++++++ src/todoit/linearAlgebra/pca01.py | 43 +++++++ src/todoit/linearAlgebra/solutions_3.py | 69 +++++++++++ src/todoit/linearAlgebra/svd_demo.py | 33 +++++ src/todoit/linearAlgebra/test.py | 8 ++ src/todoit/linearAlgebra/zuixiao2.py | 46 +++++++ 17 files changed, 607 insertions(+), 1 deletion(-) create mode 100644 src/todoit/linearAlgebra/__pycache__/load_MNIST.cpython-34.pyc create mode 100644 src/todoit/linearAlgebra/__pycache__/solutions_3.cpython-34.pyc create mode 100644 src/todoit/linearAlgebra/homework_zuixiao2.py create mode 100644 src/todoit/linearAlgebra/hw_3-1.py create mode 100644 src/todoit/linearAlgebra/hw_3-2.py create mode 100644 src/todoit/linearAlgebra/hw_3-3.py create mode 100644 src/todoit/linearAlgebra/hw_3-4.py create mode 100644 src/todoit/linearAlgebra/hw_3-5.py create mode 100644 src/todoit/linearAlgebra/hw_3-6_pca.py create mode 100644 src/todoit/linearAlgebra/leastsqTest.py create mode 100644 src/todoit/linearAlgebra/load_MNIST.py create mode 100644 src/todoit/linearAlgebra/pca01.py create mode 100644 src/todoit/linearAlgebra/solutions_3.py create mode 100644 src/todoit/linearAlgebra/svd_demo.py create mode 100644 src/todoit/linearAlgebra/test.py create mode 100644 src/todoit/linearAlgebra/zuixiao2.py diff --git a/src/todoit/linearAlgebra/__pycache__/load_MNIST.cpython-34.pyc b/src/todoit/linearAlgebra/__pycache__/load_MNIST.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..774d1384ae1b15a8f26fadf26a57d0a57e74c908 GIT binary patch literal 2779 zcmc&$TZh@0$2A51VY7ZM1VQQUwsZWwx}duMwy(>+x6 zush3)1UG`gNJL1`=tBe%6pZ>HUJ}fs|Dhj5(A{KT%#$y^=y$63ZdQD=XR5lY&Z%=w zRek5H{yo>7{bS$QEAMs@{Yq<|INHZi)EQJZ{(@>mB|)}9c8o5gjYgd8gl@;kPLiD> zJ56?mE*n&WC`)#imL_=mXd$-BvDSGsHZR*5Vf_dIfFWa+VIv*7h|OM zQ-F4irTue)-VhWRG=)A!bqws?pgCc53~D2=6kRk(jq7RY|1vGMxyAVAqQrVp1`&u( zlG;g{ic=f$C8!;zxft=Fps57S5s4)+ln`~6^4!<)FSoiV-^*JNSxRXqY1k$^M}yJm zXrX7eQu8hQT=DGa`LlJa>L^n>K~s9lw9G@#&fx!=*Q}2_()1@xt!BDf?}Ayk0_o0} z72gXi*K@sUHWN%brnIKlDIUmXLPL%(@`256JLNZPw(0qS={FqD+^52X9kd$GZ0wl( zOqi(nO)qd`X{h#}v~2jOx)pON0$1yclafs>8mOu$M8EjQ7yrUj$b@mW8upw}%8`NV zd#9!JWoUSfFg+pt`h;6^!jyE>q}6cXt>!DdqF&Nc97ve7{pNVhDWot3>Yi11LNO60 zbSR|^Vu>~?OriO*Kdp4*lwVU}GTNHYNbW#bf1W;lWNg}(7gfWmIORqwnDo7|m-MUX z2hNz1m9fCLeK#1Zxt?Rm6Sb-{F0C;>lCs|H1C3U=ZM9pD95CM1PF{Lqi?PcX6kPB# z`b3|RjqNx34awq_BuSvNk|g)gc|?-r{+}d?;0~b28SWa%F`ai`>%8k?#3EJK06z{E z=$>^~aQlKa?$p-eb~D7zjBJM4T4Z9vmnK7aEzYwU$w-qiRF8nKWRBC|mAuKW_!sC( zJ^~Hi*@1JeLjF`HO@LZ^M;-zC64R-Wlo{5{as^H$nd52;S3Rf-3Avryk8#zDN~1E` zhADVRo%h*8} z!CvdFTNh6!#}-y(`A#>ryEj(@tdp-O`I}+ZESjz|1K&sf z$to_mVAAqTc430`tHER#w`N>bNNXz9DWQ`wOiWKY(uvZMQ#4GeidA#U9{Atmd#BJ7 zCiGT@q9_NTAqQD4;d-_+Q_4i4mxo76nJ7wbrk6%lDZMnPLh))TrB_>CX|zhoz)=AP zqIYX!o~Nr$Q07G(hD-G%DoW-=&PZ|-jm=DEhqr)%OgPT0Dnh8RnQJ({@`dKUX;6GN}odY z<_G+I|9l}HaojdY!g$@WyfCH$yATV-OepH1P#e}L_dpYjK#Iwd#1^q#^b0*&NQMdA s%8gcJlJCIi;CO9XV~s7oqopRt^mBFJZq}S*ykc!N7t8I=^>07)H~P+yhyVZp literal 0 HcmV?d00001 diff --git a/src/todoit/linearAlgebra/__pycache__/solutions_3.cpython-34.pyc b/src/todoit/linearAlgebra/__pycache__/solutions_3.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9de1ade8879d6d5e074fea8f94c165d8af552187 GIT binary patch literal 1617 zcmZ`(TWcFf6h1S1(MpkQ$5yf3gwi_cWuchHeJF$w>R$Y43@!vz1+&(UPU()9Qql2$MRiDT^f4S@k!j+)MyY;C!pA)*te>6_H`K6=5S2= zJ%<;3bxb5dUt^Ykg7k@I4vi0}BAQA%7xc~RuW07d)S-c(O47igDP%6Sv8+P@L%fwj z^FPub*XR{ndh7-21D|HtV^gyfi3-}pz0ReLP-iamHfFv(H)!yHrXJgYwjo&@+Ebs} zvzpauGH+V5QjT@&V|^CTG~jx0j1!m!bYxkFp|h_A?mTA_wv0d4QPx++kBUNNv5{F} zoL*iUr%W>A45P}3?@Udr@-!-x@fM3aR8eO9G|8g0Z=581W4uV~XcE?q7#lG$A~RxW zL}KJ{Y~*Ceh%+o_zmeWV;J^32ecnCG^$%qc_0&-@sZR5(dtjISyi(m#_qtUc=SkH? z5Ebd&w6BhJ)GhOLR3&*<9(}%DOib-0AL&HtB{(hHAS8UTCf*ZUa#emLpUU@f(vQFo zh4G6?k>*v8ug)KXpn=K2C^x`G|Iu&M3~k}Pq&84@?$8Vm> zvM@o%E^W)Nh%PV$T}Zlc=)|QM!=qn;Hw3VNJ-687eVuWL0q`PXSI`xL-sjhD`3u_y z#VZsCj32>z4=8?k|JKMTnOuUS@jkI6ywztPqmp z*L4`K-hhVR>Kq)pt0C8t>$rwl9)Mf|;h>9WO7siD0DpFKB54z~5|@A~R2HxOe^dlP z3kHXwT*OBIf>KpSaa2XvJK1O$x<*DoSNRNcN3S5dMVEdTJL8WE=&DaRF&~7)6a0Q! zS4qD-&Cj-9q|h2rQby9dHCMv~K&{2Lkwv1H*(K+oYbnU``XjB3%eXY| zI8BDeoiO#sX>_bo<5fwODpQYcQ|Yw!Mp^GJGvo4QLeGd@z0MJyf^=N?eb(V5)DbLg zRYF&PzzSPTo-nZmv$d+-6ouL4V-OOL#m8b0: + y.append(0) + else: + y.append(2) +y=np.array(y) + +# write Your code, Fourier function +plt.plot(x,y,color='g',label='origin') +plt.plot(x,fourier(x,y,3),color='r',label='3') +plt.plot(x,fourier(x,y,8),color='b',label='8') +plt.plot(x,fourier(x,y,23),color='k',label='23') + +plt.legend() +plt.axis('equal') +plt.show() diff --git a/src/todoit/linearAlgebra/hw_3-3.py b/src/todoit/linearAlgebra/hw_3-3.py new file mode 100644 index 0000000..8f2d885 --- /dev/null +++ b/src/todoit/linearAlgebra/hw_3-3.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# coding: utf-8 +#copyRight by heibanke + +import numpy as np +import matplotlib.pyplot as plt +from solutions_3 import * + +A=np.array([[3,1],[2,4]])/4.0 + +# write Your code +# def eigshow(A): + +eigshow(A) \ No newline at end of file diff --git a/src/todoit/linearAlgebra/hw_3-4.py b/src/todoit/linearAlgebra/hw_3-4.py new file mode 100644 index 0000000..46b3380 --- /dev/null +++ b/src/todoit/linearAlgebra/hw_3-4.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# coding: utf-8 +#copyRight by heibanke + +import numpy as np + +import time +def time_cost(f): + def _f(*arg, **kwarg): + start = time.clock() + a=f(*arg,**kwarg) + end = time.clock() + print(f.__name__,"run cost time is ",end-start) + return a + return _f + + +@time_cost +def fib_opt_seq(seq): + return [fib_opt(i) for i in seq] + +def fib_opt(n): + a,b,i=0,1,0 + + while i