【问题标题】:in python, spyder, i import sklearn, why it doesn't find 'classification' in 'sklearn.metric'?在 python、spyder 中,我导入 sklearn,为什么在“sklearn.metric”中找不到“分类”?
【发布时间】:2021-09-30 13:31:51
【问题描述】:

它发送: 打印(metrics.classification.accuracy_score(y_test, y_pred))

AttributeError: 模块 'sklearn.metrics' 没有属性 'classification'

我的 sklearn 似乎无法导入“分类”,但我不知道为什么,您能帮帮我吗?

from sklearn import metrics
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB, BernoulliNB, GaussianNB
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline


DATA_DIR = 'enron'
target_names = ['ham', 'spam']

def get_data(DATA_DIR):
    subfolders = ['enron%d' % i for i in range(1,7)]

    data = []
    target = []
    for subfolder in subfolders:
        
        # spam
        spam_files = os.listdir(os.path.join(DATA_DIR, subfolder, 'spam'))
        for spam_file in spam_files:
            with open(os.path.join(DATA_DIR, subfolder, 'spam', spam_file), encoding='ascii', errors='ignore') as f:
                data.append(f.read())
                target.append(1)
                
        # ham
        ham_files = os.listdir(os.path.join(DATA_DIR, subfolder, 'ham'))
        for ham_file in ham_files:
            with open(os.path.join(DATA_DIR, subfolder, 'ham', ham_file), encoding='ascii', errors='ignore') as f:
                data.append(f.read())
                target.append(0)
                
    target = np.array(target)
    return(data, target)
    
X, y = get_data(DATA_DIR)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, random_state=42)

count_vec = CountVectorizer()
X_train_counts = count_vec.fit_transform(X_train)

tfidf_transformer = TfidfTransformer()
X_train_tfidf =  tfidf_transformer.fit_transform(X_train_counts)

clf = BernoulliNB().fit(X_train_tfidf, y_train)

X_test_counts = count_vec.transform(X_test)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)

y_pred = clf.predict(X_test_tfidf)

print(metrics.classification_report(y_test, y_pred, target_names=target_names))
print(metrics.classification.accuracy_score(y_test, y_pred))

【问题讨论】:

标签: python classification sklearn-pandas


【解决方案1】:

升级你的 sklearn 应该可以解决这个问题。

它在 pip 中被称为 scikit-learn 而不是 sklearn,这样做

pip install scikit-learn

如果你已经有了,试试

pip install --upgrade scikit-learn

【讨论】:

  • 好吧,这就是为什么我无法升级它,谢谢伙计!
猜你喜欢
  • 2017-07-21
  • 2016-01-21
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多