<jupyter_start><jupyter_text>Model<jupyter_code>df_train_feat.columns
X = df_train_feat.drop(columns=['opinion_seas_vacc_effective_Not at all effective',
       'opinion_seas_vacc_effective_Not very effective',
       'opinion_seas_vacc_effective_Somewhat effective',
       'opinion_seas_vacc_effective_Very effective',
       'opinion_seas_risk_Somewhat high', 'opinion_seas_risk_Somewhat low',
       'opinion_seas_risk_Very Low', 'opinion_seas_risk_Very high',
       'opinion_seas_sick_from_vacc_Not at all worried',
       'opinion_seas_sick_from_vacc_Not very worried',
       'opinion_seas_sick_from_vacc_Somewhat worried',
       'opinion_seas_sick_from_vacc_Very worried'], axis=1)
y = df_train_label['h1n1_vaccine']

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

to_scale = ['household_adults', 'household_children']

X_train[to_scale] = scaler.fit_transform(X_train[to_scale])
X_test[to_scale] = scaler.fit_transform(X_test[to_scale])
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score

tree= DecisionTreeClassifier(random_state=42)
forest= RandomForestClassifier(random_state=42)
knn= KNeighborsClassifier()
svm= SVC(random_state=42)
xboost= XGBClassifier(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1, max_depth = 5, alpha = 10, n_estimators = 10)
gnb =  GaussianNB()
logreg = LogisticRegression(max_iter=1000)

models= [tree, forest, knn, svm, xboost, gnb, logreg]

for model in models:
    model.fit(X_train, y_train) # fit the model
    y_pred= model.predict(X_test) # then predict on the test set
    accuracy= accuracy_score(y_test, y_pred) # this gives us how often the algorithm predicted correctly
    clf_report= classification_report(y_test, y_pred, zero_division=1) # with the report, we have a bigger picture, with precision and recall for each class
    print(f"The accuracy of model {type(model).__name__} is {accuracy:.2f}")
    print(clf_report)
    print("\n")<jupyter_output>The accuracy of model DecisionTreeClassifier is 0.75
              precision    recall  f1-score   support

           0       0.85      0.83      0.84      4212
           1       0.42      0.44      0.43      1130

    accuracy                           0.75      5342
   macro avg       0.63      0.64      0.63      5342
weighted avg       0.76      0.75      0.75      5342



The accuracy of model RandomForestClassifier is 0.84
              precision    recall  f1-score   support

           0       0.85      0.95      0.90      4212
           1       0.70      0.40      0.51      1130

    accuracy                           0.84      5342
   macro avg       0.78      0.67      0.70      5342
weighted avg       0.82      0.84      0.82      5342



The accuracy of model KNeighborsClassifier is 0.81
              precision    recall  f1-score   support

           0       0.84      0.94      0.89      4212
           1       0.60      0.31      0.41      1130

    accuracy         [...]<jupyter_text>Logistic Regression<jupyter_code>#Change dtypes of columns
cat_cols = ['h1n1_concern', 'h1n1_knowledge', 'opinion_h1n1_vacc_effective', 'opinion_h1n1_risk','opinion_h1n1_sick_from_vacc', 'opinion_seas_vacc_effective','opinion_seas_risk', 'opinion_seas_sick_from_vacc']
df_test_feat[cat_cols] = df_test_feat[cat_cols].astype(int)
df_test_feat[cat_cols] = df_test_feat[cat_cols].astype(str)

float_cols = ['behavioral_antiviral_meds', 'behavioral_avoidance','behavioral_face_mask', 'behavioral_wash_hands','behavioral_large_gatherings', 'behavioral_outside_home','behavioral_touch_face', 'doctor_recc_h1n1', 'doctor_recc_seasonal','chronic_med_condition', 'child_under_6_months', 'health_worker', 'health_insurance', 'household_adults','household_children']
df_test_feat[float_cols] = df_test_feat[float_cols].astype(int)

#Categoricals
df_test_feat['h1n1_concern'].replace({'0': 'Not at all concerned', '1': 'Not very concerned', '2': ' Somewhat concerned', '3': 'Very concerned'}, inplace=True)
df_test_feat['h1n1_knowledge'].replace({'0': 'No knowledge', '1': 'A little knowledge', '2': 'A lot of knowledge'}, inplace=True)
df_test_feat['opinion_h1n1_vacc_effective'].replace({'1': 'Not at all effective', '2': 'Not very effective', '3': "Don't know", '4': 'Somewhat effective', '5': 'Very effective'}, inplace=True)
df_test_feat['opinion_h1n1_risk'].replace({'1': 'Very Low', '2': 'Somewhat low', '3': "Don't know", '4': 'Somewhat high', '5': 'Very high'}, inplace=True)
df_test_feat['opinion_h1n1_sick_from_vacc'].replace({'1': 'Not at all worried', '2': 'Not very worried', '3': "Don't know", '4': 'Somewhat worried', '5': 'Very worried'}, inplace=True)
df_test_feat['opinion_seas_vacc_effective'].replace({'1': 'Not at all effective', '2': 'Not very effective', '3': "Don't know", '4': 'Somewhat effective', '5': 'Very effective'}, inplace=True)
df_test_feat['opinion_seas_risk'].replace({'1': 'Very Low', '2': 'Somewhat low', '3': "Don't know", '4': 'Somewhat high', '5': 'Very high'}, inplace=True)
df_test_feat['opinion_seas_sick_from_vacc'].replace({'1': 'Not at all worried', '2': 'Not very worried', '3': "Don't know", '4': 'Somewhat worried', '5': 'Very worried'}, inplace=True)

