text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import cross_val_score

from sklearn.naive_bayes import MultinomialNB

from sklearn.svm import LinearSVC

import matplotlib.pyplot as plt # ploting library

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('../input/machathon-20-final-round/train_ara.csv')

pd.set_option('display.max_rows', 10000)
pd.set_option('display.max_columns', 500)
pd.set_option('display.precision', 150)
pd.options.display.float_format = '{:,.3f}'.format

classes = df.intent.unique()
classes

df['intent_id'] = df['intent'].factorize()[0]
intent_id_df = df[['intent', 'intent_id']].drop_duplicates()
intent_to_id = dict(intent_id_df.values)
id_to_intent = dict(intent_id_df[['intent_id', 'intent']].values)

counts = df['intent'].value_counts()
base_color = sns.color_palette()[0]
plt.xticks(size=12)
totale = len(df)
locs, labels = plt.yticks(size=14)
for loc, label in zip(locs, labels):
    count = counts[label.get_text()]
    pct_string = '{:0.1f}%'.format(100 * count / totale)
    plt.text(count + 8, loc + 0.3, pct_string, ha='center', color='black', fontsize=12)

tfidf = TfidfVectorizer(sublinear_tf=True, ngram_range=(1, 2))
features = tfidf.fit_transform(df['lem']).toarray()
labels = df.intent_id

models = [RandomForestClassifier(n_estimators=100, max_depth=5, random_state=0), LinearSVC(), MultinomialNB(), LogisticRegression(random_state=0)]
CV = 5
cv_df = pd.DataFrame(index=range(CV * len(models)))
entries = []
for model in models:
    model_name = model.__class__.__name__
    accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)
    for fold_idx, accuracy in enumerate(accuracies):
        entries.append((model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])

mean_accuracy = cv_df.groupby('model_name').accuracy.mean()
std_accuracy = cv_df.groupby('model_name').accuracy.std()
acc = pd.concat([mean_accuracy, std_accuracy], axis=1, ignore_index=True)
acc.columns = ['Mean Accuracy', 'Standard deviation']
acc

plt.figure(figsize=(8, 5))
sns.boxplot(x='model_name', y='accuracy', data=cv_df, color='lightblue', showmeans=True)
plt.title('MEAN ACCURACY (cv = 5)\n', size=14)