ValueError:输入在python中包含NaN

时间:2019-12-02 05:19:42

标签: python pandas scikit-learn

我的代码

        var myDate = DateTime.Now;
        var firstDateOfLastMonth = myDate.AddMonths(-1);
        firstDateOfLastMonth = firstDateOfLastMonth.AddDays(-firstDateOfLastMonth.Day + 1);
        var lastDayOfNextMonth = myDate.AddMonths(1);
        lastDayOfNextMonth = new DateTime(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month, DateTime.DaysInMonth(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month));

并提出错误

from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier

mnb=MultinomialNB()
svm=SGDClassifier(max_iter=1000, tol=0.2)

mnb_bow_predictions=train_predict_evaluate_model(classifier=mnb,
                                                train_features=bow_train_features,
                                                train_labels=train_labels,
                                                test_features=bow_test_features,
                                                test_labels=test_labels)

是什么使我的程序出现此错误?数据集或函数中出现错误?

1 个答案:

答案 0 :(得分:0)

所有特征和标签值必须是有限的。如果bow_train_featurestrain_labelsbow_train_featurestrain_labels是DataFrame或Numpy数组,则可以使用下面的代码仅过滤训练/测试集中的完全有限观测值:

# Create finite observation filters for train/test sets
train_finite_filter = np.isfinite(bow_train_features) & np.isfinite(train_labels) 
test_finite_filter = np.isfinite(bow_test_features) & np.isfinite(test_labels)

# Filter for finite training observations
bow_train_features_finite = bow_train_features[train_finite_filter]
train_labels_finite = train_labels[train_finite_filter]

# Filter for finite test observations
bow_test_features_finite = bow_test_features[test_finite_filter]
test_labels_finite = test_labels[test_finite_filter]