【问题标题】:How to predict value?如何预测价值?
【发布时间】:2020-01-27 02:57:04
【问题描述】:

我已经绘制了一个散点图,其中包含几天内相对湿度的线性回归。 给定的天数是 244。 现在我应该预测第 245 天的相对湿度值。

--> 这是数据集的样本值。

指数相对湿度

0 78.80

1 80.80

2 78.60

3 76.10

4 73.85

5 71.40

x=linear.index
y=linear["RH"]
plt.title('Air Temperature vs. Relative Humidity')
plt.title(' Relative Humidity over Days')
plt.ylabel('Relative Humidity')
plt.xlabel('Days')

fit = np.polyfit(x,y,1)
fit_fn = np.poly1d(fit)
reg= plt.plot(x,y, 'yo', x, fit_fn(x), '--k')
reg

现在寻找预测值

from sklearn.linear_model import LinearRegression

regr = LinearRegression()
regr.fit(linear[["RH"]],linear.index)
regr.predict(linear[245])

我得到的错误通常在“'list' object has no attribute 'predict'”之间,因为我已经尝试了一些不同的方法和代码,但似乎都没有。

【问题讨论】:

  • regr.fit(linear[["RH"]],linear.index[240]) 中,linear.index[240]) 只是一个值。你需要传递长度为linear[["RH"]]的数组。
  • @vbrises 你能给我一个数组输入的例子吗?我不明白数组是如何工作的,因为我只想预测一个数据的值。

标签: python scikit-learn linear-regression data-science predict


【解决方案1】:

这是一个图形化 Python 多项式拟合器,使用 numpy.polyfit() 进行拟合,使用 numpy.polyval() 进行评估,此示例包含单个值。多项式顺序设置在代码的顶部,直线可以设置为“1”。

import numpy, matplotlib
import matplotlib.pyplot as plt

xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7, 0.0])
yData = numpy.array([1.1, 20.2, 30.3, 40.4, 50.0, 60.6, 70.7, 0.1])


polynomialOrder = 2 # example quadratic equation


# curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
print('Fitted Parameters:', fittedParameters)

# predict a single value
print('Single value prediction:', numpy.polyval(fittedParameters, 3.0))

# Use polyval to find model predictions
modelPredictions = numpy.polyval(fittedParameters, xData)
absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = numpy.polyval(fittedParameters, xModel)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_title('numpy.polyval example') # add a title
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 2018-02-17
    • 2020-06-19
    • 1970-01-01
    • 2017-10-13
    • 2018-11-04
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多