<jupyter_start><jupyter_text>TfidfVectorizer == CountVectorizer + TfidfTransformer<jupyter_code>from sklearn.feature_selection import SelectKBest, chi2
from sklearn.pipeline import Pipeline<jupyter_output><empty_output><jupyter_text>Models<jupyter_code>from xgboost import XGBClassifier

from sklearn.linear_model import PassiveAggressiveClassifier, RidgeClassifierCV, SGDClassifier,LogisticRegressionCV                                 
from sklearn.ensemble import (AdaBoostClassifier,BaggingClassifier,ExtraTreesClassifier,
                              GradientBoostingClassifier,RandomForestClassifier)                               
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
from helper import (load_data, token,time_me,
                    show_diagram, show_most_informative_features)
import warnings
warnings.simplefilter(action='ignore', category=(UserWarning,FutureWarning))
%matplotlib inline
plt.rcParams['figure.figsize'] = (15,5)
plt.style.use('fivethirtyeight') # plt.style.available
stops = joblib.load('../stops.pkl')
df = load_data('../sentiment_data', balanced=True)
df.head()
 X_train, X_test, y_train, y_test = train_test_split(df['features'], df['y'],
                                                    test_size=.2, random_state=7,stratify=df['y'])
print(f'Traing Size: {X_train.shape[0]}\nTest Size: {X_test.shape[0]:>8}')
print(f'Traing Size\n\tPositive||Negative Sample\n\t  {y_train[y_train==1].shape[0]}||{y_train[y_train==0].shape[0]}')
print(f'Test Size\n\tPositive||Negative Sample\n\t  {y_test[y_test==1].shape[0]}||{y_test[y_test==0].shape[0]}')
def model_test(model, bench_mark):
    
    clf = Pipeline(steps=[
                         ('vec',  CountVectorizer(ngram_range=(1, 2), max_features=100000,
                                 tokenizer=token, stop_words=stops)),
                        ('fe_se', SelectKBest(chi2, k=5000)),
                        ('tfidf', TfidfTransformer(sublinear_tf=True)),
                        ('classifier', model),
                ])
    print(f'Model Training {clf.steps[3][1].__class__.__name__}')
    print(f'params:{clf.steps[3][1].get_params()}')
    print('-'*15)
    
    try:
        train_start_time = pd.datetime.now()
        clf.fit(X_train,y_train)
        train_duration = pd.datetime.now() - train_start_time
        
        test_start_time = pd.datetime.now()
        score = clf.score(X_test,y_test)
        test_duration = pd.datetime.now() - test_start_time

        print(f'Training duration: {train_duration}')
        print(f'Testing duration: {test_duration}')
        print(f'Model Score: {score}')
        print('*'*15,'\n')



        bench_mark['model_name'].append(clf.steps[3][1].__class__.__name__)
        bench_mark['model'].append(clf.steps[3][1])
        bench_mark['score'].append(clf.score(X_test,y_test))
        bench_mark['train_duration'].append(train_duration.total_seconds())
        bench_mark['test_duration'].append(test_duration.total_seconds())
        bench_mark['params'].append(clf.steps[3][1].get_params())

        try:
            show_diagram(clf, X_train, y_train, X_test, y_test, compare_test=True)
        except Exception as e:
            print(f'failed to show diagram: {e}')
            pass
        
    except Exception as m:
        print(f'Training failed with {m}')
        pass
    
    return bench_mark
linear_models = [PassiveAggressiveClassifier(max_iter=1000, random_state=0,tol=1e-3), 
                 PassiveAggressiveClassifier(C=1e-1,max_iter=1e3,  tol=1e3), 
                 RidgeClassifierCV(scoring='roc_auc', cv=10),
                 LogisticRegressionCV(cv=5,solver='saga',scoring='accuracy', n_jobs=-1),
                 SGDClassifier(loss='log', random_state=1, max_iter=1),
                 
                ]

ensemble_models = [AdaBoostClassifier(n_estimators=100,learning_rate=.01,),
                   ExtraTreesClassifier(criterion='gini',  min_samples_split=5,),
                   ExtraTreesClassifier(criterion='entropy',  min_samples_split=5,),
                   GradientBoostingClassifier(max_depth=5),
                   RandomForestClassifier(),
                   RandomForestClassifier(criterion='entropy'),
                  ]
tree_models = [DecisionTreeClassifier(),
              ExtraTreeClassifier()
             ]

# GradientBoosting

parameters = {
    'loss':['deviance','exponential'],
    'learning_rate': [0.01, 0.05, 0.1],
    'min_samples_split': np.linspace(0.1, 0.5, 12),
    'min_samples_leaf': np.linspace(0.1, 0.5, 12),
    'max_depth':[3,5,8],
    'max_features':['log2','sqrt'],
    'criterion': ['friedman_mse',  'mae'],
    'subsample':[0.5, 0.8, 1.0],
    'n_estimators':[10]
    }
xgb_para = dict(booster = 'dart',
                learning_rate =0.03,
                 n_estimators=100,
                 max_depth=3,
                 min_child_weight=1,
                 gamma=0,
                 subsample=0.8,
                 colsample_bytree=0.8,
                 objective= 'binary:logistic',
                 nthread=4,
                 scoring='error',            
                 scale_pos_weight=1,
                 silent=True,
                 seed=42)
final_models = [GridSearchCV(GradientBoostingClassifier(), parameters,scoring='roc_auc', cv=10, n_jobs=-1, verbose=1),
                XGBClassifier(**xgb_para),]
bench_mark = defaultdict(list)
models = linear_models + ensemble_models + tree_models + final_models
for model in models:
    model_test(model,bench_mark)
pd.DataFrame(bench_mark).to_pickle('models_wars.pkl')
# feature_names = lr_clf.steps[0][1].get_feature_names()

# best_features = [feature_names[i] for i in lr_clf.steps[1][1].get_support(indices=True)]

# show_most_informative_features(best_features, lr_clf.steps[3][1], n=100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13183224475764868, 'fraction_numerical': 0.022860089377793057, 'mean_word_length': 2.843461030383091, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '376467', 'license': 'MIT', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1888, 'n_words': 252, 'path': 'notebooks/model_wars.ipynb', 'repo_name': 'Proteusiq/DanishSentiments', 'size': '5818'}