<jupyter_start><jupyter_text>Supervised Learning with scikit-learn* http://scikit-learn.org/stable/supervised_learning.html* Bare bones Python ML implementations: https://github.com/eriklindernoren/ML-From-Scratch Classification Metrics:* http://scikit-learn.org/stable/modules/model_evaluation.html* Today we will be using accuracy (compare to fscore, mrr, ...)<jupyter_code>import pandas as pd
import numpy as np
from collections import Counter<jupyter_output><empty_output><jupyter_text>Prepare Data (once again, sentiment analysis)<jupyter_code>data = pd.read_pickle('sentiment.pkl')

data.shape
data[:5]
dev=data.sample(frac=0.1,random_state=200)
train=data.drop(dev.index)


train.shape, dev.shape, Counter(train.polarity)
neg = train[train.polarity == 0]
pos = train[train.polarity == 4]

neg.w2v.shape, pos.w2v.shape
pos.w2v[:1]
[0] * 10
pos = [x for x in pos.w2v if type(x) is not np.float64]
neg = [x for x in neg.w2v if type(x) is not np.float64]
labels = len(neg) * [0] + len(pos) * [1]

train_data =  list(neg) + list(pos)

len(train_data)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>from sklearn import linear_model
model = linear_model.LogisticRegression(penalty='l2')<jupyter_output><empty_output><jupyter_text>Support Vector Machines<jupyter_code>from sklearn import svm
model = svm.SVC()
# Linear SVM
model = svm.LinearSVC()<jupyter_output><empty_output><jupyter_text>Decision Tree* A deeper explanation: http://nbviewer.jupyter.org/github/justmarkham/DAT4/blob/master/notebooks/15_decision_trees.ipynb<jupyter_code>from sklearn import tree
model = tree.DecisionTreeClassifier()<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=10, max_depth=None, min_samples_split=2, random_state=0)<jupyter_output><empty_output><jupyter_text>Neural Network<jupyter_code>from sklearn.neural_network import MLPClassifier
model = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(1, 2), random_state=1)<jupyter_output><empty_output><jupyter_text>Pipeline (logres & SVM)<jupyter_code>import sklearn
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
model = Pipeline([('logres', linear_model.LogisticRegression(penalty='l2')),
                  ('svm', svm.SVC())
                 ])<jupyter_output><empty_output><jupyter_text>Train & Test<jupyter_code>from sklearn.metrics import accuracy_score

model.fit(train_data, labels)

dev = data[data.index.isin(dev.index)]

neg = dev[dev.polarity == 0].w2v
pos = dev[dev.polarity == 1].w2v

pos = [x for x in pos if type(x) is not np.float64]
neg = [x for x in neg if type(x) is not np.float64]

neg_guess = [model.predict(v.reshape(1, -1)) for v in neg]
pos_guess = [model.predict(v.reshape(1, -1)) for v in pos]

accuracy_score(neg_guess+pos_guess, len(neg_guess)*[0] + len(pos_guess) * [1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12210796915167095, 'fraction_numerical': 0.016709511568123392, 'mean_word_length': 6.881012658227848, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '576744', 'license': 'MIT', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1145, 'n_words': 221, 'path': 'introds/classes/Scikit/scikit-learn-supervised.ipynb', 'repo_name': 'gcaracas/bsu', 'size': '3112'}