<jupyter_start><jupyter_text><NAME>  9721973137 Decision tree on life expectancy Dataset  Import essential libraries<jupyter_code>import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Read CSV file with read_csv function of pandas<jupyter_code>df = pd.read_csv('./Life Expectancy Data.csv')<jupyter_output><empty_output><jupyter_text>show Some of our data<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Show some important variables of dataset<jupyter_code>print("Min: ",df['Life expectancy '].min())
print("Max: ",df['Life expectancy '].max())
mean = df['Life expectancy '].mean()
median = df['Life expectancy '].median()
print("Mean: ", mean)
print("Median: ", median)
df.describe()<jupyter_output><empty_output><jupyter_text>Remove rows with missed values because of the importance of all features I can't set 0 for missed values so the best way is deleting them<jupyter_code>df.dropna(how='any', inplace=True)<jupyter_output><empty_output><jupyter_text>Country name is not important because we have other features that reperesent each countryI change the Developing to 1 and Develped to 0 for removing Strings<jupyter_code>df.drop(columns='Country', inplace=True)
df.Status = df.Status.map({'Developing': 1, 'Developed': 0})<jupyter_output><empty_output><jupyter_text>Adding a new categorical feature for sake of classification because my dataset type is regression<jupyter_code>df['Life expectancy status'] = pd.cut(df['Life expectancy '], 
                               bins=[df['Life expectancy '].min(), median, df['Life expectancy '].max()], 
                               labels=['Bad', 'Good'])
df['Life expectancy status'] = df['Life expectancy status'].map({'Bad': -1, 'Good': 1})
df.dropna(how='any', inplace=True)
df<jupyter_output><empty_output><jupyter_text>In order to remove the impact of Life expectancy and do our classification based on our label we should remove the old feature.<jupyter_code>df.drop('Life expectancy ', axis = 1, inplace=True)<jupyter_output><empty_output><jupyter_text>splite our dataset to testset and train set<jupyter_code>from sklearn.model_selection import train_test_split
x = df.drop('Life expectancy status', axis = 1)
y = df['Life expectancy status']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)<jupyter_output><empty_output><jupyter_text>Making an object and train the model<jupyter_code>from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(x_train, y_train)
predictions = model.predict(x_test)<jupyter_output><empty_output><jupyter_text>evaluating the score with test set<jupyter_code>model.score(x_test,y_test)<jupyter_output><empty_output><jupyter_text>Show the importance of each feature for model<jupyter_code>model.feature_importances_<jupyter_output><empty_output><jupyter_text>draw the confusion matrix<jupyter_code>from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
print(classification_report(y_test, predictions))
print(confusion_matrix(y_test, predictions))<jupyter_output>precision    recall  f1-score   support

          -1       0.90      0.91      0.91       268
           1       0.90      0.88      0.89       227

    accuracy                           0.90       495
   macro avg       0.90      0.90      0.90       495
weighted avg       0.90      0.90      0.90       495

[[245  23]
 [ 27 200]]<jupyter_text>draw the graph of DT<jupyter_code>feature_names = x.columns
import graphviz
from sklearn import tree
# DOT data
dot_data = tree.export_graphviz(model, out_file=None, 
                                feature_names=feature_names,
                                class_names='Life expectancy status',
                                filled=True)

# Draw graph
graph = graphviz.Source(dot_data, format="png") 
graph<jupyter_output><empty_output><jupyter_text>Save the graph as a png<jupyter_code>graph.render("decision_tree_graphivz")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10096035459246491, 'fraction_numerical': 0.02166953952228515, 'mean_word_length': 4.633841886269071, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '95421', 'license': 'MIT', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1263, 'n_words': 320, 'path': 'DecisionTree.ipynb', 'repo_name': 'AshkanGanj/Life-Expectancy-Analysis', 'size': '4066'}