You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/mooreslaw-tutorial.md
+18-59Lines changed: 18 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -44,27 +44,24 @@ the 53 years following his prediction. You will determine the best-fit constants
44
44
45
45
* NumPy
46
46
*[Matplotlib](https://matplotlib.org/)
47
-
*[statsmodels](https://www.statsmodels.org) ordinary linear regression
48
47
49
48
imported with the following commands
50
49
51
50
```{code-cell}
52
51
import matplotlib.pyplot as plt
53
52
import numpy as np
54
-
import statsmodels.api as sm
55
53
```
56
54
57
55
**2.** Since this is an exponential growth law you need a little background in doing math with [natural logs](https://en.wikipedia.org/wiki/Natural_logarithm) and [exponentials](https://en.wikipedia.org/wiki/Exponential_function).
58
56
59
-
You'll use these NumPy, Matplotlib, and statsmodels functions:
57
+
You'll use these NumPyand Matplotlib functions:
60
58
61
59
*[`np.loadtxt`](https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html): this function loads text into a NumPy array
62
60
*[`np.log`](https://numpy.org/doc/stable/reference/generated/numpy.log.html): this function takes the natural log of all elements in a NumPy array
63
61
*[`np.exp`](https://numpy.org/doc/stable/reference/generated/numpy.exp.html): this function takes the exponential of all elements in a NumPy array
64
62
*[`lambda`](https://docs.python.org/3/library/ast.html?highlight=lambda#ast.Lambda): this is a minimal function definition for creating a function model
65
63
*[`plt.semilogy`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.semilogy.html): this function will plot x-y data onto a figure with a linear x-axis and $\log_{10}$ y-axis
66
64
[`plt.plot`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot.html): this function will plot x-y data on linear axes
67
-
*[`sm.OLS`](https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.OLS.html): find fitting parameters and standard errors using the statsmodels ordinary least squares model
68
65
* slicing arrays: view parts of the data loaded into the workspace, slice the arrays e.g. `x[:10]` for the first 10 values in the array, `x`
69
66
* boolean array indexing: to view parts of the data that match a given condition use boolean operations to index an array
70
67
*[`np.block`](https://numpy.org/doc/stable/reference/generated/numpy.block.html): to combine arrays into 2D arrays
@@ -215,59 +212,31 @@ where $\mathbf{y}$ are the observations of the log of the number of
215
212
transistors in a 1D array and $\mathbf{Z}=[\text{year}_i^1,~\text{year}_i^0]$ are the
216
213
polynomial terms for $\text{year}_i$ in the first and second columns. By
217
214
creating this set of regressors in the $\mathbf{Z}-$matrix you set
218
-
up an ordinary least squares statistical model. Some clever
219
-
NumPy array features will build $\mathbf{Z}$
215
+
up an ordinary least squares statistical model.
220
216
221
-
1.`year[:,np.newaxis]` : takes the 1D array with shape `(179,)` and turns it into a 2D column vector with shape `(179,1)`
222
-
2.`**[1, 0]` : stacks two columns, in the first column is `year**1` and the second column is `year**0 == 1`
217
+
`Z` is a linear model with two parameters, i.e. a polynomial with degree `1`.
218
+
Therefore we can represent the model with `numpy.polynomial.Polynomial` and
219
+
use the fitting functionality to determine the model parameters:
223
220
224
221
```{code-cell}
225
-
Z = year[:, np.newaxis] ** [1, 0]
222
+
model = np.polynomial.Polynomial.fit(year, yi, deg=1)
226
223
```
227
224
228
-
Now that you have the created a matrix of regressors, $\mathbf{Z},$ and
229
-
the observations are in vector, $\mathbf{y},$ you can use these
230
-
variables to build the an ordinary least squares model with
0 commit comments