# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.4 64-bit
#     name: python3
# ---

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5"

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import os
from tqdm import tqdm
tqdm.pandas()
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.svm import SVR
from sklearn.model_selection import RandomizedSearchCV,GridSearchCV
# -

train_df=pd.read_csv('./Data/train.csv')

train_df.head()

columns=[f'ROI{i}-ROI{j}'  for i in range(1,116) for j in range (i+1,117) ]


def get_corr(row,test=False):
    if test:
        brain = np.array(row[4:])
    else:
        brain = np.array(row[5:])
    brain = brain.reshape((116, 115)).astype(float)
    corr = np.corrcoef(brain)
    flat = corr[np.triu_indices(corr.shape[0], 1)]
    return flat.tolist()



corr=train_df.progress_apply(lambda x :get_corr(x),axis=1)

# +
corr_df=pd.DataFrame(np.array(corr.values.tolist()),columns=columns).fillna(0)


# -

corr_df

# +
#normalized_corr_df=(corr_df-corr_df.mean())/corr_df.std()

# +
# normalized_corr_df=(corr_df-corr_df.min())/(corr_df.max()-corr_df.min())
# -

new_train=corr_df
#new_train['var1']=train_df['var1']
#new_train['y']=train_df['y']

first_cols = train_df.iloc[:, [1,2,3,4]]

new_train = first_cols.join(corr_df)

new_train['var2'].replace({'A':0, 'C':1}, inplace = True)
new_train['var3'].replace({'A':0, 'C':1}, inplace = True)

new_train

new_train['mean']=np.mean(new_train.iloc[:,4:],axis=1)

new_train['max']=np.max(new_train.iloc[:,4:],axis=1)

new_train['min']=np.min(new_train.iloc[:,4:],axis=1)

new_train

new_train.groupby('y').size().sort_values(ascending=False)

new_train=new_train.groupby('y').mean().reset_index()

new_train

# # Train

# ## Linear Regession

reg = LinearRegression()

X_train, X_test, y_train, y_test = train_test_split(new_train.drop(columns='y'), new_train['y'], test_size=0.25, random_state=42)

reg.fit(X_train,y_train)

y_pred=reg.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

# ## SVR

params = [{'kernel' : ['poly'],
        'C' : [1,5,10],
        'degree' : [1,2,3,4]}]
        #'coef0' : [0.01,10,0.5],
        #'gamma' : ('auto','scale')}]


svr_reg=SVR()
grids = GridSearchCV(svr_reg,params,cv=3,verbose=5,n_jobs=-1)
grids.fit(X_train,y_train)


grids.best_params_

y_pred=grids.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

#retrain on all dataset
svr_reg=SVR(C=1, degree= 1, kernel= 'poly')
svr_reg.fit(new_train.drop(columns='y'),new_train['y'])



plt.scatter(y_test,y_test-y_pred,)

y_pred=svr_reg.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

svr_reg_poly=SVR(kernel='poly',degree=2)
svr_reg_poly.fit(X_train,y_train)
y_pred=svr_reg_poly.predict(X_test)
print(mean_squared_error(y_test, y_pred,squared=False))

svr_reg_poly.fit(new_train.drop(columns='y'),new_train['y'])

# ## K-neigbors regression



from sklearn.neighbors import KNeighborsRegressor

for k in range(30,45):
    neigh = KNeighborsRegressor(n_neighbors=k,weights='distance').fit(X_train, y_train)
    y_pred=neigh.predict(X_test)
    print(k,mean_squared_error(y_test, y_pred,squared=False))

neigh = KNeighborsRegressor(n_neighbors=33,weights='distance',p=2).fit(X_train, y_train)

y_pred=neigh.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

neigh = KNeighborsRegressor(n_neighbors=33,weights='distance',p=2).fit(new_train.drop(columns='y'),new_train['y'])

y_pred=neigh.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

# ## Elastic net

# +
from sklearn.linear_model import ElasticNetCV

regr = ElasticNetCV(cv=3, random_state=0)

regr.fit(X_train,y_train)

# -

y_pred=regr.predict(X_test)

mean_squared_error(y_test, y_pred,squared=False)

# # Test 

test_df=pd.read_csv('./Data/test.csv')

test_df

corr_test=test_df.progress_apply(lambda row:get_corr(row,test=True),axis=1)

test_final_df=pd.DataFrame(np.array(corr_test.values.tolist()),columns=columns).fillna(0)

# +
#normalized_corr_df_test=(test_final_df-test_final_df.mean())/test_final_df.std()
# -

test_final_df

test_df

first_cols_test = test_df.iloc[:, [1,2,3]]
new_test = first_cols_test.join(normalized_corr_df_test)
new_test['var2'].replace({'A':0, 'C':1}, inplace = True)
new_test['var3'].replace({'A':0, 'C':1}, inplace = True)

new_test

new_test['mean']=np.mean(new_test.iloc[:,3:],axis=1)

new_test['min']=np.min(new_test.iloc[:,4:],axis=1)
new_test['max']=np.max(new_test.iloc[:,4:],axis=1)

new_test

y_pred_test=svr_reg.predict(new_test)

plt.hist(y_pred_test)

df_leaderboard=pd.DataFrame({'id':test_df['id'],'target':y_pred_test})

df_leaderboard

df_leaderboard

df_leaderboard.to_csv('G14_08',index=False)

tuning_params=[{'n_neighbors':[range(1,45,2)],
                'weights':['uniform','distance'],
                'p':[1,2,4,6]}]
neigh_r=NearestNeighbors()
grids = GridSearchCV(neigh_r,tuning_params,cv=3,verbose=5,n_jobs=-1)
grids.fit(X_train,y_train)
grids.best_params_               


y_test



StarcoderdataJupyterScriptsDedupFiltered