-
Notifications
You must be signed in to change notification settings - Fork 4
/
01-operations.html
140 lines (140 loc) · 6.33 KB
/
01-operations.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Advanced NumPy</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Advanced NumPy</h1></a>
<h2 class="subtitle">Operations on NumPy arrays</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Learner will explain the difference between element-wise and matrix product of two arrays.</li>
<li>Learner will apply reduction functions (mean, min, max) along a given axis.</li>
<li>Learner will be able to find a specialised numerical algorithm from the ones available in numpy.</li>
<li>Learner will be able to sort array along given axis.</li>
</ul>
</div>
</section>
<p>Multiplication of two arrays is elementwise. For example, to calculate a square of each element we may use:</p>
<pre><code>>>> a = np.arange(3)
>>> a
array([0, 1, 2])
>>> b = a * a
>>> b
array([0, 1, 4])</code></pre>
<p>Matrix products are calculated using the <code>np.dot</code> function:</p>
<pre><code>>>> np.dot(a, a)
5</code></pre>
<p>For 1-D arrays the same result can be obtained by:</p>
<pre><code>>>> np.sum(a * a)
5</code></pre>
<h3 id="axis-based-reductions">Axis-based reductions</h3>
<p>The <code>np.sum</code> function sums all elements regardless of the number of array dimensions:</p>
<pre><code>>>> b = np.arange(9).reshape(3,3)
>>> b
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> np.sum(b)
36</code></pre>
<p>If you want to sum only columns or rows, you need to pass the index of the axis over which you want to sum:</p>
<pre><code>>>> np.sum(b, 0)
array([ 9, 12, 15])
>>> np.sum(b, 1)
array([ 3, 12, 21])</code></pre>
<p>Other similar reduction functions are <code>np.min</code>, <code>np.max</code> or <code>np.mean</code>:</p>
<pre><code>>>> np.min(b)
0
>>> np.min(b, 0)
array([0, 1, 2])
>>> np.min(b, 1)
array([0, 3, 6])</code></pre>
<p>You can also find the index of the minimum element in each axis:</p>
<pre><code>>>> np.argmin(b, 0)
array([0, 0, 0])</code></pre>
<h3 id="sorting">Sorting</h3>
<p>NumPy also implement various sorting algorithms. To sort elements of an array you can use <code>np.sort</code> functions:</p>
<pre><code>>>> a = np.random.rand(4)
>>> a
array([ 0.9490829 , 0.07528673, 0.17463988, 0.95964801])
>>> np.sort(a)
array([ 0.07528673, 0.17463988, 0.9490829 , 0.95964801])</code></pre>
<p>Similarly to the reduction functions, you can also pass the axis index to sort along:</p>
<pre><code>>>> b = a.reshape(2, 2)
>>> b
array([[ 0.9490829 , 0.07528673],
[ 0.17463988, 0.95964801]])
>>> np.sort(b, 0)
array([[ 0.17463988, 0.07528673],
[ 0.9490829 , 0.95964801]])
>>> np.sort(b, 1)
array([[ 0.07528673, 0.9490829 ],
[ 0.17463988, 0.95964801]])</code></pre>
<p><code>np.argsort</code> returns the order of elements in a sorted array:</p>
<pre><code>>>> np.argsort(a)
array([1, 2, 0, 3])</code></pre>
<h3 id="special-modules">Special modules</h3>
<p>NumPy also provides extra modules implementing basic numerical methods:</p>
<ul>
<li><code>np.linalg</code> – linear algebra,</li>
<li><code>np.fft</code> – fast Fourier transform,</li>
<li><code>np.random</code> – random number generators.</li>
</ul>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="finding-closest-element"><span class="glyphicon glyphicon-pencil"></span>Finding closest element</h2>
</div>
<div class="panel-body">
<p>Generate a 10 x 3 array of random numbers (using <code>np.random.rand</code>). From each row, find the column index of the element closest to 0.75. Make use of np.abs and np.argmin. The result should be a one-dimensional array of integers from 0 to 2.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="solving-linear-equations"><span class="glyphicon glyphicon-pencil"></span>Solving linear equations</h2>
</div>
<div class="panel-body">
<p>Solve the following system of linear equations using <code>np.linalg.solve</code>. Test the solution. <span class="math display">\[2x + 3y = 3\]</span> <span class="math display">\[5x - y = 6\]</span></p>
</div>
</section>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/paris-swc/advanced-numpy-lesson">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</body>
</html>