<jupyter_start><jupyter_text>> 1. **Necesity**: Build Model> 2. **Google**: How do you search for the solution?> 3. **Solution**: Find the `function()` that makes it happen  Code Thinking> Which function computes the Model?> - `fit()`>> How could can you **import the function in Python**?<jupyter_code>fit()
model.fit()<jupyter_output><empty_output><jupyter_text>`model = ?`<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=3)
model.fit()<jupyter_output><empty_output><jupyter_text>Separate Variables for the Model> Regarding their role:> 1. **Target Variable `y`**>> - [ ] What would you like **to predict**?>> Total number of accients? Or Alcohol?>> 2. **Explanatory Variable `X`**>> - [ ] Which variable will you use **to explain** the target?<jupyter_code>df.drop(labels="survived")
df.drop(labels="survived", axis=1)
df.drop(columns='survived', labels='sex')
explanatory
import pandas as pd
df
a = pd.DataFrame({'raza': ['blanco', 'blanco', 'negro', 'asiatico']})
pd.get_dummies(a)
a
pd.get_dummies(a, drop_first=True)
df.isna().sum()
mask = df.isna().sum(axis=1) > 0
df[mask]
df
df = df.dropna()
df
df = pd.get_dummies(df, drop_first=True)
df
explanatory = df.drop(columns='survived')
explanatory
target = df.survived
target<jupyter_output><empty_output><jupyter_text>Finally `fit()` the Model<jupyter_code>model.fit(X=explanatory, y=target)<jupyter_output><empty_output><jupyter_text>Calculate a Prediction with the Model > - `model.predict_proba()`<jupyter_code>juan = df.sample(random_state=42)
juan
juan_pred = juan.drop(columns='survived')
juan_pred
model.predict(X=juan_pred)
model.predict_proba(X=juan_pred)<jupyter_output><empty_output><jupyter_text>Model Visualization > - `tree.plot_tree()`<jupyter_code>tree?
from sklearn import tree
model
type(model)
import matplotlib.pyplot as plt
explanatory
explanatory.columns
plt.figure(figsize=(20,10))
tree.plot_tree(decision_tree=model, feature_names=explanatory.columns, filled=True);<jupyter_output><empty_output><jupyter_text>Model Interpretation > Why `sex` is the most important column? What has to do with **EDA** (Exploratory Data Analysis)?<jupyter_code>330
39/330
juan
juan_pred = juan.drop(columns='survived')
juan_pred
model.predict(X=juan_pred)
model.predict_proba(X=juan_pred)<jupyter_output><empty_output><jupyter_text>Prediction vs Reality > How good is our model?  Precision > - `model.score()`<jupyter_code>model.score(X=explanatory, y=target)
dfsel = df[['survived']].copy()
pred = model.predict(X=explanatory)
dfsel['pred'] = pred
dfsel
dfsel.pred == dfsel.survived
(dfsel.pred == dfsel.survived).sum()
(dfsel.pred == dfsel.survived).sum()/714
(dfsel.pred == dfsel.survived).mean()<jupyter_output><empty_output><jupyter_text>Confusion Matrix > 1. **Sensitivity** (correct prediction on positive value, $y=1$)> 2. **Specificity** (correct prediction on negative value $y=0$).> - `plot_confusion_matrix()`<jupyter_code>from sklearn.metrics import plot_confusion_matrix<jupyter_output><empty_output><jupyter_text>- imagina que tenemos 100 personas    - 90 no tienen covid    - 10 tienen covid- el modelo    - predice todas las personas con no covid 100...    - metricas    - precision: 90%        - la gente que tiene covid: 0%  → sensibilidad        - la gente que no tiene covid: 100% → especificidad<jupyter_code>plot_confusion_matrix(estimator=model, X=explanatory, y_true=target);
sensibilidad = 205/(85 + 205)
sensibilidad
especificidad = 372/(372 + 52)
especificidad
from sklearn.metrics import classification_report
classification_report(y_true=dfsel.survived, y_pred=dfsel.pred)
a = classification_report(y_true=dfsel.survived, y_pred=dfsel.pred)
print(a)<jupyter_output>precision    recall  f1-score   support

           0       0.81      0.88      0.84       424
           1       0.80      0.71      0.75       290

    accuracy                           0.81       714
   macro avg       0.81      0.79      0.80       714
weighted avg       0.81      0.81      0.81       714<jupyter_text>ROC Curve > A way to summarise all the metrics (score, sensitivity & specificity)<jupyter_code>from sklearn.metrics import plot_roc_curve
plot_roc_curve(estimator=model, X=explanatory, y=target)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(X=explanatory, y=target)
plot_roc_curve(estimator=lr, X=explanatory, y=target)
from sklearn.ensemble import RandomForestClassifier
lr = RandomForestClassifier()
lr.fit(X=explanatory, y=target)
plot_roc_curve(estimator=lr, X=explanatory, y=target)<jupyter_output>/Users/sota/miniforge3/lib/python3.9/site-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function plot_roc_curve is deprecated; Function `plot_roc_curve` is deprecated in 1.0 and will be removed in 1.2. Use one of the class methods: RocCurveDisplay.from_predictions or RocCurveDisplay.from_estimator.
  warnings.warn(msg, category=FutureWarning)
{'fraction_non_alphanumeric': 0.12469586374695864, 'fraction_numerical': 0.025952960259529603, 'mean_word_length': 5.577333333333334, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '495155', 'license': 'MIT', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1724, 'n_words': 385, 'path': 'II Machine Learning & Deep Learning/#06. Decision Tree. A Supervised Classification Model/06session.ipynb', 'repo_name': 'frands97/machine-learning-program', 'size': '4932'}