def evaluate_error(X_train, y_train, X_val, y_val, X_test, y_test): E_val = [] E_test = [] for k in [3, 4, 5, 6, 7]: #Fit transformed train data using linear regression without regularization #, using only k features of X_train w_lin = tools.linear_regression(X_train[:, :k + 1], y_train) #Predict class of validation set y_val_pred = tools.predict(X_val[:, :k + 1], w_lin) #Calculate classification error on validation set E_val.append(tools.cal_error(y_val, y_val_pred)) #Predict class of test set y_test_pred = tools.predict(X_test[:, :k + 1], w_lin) #Calculate classification error on test set E_test.append(tools.cal_error(y_test, y_test_pred)) print(E_val) print(E_test) print('Smallest error on validation set is achieved when k = {}'.format( 3 + np.argmin(E_val))) print('Smallest error on test set is {}, achieved when k = {}'.format( E_test[np.argmin(E_test)], 3 + np.argmin(E_test)))
def predict(): samples_class2, labels_class2, ID2label_class = load_prediction_data_class2() dict_class = tools.load_dict('./code/class_model/class2_weight_new.dict') # dict_sentiment_att = load_dict('./code/sentiment_weight_no_reduce_produce.dict') print(len(dict_class)) model_class1 = load_model('./code/class_model/model_class2_weight_new1.h5') model_class2 = load_model('./code/class_model/model_class2_weight_new2.h5') model_class3 = load_model('./code/class_model/model_class2_weight_new3.h5') model_class4 = load_model('./code/class_model/model_class2_weight_new4.h5') model_class5 = load_model('./code/class_model/model_class2_weight_new5.h5') # model_class6 = load_model('./code/class_model/model_class2_weight_new6.h5') # model_class7 = load_model('./code/class_model/model_class2_weight_new7.h5') # model_class8 = load_model('./code/class_model/model_class2_weight_new8.h5') # model_class9 = load_model('./code/class_model/model_class2_weight_new9.h5') # model_class10 = load_model('./code/class_model/model_class2_weight_new10.h5') worng_class_list = ["ori\tp1\tp2\tp3\tp4\tp5\tp"] count1 = 0 count2 = 0 class_label = [] for i in range(len(samples_class2)): predict_label_class2_1 = tools.predict(samples_class2[i], dict_class, model_class1, ID2label_class) predict_label_class2_2 = tools.predict(samples_class2[i], dict_class, model_class2, ID2label_class) predict_label_class2_3 = tools.predict(samples_class2[i], dict_class, model_class3, ID2label_class) predict_label_class2_4 = tools.predict(samples_class2[i], dict_class, model_class4, ID2label_class) predict_label_class2_5 = tools.predict(samples_class2[i], dict_class, model_class5, ID2label_class) # predict_label_class2_6 = tools.predict(samples_class2[i], dict_class, model_class1, ID2label_class) # predict_label_class2_7 = tools.predict(samples_class2[i], dict_class, model_class2, ID2label_class) # predict_label_class2_8 = tools.predict(samples_class2[i], dict_class, model_class3, ID2label_class) # predict_label_class2_9 = tools.predict(samples_class2[i], dict_class, model_class4, ID2label_class) # predict_label_class2_10 = tools.predict(samples_class2[i], dict_class, model_class5, ID2label_class) predict_list = [predict_label_class2_1, predict_label_class2_2, predict_label_class2_3, predict_label_class2_4, predict_label_class2_5] # predict_list = [predict_label_class2_1, predict_label_class2_2, predict_label_class2_3, predict_label_class2_4, predict_label_class2_5, # predict_label_class2_6, predict_label_class2_7, predict_label_class2_8, predict_label_class2_9, predict_label_class2_10] predict_label_class2 = max(predict_list, key=predict_list.count) class2_class1 = load_class2_to_class1('./data/class2_class1.txt') if class2_class1[predict_label_class2]!=class2_class1[ID2label_class[labels_class2[i]]]: count1 += 1 # worng_class_list.append(ID2label_class[labels_class2[i]]+"\t"+str(predict_label_class2_1)+"\t"+samples_class2[i]) worng_class_list.append(class2_class1[ID2label_class[labels_class2[i]]]+"\t"+str(predict_label_class2_1)+"\t"+str(predict_label_class2_2)+"\t"+str(predict_label_class2_3)+"\t"+str(predict_label_class2_4)+"\t"+str(predict_label_class2_5)+"\t"+str(class2_class1[predict_label_class2])+"\t"+samples_class2[i]) if predict_label_class2!=ID2label_class[labels_class2[i]]: count2 += 1 writer = codecs.open('./data/wrong_analysis/class_test_data_predict_wrong_5.txt', "w", encoding='utf-8', errors='ignore') print("class1:"+str(1-count1/len(samples_class2))) print("class2:"+str(1-count2/len(samples_class2))) writer.write("Acc:"+str(1-count1/len(samples_class2))+'\n') for text in worng_class_list: writer.write(text+'\n') writer.flush() writer.close()
def main(): args = parse_args() print_args(args) if args.json is None or args.json == '': print('\nFor usage, please use the -h switch.\n\n') sys.exit(0) with open(args.json) as json_file: configs = json.load(json_file) tools.init(configs) tools.read_labels(configs) tools.predict(configs, args.detection)
def predict(self, X, labels): if self.tree is None: sys.stderr.write( "[FATAL] your tree has not been created or loaded.\n") return False if X is None: sys.stderr.write( "[FATAL] your input dataset is none, please recheck.\n") return False if len(X[0]) != len(labels): sys.stderr.write( "[FATAL] your sample and label have different columns.") return False result = predict(X, labels, self.tree)
bias_output = f.item().get('output_b') mean = f.item().get('mean') std = f.item().get('std') df = pd.read_csv(args.dataset, names=columns) j = 0 for i in range(30): if i != 1: df['column_' + str(i)] = standardize(df['column_' + str(i)], mean[j], std[j]) j += 1 Y_M = df['column_1'].map({'M': 1, 'B': 0}).values Y_B = df['column_1'].map({'M': 0, 'B': 1}).values Y_M = np.reshape(Y_M, (Y_B.shape[0], 1)) Y_B = np.reshape(Y_B, (Y_B.shape[0], 1)) Y = np.concatenate((Y_M, Y_B), axis=1) df = df.drop('column_1', axis=1) X = df.values Y_predict = predict(X, weights_hidden_1, bias_hidden_1, weights_hidden_2, bias_hidden_2, weights_outputs, bias_output) print('Accuracy test = {:.3f}'.format(accuracy_score(Y[:, 0], Y_predict))) ### Cross entropy cross = -np.sum(Y[:, 0] * np.log(Y_predict.T + 1e-9) + (1 - Y[:, 0]) * np.log(1 - Y_predict.T + 1e-9)) / Y_predict.shape[0] print('Cross Entropy value = {:.3f}'.format(cross)) # Matrix confusion results = confusion_matrix(Y[:, 0], Y_predict) print("Matrix confusion Elements\nTP = {} TN = {} FP = {} FN = {}".format( results[0][0], results[1][1], results[1][0], results[0][1])) print("F1-score = {:.3f}".format(f1_score(Y[:, 0], Y_predict))) except: print("Error dataset to predict or weights")
def predict(): samples_sentiment, labels_sentiment, ID2label_sentiment = load_predict_data_sentiment( ) dict_sentiment = tools.load_dict( './code/sentiment_model/sentiment_weight_no_reduce_produce.dict') # dict_sentiment_att = load_dict('D:/MSRA/JD_comments_analysis/code/sentiment_weight_no_reduce_produce.dict') print(len(dict_sentiment)) model_sentiment1 = load_model( './code/sentiment_model/model_sentiment_weight_no_reduce_produce1.h5') model_sentiment2 = load_model( './code/sentiment_model/model_sentiment_weight_no_reduce_produce2.h5') model_sentiment3 = load_model( './code/sentiment_model/model_sentiment_weight_no_reduce_produce3.h5') model_sentiment4 = load_model( './code/sentiment_model/model_sentiment_weight_no_reduce_produce4.h5') model_sentiment5 = load_model( './code/sentiment_model/model_sentiment_weight_no_reduce_produce5.h5') worng_sentiment_list = ["ori\tp1\tp2\tp3\tp4\tp5\tp"] count = 0 for i in range(len(samples_sentiment)): predict_label_sentiment1 = tools.predict(samples_sentiment[i], dict_sentiment, model_sentiment1, ID2label_sentiment) predict_label_sentiment2 = tools.predict(samples_sentiment[i], dict_sentiment, model_sentiment2, ID2label_sentiment) predict_label_sentiment3 = tools.predict(samples_sentiment[i], dict_sentiment, model_sentiment3, ID2label_sentiment) predict_label_sentiment4 = tools.predict(samples_sentiment[i], dict_sentiment, model_sentiment4, ID2label_sentiment) predict_label_sentiment5 = tools.predict(samples_sentiment[i], dict_sentiment, model_sentiment5, ID2label_sentiment) predict_list = [ predict_label_sentiment1, predict_label_sentiment2, predict_label_sentiment3, predict_label_sentiment4, predict_label_sentiment5 ] predict_label_sentiment = max(predict_list, key=predict_list.count) if int(predict_label_sentiment) != int(labels_sentiment[i]): # worng_sentiment_list.append(str(labels_sentiment[i])+"\t"+str(predict_label_sentiment4)+"\t"+samples_sentiment[i]) worng_sentiment_list.append( str(labels_sentiment[i]) + "\t" + str(predict_label_sentiment1) + "\t" + str(predict_label_sentiment2) + "\t" + str(predict_label_sentiment3) + "\t" + str(predict_label_sentiment4) + "\t" + str(predict_label_sentiment5) + "\t" + str(predict_label_sentiment) + "\t" + samples_sentiment[i]) count = count + 1 writer = codecs.open( './data/wrong_analysis/sentiment_test_data_predict_wrong_5.txt', "w", encoding='utf-8', errors='ignore') print(1 - count / len(samples_sentiment)) writer.write("Acc:" + str(1 - count / len(samples_sentiment)) + '\n') for text in worng_sentiment_list: writer.write(text + '\n') writer.flush() writer.close()
delta_w11 = X dw_h_1 = np.dot(d.T, delta_w11) / X.shape[0] db_h_1 = np.sum(d, axis=0, keepdims=True) / X.shape[0] ### Update weights and bias network.layers[1].weights -= L * dw_h_1 network.layers[2].weights -= L * dw_h_2 network.layers[3].weights -= L * dw_o network.layers[1].bias -= L * db_h_1 network.layers[2].bias -= L * db_h_2 network.layers[3].bias -= L * db_o #### Loss function Y_predict = final_outputs(network.layers[3].outputs) loss.append(mean_square_error(Y[:, 0], Y_predict)) Y_val_predict = predict(X_val, network.layers[1].weights, network.layers[1].bias, network.layers[2].weights, network.layers[2].bias, network.layers[3].weights, network.layers[3].bias) val_loss.append(mean_square_error(Y_val[:, 0], Y_val_predict)) # calculate scores lr_auc.append(roc_auc_score(Y[:, 0], Y_predict)) lr_val_auc.append(roc_auc_score(Y_val[:, 0], Y_val_predict)) print( "epoch {}/{} - loss: {:.10f} - val_loss: {:.10f} - roc {:.7f} roc_val {:.7f}" .format(i + 1, epochs, loss[i], val_loss[i], lr_auc[i], lr_val_auc[i])) # calculate roc curves ns_fpr, ns_tpr, _ = roc_curve(Y[:, 0], ns_probs) lr_fpr, lr_tpr, _ = roc_curve(Y[:, 0], Y_predict) lr_val_fpr, lr_val_tpr, _ = roc_curve(Y_val[:, 0], Y_val_predict) # plot the roc curve for the model
omegas.extend(omega500) omegas = np.array(omegas) ais = np.linspace(.5, 3, 11) calc_lts = np.vectorize(tools.find_LTS) pred_range = [] act_range = [] X = [] X_vars = ['LTS', 'sst', 'RH700', 'modis_aod', 'upper_level_U', 'upper_level_V', 'w500'] for ai in ais: #for time in range(1901, 1981): time = 1901 lts = calc_lts(ts[time, :, :], t700s[time, :, :], ps[time, :, :]) #xai = np.array([(a, b, c, d, ai) for a, b, c, d in zip(lts.ravel(), ssts[time].ravel(), omegas[time].ravel(), rh700s[time].ravel())]) lgbm = tools.predict('/home/users/rosealyd/ML_sat_obs/saved_models/lightgbm_latest.sav', None, return_model = True) get_year.plot_year(lgbm, X_vars, 'cf', None, GCM = True, GCM_data = {'sst': [ssts[time]], 'LTS': [lts[time]], 'RH700': [rh700s[time]], 'modis_aod': [np.full(lts.shape, ai)], 'upper_level_U': [u250s[time]], 'upper_level_V': [v250s[time]], 'w500': [omegas[time]]}) xai = np.array([(a, b, c, ai, d, e, f) for a, b, c, d, e, f in zip(lts.ravel(), ssts[time].ravel(), rh700s[time].ravel(), u250s[time].ravel(), v250s[time].ravel(), omegas[time].ravel())]) good = np.where(np.isnan(xai) == False)[0] xai = xai[good] comp_cfs = all_cf[time].ravel()[good] predicted_cfs = tools.predict('/home/users/rosealyd/ML_sat_obs/saved_models/lightgbm_latest.sav', xai) diff = comp_cfs - predicted_cfs plt.hist2d(predicted_cfs, comp_cfs) plt.colorbar() plt.xlabel('RF derived') plt.ylabel('MIROC cf') plt.show() pred_range.append(predicted_cfs[1400]) act_range.append(comp_cfs[1400])
print("Predict runs with model {}".format(pa.pretrained_model)) image = pa.input_image path = pa.checkpoint power = pa.gpu top_k = pa.topk cat = pa.cat_name_dir pretr_model = pa.pretrained_model model = getattr(models, pretr_model)(pretrained=True) model_load = model_load(path, pa.pretrained_model) with open(pa.cat_name_dir, 'r') as json_file: cat_to_name = json.load(json_file) probs, classes = predict(image, model_load, top_k) print(probs) print(classes) names = [] for i in classes: names += [cat_to_name[i]] print( f"The image potentially contains a flower of category '{names[0]}' with the probability of {round(probs[0]*100,4)}%" ) print( "The prediction using the trained model has been made above, project ceompleted (hopefully)" )
import argparse import yaml import tools parser = argparse.ArgumentParser() parser.add_argument('-visualize', action='store_true') parser.add_argument('-predict', action='store_true') parser.add_argument('-slice', action='store_true') parser.add_argument('-evaluate', action='store_true') args = parser.parse_args() with open('./configs.yaml', 'r') as stream: try: configs = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) if args.visualize: tools.visualize(configs) elif args.predict: tools.predict(configs) elif args.slice: tools.slice_vids(configs) elif args.evaluate: tools.evaluate(configs) else: raise ValueError( 'Did not set flag: -visualize, -predict, -slice, -evaluate')
# load data # set X, Y, Theta X, Y = load_mnist(1000, '../data/') Theta = np.zeros((Y.shape[1], X.shape[1] + 1)) X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1) print(Theta.shape) # set hyper parameters learningRate = 0.001 numIter = 3000 # set cost function cost = costFunction(X, Y, Theta) print("before : ", cost) # set gradient descent Theta = gradientDescent(X, Y, Theta, learningRate, numIter) cost = costFunction(X, Y, Theta) print("after : ", cost) # test X_test, Y_test = load_mnist(1000, '../data/', is_test=True) X_test = np.concatenate((np.ones((X_test.shape[0], 1)), X_test), axis=1) pred = predict(X, Theta) print(pred) real = np.argmax(Y_test, axis=1) print(real)