<jupyter_start><jupyter_text>DS1 Predictive Modeling Challenge  Second BaselineThe initial baseline is to predict the majority class for each observation. Accuracy score for initial baseline was .53754 on Kaggle.<jupyter_code>import pandas as pd
pd.set_option('max_columns', 100)
train = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_features.csv')
test = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/test_features.csv')  # Unlabeled, for Kaggle submission
labels = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_labels.csv')
train = train.join(labels.status_group, how='inner')
train.head()
train.describe()
train.status_group.value_counts()
id = train.id
id.head()
train = train.drop('id', axis=1)
train.head()
train.date_recorded = pd.to_datetime(train.date_recorded)
train.head()
train.dtypes
from sklearn.model_selection import train_test_split
X = train_num
y = train.status_group
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(solver='liblinear', multi_class='auto').fit(X_train, y_train)
log_reg.score(X, y)
from sklearn.metrics import accuracy_score, classification_report
y_pred = log_reg.predict(X_test)
print(classification_report(y_test, y_pred))
print('accuracy', accuracy_score(y_test, y_pred))
y_pred
y_pred = pd.DataFrame({'status_group': [y_pred]})
y_pred.head()
test.head()
test_num = test.select_dtypes('number')
test_num.head()
test.dtypes
test.date_recorded = pd.to_datetime(test.date_recorded)
test.dtypes
test_num['status_group'] = log_reg.predict(test_num.drop(columns='id'))
test_num.head()
test_num.shape
sub = test_num[['id', 'status_group']]
sub.head()
sub.to_csv('submission.csv', index=False)<jupyter_output><empty_output><jupyter_text>The second submission was with this second baseline, and it scored a .55077 in the Kaggle competition.  Third Submission<jupyter_code>!pip install category_encoders
import category_encoders as ce
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
pd.set_option('max_columns', 100)
train = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_features.csv')
test = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/test_features.csv')  # Unlabeled, for Kaggle submission
labels = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_labels.csv')
train = train.join(labels.status_group, how='inner')
train = train.drop('id', axis=1)
train.head()
train.isna().sum()
train_no_nulls = train.dropna(axis=1)
train_no_nulls.head()
train_no_nulls.nunique().sort_values()
cols = ['recorded_by', 'lga', 'date_recorded', 'ward', 'wpt_name']
train = train_no_nulls.drop(columns=cols)
train.head()
X = train.drop('status_group', axis=1)
y = train.status_group
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score

pipeline = make_pipeline(
    ce.OneHotEncoder(use_cat_names=True), 
    StandardScaler(), 
    LogisticRegression(solver='liblinear', multi_class='ovr')
)

pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
pipeline.score(X, y)
accuracy_score(y_test, y_pred)
y_pred
test.head()
test.isna().sum()
test_no_nulls = test.dropna(axis=1)
test_no_nulls.head()
test_no_nulls.nunique().sort_values()
test = test_no_nulls.drop(columns=cols)
test.head()
test['status_group'] = pipeline.predict(test.drop(columns='id'))
test.head()
sub = test[['id', 'status_group']]
sub.head()
sub.to_csv('submission3.csv', index=False)<jupyter_output><empty_output><jupyter_text>This model scored 0.72948 on Kaggle.  Fourth Submission<jupyter_code>!pip install category_encoders
import category_encoders as ce
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
pd.set_option('max_columns', 100)
train = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_features.csv')
test = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/test_features.csv')  # Unlabeled, for Kaggle submission
labels = pd.read_csv('https://raw.githubusercontent.com/valogonor/DS1-Predictive-Modeling-Challenge/master/train_labels.csv')
train = train.join(labels.status_group, how='inner')
train = train.drop('id', axis=1)
train.head()
train.isna().sum()
train_no_nulls = train.dropna(axis=1)
train_no_nulls.head()
cols = ['recorded_by', 'lga', 'ward', 'wpt_name']
train = train_no_nulls.drop(columns=cols)
train.head()
train['date_recorded'].dtype
train.date_recorded = pd.to_datetime(train.date_recorded)
train['date_recorded'].dtype
train['year_recorded'] = pd.DatetimeIndex(train.date_recorded).year
train.head()
train.construction_year.value_counts()
train.describe()
import numpy as np
train.construction_year = train.construction_year.replace(0, np.nan)
train.describe()
train.num_private.value_counts()<jupyter_output><empty_output><jupyter_text>Since almost all of num_private is zero, I'm going to drop this column.<jupyter_code>train = train.drop('num_private', axis=1)
train.describe()
train.year_recorded.value_counts()
train['age'] = train.year_recorded - train.construction_year
train.describe()
train.loc[train['age'] < 0]<jupyter_output><empty_output><jupyter_text>Some of the ages are negative. Probably something going on there, but since there are only a few such observations, I'm just going to leave it as is.<jupyter_code>train.construction_year = train.construction_year.replace(np.nan, 2000.0)
train.age = train.year_recorded - train.construction_year
train.describe()
train.loc[train['age'] < 0]<jupyter_output><empty_output><jupyter_text>Even after filling nans in construction year with the median year (2000), it's still just the same 9 observations that have a negative age.<jupyter_code># Now to add columns to the test set to match the training set.
test.date_recorded = pd.to_datetime(test.date_recorded)
test['year_recorded'] = pd.DatetimeIndex(test.date_recorded).year
test['age'] = test.year_recorded - test.construction_year
test.head()
# Splitting the training set so we can validate the model
X = train.drop(['status_group', 'date_recorded'], axis=1) #Dropping date_recorded as it has been replaced with year_recorded
y = train.status_group
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
# Creating, fitting, nd testing the model
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score

pipeline = make_pipeline(
    ce.OneHotEncoder(use_cat_names=True), 
    StandardScaler(), 
    LogisticRegression(solver='liblinear', multi_class='ovr')
)

pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
pipeline.score(X, y)
accuracy_score(y_test, y_pred)
# Now to drop columns from the test set to match the training set.
test_no_nulls = test.dropna(axis=1)
cols = ['recorded_by', 'lga', 'ward', 'wpt_name', 'date_recorded', 'num_private']
test = test_no_nulls.drop(columns=cols)
test.columns
test['status_group'] = pipeline.predict(test.drop(columns='id'))
test.head()
sub = test[['id', 'status_group']]
sub.head()
sub.to_csv('submission4.csv', index=False)<jupyter_output><empty_output><jupyter_text>Despite the slight improvement in my own testing, the model did worse than the previous one on Kaggle, scoring 0.62529. Let's see what might be going on in the test set.<jupyter_code>test.describe()<jupyter_output><empty_output><jupyter_text>There are negative values for age in test, too.<jupyter_code>test.loc[test['age'] < 0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12387769958747877, 'fraction_numerical': 0.008857073525843242, 'mean_word_length': 7.334681496461072, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 33, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '190437', 'license': 'MIT', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2802, 'n_words': 568, 'path': 'DS1_Predictive_Modeling_Challenge.ipynb', 'repo_name': 'valogonor/DS1-Predictive-Modeling-Challenge', 'size': '8242'}