<jupyter_start><jupyter_text>Improving performance<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
# Load the data
df = pd.read_csv('../data/new_titanic_features.csv')
# Create Features and Labels
X = df[['Male', 'Family',
        'Pclass2_one', 'Pclass2_two', 'Pclass2_three',
        'Embarked_C', 'Embarked_Q', 'Embarked_S',
        'Age2', 'Fare3_Fare11to50', 'Fare3_Fare51+', 'Fare3_Fare<=10']]
y = df['Survived']

X.describe()
from sklearn.model_selection import train_test_split
# Train test split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=.2, random_state=0)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
pred_train = model.predict(X_train)
pred_test = model.predict(X_test)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
print('Train Accuracy: {:0.3}'.format(accuracy_score(y_train, pred_train)))
print('Test Accuracy: {:0.3}'.format(accuracy_score(y_test, pred_test)))
confusion_matrix(y_test, pred_test)
print(classification_report(y_test, pred_test))<jupyter_output><empty_output><jupyter_text>Feature importances (wrong! see exercise 1)<jupyter_code>coeffs = pd.Series(model.coef_.ravel(), index=X.columns)
coeffs
coeffs.plot(kind='barh')<jupyter_output><empty_output><jupyter_text>Cross Validation<jupyter_code>from sklearn.model_selection import cross_val_score, ShuffleSplit
cv = ShuffleSplit(n_splits=5, test_size=.4, random_state=0)
scores = cross_val_score(model, X, y, cv=cv)
scores
'Crossval score: %0.3f +/- %0.3f ' % (scores.mean(), scores.std())<jupyter_output><empty_output><jupyter_text>Learning curve<jupyter_code>from sklearn.model_selection import learning_curve
tsz = np.linspace(0.1, 1, 10)
train_sizes, train_scores, test_scores = learning_curve(model, X, y, train_sizes=tsz)
fig = plt.figure()
plt.plot(train_sizes, train_scores.mean(axis=1), 'ro-', label="Train Scores")
plt.plot(train_sizes, test_scores.mean(axis=1), 'go-', label="Test Scores")
plt.title('Learning Curve: Logistic Regression')
plt.ylim((0.5, 1.0))
plt.legend()
plt.draw()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13857337573830078, 'fraction_numerical': 0.01817355747387551, 'mean_word_length': 7.843373493975903, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '44890', 'license': 'MIT', 'n_tokens_mistral': 876, 'n_tokens_neox': 826, 'n_words': 152, 'path': 'day_2/Lab_08_ML Improving performance.ipynb', 'repo_name': 'differentmatt/ztdl-5-day-bootcamp', 'size': '2201'}