# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from sklearn import datasets
from sklearn import datasets
from sklearn.cross_validation import KFold
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC
import numpy as np
import pandas

newsgroups = datasets.fetch_20newsgroups(
                    subset='all', 
                    categories=['alt.atheism', 'sci.space']
             )

X = newsgroups.data
y = newsgroups.target

vectorizer = TfidfVectorizer()
X_tfidf = vectorizer.fit_transform(X, y)

grid = {'C': np.power(10.0, np.arange(-5, 6))}
cv = KFold(len(y), n_folds=5, shuffle=True, random_state=241)

clf = SVC(kernel='linear', random_state=241, C=1.0)
clf.fit(X_tfidf, y)

gs = GridSearchCV(clf, grid, scoring='accuracy', cv=cv)
gs.fit(X_tfidf, y)

# +
score = 0
C = 0
for attempt in gs.grid_scores_:
    if attempt.mean_validation_score > score:
        score = attempt.mean_validation_score
        C = attempt.parameters['C']

model = SVC(kernel='linear', random_state=241, C=C)
model.fit(vectorizer.transform(X), y)
# -

words = vectorizer.get_feature_names()
coef = pandas.DataFrame(model.coef_.data, model.coef_.indices)
top_words = coef[0].map(lambda w: abs(w)).sort_values(ascending=False).head(10).index.map(lambda i: words[i])
print(top_words)



StarcoderdataJupyterScriptsDedupFiltered