在sklearn中使用LogisticRegression预测降水量

问题描述 投票:0回答:1

我有一个数据集,其中包含一些有关天气的参数。所有特征都是数字的,并且目标是连续的。我想预测降水量。我的功能如下所示(年、月和日只是我的 DataFrame 的多索引):

_, _, X, y = read_daily_data()
print(X)
                   MEANT         RH         WS          WD        CCT         MSLP       MAXT      MINT
Year Month Day                                                                                         
2014 1     1    4.494412  90.203694  16.615975  166.495278  59.916667  1014.029167   8.720245  0.310245
           2    5.978995  92.044333  20.621631  184.099628  63.875000  1008.670833   9.240245  3.530245
           3    6.586079  88.778159  22.263927  183.268500  50.108334  1013.070833  10.400246  2.340245
           4    6.358579  94.172092  15.272616  158.277724  66.666667  1007.625000   8.480246  4.600245
           5    4.995662  86.622807  16.897822  225.090521  59.383333  1010.754167   7.480245  0.440245
...                  ...        ...        ...         ...        ...          ...        ...       ...
2023 11    8    7.268995  82.063136  17.965620  202.643657  33.016667  1019.379167  12.380245  3.760245
           9    7.729829  82.235617  25.143419  196.132513  69.020834  1010.795833  10.380245  3.690246
           10   9.101078  76.940065  27.342357  228.518643  61.875000  1005.745833  10.670245  7.960245
           11   7.350245  82.186650  22.030794  242.243293  49.875000  1010.391667   8.660245  4.260245
           12   5.818162  93.582846  18.648649  181.010854  85.333333  1010.112500  11.230246  2.140245

[3603 rows x 8 columns]

这也是我的目标:

print(y)
Year  Month  Day
2014  1      1       1.4
             2       6.8
             3       0.8
             4      16.5
             5       5.5
                    ... 
2023  11     8       0.0
             9       4.2
             10      9.3
             11      3.2
             12     14.0
Name: PT, Length: 3603, dtype: float64

我将线性回归应用于我的数据集:

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.3, random_state=42)
std = StandardScaler()
std.fit(X)
X_train_std = std.transform(X_train)
sgd_reg = SGDRegressor(random_state=42)
sgd_reg.fit(X_train_std, y_train)
X_test_std = std.transform(X_test)
sgd_score = sgd_reg.score(X_test_std, y_test)
print(f"{sgd_score:.3f}")

这给了我这个分数:

0.385

现在,当我想应用逻辑回归时:

lgs_reg = LogisticRegression(random_state=42)
lgs_reg.fit(X_train_std, y_train)

我收到此错误:

ValueError: Unknown label type: continuous. Maybe you are trying to fit a classifier, which expects discrete classes on a regression target with continuous values.

我知道为分类构建的模型(例如 LogisticRegression)通过返回连续值进行预测,然后我们使用阈值对其进行量化。如果我按照自己的方式实现逻辑回归(例如使用 sigmoid 函数),我绝对可以将目标值输入为连续数字。我的问题是为什么 scikit-learn 不接受这个?

还建议在我的具体问题中使用逻辑回归等分类模型。我理解的一件事是使用离散化将连续目标稀疏为间隔。但之后这个模型的分数可以与线性回归相媲美吗?

感谢您的帮助。

scikit-learn regression classification logistic-regression
1个回答
0
投票

你的问题实际上是一个回归问题而不是分类问题。虽然

LogisticRegression
从名字上看起来像是回归算法,但它实际上是一种分类算法(我知道,令人困惑)。

因此,您应该使用 scikit-learn 中提供的任何回归算法。您有所有模型的列表here,您可以选择任何适合的模型进行回归。线性模型是很好的开始,并且

HistGradientBoostingRegressor
通常是一个很好的竞争者。

© www.soinside.com 2019 - 2024. All rights reserved.