import matplotlib.pyplot as pl %matplot inline def learning_curves(X_train, y_train, X_test, y_test): """ Calculates the performance of several models with varying sizes of training data. The learning and testing error rates for each model are then plotted. """ print ("Creating learning curve graphs for max_depths of 1, 3, 6, and 10. . .") # Create the figure window fig = pl.figure(figsize=(10,8)) # We will vary the training set size so that we have 50 different sizes sizes = np.rint(np.linspace(1, len(X_train), 50)).astype(int) train_err = np.zeros(len(sizes)) test_err = np.zeros(len(sizes)) # Create four different models based on max_depth for k, depth in enumerate([1,3,6,10]): for i, s in enumerate(sizes): # Setup a decision tree regressor so that it learns a tree with max_depth = depth regressor = DecisionTreeRegressor(max_depth = depth) # Fit the learner to the training data regressor.fit(X_train[:s], y_train[:s]) # Find the performance on the training set train_err[i] = performance_metric(y_train[:s], regressor.predict(X_train[:s])) # Find the performance on the testing set test_err[i] = performance_metric(y_test, regressor.predict(X_test)) # Subplot the learning curve graph ax = fig.add_subplot(2, 2, k+1) ax.plot(sizes, test_err, lw = 2, label = 'Testing Error') ax.plot(sizes, train_err, lw = 2, label = 'Training Error') ax.legend() ax.set_title('max_depth = %s'%(depth)) ax.set_xlabel('Number of Data Points in Training Set') ax.set_ylabel('Total Error') ax.set_xlim([0, len(X_train)]) # Visual aesthetics fig.suptitle('Decision Tree Regressor Learning Performances', fontsize=18, y=1.03) fig.tight_layout() fig.show()
when I run the learning_curves()
function, it shows:
UserWarning:C:\Users\Administrator\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
To Solve UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure Error Here Error is mentioning that You must have installed GUI backend So that Just Install the GUI backend tk Using this command: sudo apt-get install python3-tk Second solution is You just need to install ...
Matplotlib is a plotting library. It relies on some backend to actually render the plots. The default backend is the agg backend.
When we get the Warning: Matplotlib is currently using agg a non-gui backend we can't see the figures or we can say that visualization of plots is not possible. Basically, this error means that we have to install the GUI backend. GUI stands for the graphical user interface.
Install MatplotlibMake sure you first have Jupyter notebook installed, then we can add Matplotlib to our virtual environment. To do so, navigate to the command prompt and type pip install matplotlib. Now launch your Jupyter notebook by simply typing jupyter notebook at the command prompt.
You don't need the line of "fig.show()". Just remove it. Then it will be no warning message.
adding %matplotlib inline while importing helps for smooth plots in notebook
%matplotlib inline import matplotlib.pyplot as plt
%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With