Skip to content

Commit 6cb30cf

Browse files
committed
refined numpy syntax
1 parent a76fa9a commit 6cb30cf

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,sebastian,Macintosh.local,16.06.2014 22:50,file:///Users/sebastian/Library/Application%20Support/LibreOffice/4;

tutorials/matrix_cheatsheet_only.html

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
<meta name="generator" content="LibreOffice 4.2.4.2 (MacOSX)">
77
<meta name="created" content="0;0">
8-
<meta name="changed" content="20140605;223752655486000">
8+
<meta name="changed" content="20140616;225009144635000">
99
<style type="text/css">
1010
<!--
1111
td p { color: #000000; font-family: "Verdana"; font-size: 12pt }
@@ -22,7 +22,7 @@
2222
<col width="511">
2323
<col width="690">
2424
<tr>
25-
<td height="57" bgcolor="#0066cc" style="border: none; padding: 0in">
25+
<td height="58" bgcolor="#0066cc" style="border: none; padding: 0in">
2626
<p align="center"><font color="#ffffff"><font face="Arial"><font size="5" style="font-size: 18pt"><b>Task</b></font></font></font></p>
2727
</td>
2828
<td bgcolor="#0066cc" style="border: none; padding: 0in">
@@ -92,10 +92,15 @@
9292
3</font></font></p>
9393
</td>
9494
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
95-
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
96-
a = np.array([[1],[2],[3]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
97-
a<br>array([[1],<br>&nbsp;&nbsp; &nbsp; &nbsp; [2],<br>&nbsp;&nbsp;
98-
&nbsp; &nbsp; [3]])</font></font></p>
95+
<p align="left"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">
96+
</font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">a</font></font><font color="#2fff12"><font face="AndaleMono, serif"><font size="4" style="font-size: 14pt">
97+
</font></font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">=
98+
np.array([1,2,3]).reshape(1,3)</font></font></font></p>
99+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">
100+
</font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape<br>(1,
101+
3)</font></font></font></p>
102+
<p align="left"><br>
103+
</p>
99104
</td>
100105
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
101106
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -124,6 +129,14 @@
124129
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
125130
b = np.array([1,2,3])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
126131
b<br>array([1, 2, 3])</font></font></p>
132+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
133+
note that numpy doesn't have <br># explicit “row-vectors”, but
134+
1-D <br># arrays</font></font></p>
135+
<p style="margin-bottom: 0in"><font face="Liberation Serif, serif"><font size="3" style="font-size: 12pt"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font color="#2fff12"><font face="AndaleMono, serif"><font size="1" style="font-size: 8pt"><b>
136+
</b></font></font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape</font></font></font></font></font></p>
137+
<p style="margin-bottom: 0in"><font face="Andale Mono"><font size="1" style="font-size: 8pt">(3,)</font></font></p>
138+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><br>
139+
</p>
127140
</td>
128141
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
129142
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -213,7 +226,7 @@
213226
</td>
214227
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
215228
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
216-
np.ones([3,2])<br>array([[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp;
229+
np.ones((3,2))<br>array([[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp;
217230
[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp; [ 1.,&nbsp; 1.]])</font></font></p>
218231
</td>
219232
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
@@ -551,10 +564,10 @@
551564
A = np.array([[1,2,3],[4,5,6],[7,8,9]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
552565
A<br>array([[1, 2, 3],<br>&nbsp;&nbsp; &nbsp; &nbsp; [4, 5, 9],<br>&nbsp;&nbsp;
553566
&nbsp; &nbsp; [7, 8, 9]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
554-
total_elements = A.shape[0] * A.shape[1]<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
555-
B = A.reshape(1, total_elements)&nbsp;<br><br># or
556-
A.reshape(1,9)<br># Alternative: A.shape = (1,9)&nbsp;<br>#
557-
to&nbsp;change the array in place<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
567+
total_elements = np.prod(A.shape)</font></font></p>
568+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
569+
B = A.reshape(1, total_elements)&nbsp;<br><br># alternative
570+
shortcut:<br># A.reshape(1,-1)<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
558571
B<br>array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])</font></font></p>
559572
</td>
560573
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
@@ -1107,7 +1120,7 @@
11071120
</td>
11081121
</tr>
11091122
<tr valign="top">
1110-
<td height="539" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
1123+
<td height="540" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
11111124
<p align="left"><font face="Arial"><font size="2" style="font-size: 10pt"><b>Generating
11121125
a Gaussian dataset:<br><br>creating random vectors from the
11131126
multivariate normal<br>distribution given mean and covariance

0 commit comments

Comments
 (0)