DDC-49: Imaging polynomials
A data challenge a day helps you master machine learning
About these daily data challenges
Each post is an exercise that helps you learn about data in Python.
Try to solve the exercise before checking my solution at the bottom of the post 🤓
You can share your solution or visualization in the comments!
Today’s challenge
Create a 3x101 matrix in which the rows correspond to the polynomials x, x^2, and x^3, and the columns correspond 101 values of x from -2 to +2. Visualize the matrix as below (colormap is RdBu_r.
.
.
.
.
Scroll down for the solution…
.
.
.
.
.
.
.
.
keep scrolling!
.
.
.
.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2,2,101)
M = np.vstack( (x**1,x**2,x**3) )
plt.figure(figsize=(10,3))
plt.imshow(M,aspect=’auto’,extent=[x[0],x[-1],-.5,2.5],
vmin=-2,vmax=2,cmap=’RdBu_r’)
plt.gca().set(xlabel=’x’,yticks=[0,1,2],
yticklabels=[’$x$’,’$x^2$’,’$x^3$’])
plt.show()


