Complete the code to fit a linear model to x and y using np.polyfit.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) coefficients = np.polyfit(x, y, [1])
For a linear regression, the degree of the polynomial is 1.
Complete the code to predict y values using the fitted coefficients.
import numpy as np x = np.array([1, 2, 3, 4, 5]) coefficients = np.polyfit(x, np.array([2, 4, 5, 4, 5]), 1) predicted_y = coefficients[[1]] * x + coefficients[1]
The first coefficient (index 0) is the slope (m) in y = mx + b.
Fix the error in the code to correctly fit a linear model and predict y.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) coefficients = np.polyfit(x, y, [1]) predicted_y = coefficients[0] * x + coefficients[[2]]
Degree must be 1 for linear fit. Intercept is at index 1.
Fill both blanks to create a dictionary of x values and their predicted y values using np.polyfit.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) coeffs = np.polyfit(x, y, [1]) predictions = {val: coeffs[0] * val + coeffs[[2]] for val in x}
Degree 1 for linear fit. Intercept is coefficients[1].
Fill all three blanks to create a list of predicted y values for x using np.polyfit and a list comprehension.
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) coeffs = np.polyfit(x, y, [1]) predicted = [coeffs[[2]] * val + coeffs[[3]] for val in x]
Degree 1 for linear fit. Slope is coeffs[0], intercept is coeffs[1].
