text_html_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.linear_model import LogisticRegression

from tqdm import tqdm

import matplotlib.pyplot as plt

import pandas as pd

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

def create_mean_std_df(df):
    means = df.mean()
    stds = df.std()
    ret_df = pd.concat([means, stds], axis=1).reset_index()
    ret_df.columns = ['index', 'mean', 'std']
    ret_df['pref'] = ret_df['index'].apply(lambda x: x.split('-')[0])
    ret_df['post'] = ret_df['index'].apply(lambda x: x.split('-')[1])
    return ret_df

def scatter(y, x=None, color= None):
    fig, ax = plt.subplots()
    un_c = color.unique()
    for i, col in enumerate(un_c):
        y_i = y[color == col]
        if type(x) != pd.core.series.Series:
            x_i = range(0 + i * (y.shape[0] - y_i.shape[0]),
                    (1 - i)*y_i.shape[0] + i * y.shape[0])
        else:
            x_i = x[color == col]
        ax.scatter(x_i, y_i, c=col, label=col,
               alpha=0.3)
        ax.set_ylabel(y.name)
        ax.legend()
        ax.grid(True)
    plt.show()

tr = create_mean_std_df(train_features.iloc[:, 4:])

corrMatrix = train_features.corr()

def mul_lab_logreg(test, train_X, train_y):
    sub = pd.DataFrame(test['sig_id'])
    col = train_y.columns.drop('sig_id')
    train_X.set_index('sig_id', inplace=True)
    df = pd.concat([train_X.iloc[:, 0], train_y.set_index('sig_id')], axis=1)
    for c in tqdm(col):
        y = df.loc[:, c]
        clf = LogisticRegression(random_state=0, class_weight=y.mean(), n_jobs=6).fit(train_X, y)
        clf.fit(train_X, y)
        sub[c] = clf.predict_proba(test.drop('sig_id', axis=1)).T[1]
    return sub

clf = RandomForestClassifier(n_estimators=15, criterion='entropy', max_depth=15, max_samples=150, max_features=0.3, verbose=1, n_jobs=-1, random_state=1998, ccp_alpha=0.0)

clf.fit(train_features.set_index('sig_id'), train_targets_scored.set_index('sig_id'))

preds = clf.predict_proba(test_features.drop('sig_id', axis=1))