Scientific Computing
The fundamental package for scientific computing with Python.
Basically, to do scientific computing it would be nice to have:
(These are the stated features of NumPy.)
intercepts.py
solution from last time.
The Python Package Index (PyPI) is a repository of software for the Python programming language.
The most popular tool for installing Python packages, and the one included with modern versions of Python.
pip
is a command line utilitypip
was the only real options for installing Python programs, but has experienced limitations as the package ecosystem has grown quite large.pip
python
or python3
import
as
, used to shorten namesimport pw as piecewise
np
import
taxes
a bittaxes[0]
is a list, we can look at that list’s initial element.Cutoff | Rate |
---|---|
9275 | .10 |
37650 | .15 |
91150 | .25 |
190150 | .28 |
413350 | .33 |
415051 | .35 |
Cutoff | Rate |
---|---|
413350 | .33 |
415051 | .35 |
.396 |
In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. For instance, if each element of the data were a number, we might visualize a “one-dimensional” array like a list:
\[ \begin{array}{|c||c|c|c|} \hline 9275 & 37650 & 91150 & 190150 \\ \hline \end{array} \]
A two-dimensional array would be like a table:
\[ \begin{array}{|c||c|c|c|} \hline 9275 & 37650 & 91150 & 190150 \\ \hline .10 & .15 & .25 & .28 \\ \hline 0 & -463.75 & -6963.25 & -16470.75 \\ \hline \end{array} \]
A three-dimensional array would be like a set of tables, perhaps stacked as though they were printed on separate pages. In NumPy, this idea is generalized to an arbitrary number of dimensions, and so the fundamental array class is called
ndarray
: it represents an “N-dimensional array”.
taxes
part of an array of tax policiesnp.array()
numpy.array
if you used import numpy
array([[9.27500e+03, 1.00000e-01],
[3.76500e+04, 1.50000e-01],
[9.11500e+04, 2.50000e-01],
[1.90150e+05, 2.80000e-01],
[4.13350e+05, 3.30000e-01],
[4.15051e+05, 3.50000e-01]])
\[a \times 10^b\]
\[a \times 10^b\]
Speed of light: The speed of light in a vacuum is approximately \(300,000,000 \text{ m/s}\) \[ 3 \times 10^8 \text{ m/s} \]
Mass of an electron: The mass of an electron is approximately \(0.00000000000000000000000000091093837 \text{ g}\). \[ 9.1093837 \times 10^{-28} \text{ g} \]
dtype
is data type - We’ll cover it soon.1:4
takes all elements at index beginning at 1
and stopping before getting to index 4
.\[ \small \begin{array}{|c|c|c|c|c|c|} \hline 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline red & orange& yellow& green& blue& indigo& violet\\ \hline \end{array} \]
\[ \small \begin{array}{|c|c|c|} \hline 1 & 2 & 3 \\ \hline orange& yellow& green\\ \hline \end{array} \]
:
from the slice, it is treated as if a zero was provided.:
from the slice, it is as if the length was provided.len
gives the length of a list or array2
- every other element.[x,y]
instead of [x][y]
=
=
, (4) new element//
30
is an integer.30/9
is a decimal value…//
integer division.
/
there. What happens?1
)array([[9.27500e+03, 1.00000e-01],
[3.76500e+04, 1.50000e-01],
[9.11500e+04, 2.50000e-01],
[1.90150e+05, 2.80000e-01],
[4.13350e+05, 3.30000e-01],
[4.15051e+05, 3.50000e-01]])
9275
9275
to 37650
.9275
is useful to two brackets.[:, 0]
means for every row (slice :
) take the initial columns (index 0
)
0
and the last bracket ends at infinity.
np.inf
insert
to add an element at an index:+
works on lists.
0
as a 0-D (zero dimensional) array.[0]
would also work.array([ 927.5 , 4256.25, 13375. , 27720. , 73656. , 595.35])
7
by a vector of length 6
?bracket_cost
does correctly describe the cost within on bracket, someone in the n+1
’th bracket pays the cost the previous n
brackets.np.cumsum
.np.cumsum
, there are other functions over arrays we often use.np.mean
is quite common:np.inf
support mathematicsYou might hear of a 0-D (zero-dimensional) array referred to as a “scalar”, a 1-D (one-dimensional) array as a “vector”, a 2-D (two-dimensional) array as a “matrix”, or an N-D (N-dimensional, where “N” is typically an integer greater than 2) array as a “tensor”.
For clarity, it is best to avoid the mathematical terms when referring to an array because the mathematical objects with these names behave differently than arrays (e.g. “matrix” multiplication is fundamentally different from “array” multiplication), and there are other objects in the scientific Python ecosystem that have these names (e.g. the fundamental data structure of PyTorch is the “tensor”).
taxes
, create the array on the right:array([[9.27500e+03, 1.00000e-01, 3.76500e+04, 1.50000e-01, 9.11500e+04,
2.50000e-01],
[1.90150e+05, 2.80000e-01, 4.13350e+05, 3.30000e-01, 4.15051e+05,
3.50000e-01]])
0
and 500000
.
time
0
to 50000
import numpy as np
rng = np.random.default_rng()
incomes = rng.integers(0, 500000, 1000000)
print(incomes[::10000])
[164219 205640 135407 395905 409039 335473 226284 193945 168204 466685
108853 220358 409575 13367 44930 27362 242105 278487 35833 131346
99155 31995 53368 332440 161793 284517 408948 415420 405893 392534
134528 373840 431580 203174 180125 324829 177619 97037 432494 305369
496279 11634 85458 467016 159980 348472 73514 365492 426172 20126
53096 57521 412181 125836 219181 92477 93626 425009 99766 406239
218541 485387 187187 486400 50693 409695 71800 213456 405162 148490
362513 118057 491664 479600 22245 348599 338973 380644 119857 234855
58350 464280 332301 291197 159519 108239 425589 201118 477737 241862
393534 322556 163955 316297 332545 316997 281938 77769 68896 259183]
time
time
before a command.
real
time is how much passes in real life$ time python3 onemil.py
[123177 422613 471310 380518 95385 143328 426503 453832 427403 106416
327306 476263 65814 281381 422404 59938 14231 232824 342190 329545
412684 112339 202498 5071 59114 394601 451216 92268 381107 487447
55089 339493 344836 261917 148326 452850 409130 484951 427839 307217
259268 485208 331277 183015 132480 345930 439366 6814 39743 268276
80739 293355 170394 4220 48082 15668 453927 58059 320294 101182
1864 492297 130465 9920 76321 345944 268312 255875 46614 195236
233737 443948 343483 116870 165561 326265 103567 327780 475672 392212
396479 328248 43273 32596 246212 4258 60202 66783 135035 155327
469638 378485 175496 428130 493185 154716 193012 424037 197666 103758]
real 0m0.155s
user 0m1.883s
sys 0m0.033s
single_tax
function as in the “Shell” exercise.