<jupyter_start><jupyter_text>Preprocessing<jupyter_code>X = df.drop(columns=['h1n1_vaccine', 'seasonal_vaccine'])
y = df[['h1n1_vaccine', 'seasonal_vaccine']]

for column in numerical_columns:
    df[column] = np.log(df[column])

df
#Categorical
cat_df = X[categorical_columns]
recat_df = pd.get_dummies(data=cat_df)

#Numerical
num_df = X[numerical_columns]

scaler = StandardScaler()
scaled_num = scaler.fit_transform(num_df)
scaled_num_df = pd.DataFrame(scaled_num, index=num_df.index, columns=num_df.columns)

encoded_df = pd.concat([recat_df, scaled_num_df], axis=1)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(encoded_df, y, test_size=0.3, random_state=42)

y_train_h1n1 = y_train['h1n1_vaccine']
y_train_seas = y_train['seasonal_vaccine']

y_test_h1n1 = y_test['h1n1_vaccine']
y_test_seas = y_test['seasonal_vaccine']
#Create arrays
X_train = np.asarray(X_train)
X_test = np.asarray(X_test)
y_train = np.asarray(y_train)
y_test = np.asarray(y_test)

y_train_h1n1 = np.asarray(y_train_h1n1)
y_test_h1n1 = np.asarray(y_test_h1n1)

y_train_seas = np.asarray(y_train_seas)
y_test_seas = np.asarray(y_test_seas)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score

model = RandomForestRegressor()
model.fit(X_train, y_train_h1n1)

y_pred_h1n1 = model.predict(X_test)
#Set up RandomSearch
n_estimators = [int(x) for x in np.linspace(start=200, stop=2000, num=10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 100, num=11)]
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]

random_grid = {'n_estimators': n_estimators,
              'max_features': max_features,
              'max_depth': max_depth,
              'min_samples_split': min_samples_split,
              'min_samples_leaf': min_samples_leaf,
              'bootstrap': bootstrap}

rf = RandomForestRegressor()
rf_random = RandomizedSearchCV(estimator=rf, 
                               param_distributions=random_grid, 
                               n_iter=50, 
                               cv=3, 
                               verbose=2, 
                               random_state=42, 
                               n_jobs=-1)<jupyter_output><empty_output><jupyter_text>H1N1<jupyter_code>#model = rf_random
model_h1n1 = RandomForestRegressor(n_estimators=800,
                              min_samples_split=2,
                              min_samples_leaf=4,
                              max_features='sqrt',
                              max_depth=20,
                              bootstrap=False)
model_h1n1.fit(X_train, y_train_h1n1)
#model.best_params_
base_model = RandomForestRegressor(n_estimators=10, random_state=42)
base_model.fit(X_train, y_train_h1n1)
evaluate(base_model, X_test, y_test_h1n1)
#best_random = rf_random.best_estimator_
evaluate(model_h1n1, X_test, y_test_h1n1)
y_predicted_h1n1 = model_h1n1.predict(X_test)<jupyter_output><empty_output><jupyter_text>Seasonal<jupyter_code>#model = rf_random
model_seas = RandomForestRegressor(n_estimators=800,
                              min_samples_split=2,
                              min_samples_leaf=4,
                              max_features='sqrt',
                              max_depth=20,
                              bootstrap=False)

model_seas.fit(X_train, y_train_seas)
#model.best_params_
base_model = RandomForestRegressor(n_estimators=10, random_state=42)
base_model.fit(X_train, y_train_seas)
base_accuracy = evaluate(base_model, X_test, y_test_seas)
evaluate(model_seas, X_test, y_test_seas)
y_predicted_seas = model_seas.predict(X_test)<jupyter_output><empty_output><jupyter_text>Combine Targets<jupyter_code>y_predicted = np.vstack((y_predicted_h1n1, y_predicted_seas)).T
y_predicted
roc_auc_score(y_test, y_predicted)<jupyter_output><empty_output><jupyter_text>Transform Test Data<jupyter_code>df_full = pd.read_csv('../Data/test_set_features.csv')

df = df_full.drop(columns=['employment_occupation', 'employment_industry', 'health_insurance', 'respondent_id'])
categorical_columns = [
    #'education',
    #'income_poverty',
    #'marital_status',
    #'rent_or_own',
    #'employment_industry',
    #'employment_occupation',
    'sex',
    #'hhs_geo_region',
    #'census_msa',
    'race',
    'age_group',
    'behavioral_face_mask',
    'behavioral_wash_hands',
    'behavioral_antiviral_meds',
    'behavioral_outside_home',
    'behavioral_large_gatherings',
    'behavioral_touch_face',
    'behavioral_avoidance',
    'health_worker',
    #'child_under_6_months',
    'chronic_med_condition',
    'doctor_recc_h1n1',
    'doctor_recc_seasonal',
]

