如何告诉SciKit的LinearRegression模型预测值不能小于零?

我有以下代码,尝试根据非价格基础特征来估值股票。

price = df.loc[:,'regularMarketPrice']features = df.loc[:,feature_list]# X_train, X_test, y_train, y_test = train_test_split(features, price, test_size = 0.15, random_state = 1)if len(X_train.shape) < 2:    X_train = np.array(X_train).reshape(-1,1)    X_test = np.array(X_test).reshape(-1,1)# model = LinearRegression()model.fit(X_train,y_train)# print('Train Score:', model.score(X_train,y_train))print('Test Score:', model.score(X_test,y_test))# y_predicted = model.predict(X_test)

在我的df中(非常大),从来没有’regularMarketPrice’小于0的实例。然而,我偶尔会收到一些点在y_predicted中的值小于0的情况。

在Scikit中有没有一种方法可以说任何小于0的值都是无效预测?我希望这能使我的模型更准确。

如果需要进一步解释,请评论。


回答:

为了使更多预测值大于0,你不应该使用线性回归。你应该考虑使用广义线性回归(glm),比如泊松回归。

from sklearn.linear_model import PoissonRegressorprice = df.loc[:,'regularMarketPrice']features = df.loc[:,feature_list]# X_train, X_test, y_train, y_test = train_test_split(features, price, test_size = 0.15, random_state = 1)if len(X_train.shape) < 2:    X_train = np.array(X_train).reshape(-1,1)    X_test = np.array(X_test).reshape(-1,1)# model = PoissonRegressor()model.fit(X_train,y_train)# print('Train Score:', model.score(X_train,y_train))print('Test Score:', model.score(X_test,y_test))# y_predicted = model.predict(X_test)

所有预测值都大于或等于0

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注