application_vnd.jupyter.stderr_output_9.png application_vnd.jupyter.stderr_output_7.png text_plain_output_4.png text_plain_output_10.png text_plain_output_6.png application_vnd.jupyter.stderr_output_3.png application_vnd.jupyter.stderr_output_5.png text_plain_output_8.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import LinearRegression,SGDRegressor

from sklearn.metrics import mean_squared_error

from sklearn.multioutput import MultiOutputRegressor

import numpy as np # linear algebra

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

import sys

dftr = pd.read_csv('/kaggle/input/feedback-prize-english-language-learning//train.csv')
dftr['src'] = 'train'
dfte = pd.read_csv('/kaggle/input/feedback-prize-english-language-learning//test.csv')
dfte['src'] = 'test'
df = pd.concat([dftr, dfte], ignore_index=True)

target_cols = ['cohesion', 'syntax', 'vocabulary', 'phraseology', 'grammar', 'conventions']

import sys
sys.path.append('../input/iterativestratification')
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold
FOLDS = 5
skf = MultilabelStratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=2022)
for i, (train_index, val_index) in enumerate(skf.split(dftr, dftr[target_cols])):
    dftr.loc[val_index, 'FOLD'] = i
dftr.FOLD.value_counts()

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import roc_auc_score
from sklearn.linear_model import LinearRegression, SGDRegressor
from scipy import sparse
from sklearn.multioutput import MultiOutputRegressor
from scipy.stats import pearsonr
from sklearn.metrics import mean_squared_error
preds = []
scores = []
def comp_score(y_true, y_pred):
    rmse_scores = []
    for i in range(len(target_cols)):
        rmse_scores.append(np.sqrt(mean_squared_error(y_true[:, i], y_pred[:, i])))
    return np.mean(rmse_scores)
for fold in range(FOLDS):
    dftr_ = dftr[dftr['FOLD'] != fold]
    dfev_ = dftr[dftr['FOLD'] == fold]
    tf = TfidfVectorizer(ngram_range=(1, 2))
    tf = tf.fit(dftr_['full_text'])
    tr_text_feats = tf.transform(dftr_['full_text'])
    ev_text_feats = tf.transform(dfev_['full_text'])
    te_text_feats = tf.transform(dfte['full_text'])
    clf = MultiOutputRegressor(LinearRegression(n_jobs=-1, normalize=True))
    clf.fit(tr_text_feats, dftr_[target_cols].values)
    ev_preds = clf.predict(ev_text_feats)
    score = comp_score(dfev_[target_cols].values, ev_preds)
    scores.append(score)
    print('Fold : {} EV score: {}'.format(fold, score))
    preds.append(clf.predict(te_text_feats))