#!/usr/bin/python

# system tools
import os, sys, argparse
sys.path.append(os.path.join(".."))

import pandas as pd

# import my classifier utility functions - see the Github repo!
import utils.classifier_utils as clf

# Machine learning stuff
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import ShuffleSplit
from sklearn import metrics

# matplotlib
import matplotlib.pyplot as plt

def main():
    # Load in the data:
    script = pd.read_csv(os.path.join("..", "data", "Game_of_Thrones_Script.csv"))

    # Splitting into train and test sets
    X_train, X_test, y_train, y_test = train_test_split(script['Sentence'].values, # "Features" (sentences)
                                                        script['Season'].values, # Labels
                                                        test_size = 0.15, 
                                                        random_state = 42,
                                                        stratify = script['Season'].values) # If full dataset has 12% sentences from season 1, have 12% of sentences in train + test

    # Instead of having sentences, get token count vectors
    vectorizer = CountVectorizer()
    X_train_feats = vectorizer.fit_transform(X_train)
    X_test_feats = vectorizer.transform(X_test) 
    feature_names = vectorizer.get_feature_names() # Get feature names (words from the numbers)
    
    # Basic logistic regression, train + predict
    # Define classifer
    classifier = LogisticRegression(random_state=42)

    # Train the model
    classifier.fit(X_train_feats, y_train)

    # Test model on validation set
    y_pred = classifier.predict(X_test_feats)

    # Evaluation
    classif_report = pd.DataFrame(metrics.classification_report(y_test, y_pred, output_dict = True))
    print(classif_report)
    classif_report.to_csv(os.path.join("out", "lr_classif_report.csv"))
    conf_matrix = pd.DataFrame(metrics.confusion_matrix(y_test, y_pred))
    print(conf_matrix)
    conf_matrix.to_csv(os.path.join("out", "lr_conf_matrix.csv"))

if __name__=="__main__":
    # Initialise ArgumentParser class
    parser = argparse.ArgumentParser(description = "")
    
    # Add inpath argument
    parser.add_argument(
        "-c",
        "--cnn", 
        type = str,
        default = "ShallowNet",
        required = False,
        help= "")
    
    # Add outpath argument
    parser.add_argument(
        "-r",
        "--resizedim",
        type = list, 
        default = [32, 32],
        required = False,
        help = "")
        
    # Add outpath argument
    parser.add_argument(
        "-b",
        "--batch_size",
        type = int, 
        default = 200,
        required = False,
        help = "")
    
    # Taking all the arguments we added to the parser and input into "args"
    args = parser.parse_args()
    
    # Performing main function
    main()
#!/usr/bin/python

# system tools
import os, sys, argparse
sys.path.append(os.path.join(".."))

import pandas as pd

# import my classifier utility functions - see the Github repo!
import utils.classifier_utils as clf

# Machine learning stuff
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import ShuffleSplit
from sklearn import metrics

# matplotlib
import matplotlib.pyplot as plt

def main():
    # Load in the data:
    script = pd.read_csv(os.path.join("..", "data", "Game_of_Thrones_Script.csv"))

    # Splitting into train and test sets
    X_train, X_test, y_train, y_test = train_test_split(script['Sentence'].values, # "Features" (sentences)
                                                        script['Season'].values, # Labels
                                                        test_size = 0.15, 
                                                        random_state = 42,
                                                        stratify = script['Season'].values) # If full dataset has 12% sentences from season 1, have 12% of sentences in train + test

    # Instead of having sentences, get token count vectors
    vectorizer = CountVectorizer()
    X_train_feats = vectorizer.fit_transform(X_train)
    X_test_feats = vectorizer.transform(X_test) 
    feature_names = vectorizer.get_feature_names() # Get feature names (words from the numbers)
    
    # Basic logistic regression, train + predict
    # Define classifer
    classifier = LogisticRegression(random_state=42)

    # Train the model
    classifier.fit(X_train_feats, y_train)

    # Test model on validation set
    y_pred = classifier.predict(X_test_feats)

    # Evaluation
    classif_report = pd.DataFrame(metrics.classification_report(y_test, y_pred, output_dict = True))
    print(classif_report)
    classif_report.to_csv(os.path.join("out", "lr_classif_report.csv"))
    conf_matrix = pd.DataFrame(metrics.confusion_matrix(y_test, y_pred))
    print(conf_matrix)
    conf_matrix.to_csv(os.path.join("out", "lr_conf_matrix.csv"))

if __name__=="__main__":
    # Initialise ArgumentParser class
    parser = argparse.ArgumentParser(description = "")
    
    # Add inpath argument
    parser.add_argument(
        "-c",
        "--cnn", 
        type = str,
        default = "ShallowNet",
        required = False,
        help= "")
    
    # Add outpath argument
    parser.add_argument(
        "-r",
        "--resizedim",
        type = list, 
        default = [32, 32],
        required = False,
        help = "")
        
    # Add outpath argument
    parser.add_argument(
        "-b",
        "--batch_size",
        type = int, 
        default = 200,
        required = False,
        help = "")
    
    # Taking all the arguments we added to the parser and input into "args"
    args = parser.parse_args()
    
    # Performing main function
    main()
#!/usr/bin/python # system tools # import my classifier utility functions - see the Github repo! # Machine learning stuff # matplotlib # Load in the data: # Splitting into train and test sets # "Features" (sentences) # Labels # If full dataset has 12% sentences from season 1, have 12% of sentences in train + test # Instead of having sentences, get token count vectors # Get feature names (words from the numbers) # Basic logistic regression, train + predict # Define classifer # Train the model # Test model on validation set # Evaluation # Initialise ArgumentParser class # Add inpath argument # Add outpath argument # Add outpath argument # Taking all the arguments we added to the parser and input into "args" # Performing main function