def confusion_matrix(y_pred, y_real, DT_predict_prob): y_real = y_real.astype(int) array_different_letters = [] array_different_letters.append(y_pred[0]) for i in range(1, len(y_pred)): if y_pred[i] not in array_different_letters: array_different_letters.append(int(y_pred[i])) for i in range(len(y_real)): if y_real[i] not in array_different_letters: array_different_letters.append(int(y_real[i])) c_m_len = np.max(array_different_letters) + 1 confusion_mat = np.zeros((c_m_len, c_m_len)) max_predict_prob = [] for i in range(len(y_real)): if max(DT_predict_prob[i]) > 0.6: confusion_mat[y_real[i]][y_pred[i]] += 1 counter_right = 0 counter_all = 0 for i in range(c_m_len): for j in range(c_m_len): if i != j: counter_all += confusion_mat[i][j] else: counter_right += confusion_mat[i][j] counter_all += confusion_mat[i][j] accuracy = counter_right / counter_all print("accuracy after:" + str(accuracy)) heatmap = plt.axes() heatmap = sn.heatmap(confusion_mat, annot=True, fmt='g', cmap="Blues") heatmap.set_title('confusion_matrix') plt.savefig("final_result.png")
def threshold_search(true, prob, criteria): true = true.to_numpy() prob_train, prob_test, true_train, true_test = train_test_split(prob, true, test_size=0.2, random_state=1234) thresholds = np.linspace(0, 1, 101) all_f1_train = np.zeros(len(thresholds)) for j in range(len(thresholds)): predictions_train = np.ones(len(prob_train)) predictions_train[prob_train < thresholds[j]] = 0 macro_f1_train = macro_weighted_f1(true_train, predictions_train, [0, 1]) all_f1_train[j] = macro_f1_train best_threshold = thresholds[np.where(max(all_f1_train) == all_f1_train)] best_threshold = best_threshold[0] predictions_test = np.ones(len(prob_test)) predictions_test[prob_test < best_threshold] = 0 print("The best threshold for this prediction is: %s" % best_threshold) plt.plot(thresholds, all_f1_train, 'b') plt.axvline(x=0.5, linestyle=':', color='r') plt.axvline(x=best_threshold, linestyle='--', color='g') plt.axhline(y=all_f1_train[np.where(thresholds == 0.5)], linestyle=':', color='r') plt.axhline(y=max(all_f1_train), linestyle='--', color='g') plt.xlabel("Threshold") plt.ylabel("Macro F1") plt.savefig('{0} threshold plot.png'.format(criteria), bbox_inches='tight') plt.clf() return best_threshold
def create_subgraph_of_node_with_value_different_to_zero_in_vector_a( a, array_of_index, init_graph, superg_graph): a, array_of_index = map( list, zip(*sorted(zip(a, array_of_index), key=lambda x: x[0]))) c = 0 for i in a: if i > 0: c += 1 list_node = array_of_index[-c::][::-1] subg = init_graph.subgraph(list_node) edges_color_subg = np.repeat('b', len(subg.edges)) plt.clf() nx.draw_circular(subg, with_labels=True, edge_color=edges_color_subg) plt.savefig( "subgraph_init.png" ) # sottografo del grafo iniziale contenete i nodi con a[i] > 0 subg_of_superg = superg_graph.subgraph(list_node) edges_color_subg_of_superg = np.repeat('r', len(subg_of_superg.edges)) for i, edge in enumerate(subg_of_superg.edges): if edge in subg.edges: edges_color_subg_of_superg[i] = 'b' plt.clf() nx.draw_circular(subg_of_superg, with_labels=True, edge_color=edges_color_subg_of_superg) plt.savefig("subgraph_supergraph.png")
def visualization(self): feature_1 = self.x_pca[:, 0] feature_2 = self.x_pca[:, 1] labels = self.y cdict = {0: 'red', 1: 'green'} labl = {0: 'without bug', 1: 'bug'} marker = {0: '*', 1: 'o'} alpha = {0: .3, 1: .5} fig, ax = plt.subplots(figsize=(7, 5)) fig.patch.set_facecolor('white') for l in np.unique(labels): ix = np.where(labels == l) ax.scatter(feature_1[ix], feature_2[ix], c=cdict[l], s=100, label=labl[l], marker=marker[l], alpha=alpha[l]) # for loop ends plt.xlabel("First Principal Component", fontsize=14) plt.ylabel("Second Principal Component", fontsize=14) plt.legend() plt.savefig( str(pathlib.Path().absolute()) + "/File/PCA_visualization.png")
def create_plot(logbook, name_file): maxFitnessValues, meanFitnessValues, minFitnessValues, medianFitnessValues, stdFitnessValues = \ logbook.select("max", "avg", "min", "median", "std") plt.plot(maxFitnessValues, color='red', label="Worst Fitness") plt.plot(meanFitnessValues, color='green', label="Mean Fitness") plt.plot(minFitnessValues, color='orange', label="Best Fitness") plt.plot(medianFitnessValues, color='blue', label="Avg. Fitness") plt.plot(stdFitnessValues, color='pink', label="Std. Fitness") plt.xlabel('Generation') plt.ylabel('Max / Average / Min / Median/ Std Fitness') plt.title('Max, Average, Min, Median and Std Fitness over Generations') plt.legend(loc='lower right') plt.savefig(name_file) plt.close()
def confusion_matrix(y_pred, y_real, DT_predict_prob): array_different_letters = [] array_different_letters.append(y_pred[0]) for i in range(1, len(y_pred)): if y_pred[i] not in array_different_letters: array_different_letters.append(y_pred[i]) for i in range(len(y_real)): if y_real[i] not in array_different_letters: array_different_letters.append(y_real[i]) c_m_len = np.max(array_different_letters) + 1 confusion_mat = np.zeros((c_m_len, c_m_len)) max_predict_prob = [] for i in range(len(y_real)): if max(DT_predict_prob[i]) > 0.6: confusion_mat[y_real[i]][y_pred[i]] += 1 heatmap = plt.axes() heatmap = sn.heatmap(confusion_mat, annot=True, fmt='g', cmap="Blues") heatmap.set_title('confusion_matrix') plt.savefig("final_result.png")
for line in content: value = line.split(" ") if len(value): supergraph_cc.append(float(value.pop())) original_cc.append(float(value.pop())) plt.clf() plt.plot(k_array, original_cc, 'r--', k_array, supergraph_cc, 'g-') plt.ylabel("Clustering Coefficent") plt.xlabel("k_degree") plt.legend(('Original Graph', 'Supergraph'), loc='lower center', shadow=True) plt.title(str(sys.argv[4])) plt.savefig("metric_cc_web.png") #if choose dataset web #plt.savefig("metric_cc_socfb.png") plt.clf() list_norm = [] for i in norm: list_norm.append(float(i)) list_k_array = [] for i in k_array: list_k_array.append(float(i)) plt.plot(list_k_array, list_norm, 'r--') plt.ylabel("Norm of vector a") plt.xlabel("k_degree") plt.title(str(sys.argv[4])) plt.savefig("metric_norm_web.png") #if choose dataset web #plt.savefig("metric_norm_socfb.png") #otherwise
def saveDendrogram(model): sns.clustermap(model, figsize=(50, 50)) plt.savefig('out/hierarchical_clustered_heatmap.png', dpi=300)
scaled_X[y_ac == 0]['W1'], c='lightblue', marker='o', s=40, label='cluster 1') ax2.scatter(scaled_X[y_ac == 1]['W0'], scaled_X[y_ac == 1]['W1'], c='red', marker='s', s=40, label='cluster 2') ax2.set_title('Agglomerative clustering') ax2.legend() db = DBSCAN(eps=0.2, min_samples=5, metric='euclidean') y_db = db.fit_predict(scaled_X) ax3.scatter(scaled_X[y_db == 0]['W0'], scaled_X[y_db == 0]['W1'], c='lightblue', marker='o', s=40, label='cluster 1') ax3.scatter(scaled_X[y_db == 1]['W0'], scaled_X[y_db == 1]['W1'], c='red', marker='s', s=40, label='cluster 2') ax3.set_title('DBSCAN clustering') ax3.legend() plt.savefig('out/analyse.png', dpi=300)
from src.recurrent_attention_network_paper.StanfordDogLoader import StanfordDogLoader if __name__ == '__main__': # trainset=CUB200_loader('external/CUB_200_2011',split='train') trainset = StanfordDogLoader('external/StanfordDog', split='train') trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, collate_fn=trainset.CUB_collate, num_workers=4) first,labels= next(iter(trainloader)) print(first[0].mean()) print(first[0].std()) firstImage= trainset.tensor_to_img(first[0]) plt.imshow(firstImage) plt.savefig('test.jpg') # num = len(trainset) # # imageNum=10 # # total_sum = 0 # dataIter= iter(trainloader) # for i in range(0,imageNum): # curr = next(dataIter) # total_sum += curr[0].mean() # # mean = total_sum / imageNum # print(mean)
if size == 1: for line in content: k_array = line.split(" ") file_graph = str(sys.argv[2]) if os.path.exists(file_graph): # if file exist with open(file_graph) as f: content = f.readlines() # read each line content = [x.strip() for x in content] ratio = NULL size = len(content) if size == 1: for line in content: ratio = line.split(" ") list_ratio = [] for i in ratio: list_ratio.append(float(i)) list_k_array = [] for i in k_array: list_k_array.append(float(i)) plt.clf() plt.figure(figsize=(16, 10)) plt.ylabel("Ratio") plt.xlabel("k_degree") plt.plot(list_k_array, list_ratio, 'r--') plt.title("Graph friend 1000 10 100") plt.savefig("ratio_fakedataset.png", dpi=120)
epochs = 5 #训练5次 model1.summary() #模型输出 model1.compile( loss='sparse_categorical_crossentropy', #模型编译 optimizer='adam', metrics=['accuracy']) #从训练集中抽取0.2进行验证 history = model1.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2) #-----------------------------------------------保存模型,可视化-------------------------- #保存模型 model1.save('model_CNN_text.h5') #模型可视化 plot_model(model1, to_file='model_CNN_text.png', show_shape=True) #加载模型 model = load_model('model_CNN_text.h5') y_new = model.predict(x_train[0].reshape(1, 50)) #训练结果可视化 plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend(['Train', 'Valid'], loc='upper left') plt.savefig('Valid_acc.png') plt.show()
for i in range(1, len(final_index) - 1): ind = final_index[i] + 1 for j in range(final_index[i] + 1, final_index[i + 1] + 1): final_value[j] = array_degrees[ind] # final_value[k_degree - 1] = final_value[0] return final_value if __name__ == "__main__": k_degree = int(sys.argv[1]) l = 50 """---------------------------------""" G = nx.karate_club_graph() # this graph is inside the library 'networkx' edges_color_G = np.repeat('b', len(G.edges)) nx.draw_circular(G, with_labels=True, edge_color=edges_color_G) plt.savefig("karate.png") d = [x[1] for x in G.degree()] array_index = np.argsort(d)[::-1] array_degree = np.sort(d)[::-1] """---------------------------------""" # compute the greedy alghoritm for k-degree vertex_degree_dp = dp_graph_anonymization(array_degree.copy(), k_degree) """---------------------------------""" # compute the construct graph graph_degree = construct_graph(array_index.copy(), vertex_degree_dp.copy()) if graph_degree is not None: edges_color_construct = np.repeat('b', len(graph_degree.edges)) plt.clf() nx.draw_circular(graph_degree, with_labels=True,
workers=10) #------------------------------------------------------保存模型--------------------------------------- model.summary() #判断路径是否存在,不存在创建 if not os.path.isdir(save_dir): os.makedirs(save_dir) model_path = os.path.join(save_dir, model_name) model.save(model_path) #保存模型 #------------------------------------------------------训练过程可视化----------------------------------- #绘制训练与验证的准确率值 plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Valid'], loc='upper left') plt.savefig('tradition_cnn_valid_acc.png') plt.show() #绘制训练与验证的损失 plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Valid'], loc='upper left') plt.savefig('tradition_cnn_valid_loss.png') plt.show()