import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split, cross_val_score

def cleanup_names(name):
    return str(name).strip().replace(" ","_")

"""
Explanation: Data Science samples
End of explanation
"""


col_names = ["Mean of the integrated profile","Standard deviation of the integrated profile",
"Excess kurtosis of the integrated profile",
"Skewness of the integrated profile",
"Mean of the DM-SNR curve",
"Standard deviation of the DM-SNR curve",
"Excess kurtosis of the DM-SNR curve",
"Skewness of the DM-SNR curve",
"Class"]

col_names = [cleanup_names(col) for col in col_names]

print(col_names)

df = pd.read_csv("HTRU_2.csv", sep=",", names=col_names)

df.info()

"""
Explanation: Classifiers and Regressors
Pulsar stars
End of explanation
"""


df.head(3)

# df.info()
len(df)

from sklearn.linear_model import LogisticRegression

X = df.iloc[:, 0:8]
y = df.iloc[:,8]

def clf_model(model):
    clf = model
    
    scores = cross_val_score(clf, X, y)
    print(f"Scores: {scores}")
    print(f"Mean Score: {scores.mean()}")

clf_model(LogisticRegression())

from sklearn.naive_bayes import GaussianNB

clf_model(GaussianNB())

from sklearn.neighbors import KNeighborsClassifier
clf_model(KNeighborsClassifier())

from sklearn.tree import DecisionTreeClassifier
clf_model(DecisionTreeClassifier())

from sklearn.ensemble import RandomForestClassifier
clf_model(RandomForestClassifier())

df.Mean_of_the_integrated_profile.count()

df.Class.count()

df[df.Class == 1].Class.count()

df[df.Class == 1].Class.count()/df.Class.count()

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25)

def confusion(model):
    clf = model
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print(f'Confusion Matrix: {y_test, y_pred}')
    print(f'Classification Report: {classification_report(y_test,y_pred)}')
    return clf

confusion(LogisticRegression())

confusion(RandomForestClassifier())

from sklearn.ensemble import AdaBoostClassifier
clf_model(AdaBoostClassifier())

confusion(AdaBoostClassifier())

"""
Explanation: Mean of the integrated profile.
Standard deviation of the integrated profile.
Excess kurtosis of the integrated profile.
Skewness of the integrated profile.
Mean of the DM-SNR curve.
Standard deviation of the DM-SNR curve.
Excess kurtosis of the DM-SNR curve.
Skewness of the DM-SNR curve.
Class
End of explanation
"""


churnDF = pd.read_csv("CHURN.csv")

churnDF.Churn.head(3)

churnDF['Churn'] = churnDF['Churn']. \
replace(to_replace=['No', 'Yes'], value=[0,1])

churnDF.Churn.head()

len(churnDF.columns)

X = churnDF.iloc[:, 0:20]
y = churnDF.iloc[:,20]

X = pd.get_dummies(X)

def clf_models(model, cv=3):
    clf = model
    
    scores = cross_val_score(clf, X, y, cv=cv)
    print(f"Scores: {scores}")
    print(f"Mean Score: {scores.mean()}")

clf_models(RandomForestClassifier())

clf_models(KNeighborsClassifier())

clf_models(LinearRegression())

clf_models(AdaBoostClassifier())
# GaussianNB
# DecisionTree
# X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25)

clf_models(GaussianNB())

clf_models(DecisionTreeClassifier())

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25)

# def confusion_churn(model):
#     clf = model
#     clf.fit(X_train, y_train)
#     y_pred = clf.predict(X_test)
#     print(f'Confusion Matrix: {y_test, y_pred}')
#     print(f'Classification Report: {classification_report(y_test,y_pred)}')
#     return clf

confusion(AdaBoostClassifier(n_estimators=250))

confusion(RandomForestClassifier())

clf_models(LogisticRegression())

confusion(LogisticRegression())

"""
Explanation: Customer Churn
End of explanation
"""


