def str_img(txt, datadir_path): img = Image.new('RGB', (900, 900), color='white') if type(txt) == 'dict': txt = json.dumps(txt) d = ImageDraw.Draw(img) d.text((10, 10), str(txt), fill=(0, 0, 0)) output_path = os.path.join(datadir_path, 'pil_text.png') img.save(output_path) PImage(output_path) else: img = Image.new('RGB', (100, 30), color='white') d = ImageDraw.Draw(img) d.text((10, 10), str(txt), fill=(0, 0, 0)) output_path = os.path.join(datadir_path, 'pil_text.png') img.save(output_path) PImage(output_path)
def visualize(classifier, X_test): with open("tree1.dot", 'w') as f: f = tree.export_graphviz(classifier, out_file=f, impurity=False, feature_names=X_test.columns.values, class_names=['No', 'Yes'], rounded=True, filled=True) check_call(['dot', '-Tpng', 'tree1.dot', '-o', 'tree1.png']) img = Image.open('tree1.png') draw = ImageDraw.Draw(img) img.save('sample-out.png') PImage('sample-out.png')
f = tree.export_graphviz(classifier, out_file=f, impurity=False, feature_names=X_test.columns.values, class_names=['No', 'Yes'], rounded=True, filled=True) # Convert .dot to .png to allow display in web notebook check_call(['dot', '-Tpng', 'tree1.dot', '-o', 'tree1.png']) # Annotating chart with PIL img = Image.open("tree1.png") draw = ImageDraw.Draw(img) img.save('sample-out.png') PImage("sample-out.png") classifier = DecisionTreeClassifier(max_depth=3) classifier.fit(X_train, y_train) print('train score...', accuracy_score(y_train, classifier.predict(X_train))) print('test score...', accuracy_score(y_test, classifier.predict(X_test))) with open("tree1.dot", 'w') as f: f = tree.export_graphviz(classifier, out_file=f, impurity=False, feature_names=X_test.columns.values, class_names=['No', 'Yes'], rounded=True, filled=True) # Convert .dot to .png to allow display in web notebook check_call(['dot', '-Tpng', 'tree1.dot', '-o', 'tree1.png'])
# exportar el modelo a archivo .dot with open(r"tree1.dot", 'w') as f: f = tree.export_graphviz(decision_tree, out_file=f, max_depth=7, impurity=True, feature_names=list( artists_encoded.drop(['top'], axis=1)), class_names=['No', 'N1 Billboard'], rounded=True, filled=True) # Convertir el archivo .dot a png para poder visualizarlo check_call(['dot', '-Tpng', r'tree1.dot', '-o', r'tree1.png']) PImage("tree1.png") # In[ ]: acc_decision_tree = round(decision_tree.score(x_train, y_train) * 100, 2) # In[12]: #predecir artista CAMILA CABELLO featuring YOUNG THUG # con su canción Havana llego a numero 1 Billboard US en 2017 #response.json() #RECIBIR JSON CON ARRAY try: data = json.loads(sys.argv[1]) except:
with open("safe-loans.dot", 'w') as f: f = tree.export_graphviz(clf, out_file=f, max_depth=sDepth, impurity=True, feature_names=list(X_train), class_names=['not safe', 'safe'], rounded=True, filled=True) check_call(['dot', '-Tpng', 'safe-loans.dot', '-o', 'safe-loans.png']) img = Image.open("safe-loans.png") draw = ImageDraw.Draw(img) img.save('output.png') PImage("output.png") trainPredictions = clf.predict(X_train) testPredictions = clf.predict(X_test) fprTrain, tprTrain, thresholdsTrain = roc_curve(Y_train, trainPredictions) roc_auc_train = auc(fprTrain, tprTrain) print(roc_auc_train) fprTest, tprTest, thresholdsTest = roc_curve(Y_test, testPredictions) roc_auc_test = auc(fprTest, tprTest) print(roc_auc_test) pl.clf() pl.plot(fprTest, tprTrain, label='ROC curve (area = %0.2f)' % roc_auc_test) pl.plot([0, 1], [0, 1], '-k')
#https://www.kaggle.com/dmilla/introduction-to-decision-trees-titanic-dataset from IPython.display import Image as PImage from subprocess import check_call def drawTree(datree): with open("tree1.dot", 'w') as f: f = tree.export_graphviz(datree, out_file=f, rounded = True, filled= True ) #Convert .dot to .png to allow display in web notebook check_call(['dot','-Tpng','tree1.dot','-o','tree1.png']) return("tree1.png") #use the function to draw the tree PImage(drawTree(dtc)) ###some code qualifying the data # <h3>Let's optimize the pruning level of the code</h3> # In[53]: ### some optimization code using cross validation!? # Tree complexity parameter - constraint on maximum depth tc = np.arange(2, 41, 1) # K-fold crossvalidation K = 10
out_file=f, max_depth=4, impurity=False, feature_names=attrition_final.columns.values, class_names=['No', 'Yes'], rounded=True, filled=True) #Convert .dot to .png to allow display in web notebook check_call(['dot', '-Tpng', 'tree1.dot', '-o', 'tree1.png']) # Annotating chart with PIL img = Image.open("tree1.png") draw = ImageDraw.Draw(img) img.save('sample-out.png') PImage("sample-out.png", height=2000, width=1900) # Gradient Boosting Parameters gb_params = { 'n_estimators': 1500, 'max_features': 0.9, 'learning_rate': 0.25, 'max_depth': 4, 'min_samples_leaf': 2, 'subsample': 1, 'max_features': 'sqrt', 'random_state': seed, 'verbose': 0 } gb = GradientBoostingClassifier(**gb_params) # Fit the model to our SMOTEd train and target gb.fit(smote_train, smote_target)
rounded = True, filled= True )#) #Convert .dot to .png to allow display in web notebook check_call(['dot','-Tpng','tree1.dot','-o','tree1.png']) # Annotating chart with PIL img = Image.open("tree1.png") draw = ImageDraw.Draw(img) font = ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf', 26) draw.text((10, 0), # Drawing offset (position) '"Title <= Income', # Text to draw (0,0,255), # RGB desired color font=font) # ImageFont object with desired font img.save('tree_income.png') PImage("tree_income.png") ####### ###METRICS DECISION TREE y_pred=decision_tree.predict(val_X) print("Accuracy:", decision_tree.score(val_X, val_y)) print("Precision:", precision_score(val_y, y_pred)) print("Recall:", recall_score(val_y, y_pred)) print("F1:", f1_score(val_y, y_pred)) print("Area under precision Recall:", average_precision_score(val_y, y_pred)) ##############3 #####3 #######3 RANDOM FOREST##################################################################################################
rounded = True, filled= True )#) #Convert .dot to .png to allow display in web notebook check_call(['dot','-Tpng','treeisfraud.dot','-o','treeisfraud.png']) # Annotating chart with PIL img = Image.open("treeisfraud.png") draw = ImageDraw.Draw(img) font = ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf', 26) draw.text((10, 0), # Drawing offset (position) '"Title <= Is Fraud', # Text to draw (0,0,255), # RGB desired color font=font) # ImageFont object with desired font img.save('tree_isfraud.png') PImage("tree_isfraud.png") ########33 ####### KNeighborsClassifier ###################################################################################################3 ############## from sklearn.neighbors import KNeighborsClassifier neiclassifier = KNeighborsClassifier(n_neighbors=5) neiclassifier.fit(train_X, train_y) y_pred_nei = neiclassifier.predict(val_X) KNeighbors=[neiclassifier.score(val_X, val_y),precision_score(val_y, y_pred_nei),recall_score(val_y, y_pred_nei),f1_score(val_y,y_pred_nei)] results['KNeighbors']=KNeighbors