df_test_feat = pd.get_dummies(df_test_feat, drop_first=True)

import re
regex = re.compile(r"\[|\]|<", re.IGNORECASE)

# test input data with string, int, and symbol-included columns 
df_test_feat.columns = [regex.sub("_", col) if any(x in str(col) for x in set(('[', ']', '<'))) else col for col in df_test_feat.columns.values]

target = df_test_feat.iloc[:, 0]
predictors = df_test_feat.iloc[:, 1:]

df_test_feat.head(5)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

to_scale = ['household_adults', 'household_children']

df_test_feat[to_scale] = scaler.fit_transform(df_test_feat[to_scale])<jupyter_output><empty_output><jupyter_text>H1N1 VACCINE<jupyter_code>predict_h1n1 = df_test_feat.drop(columns=['opinion_seas_vacc_effective_Not at all effective',
       'opinion_seas_vacc_effective_Not very effective',
       'opinion_seas_vacc_effective_Somewhat effective',
       'opinion_seas_vacc_effective_Very effective',
       'opinion_seas_risk_Somewhat high', 'opinion_seas_risk_Somewhat low',
       'opinion_seas_risk_Very Low', 'opinion_seas_risk_Very high',
       'opinion_seas_sick_from_vacc_Not at all worried',
       'opinion_seas_sick_from_vacc_Not very worried',
       'opinion_seas_sick_from_vacc_Somewhat worried',
       'opinion_seas_sick_from_vacc_Very worried'], axis=1)

logreg.fit(X_train, y_train) # fit the model
y_pred= logreg.predict_proba(predict_h1n1) # then predict on the test set
h1n1_vaccine = pd.DataFrame(y_pred)
h1n1_vaccine = h1n1_vaccine.drop([0], axis=1)
h1n1_vaccine = h1n1_vaccine.rename(columns={1:'h1n1_vaccine'})
h1n1_vaccine = pd.DataFrame(h1n1_vaccine['h1n1_vaccine'].round(1))
h1n1_vaccine<jupyter_output><empty_output><jupyter_text>SEASONAL VACCINE<jupyter_code>df_test_feat.columns
predict_seasonal = df_test_feat.drop(columns=['opinion_h1n1_vacc_effective_Not at all effective',
       'opinion_h1n1_vacc_effective_Not very effective',
       'opinion_h1n1_vacc_effective_Somewhat effective',
       'opinion_h1n1_vacc_effective_Very effective',
       'opinion_h1n1_risk_Somewhat high', 'opinion_h1n1_risk_Somewhat low',
       'opinion_h1n1_risk_Very Low', 'opinion_h1n1_risk_Very high',
       'opinion_h1n1_sick_from_vacc_Not at all worried',
       'opinion_h1n1_sick_from_vacc_Not very worried',
       'opinion_h1n1_sick_from_vacc_Somewhat worried',
       'opinion_h1n1_sick_from_vacc_Very worried'], axis=1)

logreg.fit(X_train, y_train) # fit the model
y_pred= logreg.predict_proba(predict_seasonal) # then predict on the test set
seasonal_vaccine = pd.DataFrame(y_pred)
seasonal_vaccine = seasonal_vaccine.drop([0], axis=1)
seasonal_vaccine = seasonal_vaccine.rename(columns={1:'seasonal_vaccine'})
seasonal_vaccine = pd.DataFrame(seasonal_vaccine['seasonal_vaccine'].round(1))
seasonal_vaccine
df = pd.read_csv('test_set_features_cleaned.csv')

final = pd.concat([h1n1_vaccine, seasonal_vaccine[['seasonal_vaccine']]], axis = 1)
final = pd.concat([df[['respondent_id']], final], axis=1)
final
final.to_csv('Final_LogReg2.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1132378223495702, 'fraction_numerical': 0.03644699140401146, 'mean_word_length': 4.95225102319236, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '266238', 'license': 'MIT', 'n_tokens_mistral': 3391, 'n_tokens_neox': 3098, 'n_words': 625, 'path': 'Logistic_Regression.ipynb', 'repo_name': 'robibanadera/Flu-Shot-Learning-Predict-H1N1-and-Seasonal-Flu-Vaccines', 'size': '8725'}