Warning: Declaration of action_plugin_tablewidth::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/tablewidth/action.php on line 93
python_getting_started [CS545 fall 2016]

User Tools

Site Tools


python_getting_started

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python_getting_started [2013/08/14 11:10]
asa created
python_getting_started [2016/08/09 10:25] (current)
Line 105: Line 105:
 To plot the data, first import the ''​pyplot''​ module. To plot the data, first import the ''​pyplot''​ module.
  
-<​code>​+<​code ​python>
 In [6]: import matplotlib.pyplot as plt In [6]: import matplotlib.pyplot as plt
  
Line 148: Line 148:
 <code python> <code python>
 In [14]: x = np.arange(10,​ 0.1) In [14]: x = np.arange(10,​ 0.1)
- 
-In [15]: plt.pl 
-plt.plot ​      ​plt.plot_date ​ plt.plotfile ​  ​plt.plotting ​   
  
 In [15]: plt.plot(x, x**2, '​ob'​) In [15]: plt.plot(x, x**2, '​ob'​)
 Out[15]: [<​matplotlib.lines.Line2D at 0x1054162d0>​] Out[15]: [<​matplotlib.lines.Line2D at 0x1054162d0>​]
- 
 </​code>​ </​code>​
-{{ Notes:​plot2.png?​400 ​ }}+/* {{ Notes:​plot2.png?​400 ​ }}*/
  
  
-We can add a second plot to the same axes by calling //plot// again without the call to //clf()//. +We can add a second plot to the same axes by calling //plot// again: 
-<​code>​ +<​code ​python
-In [47]: plt.plot(x,​x**2+In [16]: plt.plot(x, x, '​dr'​
-Out[47]: [<​matplotlib.lines.Line2D object at 0x3608990>​]+Out[16]: [<​matplotlib.lines.Line2D object at 0x3608990>​]
 </​code>​ </​code>​
  
-{{ Notes:​plot3.png?​400 ​ }}+/*{{ Notes:​plot3.png?​400 ​ }}*/
  
  
Line 173: Line 169:
  
 Of course! ​ No data analysis tool is worth the bytes it burns if it Of course! ​ No data analysis tool is worth the bytes it burns if it
-doesn'​t. The python ​ //numpy// module ​provides the magic to work with matrices as +doesn'​t. The ''​numpy''​ package ​provides the required ​magic. 
-//ndarray//'s.  +Let's create an array that represents the following matrix:
- +
- +
-We have several ways to create an array.  Make this array +
 \[\left ( \begin{array}{cc} \[\left ( \begin{array}{cc}
  1 & 2\\  1 & 2\\
Line 196: Line 189:
 </​code>​ </​code>​
  
-This array can be copied and reshaped by +Let'​s ​construct ​the matrices
-<​code>​ +
-In [22]: m.reshape(1,​6) +
-Out[22]: array([[1, 2, 3, 4, 5, 6]]) +
- +
-In [23]: m +
-array([[1, 2], +
-       [3, 4], +
-       [5, 6]]) +
-</​code>​ +
-To change m, you must assign it or use resize. +
-<​code>​ +
-In [24]: m = m.reshape(1,​6) +
- +
-In [7]: m +
-Out[7]: array([[1, 2, 3, 4, 5, 6]]) +
- +
-In [8]: m.resize((2,​3)) +
- +
-In [9]: m +
-Out[9]:  +
-array([[1, 2, 3], +
-       [4, 5, 6]]) +
-</​code>​ +
-We could use the //numpy// function //arange// followed by //​reshape//:​ +
-<​code>​ +
-In [26]: np.arange(10) +
-Out[26]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) +
- +
-In [27]: np.arange(10).reshape(2,​5) +
-Out[27]:  +
-array([[0, 1, 2, 3, 4], +
-       [5, 6, 7, 8, 9]]) +
-</​code>​ +
-Want to know more?  Try +
-  np.reshape?​ +
- +
-Let'​s ​make the matrices+
 \[a = \left ( \begin{array}{cc} \[a = \left ( \begin{array}{cc}
  2 & 2 & 2\\  2 & 2 & 2\\
Line 245: Line 201:
  7 & 8 & 9  7 & 8 & 9
  \end{array} \right ) \]  \end{array} \right ) \]
-We can use //resize// for the first one, and //resize// or //​reshape//​ +<​code ​python
-for the second. ​ (//resize// changes the array it is applied to; +In [16]: a = np.ones((3,​3)) ​* 2
-//reshape// makes a new version) +
-<​code>​ +
-In [65]: a = np.resize(2,(3,3))+
  
-In [66]: a +In [17]: a 
-Out[66]:  +Out[17]:  
-array([[2, 2, 2], +array([[ 2. 2. 2.], 
-       [2, 2, 2], +       [ 2. 2. 2.], 
-       [2, 2, 2]])+       [ 2. 2. 2.]])
  
-In [67]: b = np.resize(np.arange(9)+1,​(3,​3))+In [18]: b = np.resize(np.arange(9)+1,​(3,​3))
  
-In [68]: b +In [19]: b 
-Out[68]: +Out[19]: 
 array([[1, 2, 3], array([[1, 2, 3],
        [4, 5, 6],        [4, 5, 6],
Line 267: Line 220:
  
 What is the value of $a * b$? What is the value of $a * b$?
-<​code>​ +<​code ​python
-In [69]: a * b +In [20]: a * b 
-Out[69]: +Out[21]: 
 array([[ 2,  4,  6], array([[ 2,  4,  6],
        [ 8, 10, 12],        [ 8, 10, 12],
        [14, 16, 18]])        [14, 16, 18]])
 </​code>​ </​code>​
-The //*// operator does a component-wise multiplication. ​ Use the +The ''​*'' ​operator does a component-wise multiplication. ​ Use the 
-//numpy// function ​//dot// to do matrix multiplication. +''​numpy'' ​function ​''​dot'' ​to do matrix multiplication. 
-<​code>​ + 
-In [70]: np.dot(a,​b) +<​code ​python
-Out[70]: +In [22]: np.dot(a,​b) 
 +Out[22]: 
 array([[24, 30, 36], array([[24, 30, 36],
        [24, 30, 36],        [24, 30, 36],
Line 285: Line 239:
  
 An array is transposed by An array is transposed by
-<​code>​ +<​code ​python
-In [75]: b.transpose() +In [23]: b.transpose() 
-Out[75]:  +Out[23]: 
-array([[1, 4, 7], +
-       [2, 5, 8], +
-       [3, 6, 9]]) +
- +
-In [76]: b.T +
-Out[76]: +
 array([[1, 4, 7], array([[1, 4, 7],
        [2, 5, 8],        [2, 5, 8],
        [3, 6, 9]])        [3, 6, 9]])
  
-In [77]: np.transpose(b) +In [24]: b.T 
-Out[77]: +Out[24]: 
 array([[1, 4, 7], array([[1, 4, 7],
        [2, 5, 8],        [2, 5, 8],
Line 305: Line 253:
 </​code>​ </​code>​
  
-What is $a b^T$? +Elements and sub-matrices are easily extracted: 
-<​code>​ +<​code ​python
-In [78]: np.dot(a,​b.T) +In [25]: b 
-Out[78]:  +Out[25]: 
-array([[12, 30, 48], +
-       [12, 30, 48], +
-       [12, 30, 48]]) +
-</​code>​ +
- +
-Elements and sub-matrices are easily extracted. Given the previous +
-assignment of $b$, +
-<​code>​ +
-In [79]: b +
-Out[79]: +
 array([[1, 2, 3], array([[1, 2, 3],
        [4, 5, 6],        [4, 5, 6],
        [7, 8, 9]])        [7, 8, 9]])
  
-In [80]: b[0,0] +In [26]: b[0,0] 
-Out[80]: 1+Out[26]: 1
  
-In [81]: b[0,1] +In [27]: b[0,1] 
-Out[81]: 2+Out[27]: 2
  
-In [82]: b[0:​1,​0:​1] +In [28]: b[0:2, 1:3] 
-Out[82]: array([[1]]) +Out[28]: 
- +
-In [83]: b[0:​2,​0:​1] +
-Out[83]:  +
-array([[1],​ +
-       ​[4]]) +
- +
-In [84]: b[0:​2,​1:​2] +
-Out[84]:  +
-array([[2],​ +
-       ​[5]]) +
- +
-In [85]: b[0:​2,​1:​3] +
-Out[85]: +
 array([[2, 3], array([[2, 3],
        [5, 6]])        [5, 6]])
 </​code>​ </​code>​
  
-Let's multiply $a$ by the first column of $b$. +Let's multiply ​the first row of a $a$ by the second ​column of $b$. 
-<​code>​ +<​code ​python
-In [89]: a +In [29]: np.dot(a[0], b[:,1]) 
-Out[89]:  +Out[29]: 30.0
-array([[22, 2], +
-       [2, 2, 2], +
-       [2, 2, 2]]) +
- +
-In [91]: b +
-Out[91] +
-array([[1, 2, 3], +
-       [4, 5, 6], +
-       [7, 8, 9]]) +
- +
-In [92]: b[:,0+
-Out[92]: array([1, 4, 7])+
  
-In [93]: np.dot(a,b[:,0]) +In [30]: np.dot(a[0],b.T[1]) 
-Out[93]: array([24, 24, 24])+Out[30]: 30.0
 </​code>​ </​code>​
-What happenend? ​ This should be a 3x3 times 3x1 operation, resulting 
-in a 3x1 matrix, but the answer shows a 1x3 matrix.  ​ 
  
-While ''​b''​ is two-dimensonal,​ ''​b[:,​0]''​ is one-dimensional. ​ It is the +How do I find the inverse of a matrix? ​  
-first column, but as a vector. ​ To keep the two-dimensional nature, +<​code ​python>
-use ''​b[:,​0:​1]''​ or just ''​b[:,:​1]''​. +
-<​code>​ +
-In [94]: b[:,:1] +
-Out[94]:  +
-array([[1],​ +
-       ​[4],​ +
-       ​[7]]) +
- +
-In [95]: np.dot(a,​b[:,:​1]) +
-Out[95]:  +
-array([[24],​ +
-       ​[24],​ +
-       ​[24]]) +
-</​code>​ +
- +
-How do I find the inverse of a matrix?  ​Search the net for //numpy +
-inverse//​. ​ +
-<​code>​+
 In [2]: z = np.array([[2,​1,​1],​[1,​2,​2],​[2,​3,​4]]) In [2]: z = np.array([[2,​1,​1],​[1,​2,​2],​[2,​3,​4]])
  
python_getting_started.1376500229.txt.gz · Last modified: 2016/08/09 10:25 (external edit)