-2

I would like to display the first 4 digit of fraction number and pass it to string to display in title of my plot. I checked this post but I couldn't find an elegant way.

I've tried following code as the easiest way the problem is I don't want to display % after that:

train_MSE=mean_squared_error(Y_train, Y_RNN_Train_pred)
print("Train MSE:",train_MSE_)
#Train MSE: 0.33068236552127656

train_MSE_ = "%.4f%%" % train_MSE
print("Train MSE:",train_MSE_)
#Train MSE: 0.3307% 
#expected result without '%' ---> 0.337

plt.plot(Y_RNN_Test_pred[0],'b-')
plt.title(f'Test MSE={test_MSE_}', fontsize=15, fontweight='bold')
plt.show()
Mario
  • 1,631
  • 2
  • 21
  • 51

3 Answers3

2

You need to remove the %% at the end.

train_MSE_ = "%.4f" % train_MSE_
David Robles
  • 9,477
  • 8
  • 37
  • 47
0

you may use the format command

print('{0:.4f}'.format(0.264875464))

result

0.2649

So you may write your code like:

train_MSE_=0.264875464
print('Train MSE:{0:.4f}'.format(train_MSE_))

reslut

Train MSE:0.2649
Rebin
  • 516
  • 1
  • 6
  • 16
0

You can do this:

print("Train MSE : {:.4f}".format(train_MSE_))

You can see more details about format string here : https://docs.python.org/3.7/library/string.html#formatstrings