application_vnd.jupyter.stderr_output_1.png image_output_1.png
from scipy.optimize import minimize

from sklearn.metrics import f1_score, classification_report

import matplotlib.pyplot as plt

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

import seaborn as sns

train = pd.read_csv('/kaggle/input/data-without-drift/train_clean.csv')
test = pd.read_csv('/kaggle/input/data-without-drift/test_clean.csv')

batch_indices = [slice(500000 * i, 500000 * (i + 1)) for i in range(10)]

fig, axes = plt.subplots(2,5,sharex=False,sharey=True, figsize=(25,10))
for i,ax in enumerate(axes.ravel()):
    train.iloc[batch_indices[i]].plot(kind='line',x='time',y=['signal'],ax=ax,linewidth=.1)
    ax.set_title('Batch_'+str(i))
ax.set_ylim(-5,14)
ax.legend()
fig.suptitle('Training Data',y=1.05)

plt.tight_layout()

fig, axes = plt.subplots(1,4,sharex=False,sharey=True, figsize=(25,10))
for i,ax in enumerate(axes.ravel()):
    test.iloc[batch_indices[i]].plot(kind='line',x='time',y=['signal'],ax=ax,linewidth=.1)
    ax.set_title('Batch_'+str(i))
ax.set_ylim(-5,11)
ax.legend()
fig.suptitle('Testing Data',y=1.05)

plt.tight_layout()

fig, axes = plt.subplots(2,5,sharex=True,sharey=True, figsize=(20,8))
for i,ax in enumerate(axes.ravel()):
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 0')['signal'],ax=ax,label='0')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 1')['signal'],ax=ax,label='1')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 2')['signal'],ax=ax,label='2')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 3')['signal'],ax=ax,label='3')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 4')['signal'],ax=ax,label='4')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 5')['signal'],ax=ax,label='5')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 6')['signal'],ax=ax,label='6')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 7')['signal'],ax=ax,label='7')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 8')['signal'],ax=ax,label='8')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 9')['signal'],ax=ax,label='9')
        sns.distplot(train.iloc[batch_indices[i]].query('open_channels == 10')['signal'],ax=ax,label='10')
        ax.set_title('Batch_'+str(i))
ax.legend()
fig.suptitle('Training Data',y=1.05)

ax.set_ylim(0,2)
plt.tight_layout()

means = train.groupby(['signal_type', 'open_channels']).mean().signal
train['scaled_signal'] = train['signal']
test['scaled_signal'] = test['signal']

def shift_model(x, sig_type):
    scaled = (train.loc[train.signal_type == sig_type, 'signal'] - x[0]) * x[1]
    target = train.loc[train.signal_type == sig_type, 'open_channels']
    return -f1_score(target, scaled.clip(0, 10).round(), average='weighted')

for i in range(1, 5):
    print(i)
    min_f = minimize(shift_model, [means.loc[i, 0], 1 / (means.loc[i, 1] - means.loc[i, 0])], args=i, method='Powell')
    train.loc[train.signal_type == i, 'scaled_signal'] = (train.loc[train.signal_type == i, 'signal'] - min_f['x'][0]) * min_f['x'][1]
    test.loc[test.signal_type == i, 'scaled_signal'] = (test.loc[test.signal_type == i, 'signal'] - min_f['x'][0]) * min_f['x'][1]
i = 5
min_f = minimize(shift_model, [means.loc[i, 1] - 1, 5 / (means.loc[i, 6] - means.loc[i, 1])], args=i, method='Powell')
train.loc[train.signal_type == i, 'scaled_signal'] = (train.loc[train.signal_type == i, 'signal'] - min_f['x'][0]) * min_f['x'][1]
test.loc[test.signal_type == i, 'scaled_signal'] = (test.loc[test.signal_type == i, 'signal'] - min_f['x'][0]) * min_f['x'][1]
del means