Does anyone know how to calculate RMSPE in Python? And what is the difference between RMSE and RMSPE?
With the Root Mean Squared Percent Error (RMSPE), you have somewhat more of an idea of the magnitude of the error in relation to the actual values.
You’d probably calculate it like so:
rmspe = (np.sqrt(np.mean(np.square((y_true - y_pred) / y_true)))) * 100
To give an example, let’s say the mean of the actual values is 10,000, and the RMSE is 1,000.
The RMSPE in this case might be 10%, and this 10% gives you an idea of the error in relation to the mean of the actual values.
One caveat to keep in mind is that with RMSPE, you run the risk of possibly facing a division by zero problem, if the
y_true value at any point happens to be 0.
There are two ways around this:
- You can add a very small constant to the denominator so that even in the case that y_true equals 0, the denominator as a whole won’t:
EPSILON = 1e-10 # Yes, Python is awesome and supports scientific notation! rmspe = (np.sqrt(np.mean(np.square((y_true - y_pred) / (y_true + EPSILON))))) * 100
- Or you can wrap your code in a Try … Except block like so:
Try: rmspe = (np.sqrt(np.mean(np.square((y_true - y_pred) / y_true)))) * 100 Except ZeroDivisionError: print("Oh, no! You tried to divide by zero!")
Edited because I had added EPSILON in the wrong place in option 1. Oops!
If you don’t mind, can you tell me how to get better at Machine Learning and Python Programming? Do you have any specific suggestions about it?
If you haven’t already, I’d recommend taking an introductory Computer Science course. This one from MITx is a good choice in my opinion. It’s also free.
If you have already taken beginner/intermediate courses in Python, including here on DQ, then the first modules will possibly feel to easy. If so, skip the videos and just do the exercises and programming assignments as a means to review and consolidate concepts you already now.
This is not a “Python” course per se and won’t teach you how to best leverage the languages various features (in-built functions, libraries, syntactic sugar…), but uses Python as a means to the end of teaching various high-level concepts which are at the core of programming - in any language - and of thinking like a computer scientist.
As for Machine Learning, it’s just a fancy name for “Computational Statistics” in my opinion (others may beg to differ…). So yeah, make sure you have a solid understanding of Statistics, both descriptive and inferential, as well as the underlying math - mostly Linear Algebra and Multivariate Calculus. I don’t know where you are on your learning path but Dataquest is a good starting point for all of the above.
Below are some guides to help you improve on writing good Python code.
Damn straight. Like string interpolation… or indexing into Pandas DataFrames… oh, wait…
The following are python programming resources:
Lecture Videos on Programming
- Online text: Algorithms by Jeff Erickson
- Online text: Introduction to Programming in Python
- Online text: Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne
- Introduction to Algorithms, Third Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein.
- The Algorithm Design Manual by Second Edition by Steven S. Skiena.
- Programming Pearls, Second Edition by Jon Bentley
- Data Structures and Algorithms with Python, Springer Press, by Kent D. Lee and Steve Hubbard
- Python Algorithms - Mastering Basic Algorithms in the Python Language, Second Edition by Magnus Lie Hetland
There is always redundancy build in place. Hence multiple ways to do a single operation.
Practice what you have had learnt. For example, do a Kaggle competition, and read forum to learn what others do.
It’s a great textbook but one should be comfortable with at least reading Java. So if you are a beginner and your only language is Python then this book can feel rather challenging, in my experience.
The Java book has more information compare to the Python book. You can access the Java book listed as Algorithms, 4th Edition.
Forgot to mention, one of my favorite online resources for Python specific things is this one:
I find the articles and tutorials there to be very well written and researched, and there is a lot of materials for all levels, from beginner to advanced to ML specific stuff!