<jupyter_start><jupyter_text>Load Data<jupyter_code>path = '/Users/ridleyleisy/Documents/lambda/unit_two/DS-Unit-2-Classification-1/ds4-predictive-modeling-challenge/'
train = pd.read_csv(path + 'train_features.csv')
test = pd.read_csv(path + 'test_features.csv')
labels = pd.read_csv(path + 'train_labels.csv')
sample_submission = pd.read_csv('https://drive.google.com/uc?export=download&id=1kfJewnmhowpUo381oSn3XqsQ6Eto23XV')<jupyter_output><empty_output><jupyter_text>clean data  numeric<jupyter_code>def drop_cols_rows(df):
    df.drop('num_private',axis=1,inplace=True)
    df.drop('recorded_by',axis=1,inplace=True)
    return df
train = drop_cols_rows(train)<jupyter_output><empty_output><jupyter_text>replace longitude<jupyter_code>#test whether you want to keep longitude
long_mean = train.loc[train['longitude'] !=0]['longitude'].mean()
train['longitude'].replace(0,long_mean,inplace=True)
test['longitude'].replace(0,long_mean,inplace=True)<jupyter_output><empty_output><jupyter_text>drop longitude<jupyter_code>#test whether you want to drop longitude from dataset
train = train.loc[train['longitude'] != 0]
test = test.loc[test['longitude'] != 0]<jupyter_output><empty_output><jupyter_text>Train construction year data  feature engineering<jupyter_code>from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
import seaborn as sns<jupyter_output><empty_output><jupyter_text>construction year predicted by random forest<jupyter_code>train['construction_year'] = train['construction_year'].replace(0,np.nan)
test['construction_year'] = test['construction_year'].replace(0,np.nan)
def transform_construction(df):
    
    df = df.select_dtypes(include=np.number)
    X = df.loc[~df['construction_year'].isna()]
    
    # can only use these featuers since they differ 
    features = ['amount_tsh', 'gps_height', 'longitude', 'latitude',
       'region_code', 'district_code', 'population']
    target = 'construction_year'
    
    X_train = X[features]
    y_train = X[target]
    
    X_train, X_test, y_train, y_test = train_test_split(X_train,y_train)
    m = RandomForestRegressor(n_estimators=20,max_depth=25)
    m.fit(X_train, y_train)
    
    vals = m.predict(df.loc[df['construction_year'].isna()][features])
    
    return  vals
train.loc[train['construction_year'].isna(),'construction_year'] = transform_construction(train)
test.loc[test['construction_year'].isna(),'construction_year'] = transform_construction(test)
train['construction_year'] = round(train['construction_year'])
test['construction_year'] = round(test['construction_year'])<jupyter_output><empty_output><jupyter_text>construction year based on distribution<jupyter_code># getting distributions for construction years
dist = train['construction_year'].value_counts(normalize=True)
dist_test = test['construction_year'].value_counts(normalize=True)
# applying distribution of construction year to na values for train and test sets
train.loc[train['construction_year'].isna(),'construction_year'] = np.random.choice(dist.index, size=train['construction_year'].isna().sum(),p=dist.values)
test.loc[test['construction_year'].isna(),'construction_year'] = np.random.choice(dist_test.index, size=test['construction_year'].isna().sum(),p=dist_test.values)<jupyter_output><empty_output><jupyter_text>Add time since construction<jupyter_code>def add_construction_diff(df):
    df['date_recorded'] = pd.to_datetime(df['date_recorded'])
    df['construction_year'] = pd.to_datetime(df['construction_year'],format="%Y")
    df['time_since_construction'] = (df['date_recorded'] - df['construction_year']).dt.days
    df.loc[df['time_since_construction'] < 0,'time_since_construction'] = 0    
    df['construction_year'] = df['construction_year'].dt.year
    return df
test = add_construction_diff(test)
train = add_construction_diff(train)<jupyter_output><empty_output><jupyter_text>Feature engineering<jupyter_code>def add_ratios(df):
    df['tsh_by_longitude'] = df['amount_tsh'] / df['longitude']
    df['tsh_by_latitude'] = df['amount_tsh'] / abs(df['latitude'])
    df['tsh_by_height'] =  df['amount_tsh'] / df['gps_height']
    df['tsh_by_height'] = df['tsh_by_height'].replace(np.inf,0).replace(np.nan,0)
    return df
