gpt4 book ai didi

python - 我如何找到模型将输入分类为 [0,1] 的概率

转载 作者:行者123 更新时间:2023-11-30 09:43:47 24 4
gpt4 key购买 nike

我正在研究一个分类问题,我想找到“输入被分类为 [1,0]”和“不是 [1,0]”的概率

我尝试使用SVC的predict_proba方法,它给出了我不寻找的类的概率

from sklearn.svm import SVC

model = SVC(probability=True)
model.fit(final_data,foreclosure_y)
results = model.predict_proba(final_data_test)[0]

我希望我的输出是这样的

index,y
---------
0,0.45
1,0.62
2,0.43
3,0.12
4,0.55

注意:以上输出采用 .csv 格式,其中 y 是 test_y

这里的 y 列是索引从 0 到 4 的每个实例的概率,可以分类为 0 或 1

例如:- 索引 0 有 0.45 的概率被分类为 0 或 1

最佳答案

请注意

sum([0.58502114, 0.41497886])
# 1.0

predict_proba 给出两个类的概率(因此数组元素总和为 1),按照它们出现在 model.classes_< 中的顺序;引用自docs (在这种情况下,它们永远是你最好的 friend ):

Returns the probability of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

这是一个带有玩具数据的示例来说明这个想法:

from sklearn.svm import SVC
model = SVC(probability=True)
X = [[1,2,3], [2,3,4]] # feature vectors
Y = [0, 1] # classes
model.fit(X, Y)

现在让我们获取训练集中第一个实例的预测概率[1,2,3]:

model.predict_proba(X)[0]
# array([0.39097541, 0.60902459])

好的,顺序是什么 - 即哪个概率属于哪个类?

model.classes_
# array([0, 1])

因此,这意味着属于类0的实例的概率是数组0.39097541的第一个元素,而属于类的概率1 是第二个元素0.60902459;再次,它们的总和为 1,正如预期的那样:

sum([0.39097541, 0.60902459])
# 1.0

更新

现在,在您需要的输出中,我们不会同时输入两种概率;按照惯例,对于二元分类,我们仅包含属于类别 1 的每个实例的概率;以下是我们如何对上面显示的只有 2 个实例的玩具数据集 X 执行此操作:

pred = model.predict_proba(X)
pred
# array([[ 0.39097541, 0.60902459],
# [ 0.60705475, 0.39294525]])

import pandas as pd
out = pd.DataFrame(pred[:,1],columns=['y']) # keep only the second element of the arrays in pred, i.e. the probability for class 1
print(out)

结果:

          y
0 0.609025
1 0.392945

关于python - 我如何找到模型将输入分类为 [0,1] 的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com