numerical_columns = [
    #'household_children',
    #'household_adults',
    'h1n1_concern',
    'h1n1_knowledge',
    'opinion_h1n1_risk',
    'opinion_h1n1_vacc_effective',
    'opinion_h1n1_sick_from_vacc',
    'opinion_seas_vacc_effective',
    'opinion_seas_risk',
    'opinion_seas_sick_from_vacc',
    #'health_insurance'
]

for column in numerical_columns:
    df[column] = df[column].fillna(df[column].mean())

health_worker_mean = df['health_worker'].mean()
behavioral_face_mask_mean = df['health_worker'].mean()
behavioral_wash_hands_mean = df['behavioral_wash_hands'].mean()
behavioral_antiviral_meds_mean = df['behavioral_antiviral_meds'].mean()
behavioral_outside_home_mean = df['behavioral_outside_home'].mean()
behavioral_large_gatherings_mean = df['behavioral_large_gatherings'].mean()
behavioral_touch_face_mean = df['behavioral_touch_face'].mean()
behavioral_avoidance_mean = df['behavioral_avoidance'].mean()
child_under_6_months_mean = df['child_under_6_months'].mean()
chronic_med_condition_mean = df['chronic_med_condition'].mean()
doctor_recc_h1n1_mean = df['doctor_recc_h1n1'].mean()
doctor_recc_seasonal_mean = df['doctor_recc_h1n1'].mean()

df['health_worker'] = df['health_worker'].fillna(health_worker_mean)
df['behavioral_face_mask'] = df['behavioral_face_mask'].fillna(behavioral_face_mask_mean)
df['behavioral_wash_hands'] = df['behavioral_wash_hands'].fillna(behavioral_wash_hands_mean)
df['behavioral_antiviral_meds'] = df['behavioral_antiviral_meds'].fillna(behavioral_antiviral_meds_mean)
df['behavioral_outside_home'] = df['behavioral_outside_home'].fillna(behavioral_outside_home_mean)
df['behavioral_large_gatherings'] = df['behavioral_large_gatherings'].fillna(behavioral_large_gatherings_mean)
df['behavioral_touch_face'] = df['behavioral_touch_face'].fillna(behavioral_touch_face_mean)
df['behavioral_avoidance'] = df['behavioral_avoidance'].fillna(behavioral_avoidance_mean)
df['child_under_6_months'] = df['child_under_6_months'].fillna(child_under_6_months_mean)
df['chronic_med_condition'] = df['chronic_med_condition'].fillna(chronic_med_condition_mean)
df['doctor_recc_h1n1'] = df['doctor_recc_h1n1'].fillna(doctor_recc_h1n1_mean)
df['doctor_recc_seasonal'] = df['doctor_recc_seasonal'].fillna(doctor_recc_seasonal_mean)

full_cols = categorical_columns + numerical_columns
df = df[full_cols]<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code>X = df.copy()
#Categorical
cat_df = X[categorical_columns]
recat_df = pd.get_dummies(data=cat_df)

#Numerical
num_df = X[numerical_columns]

scaler = StandardScaler()
scaled_num = scaler.fit_transform(num_df)
scaled_num_df = pd.DataFrame(scaled_num, index=num_df.index, columns=num_df.columns)

encoded_df = pd.concat([recat_df, scaled_num_df], axis=1)
X = np.asarray(encoded_df)
results_h1n1 = model_h1n1.predict(X)
results_seas = model_seas.predict(X)

results = np.vstack((results_h1n1, results_seas)).T
results = pd.DataFrame(results, columns=['h1n1_vaccine', 'seasonal_vaccine'])

submission = pd.concat([df_full, results], axis=1)
submission = submission[['respondent_id', 'h1n1_vaccine', 'seasonal_vaccine']]
submission.to_csv('../Submissions/Submission 7.1.21.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11585136994870511, 'fraction_numerical': 0.018766420618040784, 'mean_word_length': 5.319367588932806, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '232879', 'license': 'MIT', 'n_tokens_mistral': 3183, 'n_tokens_neox': 2987, 'n_words': 346, 'path': 'Notebooks/H1N! Selective Modeling.ipynb', 'repo_name': 'MikeAnderson89/H1N1_and_Flu_Predictions', 'size': '7993'}