def try_different_method(clf, x_train, y_train, x_test, y_test): clf.fit(x_train, y_train) score = clf.score(x_test, y_test) print('score: ', score) result = clf.predict(x_test) plt.figure() plt.plot(np.arange(len(result)), y_test, 'go-', label='true value') plt.plot(np.arange(len(result)), result, 'ro-', label='predict value') plt.title('score: %f' % score) plt.legend() plt.show() # 保存Model with open('./clf.pickle', 'wb') as f: pickle.dump(clf, f) # 读取Model with open('./clf.pickle', 'rb') as f: clf2 = pickle.load(f) # 测试读取后的Model print(clf2.predict(x_test[0:1]), y_test[0:1]) print('relative x error: ', x_err_esti(result, y_test)) print('mean absolute error: ', vx_err_esti(result, y_test)) result = clf2.predict([x_test[0]]) print(x_test[0]) print(result)
def train_model(clf, t_smples, t_targets): x_train, x_test, y_train, y_test = train_test_split(t_smples, t_targets, test_size=0.1, random_state=42) clf.fit(x_train, y_train) result = clf.predict(x_test) # print('Train relative x error: ', x_err_esti(result, y_test)) print('Train mean absolute error: ', vx_err_esti(result, y_test)) return clf
def test(x_test, y_test): # 读取Model with open('./clf.pickle', 'rb') as f: clf = pickle.load(f) # 测试读取后的Model print(clf.predict(x_test[0:1]), y_test[0:1]) result = clf.predict(x_test) # print('relative x error: ', x_err_esti(result, y_test)) print('mean absolute error: ', vx_err_esti(result, y_test))
def err_estimation(self, gt_file, before): result = self.result fids = [r['fid'] for r in result] vx = [r['vx'] for r in result] x = [r['x'] for r in result] with open(gt_file) as f: ground_truth = json.load(f) gt_x = [gt['x'] for gt in ground_truth["frame_data"]] gt_vx = [gt['vx'] for gt in ground_truth["frame_data"]] # before = 10 err_x = x_err_esti(x[:before], gt_x[:before]) err_vx = vx_err_esti(vx[:before], gt_vx[:before]) high = high_esti(x[:before], gt_x[:before]) print('Error for x :', err_x) print('Error for vx:', err_vx) print('high :', high)
def test_model(clf, x_test, y_test): result = clf.predict(x_test) # print('Test relative x error: ', x_err_esti(result, y_test)) print('Test mean absolute error: ', vx_err_esti(result, y_test))
# print(outputs) xs = np.arange(500) if x_vx_mode == 'x': print( 'error for x:', x_err_esti(y_test_pred[:, :, 0], y_test_batch[:, :, 0])) draw_line(xs, y_test_pred, y_test_batch, name='show/curve_train/x_%s.png' % test_flies[ind][:-7]) elif x_vx_mode == 'vx': print( 'error for vx:', vx_err_esti(y_test_pred[:, :, 0], y_test_batch[:, :, 0])) draw_line(xs, y_test_pred, y_test_batch, name='show/curve_train/vx_%s.png' % test_flies[ind][:-7]) elif x_vx_mode == 'x_vx': print( 'error for x:', x_err_esti(y_test_pred[:, :, 0], y_test_batch[:, :, 0])) print( 'error for vx:', vx_err_esti(y_test_pred[:, :, 1], y_test_batch[:, :, 1])) draw_x_vx(xs,