image_output_1.png
from sklearn.feature_selection import mutual_info_classif

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report, f1_score, make_scorer

from sklearn.model_selection import cross_val_score, GridSearchCV, RandomizedSearchCV

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier

from sklearn.svm import LinearSVC

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

rs = 42
mi = 10000
sns.set_style('whitegrid')
models = [DecisionTreeClassifier(random_state=rs), KNeighborsClassifier(), GaussianNB(), LinearSVC(random_state=rs, max_iter=mi), SVC(random_state=rs, max_iter=mi), LogisticRegression(random_state=rs, max_iter=mi)]
train_path = '../input/cs-3110-mini-project/train.csv'
test_path = '../input/cs-3110-mini-project/test.csv'

def plt_distribution(dataset):
    data = dataset.copy()
    fig, axes = plt.subplots(5, 3)
    fig.set_figwidth(16)
    fig.set_figheight(20)
    sns.histplot(data=data, x='account_length', kde=True, ax=axes[0,0])
    sns.histplot(data=data, x='number_vm_messages', kde=True, ax=axes[0,1])
    sns.histplot(data=data, x='total_day_min', kde=True, ax=axes[0,2])
    sns.histplot(data=data, x='total_day_calls', kde=True, ax=axes[1,0])
    sns.histplot(data=data, x='total_day_charge', kde=True, ax=axes[1,1])
    sns.histplot(data=data, x='total_eve_min', kde=True, ax=axes[1,2])
    sns.histplot(data=data, x='total_eve_calls', kde=True, ax=axes[2,0])
    sns.histplot(data=data, x='total_eve_charge', kde=True, ax=axes[2,1])
    sns.histplot(data=data, x='total_night_minutes', kde=True, ax=axes[2,2])
    sns.histplot(data=data, x='total_night_calls', kde=True, ax=axes[3,0])
    sns.histplot(data=data, x='total_night_charge', kde=True, ax=axes[3,1])
    sns.histplot(data=data, x='total_intl_minutes', kde=True, ax=axes[3,2])
    sns.histplot(data=data, x='total_intl_calls', kde=True, ax=axes[4,0])
    sns.histplot(data=data, x='total_intl_charge', kde=True, ax=axes[4,1])
    sns.histplot(data=data, x='customer_service_calls', kde=True, ax=axes[4,2])
    plt.show()
    
def make_mi_scores(X, y):
    discrete_features = X.dtypes == int
    mi_scores = mutual_info_classif(X, y, discrete_features=discrete_features, random_state=rs)
    mi_scores = pd.Series(mi_scores, name="MI Scores", index=X.columns)
    mi_scores = mi_scores.sort_values(ascending=False)
    return mi_scores

def plot_mi_scores(scores):
    plt.figure(dpi=100, figsize=(8, 10))
    scores = scores.sort_values(ascending=True)
    width = np.arange(len(scores))
    ticks = list(scores.index)
    plt.barh(width, scores)
    plt.yticks(width, ticks)
    plt.title("Mutual Information Scores")
    plt.show()
    
def evaluate_for_models(models, X, y):
    results = pd.DataFrame({'Model': [], 
                            'ScoreMean(F1)': [], 'Score Standard Deviation(F1)': [], 
                            'ScoreMean': [], 'Score Standard Deviation': []})
    for model in models:
        score_f1 = cross_val_score(model, X, y, 
                                scoring='f1')
        score = cross_val_score(model, X, y)
        new_result = {'Model': model.__class__.__name__, 
                      'ScoreMean(F1)': score_f1.mean(), 'Score Standard Deviation(F1)': score_f1.std(), 
                      'ScoreMean': score.mean(), 'Score Standard Deviation': score.std()}
        results = results.append(new_result, ignore_index=True)
    return results.sort_values(by=['ScoreMean(F1)', 'Score Standard Deviation(F1)', 
                                   'ScoreMean', 'Score Standard Deviation'], ascending=False)

def classification_report_with_accuracy_score(y_true, y_pred):
    print(classification_report(y_true, y_pred))
    return f1_score(y_true, y_pred)

def encode(dataframe, is_train=True):
    data = dataframe.copy()
    encoded_data = pd.get_dummies(data, columns=['location_code'])
    if is_train:
        encoded_data['Churn'] = encoded_data['Churn'].map({'Yes': 1, 'No': 0})
    for col in ['intertiol_plan', 'voice_mail_plan']:
        encoded_data[col] = encoded_data[col].map({'yes': 1, 'no': 0})
    return encoded_data

def add_features(dataframe):
    
    global lr_day
    global lr_eve
    global lr_nyt
    
    data = dataframe.copy()
    
    data['total_min'] = data['total_day_min'] + data['total_eve_min'] + data['total_night_minutes'] + data['total_intl_minutes']

    try:
        data['expected_total_day_charge'] = lr_day.predict(data[['total_day_min']])
        data['expected_total_eve_charge'] = lr_eve.predict(data[['total_eve_min']])
        data['expected_total_nyt_charge'] = lr_nyt.predict(data[['total_night_minutes']])
    except NameError:
        lr_day = LinearRegression()
        lr_eve = LinearRegression()
        lr_nyt = LinearRegression()

        lr_day.fit(data[['total_day_min']], data['total_day_charge'])
        lr_eve.fit(data[['total_eve_min']], data['total_eve_charge'])
        lr_nyt.fit(data[['total_night_minutes']], data['total_night_charge'])

        data['expected_total_day_charge'] = lr_day.predict(data[['total_day_min']])
        data['expected_total_eve_charge'] = lr_eve.predict(data[['total_eve_min']])
        data['expected_total_nyt_charge'] = lr_nyt.predict(data[['total_night_minutes']])
    
    data['error_total_day_charge'] = abs(data['expected_total_day_charge'] - data['total_day_charge'])
    data['error_total_eve_charge'] = abs(data['expected_total_eve_charge'] - data['total_eve_charge'])
    data['error_total_nyt_charge'] = abs(data['expected_total_nyt_charge'] - data['total_night_charge'])
    
    return data

train_df = pd.read_csv(train_path)
test_df = pd.read_csv(test_path)

plt_distribution(train_df)