test = add_ratios(test)
train = add_ratios(train)<jupyter_output><empty_output><jupyter_text>reduce label size to fit feature engineers<jupyter_code>labels = labels.merge(train,on='id')[['id','status_group']]<jupyter_output><empty_output><jupyter_text>Encoding and Scaling Categorical Data<jupyter_code>import category_encoders as ce
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
unique = train.describe(exclude=np.number).T.sort_values(by='unique')
cat_features = list(unique.loc[unique['unique'] < 130].index)
numeric_features = list(train.describe().columns[1:])
encode_features = cat_features
features = numeric_features + encode_features<jupyter_output><empty_output><jupyter_text>Pipeline<jupyter_code>def transform_data_hot(features):
    encoder = ce.OneHotEncoder(use_cat_names=True)
    scaler = RobustScaler()
    X_train = train[features]
    y_train = labels['status_group']

    X_train, X_val, y_train, y_val = train_test_split(
        X_train, y_train, train_size=0.80, test_size=0.20, 
        stratify=y_train, random_state=42)

    X_train_sub = X_train[features]
    X_val_sub = X_val[features]
    
    pipeline1 = Pipeline([('encoder',encoder),('scaler',scaler)])
    X_train_sub_encoded = pipeline1.fit_transform(X_train_sub)
    X_val_sub_encoded = pipeline1.transform(X_val_sub)
    
    test_encoded = pipeline1.transform(test[features])
    X_encode_cols = encoder.transform(X_train_sub)
    
    return X_train_sub_encoded, X_val_sub_encoded, y_train, y_val, X_encode_cols,test_encoded
def transform_data_ordinal(features):
    encoder = ce.OrdinalEncoder()
    scaler = RobustScaler()
    X_train = train[features]
    y_train = labels['status_group']

    X_train, X_val, y_train, y_val = train_test_split(
        X_train, y_train, train_size=0.80, test_size=0.20, 
        stratify=y_train, random_state=42)

    X_train_sub = X_train[features]
    X_val_sub = X_val[features]
    
    pipeline1 = Pipeline([('encoder',encoder),('scaler',scaler)])
    X_train_sub_encoded = pipeline1.fit_transform(X_train_sub)
    X_val_sub_encoded = pipeline1.transform(X_val_sub)
    
    X_encode_cols = encoder.transform(X_train_sub)
    return X_train_sub_encoded, X_val_sub_encoded, y_train, y_val, X_encode_cols<jupyter_output><empty_output><jupyter_text>Logistical Regression<jupyter_code>#model = LogisticRegression(solver='lbfgs', multi_class='auto', max_iter=1000)
#model.fit(X_train_scaled, y_train)
model.score(X_val_scaled,y_val)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>from sklearn.tree import DecisionTreeClassifier
m = DecisionTreeClassifier(max_depth=25)
m.fit(X_train_scaled,y_train)
m.score(X_val_scaled,y_val)
m.score(X_train_scaled,y_train)
pd.DataFrame(m.feature_importances_,X_train_sub_encoded.columns).sort_values(by=0).tail(10)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier<jupyter_output><empty_output><jupyter_text>ordinal<jupyter_code>all_cats = train.describe(exclude='number').T.sort_values(by='unique').index
all_cats = all_cats.delete(all_cats.get_loc('date_recorded'))
X_train_sub_encoded, X_val_sub_encoded, y_train, y_val = transform_data_ordinal(features)<jupyter_output><empty_output><jupyter_text>hot encoded<jupyter_code>X_train_sub_encoded, X_val_sub_encoded, y_train, y_val,cols, test_encoded = transform_data_hot(features)<jupyter_output><empty_output><jupyter_text>model<jupyter_code>m = RandomForestClassifier(n_estimators=300,max_depth=28,max_features='auto',n_jobs=-1)
m.fit(X_train_sub_encoded,y_train)
m.score(X_val_sub_encoded,y_val)
m.score(X_train_sub_encoded,y_train)
pd.DataFrame(m.feature_importances_,cols.columns).sort_values(by=0)
preds = m.predict(test_encoded)<jupyter_output><empty_output><jupyter_text>Gradient Boosting<jupyter_code>gbc.score(X_val_sub_encoded,y_val)
gbc.score(X_train_sub_encoded,y_train)<jupyter_output><empty_output><jupyter_text>Reduce Dimensions<jupyter_code>feat_impt = pd.DataFrame(m.feature_importances_,encoded_cols).sort_values(by=0)
feat_impt[::-1]
cuml = np.cumsum(feat_impt[::-1])
(cuml.loc[cuml[0] < .95]).head()
cuml.loc[cuml[0] < .95].plot()
plt.xticks(rotation=90)
reduced_features = cuml.loc[cuml[0] < .95].index
train.columns<jupyter_output><empty_output><jupyter_text>Random Forest Reduced Dims  Predicting for Kaggle<jupyter_code>submission = pd.DataFrame(test['id'])
submission['status_group'] = preds
submission.to_csv('submission-01.csv',index=False)
import pandas as pd

# Filenames of your submissions you want to ensemble
files = ['submission-01.csv', 'submission-02.csv', 'submission-03.csv']

submissions = (pd.read_csv(file)[['status_group']] for file in files)
ensemble = pd.concat(submissions, axis='columns')
majority_vote = ensemble.mode(axis='columns')[0]

sample_submission = pd.read_csv('sample_submission.csv')
submission = sample_submission.copy()
submission['status_group'] = majority_vote
submission.to_csv('my-ultimate-ensemble-submission.csv', index=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup