Skip to content

Commit 9341046

Browse files
committed
MAINT: Replace polyfit/polyval with Polynomial class.
The Polynomial class is recommended over the poly* functional interface for new code.
1 parent 87be1e4 commit 9341046

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

content/tutorial-ma.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,20 @@ Finally, we can use the [numpy.polyfit](https://numpy.org/devdocs/reference/gene
268268

269269
```{code-cell}
270270
t = np.arange(len(china_total))
271-
params = np.polyfit(t[~china_total.mask], valid, 3)
272-
cubic_fit = np.polyval(params, t)
271+
model = np.polynomial.Polynomial.fit(t[~china_total.mask], valid, 3)
273272
plt.plot(t, china_total)
274-
plt.plot(t, cubic_fit, "--")
273+
plt.plot(t, model(t), "--")
275274
```
276275

277276
This plot is not so readable since the lines seem to be over each other, so let's summarize in a more elaborate plot. We'll plot the real data when
278277
available, and show the cubic fit for unavailable data, using this fit to compute an estimate to the observed number of cases on January 28th 2020, 7 days after the beginning of the records:
279278

280279
```{code-cell}
281280
plt.plot(t, china_total)
282-
plt.plot(t[china_total.mask], cubic_fit[china_total.mask], "--", color="orange")
283-
plt.plot(7, np.polyval(params, 7), "r*")
281+
plt.plot(t[china_total.mask], model(t)[china_total.mask], "--", color="orange")
282+
plt.plot(7, model(7), "r*")
284283
plt.xticks([0, 7, 13], dates[[0, 7, 13]])
285-
plt.yticks([0, np.polyval(params, 7), 10000, 17500])
284+
plt.yticks([0, model(7), 10000, 17500])
286285
plt.legend(["Mainland China", "Cubic estimate", "7 days after start"])
287286
plt.title(
288287
"COVID-19 cumulative cases from Jan 21 to Feb 3 2020 - Mainland China\n"

0 commit comments

Comments
 (0)