Final Report
Final Report
PROJECT REPORT
by
School of Electronics
Vellore Institute of Technology
Vellore
OCTOBER 2023
ABSTRACT:
Rainfall prediction is one of the challenging and uncertain tasks which has a significant impact
on human society. Timely and accurate predictions can help to proactively reduce human and
financial loss. Accuracy of rainfall forecasting has great importance for countries like India
whose economy is largely dependent on agriculture. Due to the dynamic nature of the
atmosphere, Statistical techniques fail to provide good accuracy for rainfall forecasting.
Droughts and floods have been affecting our farmers since very long.Farming is a long process
and depends majorly on the rainfall cycle which makes our main focus to be farmers of our
nation. It is important to exactly determine the rainfall for effective use of water resources, crop
productivity and pre-planning of water structures In this project, we have proposed a “SMART
CAP” [Future improvement only] for farmers which records the atmosphere parameters while
they are working on the field using node MCU and required sensors, this data is logged in
thingspeak via the internet and is then processed by our trained prediction model and the
predictions are shown on thingspeak which can be accessed from anywhere in the world. The
Linear Regression method is modified in order to obtain the most optimum error percentage by
iterating and adding some percentage of error to the input values. This method provides an
estimate of rainfall using different atmospheric parameters like average temperature and cloud
cover to predict the rainfall. Further we have also modified our model to make it simple for
farmers to understand by implementing a logistic regression model which shows if there is
sufficient rainfall on that specific day or does he have to water the crops normally.
INTRODUCTION
Extreme variations in rainfall has a drastic effect on agriculture. Drought can kill crops while
heavy rainfall can increase soil erosion and spoil the plantations. Optimum amount of water is
required for survival of crops. Too much or too little water is harmful to the crops and can affect
the yield in the long run causing major loss to farmers. Hence rainfall is a major factor affecting
the crop yield. Therefore there is a need to predict rainfall for effective use of water resources
for crop productivity to give a better yield and decrease agricultural loss.
Existing rainfall prediction methods are large scale/locality based and don't sense atmospheric
parameters for a specific place which can sometimes be a problem.
Our project is essentially a wearable cap which can be worn by the farmer while working on
the field and this device automatically logs atmospheric parameters to the ThingSpeak cloud
and predicts the rainfall for that day using its trained model. This makes our device portable
and specific to the farmer's location and hence eases the decision of the farmer of how much he
has to water the plants and avoid agricultural loss.
The main aim of our project is to predict the rainfall of a specific location by analysing the
area’s atmospheric parameters like temperature, humidity, dew point, wind speed and alert the
farmer about the type of rainfall to expect on that day and water his plants accordingly or at
least be prepared and take preventive measures thereby decreasing agricultural loss to the
farmers of or nation.
METHODOLOGY
Proposed Design
Software Implementation
IOT Integration
This part focuses on sending the real time data to the thingspeak cloud and establishing
a connection between our analysis and thingspeak. This is generally easily done using
the node MCU and creating a webrequest using thingspeak API. Now as an alternative
to this we make the web request to post the data on thingspeak via our python code
itself. The “urllib” library helps us do this with the thing speak API of our channel. We
have created a new field in thingspeak that represents our output(precipitation level in
inches). And the write api key of our channel can be taken from thingspeak to create a
POST request from python. The python code for the same is shown later in the report
After the training phase is done we take a few values of temperature, humidity, dew point, wind
speed and input these to our model. And now our model predicts the following output.
Linear regression analysis of our dataset shows the below trends in each parameter
Logistic regression results
After the training phase is done we take a few values of temperature, humidity, dew point, wind
speed and input these to our model. And now our model predicts the following categorical
output.
Logistic regression analysis of our dataset shows the below trends in each parameter
3.3 IOT integration results
Finally since we have also uploaded the entire data on thingspeak we can view it as below on
our thingspeak channel
The same can be viewed in things view app on our mobile phone as shown below
CONCLUSION
The choice of algorithm basically depends on the nature of prediction. For the above kind of
input data and required output, linear regression gives more accurate results than logistic
regression. A bad rainfall prediction can affect the agriculture mostly farmers as their whole
crop is dependent on the rainfall and agriculture is always an important part of every economy.
So, making an accurate prediction of the rainfall somewhat good. There are a number of
techniques used in machine learning but accuracy is always a matter of concern in prediction
made in rainfall. In the future, this system can be used to help in the agriculture and food
industry as it will help farmers predict the outcome of their plot. It will also improve on weather
prediction systems and there might be personalized apps to regulate people in day to day life.
These apps can be integrated in the working of organizations centered around agriculture and
other businesses related to it. Since agriculture is often related to the economy of a country,
accurate weather prediction will provide great help to the lives of those directly involved in the
processing as well as most other citizens.
Multiple Linear Regression Code
#importing libraries
import pandas as pd
import numpy as np
# read the data in a pandas dataframe data
= pd.read_csv("vellore_weather.csv")
# drop or delete the unnecessary columns in the data. data = data.drop(['Events', 'Date',
'SeaLevelPressureHighInches', 'SeaLevelPressureLowInches'], axis = 1)
# some values have 'T' which denotes trace rainfall we need to replace all occurrences of T with 0
# so that we can use the data in our model data
= data.replace('T', 0.0)
# The data also contains '-' which indicates no or NIL. This means that data is not available we need to replace #these
values as well. data = data.replace('-', 0.0)
# the features or the 'x' values of the data these columns are used to train the model the last column, i.e,
#precipitation column will serve as the label X =
data.drop(['PrecipitationSumInches'], axis = 1) #
the output or the label.
Y = data['PrecipitationSumInches']
# reshaping it into a 2-D vector Y
= Y.values.reshape(-1, 1)
# consider a random day in the dataset we shall plot a graph and observe this day day_index
= 1000 days = [i for i in range(Y.size)]
# initialize a linear regression classifier
clf = LinearRegression() # train the
classifier with our # input data. clf.fit(X,
Y)
# give a sample input to test our model this is a 2-D vector that contains values for each column in the dataset. inp
= np.array([[74], [60], [45], [67], [49], [43], [33], [45],
[57], [29.68], [10], [7], [2], [0], [20], [4], [31]])
inp = inp.reshape(1, -1)
plt.show()
x_vis = X.filter(['TempAvgF', 'DewPointAvgF', 'HumidityAvgPercent', 'WindAvgMPH'], axis = 1)
# plot a graph with a few features (x values) against the precipitation or rainfall to observe the trends
print("Precipitation vs selected attributes graph: ") for i in range(x_vis.columns.size):
plt.subplot(2, 2, i + 1) plt.scatter(days,
x_vis[x_vis.columns.values[i][:100]], color =
'g') plt.scatter(days[day_index],
x_vis[x_vis.columns.values[i]][day_index], color ='r')
plt.title(x_vis.columns.values[i])
plt.show()
import pandas as pd
import numpy as np import sklearn as sk from sklearn import
metrics, datasets from sklearn.linear_model import
LogisticRegression import matplotlib.pyplot as plt import
matplotlib.patches as mpatches data =
pd.read_csv("vellore2_final.csv") X =
data.drop(['PrecipitationSumInches'], axis=1)
Y_temp = data['PrecipitationSumInches']
Y_temp = Y_temp.values.reshape(-1, 1)
'''
# Generate multiclass one-hot columns
Y = pd.DataFrame(columns=['No Rains', 'Drizzle', 'Moderate Rains', 'Heavy Rains']) for i
in range(Y_temp.size):
if(Y_temp[i]<0.001):
Y.loc[i] = [1, 0, 0, 0] elif(Y_temp[i]>=0.001
and Y_temp[i]<0.1):
Y.loc[i] = [0, 1, 0, 0]
elif(Y_temp[i]>=0.1 and Y_temp[i]<0.4): Y.loc[i] = [0, 0, 1, 0]
else:
Y.loc[i] = [0, 0, 0, 1]
'''
Y = [] x1 =
pd.DataFrame(columns=X.columns.values) x2 =
pd.DataFrame(columns=X.columns.values) x3 =
pd.DataFrame(columns=X.columns.values) x4 =
pd.DataFrame(columns=X.columns.values) for i
in range(Y_temp.size): if(Y_temp[i]<0.001):
Y.append(1) x1.loc[i]
= X.loc[i]
elif(Y_temp[i]>=0.001 and
Y_temp[i]<0.1): Y.append(2) x2.loc[i]
= X.loc[i] elif(Y_temp[i]>=0.1 and
Y_temp[i]<1.2): Y.append(3) x3.loc[i] = X.loc[i]
else:
Y.append(4) x4.loc[i]
= X.loc[i]
The below code snippet is an extension to the first linear regression code
prinches=clf.predict(inp) with
urllib.request.urlopen("https://api.thingspeak.com/update?api_key=RIGFWEHK261UM8F6&field5="
+str(prinches[0][0])) as url:
apidata=url.read()
print(apidata)
Smart Band To Show Rainfall Prediction [Future improvements]
An advancement to this project would be to implement the hardware setup(cap) and further
develop a smart band which can be worn by farmers to show the rainfall status of the day on
their wearable smart band. As this status is already updating live on thingspeak channel our
next work to implement this would be to do web scraping and read this specific field and display
it on this band.
REFERENCES
3. https://www.researchgate.net/publication/319503839_Machine_Learning_Techniques
_For_Rainfall_Prediction_A_Review
4. https://ukdiss.com/examples/rainfall-prediction-machine-learning.php
5. https://www.youtube.com/watch?v=ibNG4RSHXEw&feature=youtu.be&ab_channel
=VikasYadav