-
Notifications
You must be signed in to change notification settings - Fork 4
/
00-intro.html
105 lines (105 loc) · 5.74 KB
/
00-intro.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
<!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">Introduction</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 know how to import <code>numpy</code></li>
<li>Learner will be able to create one- and more dimensional arrays with zeros/ones, given elements or random elements.</li>
<li>Learner will be able to apply a function to all elements of an array.</li>
</ul>
</div>
</section>
<p>NumPy array is a data container. It is similar to Python lists, but it’s specialised for working on numerical data. NumPy is at the center of scientific Python ecosystem and it is a work-horse of many scientific libraries including scikit-learn, scikit-image, matplotlib, SciPy.</p>
<p>To use NumPy we need to start python interpreter and import <code>numpy</code> package – it’s customary the use the following import statement, which will make all NumPy functions available under the <code>np</code> prefix:</p>
<pre><code>import numpy as np</code></pre>
<p>If <code>numpy</code> was installed correctly, this should not produce any messages. Let’s create a simple three-element NumPy array:</p>
<pre><code>>>> x = np.array([2, 1, 5])
>>> x
array([2, 1, 5])</code></pre>
<p>One of the advantages of NumPy is that it allows to apply functions (called <code>ufunc</code>s) to all elements of an array without the need of for loops:</p>
<pre><code>>>> np.sin(x)
array([ 0.90929743, 0.84147098, -0.95892427])</code></pre>
<p>This is not only convenient but also more efficient than iterating through the elements using for loops. Similarly, we can add scalars to all elements or multiply them by a constant:</p>
<pre><code>>>> x + 1
array([3, 2, 6])</code></pre>
<p>To construct an array with pre-defined elements we can also use one of the built-in helper functions. <code>np.arange</code> works like Python built-in <code>range</code>, but it returns an array; <code>np.ones</code> and <code>np.zeros</code> returns arrays of 0s or 1s; <code>np.random.rand</code> creates an array of random number from an interval [0, 1]:</p>
<pre><code>>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
>>> np.random.rand(5)
array([ 0.27386612, 0.42769767, 0.38762774, 0.63308478, 0.46215844])</code></pre>
<p>We can also construct a two- or more dimensional arrays:</p>
<pre><code>>>> x = np.array([[1, 2], [5, 6]])
>>> x
array([[1, 2],
[5, 6]])
>>> np.ones((2, 2))
array([[ 1., 1.],
[ 1., 1.]])</code></pre>
<p>Alternatively, a n-dimensional array can be obtained by reshaping a 1-D array:</p>
<pre><code>>>> a = np.arange(9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a.reshape(3,3)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])</code></pre>
<p>Note that in this case we used a method of the array itself called <code>reshape</code> rather than a function from NumPy module (<code>np.reshape</code>). Both ways are possible and it’s usually only a matter of convenience which one we choose in a particular case.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="creating-a-square-array"><span class="glyphicon glyphicon-pencil"></span>Creating a square array</h2>
</div>
<div class="panel-body">
<p>Create a 5x5 square array with number 5 on diagonal and zeros otherwise. Consider using <code>np.eye</code> function (you can check the docstring).</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>