1. 程式人生 > >資料探勘——單層感知器的Python實現

資料探勘——單層感知器的Python實現

Python——scikit-learn實現單層感知器

scikit-learn 提供了感知器功能。和我們用過的其他功能類似,Perceptron類的構造器接受超引數設定。Perceptron類有fit_transform()和predict()方法。Perceptron類還提供了partial_fit()方法,允許分類器訓練流式資料(streaming data)並做出預測。

# coding=utf-8
from sklearn.datasets import fetch_20newsgroups
from sklearn.metrics import f1_score, classification_report
from
sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import Perceptron categories = ['rec.sport.hockey', 'rec.sport.baseball', 'rec.autos'] newsgroups_train = fetch_20newsgroups(subset='train', categories=categories, remove=('headers', 'footers', 'quotes')) newsgroups_test = fetch_20newsgroups(subset='test'
, categories=categories, remove=('headers', 'footers', 'quotes')) vectorizer = TfidfVectorizer() X_train = vectorizer.fit_transform(newsgroups_train.data) X_test = vectorizer.trasform(X_train, newsgroups_train.target) classifier = Perceptron(n_iter=100, eta0=0.1) classifier.fit_transform(X_train, newsgroups_train.target) predictions = classifier.predict(X_test) print
classification_report(newsgroups_test.target, predictions)