简体   繁体   English

如果斜率 a 设置为 10,截距 b 设置为 0,我如何计算每个 x 值的 y?

[英]How do I calculate y for every value of x if the slope a set to 10 and the intercept b to 0?

I have a small set of data .我有一小部分数据 I used python3 to read it and created a scatter plot .我使用 python3 读取它并创建了一个散点图 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 .我的下一个任务是将斜率a设置为 10,将截距b设置为 0,并为x的每个值计算y 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.如果您的斜率已经设置为 10,我不明白为什么需要使用线性回归。 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:但是,如果您需要在 python 中获取所有元素乘以斜率a的列表,则保留这一点,那么您可以使用列表推导通过以下方式找到这个新列表:

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).您实际上可以在同一 plot 上绘制一条具有恒定斜率 (10) 的线,然后根据该线“估计”计算预测的 y 值(如果需要,您还可以找到估计的误差)。 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算多项式拟合的误差(斜率和截距) - How to calculate error for polynomial fitting (in slope and intercept) 如何使用 Python 在给定斜率和截距坐标 (x, y) 的图像上绘制一条线 - How to draw a line on an image given the slope and the intercept coordinates (x, y) using Python 如何在python中给定点(x,y)和线与y轴的夹角的情况下找到线的斜率(m)? - How do I find the slope (m) for a line given a point (x,y) on the line and the line's angle from the y axis in python? 如何计算一组 x、y 坐标和位置变量之间的距离? - How do I calculate the distance between a set x, y coordinate and location variables? 如何计算斜率 b/w 2 点? - How to calculate slope b/w 2 points? 如何在Python中计算x / y坐标的长度? - How do I calculate the length of an x/y coordinate in Python? 我想使用pykalman模块计算线性拟合的斜率和截距 - I want to calculate slope and intercept of a linear fit using pykalman module 如何计算python中简单线性回归拟合的截距和斜率? - How to calculate intercept and slope of the simple linear regression fit in python? 对于折线图(matplotlib,pandas)的 dataframe 中缺少 x 值,如何将 y 值设置为 0 - How do I set y value as 0 for missing x values in dataframe for line graph (matplotlib, pandas) 我如何在xy平面中生成一个与y0(a,b)相距10步的随机点(x,y)? - How can I generate a random point (x, y) 10 steps apart from y0(a, b) in xy-plane?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM