引发ValueError(“输入包含NaN”)ValueError:输入在异常检测python代码中包含NaN

时间:2019-11-23 06:41:08

标签: python pandas scikit-learn analytics

我正在尝试运行以下代码

#ROC
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
from scipy import interp
from sklearn.ensemble import RandomForestClassifier

clf_1 = RandomForestClassifier()

cv = StratifiedKFold(n_splits=10)
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)

for i, (train, test) in enumerate(cv.split(X, y)):

    probas_ = clf_1.fit(X[train], y[train]).predict_proba(X[test])
    fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1], pos_label="normal")
    tprs.append(interp(mean_fpr, fpr, tpr))
    tprs[-1][0] = 0.0
    roc_auc = auc(fpr, tpr)
    aucs.append(roc_auc)
    # plt.plot(fpr, tpr, lw=1, alpha=0.3,
    #          label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))

    i += 1
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
         label='Luck', alpha=.8)

mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='g',
         label=r'RF Mean ROC (AUC = %0.6f $\pm$ %0.4f)' % (mean_auc, std_auc),
         lw=2, alpha=.8)

std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
                 label=r'$\pm$ 1 std. dev.')

plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

我收到以下错误消息

ValueError                                Traceback (most recent call last)
<ipython-input-13-ea49bde52419> in <module>
     14 for i, (train, test) in enumerate(cv.split(X, y)):
     15 
---> 16     probas_ = clf_1.fit(X[train], y[train]).predict_proba(X[test])
     17     fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1], pos_label="normal")
     18     tprs.append(interp(mean_fpr, fpr, tpr))

~\user data\installation\Anaconda\lib\site-packages\sklearn\ensemble\forest.py in fit(self, X, y, sample_weight)
    248         # Validate or convert input data
    249         X = check_array(X, accept_sparse="csc", dtype=DTYPE)
--> 250         y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
    251         if sample_weight is not None:
    252             sample_weight = check_array(sample_weight, ensure_2d=False)

~\user data\installation\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    540         if force_all_finite:
    541             _assert_all_finite(array,
--> 542                                allow_nan=force_all_finite == 'allow-nan')
    543 
    544     if ensure_min_samples > 0:

~\user data\installation\Anaconda\lib\site-packages\sklearn\utils\validation.py in _assert_all_finite(X, allow_nan)
     58     elif X.dtype == np.dtype('object') and not allow_nan:
     59         if _object_dtype_isnan(X).any():
---> 60             raise ValueError("Input contains NaN")
     61 
     62 

ValueError: Input contains NaN

这里所有其他代码都能正常工作是什么问题? 该程序取自here 我不懂中文,所以不知道评论中提到了什么。 但这对我来说似乎很有趣,我正在执行的文件是this

代码中使用的数据集为this

0 个答案:

没有答案