【问题标题】:How do I calculate y for every value of x if the slope a set to 10 and the intercept b to 0?如果斜率 a 设置为 10,截距 b 设置为 0,我如何计算每个 x 值的 y?
【发布时间】:2021-06-05 07:10:41
【问题描述】:

我有一小部分 data。我使用 python3 读取它并创建了一个scatter plot。我的下一个任务是将斜率 a 设置为 10,将截距 b 设置为 0,并为 xy /em>。该任务说我不应该使用任何现有的线性回归函数。我已经被困了一段时间了。我该怎么做?

【问题讨论】:

    标签: python python-3.x dataframe scatter-plot intercept


    【解决方案1】:

    如果你的斜率已经设置为 10,我不明白你为什么需要使用线性回归。我希望我没有遗漏你的任务。

    但是,如果您需要在 python 中获取所有元素乘以斜率a 的列表,则保留这一点,然后您可以使用列表推导通过以下方式找到这个新列表:

    y_computed = [item*a for item in x]

    【讨论】:

      【解决方案2】:

      您实际上可以在同一个图上画一条具有恒定斜率 (10) 的线,然后根据该线“估计”计算预测的 y 值(如果需要,您还可以找到估计的误差)。使用以下方法完成:

      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)
      

      【讨论】:

        猜你喜欢
        • 2018-08-01
        • 1970-01-01
        • 2017-12-17
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 2020-07-11
        • 2019-05-16
        • 2014-02-19
        相关资源
        最近更新 更多