我正在尝试比较不同的算法,以查看哪种算法最适合我的问题。
我正在直接从本教程中尝试代码:https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
尤其是下面的代码:
import sys
import pandas as pd
import scipy as sp
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
models = []
models.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))
results = []
names = []
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
运行此命令时,我不断得到(第12行的<----):
NameError Traceback (most recent call last)
<ipython-input-25-e6a861b6e218> in <module>()
10 names = []
11 for name, model in models:
12 kfold = model_selection.KFold(n_splits=10, random_state=seed) <----
13 cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
14 results.append(cv_results)
NameError: name 'model_selection' is not defined
有人可以向我解释KFold如何正常工作以及为什么它不接受实例吗?
答案 0 :(得分:1)
KFold
是sklearn.model_selection
模块的一部分。
通过执行以下操作确保将名称导入到您的工作空间中
from sklearn import model_selection
并使用
model_selection.KFold
或
import sklearn.model_selection
sklearn.model_selection.KFold
甚至
from sklearn.model_selection import KFold
KFold