<jupyter_start><jupyter_text>Step 1: Data Wrangling<jupyter_code>import pandas as pd 
import numpy as np
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn import metrics 
clf = DecisionTreeClassifier()
###show all columns
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
df_ESG=pd.read_csv('https://raw.githubusercontent.com/sunshineluyao/Econ-211/main/ESG_DecisionTree/MSCI_ESG_Rating.csv')
df_ESG= df_ESG[df_ESG.Month==1]
df_ESG.head()
df_YearlyReturn=pd.read_csv('https://raw.githubusercontent.com/sunshineluyao/Econ-211/main/ESG_DecisionTree/S%26P1500_2020_Yearly_Return.csv')
df_YearlyReturn=df_YearlyReturn.dropna()
df_YearlyReturn.head()
df_YearlyReturn['Return']=pd.qcut(df_YearlyReturn.Yearly_Return,3,labels=np.arange(1,4,1) )
df_YearlyReturn.head()
df_YearlyReturn['Rank']=df_YearlyReturn['Return'].apply(lambda x: 'High' if x==3 else 'Average' if x==2 else 'Low')
df = df_ESG.merge(df_YearlyReturn, left_on='Ticker',right_on='Symbol')
df.head()<jupyter_output><empty_output><jupyter_text>Build Decision Tree<jupyter_code>feature_names=['ESG_Score']
features=df[feature_names]
targets=df['Rank']
targets_names = targets.unique()
from sklearn.model_selection import train_test_split
train_features, test_features, train_targets, test_targets = train_test_split(features, targets, test_size=0.2, random_state=42)
from sklearn import metrics
from sklearn.metrics import accuracy_score
#####Optimizing Decision Tree Performance
# Create Decision Tree classifer object
clf = DecisionTreeClassifier(criterion='entropy', max_depth=2,random_state=0)

# Train Decision Tree Classifer
clf = clf.fit(train_features,train_targets)

#Predict the response for test dataset
y_pred = clf.predict(test_features)

# Compute test set accuracy  
acc = accuracy_score(y_pred, test_targets)
print("Test set accuracy: {:.2f}".format(acc))
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO  
from IPython.display import Image  
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True,feature_names = feature_names,class_names=clf.classes_)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('ESG.png')
Image(graph.create_png())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10992177850967476, 'fraction_numerical': 0.012762453684643887, 'mean_word_length': 6.993421052631579, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '523869', 'license': 'MIT', 'n_tokens_mistral': 888, 'n_tokens_neox': 855, 'n_words': 145, 'path': 'ESG_DecisionTree/ESG_DecisionTree.ipynb', 'repo_name': 'sunshineluyao/Econ-211', 'size': '2429'}