<jupyter_start><jupyter_text>Lambda School Data Science*Unit 2, Sprint 1, Module 4*---<jupyter_code>%%capture
import sys

# If you're on Colab:
if 'google.colab' in sys.modules:
    DATA_PATH = 'https://raw.githubusercontent.com/LambdaSchool/DS-Unit-2-Linear-Models/master/data/'
    !pip install category_encoders==2.*

# If you're working locally:
else:
    DATA_PATH = '../data/'

# Libraries for today's project
from category_encoders import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Part 1: Sprint Challenge Practice- Can I import a CSV file into a DataFrame?- Can I create a scatter plot?- Can I split a DataFrame into a target vector and feature matrix?- Can I split a dataset into a training set and a test set?- Can I establish the baseline mean absolute error for a regression problem?- Can I combine transformers with a predictor using a pipeline?- Can I build a linear regresion model and a ridge regression model?- Can I evaluate a model using common metrics like mean absolute error, root mean squared error, and R^2?- If given a feature matrix, can I use my model to create a list of predictions?- Can I create a horizontal bar chart with the coefficients from a linear model?  Part 2: Pipelines and PlottingLet's revise [our work from yesterday](https://tinyurl.com/yb9cz6n5).  Part 3: Logistic Regression  I. Wrangle Data<jupyter_code>def wrangle(filepath):
  df = pd.read_csv(filepath, index_col='PassengerId', dtype={'Pclass': 'object'})
  # drop high cardinality categorical columns 
  df.drop(columns=['Name', 'Ticket', 'Cabin'], inplace=True)
  return df

df = wrangle(DATA_PATH + 'titanic/train.csv')
X_test = wrangle(DATA_PATH + 'titanic/test.csv')
df.head()
df.shape
df.info()
df['Survived'].head()
X_test.head()
X_test.shape<jupyter_output><empty_output><jupyter_text>II. Split Data<jupyter_code># split target vector from feature matrix
target = 'Survived'
y = df[target]
X = df.drop(columns=target)
X.head()
y.head()<jupyter_output><empty_output><jupyter_text>Since there's no time series component, we should do a randomized train-validation split.<jupyter_code># split our data into a TRAINING set and a VALIDATION set 
# test_size = validation set size (want to be 20%)
# random_state can be any integer to see the randomness so that it can be reproducable 
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
len(X_train) + len(X_val) == len(X)<jupyter_output><empty_output><jupyter_text>III. Establish our baseline- This is a **classification problem**, so we look at the **majority class** to calculate baseline **accuracy score**.<jupyter_code>y_train.value_counts(normalize=True)


# between 0 and 1, 1 is best and 0 is worst 
print('Baseline Accuracy Score:', y_train.value_counts(normalize=True).max())<jupyter_output>Baseline Accuracy Score: 0.6235955056179775<jupyter_text>IV. Build Model- Why would regular linear regression not work here?<jupyter_code>plt.scatter(X_train['Age'], y_train)
plt.xlabel('Age')
plt.ylabel('Survived');

# linear regression would not work because it would try to get between the two 
# lines which would always be around 0.4-0.6 and since survived is binary it 
# wouldn't be useful to determine if they survived or not<jupyter_output><empty_output><jupyter_text>What we need for our model pipeline:- `OneHotEncoder`- `SimpleImputer`- `LogisticRegression`<jupyter_code>X_train.info()
X_train.head()
model = make_pipeline(
    OneHotEncoder(use_cat_names=True), # encode categorical features
    SimpleImputer(), # fill NaN values with column mean
    StandardScaler(), # rescales all features (mean = 0, std = 1)
    LogisticRegression()
)

model.fit(X_train, y_train)<jupyter_output>/usr/local/lib/python3.7/dist-packages/category_encoders/utils.py:21: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  elif pd.api.types.is_categorical(cols):<jupyter_text>V. Check Metrics<jupyter_code>print('Training Accuracy:', model.score(X_train, y_train))
print('Validation Accuracy:', model.score(X_val, y_val))<jupyter_output>Training Accuracy: 0.8075842696629213
Validation Accuracy: 0.7988826815642458<jupyter_text>VI. Communicate Results<jupyter_code># model is a pipeline and need to go into pipline and get the logistic regressor
model.named_steps

coefficients = model['logisticregression'].coef_[0]
features = model['onehotencoder'].get_feature_names()

print(coefficients)
print(features)
feature_importance = pd.Series(coefficients, index=features).sort_values(key=abs)
feature_importance.plot(kind='barh');
# shows that being female, in first class are the top two reasons for surviving 
# as your age goes up your probability for surviving goes down
# predict the survivability that you would have to submit somewhere to check accuracy
model.predict(X_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09435421500386698, 'fraction_numerical': 0.016241299303944315, 'mean_word_length': 5.9905405405405405, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 2, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '561516', 'license': 'MIT', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1621, 'n_words': 532, 'path': 'module4-logistic-regression/C_214_guided_project_notes.ipynb', 'repo_name': 'jduell12/DS-Unit-2-Linear-Models', 'size': '5172'}