Resolving Unrealistic Weather Predictions in MicroPython Linear Regression Model on Raspberry Pi Pic
While implementing a networked weather station on the Raspberry Pi Pico W using MicroPython, I encountered an unexpected result in the predicted temperature and weather classification. The code is supposed to fetch real-time weather data and use a simple linear regression model to predict the next hour’s weather, but the output prediction is producing unrealistic temperature values like negative temperatures in tropical regions.
Here is the relevant portion of my code:
12 Replies
Why is the prediction producing an unrealistic temperature value ( -4.0°C) even when the input temperature is reasonable? How can this be corrected in the code to provide accurate predictions and proper weather classification?
How is this supposed to work? It looks like it fetches a single point of data and simply says the next hours temperature is:
coef_temp * temp + coef_humidity * humidity + intercept
Which seems nonsensicle to me.
Because coef humidity is negative, if you have a small temperature and high humidity the next step can easily be negative. Make some plots of what that function does (you can do a 2d plot for a set of inputs) - the visualisation should make it clear its not right.
Unless I misread the code there is no linear regression here
If you derived those coefficients from a linear regression, it is over fitted and doesnt have enough factors to be useful. One of the primary drivers of temperature is time of day for exampleAlso - you call out it being odd in tropical regions- when your model doesnt account for region and where region is a factor, it's natural it does not produce useful results
I suspect you did a linear regression to get those coefficients.
Once you have some coefficients, run it against the data you used to derive the coefficients and plot the error for each predicted / measured value. If the error is high, your model is not good - I suspect you will find that across a time period of 24 hours it really doesnt work
P.P.S. I strongly suspect you need a non linear SVM to get anything useful
@ke7c2mi Thanks for the feedback , You're absolutely right that the current approach doesn't effectively predict temperature changes, especially since it doesn't consider important factors like time of day or recent trends. The coefficients I used were derived from simple experimentation rather than a proper linear regression on historical data, which likely explains the inaccuracies.
Regarding your suggestion for a nonlinear approach, do you think it would be beneficial to incorporate additional variables, such as pressure or recent temperature data?
if I switch to a nonlinear
SVM
model, would it be feasible to run this on the Raspberry Pi Pico W
given its resource limitations, or would it make more sense to handle the computation externally and use the Pico
primarily for data fetching and display?
I’d appreciate any advice on alternative methods or improvements Thanks.Re more variables - yes
Re nonlinear - it must be nonlinear as time and the relationship between time and temperature is periodic - it could be any nonlinear modelling technique
Re it working in a pico - I'm pretty sure that can work, running the model shouldn't be ridiculously bad, and it would still work if it takes 5 minutes.... note pico is weak compared to a desktop, but any 32bit cpu running at hundreds of MHz is a pretty insane amount of computation, we went to the moon with a fraction of that - its definitely possible
Thanks, that gives me some confidence to proceed. I'll start by adding time of day as a variable and explore a few non-linear models to see if I can capture those periodic patterns more effectively.
Good to know that running this on the Pico should be feasible, even if the predictions take a bit of time, it sounds manageable. I’ll also consider reducing the model complexity to fit the Pico’s constraints better. If I do hit a wall with performance, I might look into offloading part of the processing to a more powerful device, as you mentioned.
Appreciate the advice! I'll let you know how it goes once I've made these adjustments.
Hello @ke7c2mi after implementing the changes you suggested my project got a huge improvement, Adding variables like time of day and using a nonlinear model lead to more accurate predictions. The model now account for factors that affect temperature fluctuations, giving realistic outputs ( temperature rising in the afternoon or decreasing at night).
Although it's a bit slow but due to it making the prediction on hourly basis I think it works just fine for now
To futher this project to make it more useful I want to implement the use of
AI-powered
image processing by connecting a low-power camera module
(OV7670
with a resolution of 640x480)and deploying a convolutional neural network (CNN
) for cloud pattern recognition, trained offline to classify images as "Sunny, Cloudy, or Rainy" .
would this be doable/achievable continuing with the raspberry pi pico and what would the reaction time look like