FAQ: Common Classification Questions - Build a Classification Model

This community-built FAQ covers the “Build a Classification Model” exercise from the lesson “Common Classification Questions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Scientist Interview Preparation

FAQs on the exercise Build a Classification Model

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Anybody know why the solution has no code?

Believe this is it:

import pandas as pd
import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_fscore_support

df = pd.read_csv("telco_churn.csv")
# replace no internet/phone service with no
df.replace(['No internet service', 'No phone service'], 'No', inplace=True)

# replace yes and no with 1 and 0
df.replace(['YES', 'Yes', 'NO', 'No'],[1,1,0,0], inplace=True)

# since only 2 genders listed, make 'gender' into a binary column for female
df['gender'].replace(['Male', 'Female'], [1,0], inplace=True)
df.rename({'gender':'female'}, axis=1)

# one hot encoding for categorical variables
# drop_first=True removes redundant values
temp = pd.get_dummies(df[['InternetService',  'Contract', 'PaymentMethod']], drop_first=True)

# rejoin modified datasets
df1 = pd.concat([df, temp.reindex(df.index)], axis=1)

#drop columns that are now redundant
df1.drop(columns = [ 'InternetService',  'Contract', 'PaymentMethod'], inplace=True)

def model_and_eval(my_training, my_testing):
    X_train, X_test, y_train, y_test = train_test_split(my_training, my_testing, test_size=0.2, random_state=42)

    # evaluation
    clf = LogisticRegression(random_state=0).fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print(confusion_matrix(y_test, y_pred))
    tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
    print("tn:", tn, "fp:", fp, "fn:", fn, "tp:", tp)
    print(precision_recall_fscore_support(y_test, y_pred, average='weighted'))

# separate labels
df_labels = df1['Churn']

# make a dataset of everything
df_all_training = df1.drop(['Churn', 'customerID'], axis=1)

# make a dataset of just the user demographics
df_demographics_training = df1[['gender', 'SeniorCitizen', 'Partner', 'Dependents', 'tenure']]

#make a dataset of just services
df_service_training = df1[['PhoneService', 'MultipleLines', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'StreamingTV', 'StreamingMovies', 'PaperlessBilling', 'MonthlyCharges', 'InternetService_DSL', 'InternetService_Fiber optic']]
model_and_eval(df_all_training, df_labels)
model_and_eval(df_demographics_training, df_labels)
model_and_eval(df_service_training, df_labels)