I am trying to draw the line of regression onto a scatter graph. I have two functions:
def place_y(x, slope, intercept):
return slope * x + intercept
def draw_line_of_regression():
"""The line of regression can be used to predict further values"""
import matplotlib.pyplot as plt # used to draw graph
from scipy import stats
# Example shows relationship between age and speed
age_x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
speed_y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_error = stats.linregress(age_x, speed_y)
# gets variables used in drawing the line of regression
line_of_regression = list(map(place_y(age_x, slope, intercept), age_x))
plt.scatter(age_x, speed_y) # draws scatter graph
plt.plot(age_x, line_of_regression)
plt.show() # shows the graph
draw_line_of_regression()
When this is run there is an error with the place_y() function. Error:
return slope * x + intercept
TypeError: can't multiply sequence by non-int of type 'numpy.float64
from Drawing line of regression onto scatter graph in python
No comments:
Post a Comment