gpt4 book ai didi

Python scikit-learn - 类型错误

转载 作者:太空狗 更新时间:2023-10-30 01:40:23 25 4
gpt4 key购买 nike

我正在编写一个小程序来为具有交叉验证的数据集绘制 SVM 和朴素贝叶斯的学习曲线。这是绘图函数的代码

import numpy as np
import matplotlib.pyplot as plt
from sklearn import cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.learning_curve import learning_curve

def plot_learning_curves(X, y, nb=GaussianNB, svc=SVC(kernel='linear'), ylim=None, cv=None, n_jobs=1,
train_sizes=np.linspace(.1, 1.0, 5)):
plt.figure()
plt.title('Learning Curves with NB and SVM')
if ylim is not None:
plt.ylim(*ylim)

train_sizes_nb, test_scores_nb = learning_curve(
nb, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
test_scores_mean_nb = np.mean(test_scores_nb, axis=1)

train_sizes_svc, test_scores_svc = learning_curve(
svc, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
test_scores_mean_svc = np.mean(test_scores_svc, axis=1)

plt.grind()

plt.plot(train_sizes_nb, test_scores_mean_nb, 'o-', color="g",
label="NB")
plt.plot(train_sizes_svc, test_scores_mean_svc,'o',color="r",label="SVM")

return plt

这是函数调用:

digits = load_digits()
X, y = digits.data, digits.target

cv = cross_validation.ShuffleSplit(digits.data.shape[0], n_iter=100,
test_size=0.2, random_state=0)
plot_learning_curves(X, y, ylim=(0.7, 1.01), cv=cv,n_jobs=1)
plt.show()

我不知道是什么问题,但我得到了这个错误:

Traceback (most recent call last):
File "C:/Users/Gianmarco/PycharmProjects/Learning/plotLearningCurves.py", line 43, in <module>
plot_learning_curves(X, y, ylim=(0.7, 1.01), cv=cv,n_jobs=1)
File "C:/Users/Gianmarco/PycharmProjects/Learning/plotLearningCurves.py", line 19, in plot_learning_curves
nb, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
File "C:\Users\Gianmarco\Anaconda\lib\site-packages\sklearn\learning_curve.py", line 136, in learning_curve
for train, test in cv for n_train_samples in train_sizes_abs)
File "C:\Users\Gianmarco\Anaconda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 652, in __call__
for function, args, kwargs in iterable:
File "C:\Users\Gianmarco\Anaconda\lib\site-packages\sklearn\learning_curve.py", line 136, in <genexpr>
for train, test in cv for n_train_samples in train_sizes_abs)
File "C:\Users\Gianmarco\Anaconda\lib\site-packages\sklearn\base.py", line 45, in clone
new_object_params = estimator.get_params(deep=False)
TypeError: unbound method get_params() must be called with GaussianNB instance as first argument (got nothing instead)

Process finished with exit code 1

我不明白“TypeError: unbound method get_params() must be called with GaussianNB instance as first argument (got nothing instead)”这一行是什么意思。

可能的解决方案是什么?

最佳答案

解决方案非常简单。这不是

nb=GaussianNB

但是

nb=GaussianNB()

关于Python scikit-learn - 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086161/

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