<jupyter_start><jupyter_text>02 | Decision Tree. A Supervised Classification Model - Subscribe to my [Blog ↗](https://blog.pythonassembly.com/)- Let's keep in touch on [LinkedIn ↗](www.linkedin.com/in/jsulopz) 😄  Discipline to Search Solutions in Google > Apply the following steps when **looking for solutions in Google**:>> 1. **Necesity**: How to load an Excel in Python?> 2. **Search in Google**: by keywords>   - `load excel python`>   - ~~how to load excel in python~~> 3. **Solution**: What's the `function()` that loads an Excel in Python?>   - A Function to Programming is what the Atom to Phisics.>   - Every time you want to do something in programming>   - **You will need a `function()`** to make it>   - Theferore, you must **detect parenthesis `()`**>   - Out of all the words that you see in a website>   - Because they indicate the presence of a `function()`.  Load the Data > Load the Titanic dataset with the below commands> - This dataset **people** (rows) aboard the Titanic> - And their **sociological characteristics** (columns)> - The aim of this dataset is to predict the probability to `survive`> - Based on the social demographic characteristics.<jupyter_code>import seaborn as sns

df = sns.load_dataset(name='titanic').iloc[:, :4]
df.head()<jupyter_output><empty_output><jupyter_text>`DecisionTreeClassifier()` Model in Python  Build the Model > 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()
model.__dict__
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**?>> 2. **Explanatory Variable `X`**>> - [ ] Which variable will you use **to explain** the target?<jupyter_code>explanatory = df.drop(columns='survived')
target = df.survived<jupyter_output><empty_output><jupyter_text>Finally `fit()` the Model<jupyter_code>model.__dict__
model.fit(X=explanatory, y=target)
import pandas as pd
pd.get_dummies(data=df)
df = pd.get_dummies(data=df, drop_first=True)
df
explanatory = df.drop(columns='survived')
target = df.survived
model.fit(X=explanatory, y=target)
df
df.isna().sum()
df.fillna('hola')
df.dropna(inplace=True) # df = df.dropna()
df
df.dropna(inplace=True) # df = df.dropna()
df
explanatory = df.drop(columns='survived')
target = df.survived
model.fit(X=explanatory, y=target)<jupyter_output><empty_output><jupyter_text>Calculate a Prediction with the Model > - `model.predict_proba()`<jupyter_code>model.predict_proba()<jupyter_output><empty_output><jupyter_text>Model Visualization > - `tree.plot_tree()`  Model Interpretation > Why `sex` is the most important column? What has to do with **EDA** (Exploratory Data Analysis)?<jupyter_code>%%HTML

<iframe width="560" height="315" src="https://www.youtube.com/embed/7VeUPuFGJHk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><jupyter_output><empty_output><jupyter_text>Prediction vs Reality > How good is our model?<jupyter_code>dfsel = df[['survived']].copy()
dfsel['pred'] = model.predict(X=explanatory)
dfsel.sample(10)
comp = dfsel.survived == dfsel.pred
comp.sum()
comp.sum()/714
comp.mean()<jupyter_output><empty_output><jupyter_text>Precision > - `model.score()`<jupyter_code>model.score(X=explanatory, y=target)<jupyter_output><empty_output>