Kaggle competition: TReNDS Neuroimaging
<SOS> metric: WMAE Kaggle data source: trends-neuroimaging<load_pretrained>
%matplotlib inline
Image.open(".. /input/tpu-v38/tpu--sys-arch4 1.png" )<train_on_grid>
train_scores = pd.read_csv('/kaggle/input/trends-assessment-prediction/train_scores.csv' ).sort_values(by='Id')
 loadings = pd.read_csv('/kaggle/input/trends-assessment-prediction/loading.csv')
 fnc = pd.read_csv('/kaggle/input/trends-assessment-prediction/fnc.csv')
 sample = pd.read_csv('/kaggle/input/trends-assessment-prediction/sample_submission.csv')
 reveal = pd.read_csv('/kaggle/input/trends-assessment-prediction/reveal_ID_site2.csv')
 icn_nums = pd.read_csv('/kaggle/input/trends-assessment-prediction/ICN_numbers.csv')
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except ValueError:
strategy = tf.distribute.get_strategy()
print('Number of replicas:', strategy.num_replicas_in_sync )<load_from_csv>
import re
from tqdm import tqdm
train = pd.read_csv(".. /input/contradictory-my-dear-watson/train.csv")
 test = pd.read_csv(".. /input/contradictory-my-dear-watson/test.csv")
 submission = pd.read_csv('/kaggle/input/contradictory-my-dear-watson/sample_submission.csv' )<load_pretrained>
r = re.compile('\d+')
 col_dict = {}
for col in fnc.columns:
ind = r.findall(col)
 if ind:
col_dict[col] = [int(i)for i in ind]
response = requests.get('https://miro.medium.com/max/700/1*Nv2NNALuokZEcV6hYEHdGA.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<count_missing_values>
def get_matrix(df_row, return_idx=False):
matrix = np.zeros(( 100, 100))
for col in df_row.index[1:]:
i, j = col_dict[col]
matrix[i, j] = df_row[col]
matrix += matrix.T
idx = np.array([ 2, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17, 18, 20, 21, 23,
27, 32, 33, 37, 38, 40, 43, 45, 48, 51, 53, 54, 55, 56, 61, 62, 63,
66, 67, 68, 69, 70, 71, 72, 77, 79, 80, 81, 83, 84, 88, 93, 94, 96,
98, 99])
 if return_idx:
return matrix[:, idx][idx, :], idx
return matrix[:, idx][idx, :]
train.isnull().sum()<count_missing_values>
_, idx = get_matrix(fnc.iloc[0], return_idx=True)
 degrees = pd.DataFrame(degrees, columns=idx)
 degrees['Id'] = fnc['Id']
train.isnull().sum()<define_variables>
len(glob('/kaggle/input/trends-assessment-prediction/fMRI_train/*.mat')) , len(glob('/kaggle/input/trends-assessment-prediction/fMRI_test/*.mat'))
model_xml_roberta = 'jplu/tf-xlm-roberta-large'
model_bert = 'bert-base-multilingual-cased'
n_epochs = 10
max_len = 80
batch_size = 16 * strategy.num_replicas_in_sync<train_model>
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import Ridge, Lasso
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV, KFold, cross_val_predict
Image.open(".. /input/table-accurancy-models/Table accurancy models.png" )<load_pretrained>
train_ids = sorted(loadings[loadings['Id'].isin(train_scores.Id)]['Id'].values)
 test_ids = sorted(loadings[~loadings['Id'].isin(train_scores.Id)]['Id'].values)
 test_prediction = pd.DataFrame(test_ids, columns=['Id'], dtype=str)
 target_columns =('age', 'domain1_var1', 'domain1_var2','domain2_var1','domain2_var2')
 fnc_columns = fnc.columns[1:]
degrees_columns = degrees.columns[:-1]
data = pd.merge(loadings, train_scores, on='Id')
 data = pd.merge(data, fnc, on='Id')
 data = pd.merge(data, degrees, on='Id')
 X_test = pd.merge(loadings[loadings.Id.isin(test_ids)], fnc, on='Id')
 X_test = pd.merge(X_test, degrees, on='Id' ).drop('Id', axis=1 )
response = requests.get('http://jalammar.github.io/images/bert-base-bert-large-encoders.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
def MAPE(y_true, y_pred, **kwargs):

 return np.sum(np.abs(y_true - y_pred)) / y_true.sum()
mape_scorer = make_scorer(MAPE, greater_is_better=False )
response = requests.get('http://jalammar.github.io/images/bert-encoders-input.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
best_models = {}
total_score = []
for col in target_columns:
X_train = data.dropna(subset=[col], axis=0 ).drop(list(target_columns), axis=1 ).drop('Id', axis=1)
 X_train[fnc_columns] /= 600
y_train = data.dropna(subset=[col], axis=0)[col]
gs.fit(X_train, y_train)
 best_models[col] = gs.best_estimator_
y_pred = cross_val_predict(gs.best_estimator_, X_train, y_train, cv=cv, n_jobs=-1)
 total_score.append(MAPE(y_train, y_pred))
print(col, MAPE(y_train, y_pred))
total_score = np.array(total_score)
 print(f'Total score: {np.sum(total_score*[.3,.175,.175,.175,.175])}' )
response = requests.get('http://jalammar.github.io/images/bert-output-vector.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
def get_pred(col, model):
X_train = data.dropna(subset=[col], axis=0 ).drop(list(target_columns), axis=1 ).drop('Id', axis=1)
 X_train[fnc_columns] /= 600
y_train = data.dropna(subset=[col], axis=0)[col]
y_pred = cross_val_predict(model, X_train, y_train, cv=cv, n_jobs=-1)
 return y_pred
response = requests.get('https://cdn.analyticsvidhya.com/wp-content/uploads/2019/09/bert_emnedding.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
X_test[fnc_columns] /= 600
for col in target_columns:
test_prediction[col] = best_models[col].predict(X_test )
response = requests.get('https://cdn.analyticsvidhya.com/wp-content/uploads/2019/09/bert-vs-openai-.jpg')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
response = requests.get('https://miro.medium.com/max/2272/1*9cRchmIyxP4LUnONXLM82g.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
def make_sub(test_prediction):

 target_columns =('age', 'domain1_var1', 'domain1_var2','domain2_var1','domain2_var2')
 _columns =(0,1,2,3,4)
 tst = test_prediction.rename(columns=dict(zip(target_columns, _columns)))
 tst = tst.melt(id_vars='Id',
value_vars=_columns,
value_name='Predicted')
 tst['target_type'] = tst.variable.map(dict(zip(_columns, target_columns)))
 tst['Id_'] = tst[['Id', 'target_type']].apply(lambda x: '_'.join(( str(x[0]), str(x[1]))), axis=1)
 return tst.sort_values(by=['Id', 'variable'])\ 
.drop(['Id', 'variable', 'target_type'],axis=1)\ 
.rename(columns={'Id_':'Id'})\ 
.reset_index(drop=True)\
[['Id', 'Predicted']]
response = requests.get('https://images.deepai.org/converted-papers/1911.02116/x7.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
sub = make_sub(test_prediction)
 sub.head()
<categorify><EOS> 
sub.to_csv('ridge_mape_500.csv', index=False )