I have a small set of data . I used python3 to read it and created a scatter plot . My next task is to set the slope a to 10 and the intercept b to 0 and calculate y for every value of x . The task says I should not use any existing linear regression functions. I have been stuck for some time on this. How can I do that?
If your slope is already set to 10, I don't see why you need to use Linear Regression. I hope I'm not missing anything from your task.
However, keeping that aside if you need to get a list in python with all elements multiplied by your slope a
then you can use a list comprehension to find this new list in the following way:
y_computed = [item*a for item in x]
You can literally just draw a line with a constant slope (10) on the same plot, then calculate the the predicted y-value based on that line "estimate" (you can also find the error of the estimate if you want). That be done using the following:
import numpy as np
from matplotlib import pyplot as plt
def const_line(x):
y = 10 * x + 0 # Just to illustrate that the intercept is zero
return y
x = np.linspace(0, 1)
y = const_line(x)
plt.plot(x, y, c='m')
plt.show()
# Find the y-values for each sample point in your data:
for x in data:
const_line(x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.