<jupyter_start><jupyter_text>Hypothesis: I guess that the logistic regression will be the best model.<jupyter_code>train_df = pd.read_csv(Path('Resources/2019loans.csv'))
test_df = pd.read_csv(Path('Resources/2020Q1loans.csv'))
train_df.head()
# Drop redundant columns to create X training data, remove target column
X_train = train_df.drop(['Unnamed: 0', 'index', 'loan_status'], axis=1)
X_train
# get dummy the x data the entire dataframe
X_dummies_train = pd.get_dummies(X_train)
print(X_dummies_train.columns)
X_dummies_train
# loan status is the target
y_train = train_df['loan_status']
y_train
y_train_label = LabelEncoder().fit_transform(train_df['loan_status'])
# Drop redundant columns to create X training data, remove target column
X_test = test_df.drop(['Unnamed: 0', 'index', 'loan_status'], axis=1)
X_test
# get dummy the x test data the entire dataframe
X_dummies_test = pd.get_dummies(X_test)
print(X_dummies_test.columns)
X_dummies_test
# add missing dummy variables to testing set
for col in X_dummies_train.columns: 
    if col not in X_dummies_test.columns: 
        X_dummies_test[col] = 0
# loan status is the target
y_test = test_df['loan_status']
y_test

# Do I need to convert the categorical data to numeric? 
y_test_label = LabelEncoder().fit_transform(test_df['loan_status'])
# Train the Logistic Regression model on the unscaled data and print the model score
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train, y_train_label)
print(f"Training Data Score: {classifier.score(X_dummies_train, y_train_label)}")
print(f"Testing Data Score: {classifier.score(X_dummies_test, y_test_label)}")
# Train a Random Forest Classifier model and print the model score
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=1).fit(X_dummies_train, y_train)
print(f'Training Score: {clf.score(X_dummies_train, y_train)}')
print(f'Testing Score: {clf.score(X_dummies_test, y_test)}')
# Scale the data and rerun the models 
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_dummies_train)
X_train_scaled = scaler.transform(X_dummies_train)
X_test_scaled = scaler.transform(X_dummies_test)
# Train the Logistic Regression model on the scaled data and print the model score
clf = LogisticRegression().fit(X_train_scaled, y_train)
print(f'Training Score: {clf.score(X_train_scaled, y_train)}')
print(f'Testing Score: {clf.score(X_test_scaled, y_test)}')
predictions = classifier.predict(X_dummies_test)
pd.DataFrame({"Prediction": predictions, "Actual": y_test_label})
# Train a Random Forest Classifier model on the scaled data and print the model score
clf = RandomForestClassifier(random_state=1).fit(X_train_scaled, y_train)
print(f'Training Score: {clf.score(X_train_scaled, y_train)}')
print(f'Testing Score: {clf.score(X_test_scaled, y_test)}')

# How do I assess which model performed better? Do I need to use a confusion matrix and assess multiple elements?<jupyter_output>Training Score: 1.0
Testing Score: 0.6548277328796257
StarcoderdataJupyterStructuredCleanDedup