<jupyter_start><jupyter_text>XGBoost machine learning with pre-flight individual telomere length data (Telo-FISH) for prediction of post-flight means  Reading in astronaut telomere length data<jupyter_code># mean telomere length values
astro_df = pd.read_csv('../data/compiled data/All_astronauts_telomere_length_dataframe.csv')
astro_df['astro id'] = astro_df['astro id'].astype('str')
astro_df['telo data'] = astro_df['telo data'].apply(lambda row: np.array(literal_eval(row)))

# make short/long telomeres
astro_df = telo_ma.make_quartiles_columns(astro_df)

for col in ['Q1', 'Q2-3', 'Q4']:
    astro_df[col] = astro_df[col].astype('int64')
    
astro_df.drop(['telo data', 'Q2-3'], axis=1, inplace=True)
# individual telomere length data 
exploded_telos = pd.read_csv('../data/compiled and processed data/exploded_telos_astros_df.csv')
exploded_telos['astro id'] = exploded_telos['astro id'].astype('str')

exploded_telos.drop(['telo means'], axis=1, inplace=True)
exploded_telos.rename({'telo data exploded':'individual telomeres'}, axis=1, inplace=True)<jupyter_output><empty_output><jupyter_text>Preparing dataframes for machine learning<jupyter_code># create 4 separate dataframes bearing pre-flight individual telomere lengths & timepoint specific post-flight values 
# (R+7, R+60, R+180, R+270, etc)

ml_telos_df_r7 = telo_ma.make_post_flight_df_and_merge(astro_df=astro_df, exploded_telos=exploded_telos, timepoint='R+7')
ml_telos_df_r60 = telo_ma.make_post_flight_df_and_merge(astro_df=astro_df, exploded_telos=exploded_telos, timepoint='R+60')
ml_telos_df_r180 = telo_ma.make_post_flight_df_and_merge(astro_df=astro_df, exploded_telos=exploded_telos, timepoint='R+180')
ml_telos_df_r270 = telo_ma.make_post_flight_df_and_merge(astro_df=astro_df, exploded_telos=exploded_telos, timepoint='R+270')
ml_telos_df_r7.head(4)
# train/test split & stratify split according to astro id/pre-flight timepoint
df = ml_telos_df_r270.copy()
train_set, test_set = train_test_split(df, test_size=0.2, shuffle=True, stratify=df[['astro id', 'timepoint']])
# intializing data cleaning pipelines

clean_process_pipe = Pipeline([('features', telo_ma.make_features(make_log_target=False)), 
                               ('dummies', telo_ma.make_dummies(drop_first=True, how_dummify='encode')),
                               ('cleaner', telo_ma.clean_data(drop_astro_id=True, timepoint='R+270', target='telo means'))
                              ])
# cleaning data with pipeline
train_clean = train_set.copy()
test_clean = test_set.copy()

train_clean = clean_process_pipe.fit_transform(train_clean)
test_clean = clean_process_pipe.fit_transform(test_clean)<jupyter_output><empty_output><jupyter_text>train_clean.head(4)<jupyter_code>model = XGBRegressor(n_estimators=200, max_depth=12, learning_rate=0.2, 
                     objective ='reg:squarederror', random_state=1)

xgb_pipe = Pipeline([('XGB', model)
                    ])

full_pipe = Pipeline([('clean_process', clean_process_pipe),
                      ('model', model)
                     ])
# five fold cross validation for predicting postflight timepoint/target with preflight individual telomeres
fit_xgb_model, telo_row = telo_ma.cv_score_fit_mae_test(train_set=train_clean, test_set=test_clean, target='R+270 telo means',
                                                        model=model, cv=5)<jupyter_output>MAE per CV fold: 
[13.87808449 13.75387456 13.8630172  13.87507261 13.96282344] 

MEAN of MAE all folds: 13.866574461545309
STD of MAE all folds: 0.06660703561835987

MAE of predict_y_test & y_test: 13.826572352872063
R2 between predict_y_test & y_test: 0.18759597114995386<jupyter_text>Clustering mean telomere length<jupyter_code>import importlib
%load_ext autoreload
%autoreload 2
%reload_ext autoreload<jupyter_output>The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
{'fraction_non_alphanumeric': 0.1089974293059126, 'fraction_numerical': 0.04704370179948586, 'mean_word_length': 5.389162561576355, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '548015', 'license': 'MIT', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1350, 'n_words': 250, 'path': 'NASA_telomeres/04a NASA Astros Telo Data Machine Learning.ipynb', 'repo_name': 'j-Luxton/NASA-astronauts-telomeres-chromos', 'size': '3890'}