# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/terrainthesky-hub/DS-Unit-2-Kaggle-Challenge/blob/master/module2-random-forests/Lesley_Rich_LS_DS_222_assignment.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="JoxbTL-lIE29" colab_type="code" colab={}
import pandas as pd
import os



# + id="bpv19JWrIE3C" colab_type="code" colab={} outputId="e32d8a56-4af9-4ad0-e3c0-2cc31bccb3f5"
# !pip install pandas-profiling==2.*

# + id="jzOYNWEpIE3F" colab_type="code" colab={} outputId="5f9617eb-05b0-484e-92ed-86ba9025f70e"
# !pip install category_encoders==2.*

# + id="bOYns0tbIE3H" colab_type="code" colab={} outputId="0ac2760a-b7a3-4542-dab4-18a21e06d171"
from sklearn.model_selection import train_test_split

train = pd.merge(pd.read_csv(r'C:\Users\Lesley\Downloads\train_features.csv'), pd.read_csv(r'C:\Users\Lesley\Downloads\train_labels.csv'))

test = pd.read_csv(r'C:\Users\Lesley\Downloads\test_features.csv')

train, val = train_test_split(train, train_size=0.80, test_size=0.20,
                              stratify=train['status_group'], random_state=42)

train.shape, val.shape, test.shape


# + id="2As3NAxUIE3J" colab_type="code" colab={} outputId="babe9e4c-3778-4f3c-abc7-d4ea9d851f92"


# + id="QyHkyrfEIE3M" colab_type="code" colab={}
import numpy as np

def wrangle(X):
    """Wrangle train, validate, and test sets in the same way"""
    
    # Prevent SettingWithCopyWarning
    X = X.copy()
    
    # About 3% of the time, latitude has small values near zero,
    # outside Tanzania, so we'll treat these values like zero.
    X['latitude'] = X['latitude'].replace(-2e-08, 0)
    
    # When columns have zeros and shouldn't, they are like null values.
    # So we will replace the zeros with nulls, and impute missing values later.
    # Also create a "missing indicator" column, because the fact that
    # values are missing may be a predictive signal.
    cols_with_zeros = ['longitude', 'latitude', 'construction_year', 
                       'gps_height', 'population']
    for col in cols_with_zeros:
        X[col] = X[col].replace(0, np.nan)
        X[col+'_MISSING'] = X[col].isnull()
            
    # Drop duplicate columns
    duplicates = ['quantity_group', 'payment_type']
    X = X.drop(columns=duplicates)
    
    # Drop recorded_by (never varies) and id (always varies, random)
    unusable_variance = ['recorded_by']
    X = X.drop(columns=unusable_variance)
    
    # Convert date_recorded to datetime
    X['date_recorded'] = pd.to_datetime(X['date_recorded'], infer_datetime_format=True)
    
    # Extract components from date_recorded, then drop the original column
    X['year_recorded'] = X['date_recorded'].dt.year
    X['month_recorded'] = X['date_recorded'].dt.month
    X['day_recorded'] = X['date_recorded'].dt.day
    X = X.drop(columns='date_recorded')
    
    # Engineer feature: how many years from construction_year to date_recorded
    X['years'] = X['year_recorded'] - X['construction_year']
    X['years_MISSING'] = X['years'].isnull()
    
    # return the wrangled dataframe
    return X

train = wrangle(train)
val = wrangle(val)
test = wrangle(test)

# + id="l1wowJynIE3O" colab_type="code" colab={}
target = 'status_group'

train_features = train.drop(columns=[target, 'id'])

numeric_features = train_features.select_dtypes(include='number').columns.tolist()

cardinality = train_features.select_dtypes(exclude='number').nunique()

categorical_features = cardinality[cardinality <= 50].index.tolist()

features = numeric_features + categorical_features

# + id="ZuBaY-n9IE3Q" colab_type="code" colab={} outputId="b3e58e49-f307-4638-da60-fcc37f18f67a"


# + id="cm8RZNuMIE3S" colab_type="code" colab={}

X_train = train[features]
y_train = train[target]
X_val = val[features]
y_val = val[target]
X_test = test[features]

# + id="_VBxRXCkIE3U" colab_type="code" colab={} outputId="b2dd26b5-ba62-460a-c34e-e8b51337c2c1"
import category_encoders as ce
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline

pipeline = make_pipeline(
    ce.OrdinalEncoder(),
    SimpleImputer(),
    RandomForestClassifier(max_depth=20, n_estimators=22))

pipeline.fit(X_train[features], y_train)

score = pipeline.score(X_val[features], y_val)
print('Random Forest Accuracy Score', score)

# + id="ertfzNK2IE3X" colab_type="code" colab={} outputId="ce90e0b8-7bf4-4b09-ee5c-f965af0aedfa"
# %%time

pipeline = make_pipeline(
    ce.OrdinalEncoder(),
    SimpleImputer(),
    RandomForestClassifier(max_depth=20, n_estimators=22))

pipeline.fit(X_train[features], y_train)

score = pipeline.score(X_val[features], y_val)
print('Random Forest Accuracy Score', score)

# + id="AQHPwA4HIE3Z" colab_type="code" colab={}


# + id="GA23Xy3DIE3a" colab_type="code" colab={}
y_pred = pipeline.predict(X_test)

# + id="bnFKYmAAIE3c" colab_type="code" colab={}


path=r'C:\Users\Lesley\Desktop\Lambda\Lesley_Rich'
submission = test[['id']].copy()
submission['status_group'] = y_pred
# submission['status_group']
submission.to_csv(path+'DecisionTreeWaterPumpSub2.csv', index=False)

# + id="_8KkPK7pIE3e" colab_type="code" colab={}


# + id="03XsESzxIE3h" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered