gpt4 book ai didi

python - 如何以正确的方式缩放和预测单个样本

转载 作者:行者123 更新时间:2023-11-30 09:40:15 25 4
gpt4 key购买 nike

根据乳腺癌数据集(5 个特征 + 1 个诊断列),我在标准化数据 (StandardScaler()) 上训练并测试了逻辑模型。我使用 Pickle 导入模型:

log = pickle.load(open('./log.pkl', 'rb'))

并且想要预测新样本属于 0 类(良性)还是 1 类(恶性)。

下面的测试数据属于1类(我尝试了1类的多个样本,所有结果都属于0分类):

radius = 11.41
texture = 10.82
perimeter = 73.34
area = 403.3
smoothness = 0.09373

为了创建样本并获得预测,我尝试了以下操作:

temp = [radius, texture, perimeter, area, smoothness]
temp = np.array(temp).reshape((len(temp), 1))
scaler = StandardScaler()
temp = scaler.fit_transform(temp)

# print(log.predict(temp)) # results in: ValueError: X has 1 features per sample; expecting 5
print(log.predict(temp.T)) # results in: [0] which is wrong

# print(log.predict_proba(temp)) # results in: ValueError: X has 1 features per sample; expecting 5
print(log.predict_proba(temp.T)) # results in: [[9.99999972e-01 2.78352951e-08]] which does not seem right

我也尝试过:

new_sample = np.array([radius, texture, perimeter, area, smoothness])
# scaled_sample = scaler.fit_transform(new_sample.reshape(1, -1)) # resulting array: array([[0., 0., 0., 0., 0.]])
# scaled_sample = scaler.fit_transform(new_sample.reshape(1, -1).T) # same as below
scaled_sample = scaler.fit_transform(new_sample[:, np.newaxis])
print(log.predict(scaled_sample.T)) # results in [0] which is wrong
print(log.predict_proba(scaled_sample.T)) # results in: [[9.99999972e-01 2.78352951e-08]] which differs from the predict_proba above, and seems off

如何进行这种预测的正确方法?

谢谢

祝你好运,比 git

最佳答案

根据 predict 上的 scikit-learn 文档,您的代码可能看起来更简单功能:

temp = np.array([[radius, texture, perimeter, area, smoothness]]) # use double brackets
scaler = StandardScaler()
print(log.predict(scaler.fit_transform(temp)))

这是正确的使用方法。但这个函数不能说明回归器拟合的质量。

关于python - 如何以正确的方式缩放和预测单个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59266368/

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