# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

def abs_sum(y_true, y_prob):
    return sum(sum(abs(y_true - y_prob)))


def convert_to_prob(y):
    y_prob = []
    for label in y:
        row = []
        for i in range(4):
            if i == label:
                row.append(1)
            else:
                row.append(0)
        y_prob.append(row)
    return np.array(y_prob)


# +
df = pd.read_csv('train.csv')
df_list = []
for items in df.values:
    df_list.append([items[0]] + [float(i) for i in items[1].split(',')] + [items[2]])

df = pd.DataFrame(np.array(df_list))
df.columns = ['id'] + ['signal_'+str(i) for i in range(len(df_list[0])-2)] + ['label']
print(df.head())
print(df.describe())

# +
label0 = df.loc[df['label'] == 0.0, ['signal_'+str(i) for i in range(205)]]
label0_mean = np.array(label0.mean())

label1 = df.loc[df['label'] == 1.0, ['signal_'+str(i) for i in range(205)]]
label1_mean = np.array(label1.mean())

label2 = df.loc[df['label'] == 2.0, ['signal_'+str(i) for i in range(205)]]
label2_mean = np.array(label2.mean())

label3 = df.loc[df['label'] == 3.0, ['signal_'+str(i) for i in range(205)]]
label3_mean = np.array(label3.mean())

time = list(range(205))

plt.plot(time, label0_mean ,c='blue', label='label0_line')
plt.plot(time,label1_mean, c='red', label='label1_line')
plt.plot(time, label2_mean ,c='green', label='label2_line')
plt.plot(time, label3_mean ,c='yellow', label='label3_line')
plt.xlabel("time")
plt.ylabel("heartbeat_signal")
plt.legend()
plt.show()

# +
x = df.drop(['id', 'label'], axis=1).to_numpy()
y = df['label'].to_numpy()
pca = PCA(n_components=0.95)
x = pca.fit_transform(x)


from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
test_set_size = 0.1
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=test_set_size, random_state=3)
y_true_prob = convert_to_prob(y_test)

knn = KNeighborsClassifier(n_neighbors=1, weights='distance').fit(X_train, y_train)
y_pred_prob = knn.predict_proba(X_test)
print(y_pred_prob)
# -

print(y_true_prob)
loss = abs_sum(y_pred_prob, y_true_prob)
print(loss)



StarcoderdataJupyterScriptsDedupFiltered