如何更改sklearn中的whentopredictone参数?

2024-10-02 00:40:04 发布

您现在位置:Python中文网/ 问答频道 /正文

众所周知,在logistic回归算法中,当θ乘以X大于0.5时,我们就可以预测一个。我想提高精度值。所以我想改变预测函数,当θ乘以X大于0.7或其他值大于0.5时,预测1。在

如果我写算法,我可以很容易地做到。但是对于sklearn包,我不知道该怎么做。在

有人能帮我吗?在

为了更清楚地解释这个问题,下面是以八度为单位的预测函数wroten:

p = sigmoid(X*theta);

for i=1:size(p)(1)
    if p(i) >= 0.6
        p(i) = 1;
    else
        p(i) = 0;
    endif;
endfor

Tags: 函数算法forsizeif精度单位sklearn
2条回答

sklearn的LogisticRegression预测器对象有一个predict_proba方法,它输出输入示例属于某个类的概率。你可以使用这个函数和你自己定义的θ乘以X来获得你想要的功能。在

例如:

from sklearn import linear_model
import numpy as np

np.random.seed(1337)  # Seed random for reproducibility
X = np.random.random((10, 5))  # Create sample data
Y = np.random.randint(2, size=10)

lr = linear_model.LogisticRegression().fit(X, Y)

prob_example_is_one = lr.predict_proba(X)[:, 1]

my_theta_times_X = 0.7  # Our custom threshold
predict_greater_than_theta = prob_example_is_one > my_theta_times_X

这是predict_proba的docstring:

^{pr2}$

这适用于二进制和多类分类:

from sklearn.linear_model import LogisticRegression
import numpy as np

#X = some training data
#y = labels for training data
#X_test = some test data

clf = LogisticRegression()
clf.fit(X, y)

predictions = clf.predict_proba(X_test)

predictions = clf.classes_[np.argmax(predictions > threshold, axis=1)]

相关问题 更多 >

    热门问题