376 lines
24 KiB
HTML
376 lines
24 KiB
HTML
<!--
|
|
Automatically generated HTML file from DocOnce source
|
|
(https://github.com/hplgit/doconce/)
|
|
-->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Week 34: Introduction to the course, Logistics and Practicalities">
|
|
|
|
<title>Week 34: Introduction to the course, Logistics and Practicalities</title>
|
|
|
|
<!-- Bootstrap style: bootstrap -->
|
|
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
|
<!-- not necessary
|
|
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
|
-->
|
|
|
|
<style type="text/css">
|
|
|
|
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
|
.dropdown-menu {
|
|
height: auto;
|
|
max-height: 400px;
|
|
overflow-x: hidden;
|
|
}
|
|
|
|
/* Adds an invisible element before each target to offset for the navigation
|
|
bar */
|
|
.anchor::before {
|
|
content:"";
|
|
display:block;
|
|
height:50px; /* fixed header height for style bootstrap */
|
|
margin:-50px 0 0; /* negative fixed header height */
|
|
}
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<!-- tocinfo
|
|
{'highest level': 2,
|
|
'sections': [('Overview of first week', 2, None, '___sec0'),
|
|
('Thursday August 20', 2, None, '___sec1'),
|
|
('Lectures and ComputerLab', 2, None, '___sec2'),
|
|
('Course Format', 2, None, '___sec3'),
|
|
('Teachers', 2, None, '___sec4'),
|
|
('Deadlines for projects (tentative)', 2, None, '___sec5'),
|
|
('Recommended textbooks', 2, None, '___sec6'),
|
|
('Prerequisites', 2, None, '___sec7'),
|
|
('Learning outcomes', 2, None, '___sec8'),
|
|
('Topics covered in this course: Statistical analysis and '
|
|
'optimization of data',
|
|
2,
|
|
None,
|
|
'___sec9'),
|
|
('Topics covered in this course: Machine Learning',
|
|
2,
|
|
None,
|
|
'___sec10'),
|
|
('Extremely useful tools, strongly recommended',
|
|
2,
|
|
None,
|
|
'___sec11'),
|
|
('Other courses on Data science and Machine Learning at UiO',
|
|
2,
|
|
None,
|
|
'___sec12'),
|
|
('Introduction', 2, None, '___sec13'),
|
|
('What is Machine Learning?', 2, None, '___sec14'),
|
|
('Types of Machine Learning', 2, None, '___sec15'),
|
|
('Software and needed installations', 2, None, '___sec16'),
|
|
('Python installers', 2, None, '___sec17'),
|
|
('Useful Python libraries', 2, None, '___sec18'),
|
|
('Installing R, C++, cython or Julia', 2, None, '___sec19'),
|
|
('Installing R, C++, cython, Numba etc', 2, None, '___sec20'),
|
|
('Numpy examples and Important Matrix and vector handling '
|
|
'packages',
|
|
2,
|
|
None,
|
|
'___sec21'),
|
|
('Basic Matrix Features', 2, None, '___sec22'),
|
|
('Some famous Matrices', 3, None, '___sec23'),
|
|
('More Basic Matrix Features', 3, None, '___sec24'),
|
|
('Numpy and arrays', 2, None, '___sec25'),
|
|
('Matrices in Python', 2, None, '___sec26'),
|
|
('Meet the Pandas', 2, None, '___sec27'),
|
|
('Friday August 21', 2, None, '___sec28'),
|
|
('Reading Data and fitting', 2, None, '___sec29'),
|
|
('Friday August 21', 2, None, '___sec30'),
|
|
('Simple linear regression model using _scikit-learn_',
|
|
3,
|
|
None,
|
|
'___sec31'),
|
|
('To our real data: nuclear binding energies. Brief reminder on '
|
|
'masses and binding energies',
|
|
3,
|
|
None,
|
|
'___sec32'),
|
|
('Organizing our data', 3, None, '___sec33'),
|
|
('Seeing the wood for the trees', 3, None, '___sec34'),
|
|
('And what about using neural networks?', 3, None, '___sec35'),
|
|
('A first summary', 2, None, '___sec36')]}
|
|
end of tocinfo -->
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
TeX: {
|
|
equationNumbers: { autoNumber: "none" },
|
|
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
|
}
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Bootstrap navigation bar -->
|
|
<div class="navbar navbar-default navbar-fixed-top">
|
|
<div class="navbar-header">
|
|
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</button>
|
|
<a class="navbar-brand" href="week34-bs.html">Week 34: Introduction to the course, Logistics and Practicalities</a>
|
|
</div>
|
|
|
|
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
|
<ul class="nav navbar-nav navbar-right">
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
<!-- navigation toc: --> <li><a href="._week34-bs001.html#___sec0" style="font-size: 80%;"><b>Overview of first week</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs002.html#___sec1" style="font-size: 80%;"><b>Thursday August 20</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs003.html#___sec2" style="font-size: 80%;"><b>Lectures and ComputerLab</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs004.html#___sec3" style="font-size: 80%;"><b>Course Format</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs005.html#___sec4" style="font-size: 80%;"><b>Teachers</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs006.html#___sec5" style="font-size: 80%;"><b>Deadlines for projects (tentative)</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs007.html#___sec6" style="font-size: 80%;"><b>Recommended textbooks</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs008.html#___sec7" style="font-size: 80%;"><b>Prerequisites</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs009.html#___sec8" style="font-size: 80%;"><b>Learning outcomes</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs010.html#___sec9" style="font-size: 80%;"><b>Topics covered in this course: Statistical analysis and optimization of data</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs011.html#___sec10" style="font-size: 80%;"><b>Topics covered in this course: Machine Learning</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs012.html#___sec11" style="font-size: 80%;"><b>Extremely useful tools, strongly recommended</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs013.html#___sec12" style="font-size: 80%;"><b>Other courses on Data science and Machine Learning at UiO</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs014.html#___sec13" style="font-size: 80%;"><b>Introduction</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs015.html#___sec14" style="font-size: 80%;"><b>What is Machine Learning?</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs016.html#___sec15" style="font-size: 80%;"><b>Types of Machine Learning</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs017.html#___sec16" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs018.html#___sec17" style="font-size: 80%;"><b>Python installers</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs019.html#___sec18" style="font-size: 80%;"><b>Useful Python libraries</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs020.html#___sec19" style="font-size: 80%;"><b>Installing R, C++, cython or Julia</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs021.html#___sec20" style="font-size: 80%;"><b>Installing R, C++, cython, Numba etc</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs022.html#___sec21" style="font-size: 80%;"><b>Numpy examples and Important Matrix and vector handling packages</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs023.html#___sec22" style="font-size: 80%;"><b>Basic Matrix Features</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs024.html#___sec23" style="font-size: 80%;"> Some famous Matrices</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs025.html#___sec24" style="font-size: 80%;"> More Basic Matrix Features</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs026.html#___sec25" style="font-size: 80%;"><b>Numpy and arrays</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;"><b>Matrices in Python</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs028.html#___sec27" style="font-size: 80%;"><b>Meet the Pandas</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs029.html#___sec28" style="font-size: 80%;"><b>Friday August 21</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs030.html#___sec29" style="font-size: 80%;"><b>Reading Data and fitting</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs031.html#___sec30" style="font-size: 80%;"><b>Friday August 21</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec31" style="font-size: 80%;"> Simple linear regression model using <b>scikit-learn</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec32" style="font-size: 80%;"> To our real data: nuclear binding energies. Brief reminder on masses and binding energies</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec33" style="font-size: 80%;"> Organizing our data</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec34" style="font-size: 80%;"> Seeing the wood for the trees</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec35" style="font-size: 80%;"> And what about using neural networks?</a></li>
|
|
<!-- navigation toc: --> <li><a href="._week34-bs032.html#___sec36" style="font-size: 80%;"><b>A first summary</b></a></li>
|
|
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div> <!-- end of navigation bar -->
|
|
|
|
<div class="container">
|
|
|
|
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
|
|
|
<a name="part0027"></a>
|
|
<!-- !split -->
|
|
|
|
<h2 id="___sec26" class="anchor">Matrices in Python </h2>
|
|
|
|
<p>
|
|
Having defined vectors, we are now ready to try out matrices. We can
|
|
define a \( 3 \times 3 \) real matrix \( \hat{A} \) as (recall that we user
|
|
lowercase letters for vectors and uppercase letters for matrices)
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
|
<span style="color: #008000; font-weight: bold">print</span>(A)
|
|
</pre></div>
|
|
<p>
|
|
If we use the <b>shape</b> function we would get \( (3, 3) \) as output, that is verifying that our matrix is a \( 3\times 3 \) matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row-major order, see below) as
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
|
<span style="color: #408080; font-style: italic"># print the first column, row-major order and elements start with 0</span>
|
|
<span style="color: #008000; font-weight: bold">print</span>(A[:,<span style="color: #666666">0</span>])
|
|
</pre></div>
|
|
<p>
|
|
We can continue this was by printing out other columns or rows. The example here prints out the second column
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>log(np<span style="color: #666666">.</span>array([ [<span style="color: #666666">4.0</span>, <span style="color: #666666">7.0</span>, <span style="color: #666666">8.0</span>], [<span style="color: #666666">3.0</span>, <span style="color: #666666">10.0</span>, <span style="color: #666666">11.0</span>], [<span style="color: #666666">4.0</span>, <span style="color: #666666">5.0</span>, <span style="color: #666666">7.0</span>] ]))
|
|
<span style="color: #408080; font-style: italic"># print the first column, row-major order and elements start with 0</span>
|
|
<span style="color: #008000; font-weight: bold">print</span>(A[<span style="color: #666666">1</span>,:])
|
|
</pre></div>
|
|
<p>
|
|
Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the <a href="http://www.numpy.org/" target="_self">Numpy website for more details</a>. Useful functions when defining a matrix are the <b>np.zeros</b> function which declares a matrix of a given dimension and sets all elements to zero
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
|
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to zero</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros( (n, n) )
|
|
<span style="color: #008000; font-weight: bold">print</span>(A)
|
|
</pre></div>
|
|
<p>
|
|
or initializing all elements to
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
|
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to one</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones( (n, n) )
|
|
<span style="color: #008000; font-weight: bold">print</span>(A)
|
|
</pre></div>
|
|
<p>
|
|
or as unitarily distributed random numbers (see the material on random number generators in the statistics part)
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
|
<span style="color: #408080; font-style: italic"># define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]</span>
|
|
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n, n)
|
|
<span style="color: #008000; font-weight: bold">print</span>(A)
|
|
</pre></div>
|
|
<p>
|
|
As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.
|
|
As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors
|
|
\( \hat{x}, \hat{y}, \hat{z} \) with \( n \) elements each. The covariance matrix is defined as
|
|
$$
|
|
\hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\
|
|
\sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\
|
|
\sigma_{zx} & \sigma_{zy} & \sigma_{zz}
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
where for example
|
|
$$
|
|
\sigma_{xy} =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
|
|
$$
|
|
|
|
The Numpy function <b>np.cov</b> calculates the covariance elements using the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have the exact mean values.
|
|
The following simple function uses the <b>np.vstack</b> function which takes each vector of dimension \( 1\times n \) and produces a \( 3\times n \) matrix \( \hat{W} \)
|
|
$$
|
|
\hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\
|
|
x_1 & y_1 & z_1 \\
|
|
x_2 & y_2 & z_2 \\
|
|
\dots & \dots & \dots \\
|
|
x_{n-2} & y_{n-2} & z_{n-2} \\
|
|
x_{n-1} & y_{n-1} & z_{n-1}
|
|
\end{bmatrix},
|
|
$$
|
|
|
|
<p>
|
|
which in turn is converted into into the \( 3\times 3 \) covariance matrix
|
|
\( \hat{\Sigma} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
|
the mean value of each set of samples \( \hat{x} \) etc using the Numpy
|
|
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
|
covariance matrix through the <b>np.linalg.eig()</b> function.
|
|
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
|
|
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
|
|
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
|
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
|
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(x))
|
|
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
|
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(y))
|
|
z <span style="color: #666666">=</span> x<span style="color: #666666">**3+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
|
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(z))
|
|
W <span style="color: #666666">=</span> np<span style="color: #666666">.</span>vstack((x, y, z))
|
|
Sigma <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cov(W)
|
|
<span style="color: #008000; font-weight: bold">print</span>(Sigma)
|
|
Eigvals, Eigvecs <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(Sigma)
|
|
<span style="color: #008000; font-weight: bold">print</span>(Eigvals)
|
|
</pre></div>
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
|
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy</span> <span style="color: #008000; font-weight: bold">import</span> sparse
|
|
eye <span style="color: #666666">=</span> np<span style="color: #666666">.</span>eye(<span style="color: #666666">4</span>)
|
|
<span style="color: #008000; font-weight: bold">print</span>(eye)
|
|
sparse_mtx <span style="color: #666666">=</span> sparse<span style="color: #666666">.</span>csr_matrix(eye)
|
|
<span style="color: #008000; font-weight: bold">print</span>(sparse_mtx)
|
|
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-10</span>,<span style="color: #666666">10</span>,<span style="color: #666666">100</span>)
|
|
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sin(x)
|
|
plt<span style="color: #666666">.</span>plot(x,y,marker<span style="color: #666666">=</span><span style="color: #BA2121">'x'</span>)
|
|
plt<span style="color: #666666">.</span>show()
|
|
</pre></div>
|
|
<p>
|
|
<p>
|
|
<!-- navigation buttons at the bottom of the page -->
|
|
<ul class="pagination">
|
|
<li><a href="._week34-bs026.html">«</a></li>
|
|
<li><a href="._week34-bs000.html">1</a></li>
|
|
<li><a href="">...</a></li>
|
|
<li><a href="._week34-bs019.html">20</a></li>
|
|
<li><a href="._week34-bs020.html">21</a></li>
|
|
<li><a href="._week34-bs021.html">22</a></li>
|
|
<li><a href="._week34-bs022.html">23</a></li>
|
|
<li><a href="._week34-bs023.html">24</a></li>
|
|
<li><a href="._week34-bs024.html">25</a></li>
|
|
<li><a href="._week34-bs025.html">26</a></li>
|
|
<li><a href="._week34-bs026.html">27</a></li>
|
|
<li class="active"><a href="._week34-bs027.html">28</a></li>
|
|
<li><a href="._week34-bs028.html">29</a></li>
|
|
<li><a href="._week34-bs029.html">30</a></li>
|
|
<li><a href="._week34-bs030.html">31</a></li>
|
|
<li><a href="._week34-bs031.html">32</a></li>
|
|
<li><a href="._week34-bs032.html">33</a></li>
|
|
<li><a href="._week34-bs028.html">»</a></li>
|
|
</ul>
|
|
<!-- ------------------- end of main content --------------- -->
|
|
|
|
</div> <!-- end container -->
|
|
<!-- include javascript, jQuery *first* -->
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
|
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
|
|
|
<!-- Bootstrap footer
|
|
<footer>
|
|
<a href="http://..."><img width="250" align=right src="http://..."></a>
|
|
</footer>
|
|
-->
|
|
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright only on the titlepage -->
|
|
</center>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|
|
|