Rainfall Prediction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

6/5/24, 6:48 PM Rainfall Prediction.

ipynb - Colab

keyboard_arrow_down Rainfall Prediction


import pandas as pd #importing Libraries
import numpy as np
import matplotlib.pyplot as plt

ds=pd.read_csv("fall.csv") #Reading Dataset

ds.head() #Displaying first 5 records

year month day tempavg DPavg humidity avg SLPavg visibilityavg windavg Rainfall

0 2011 2 13 21 13 60 1009 3 5 0.0

1 2011 2 14 21 18 75 1009 3 5 0.0

2 2011 2 15 22 18 75 1010 3 10 0.0

3 2011 2 16 23 21 86 1011 3 8 0.0

4 2011 2 17 23 19 75 1011 3 8 0.0

ds=ds.drop(['month','day'], axis=1) #Droping Unnecessary columns

ds.head() #Displaying first 5 records

year tempavg DPavg humidity avg SLPavg visibilityavg windavg Rainfall

0 2011 21 13 60 1009 3 5 0.0

1 2011 21 18 75 1009 3 5 0.0

2 2011 22 18 75 1010 3 10 0.0

3 2011 23 21 86 1011 3 8 0.0

4 2011 23 19 75 1011 3 8 0.0

ds.isnull().sum() #Checking for any missing values

year 0
tempavg 0
DPavg 0
humidity avg 0
SLPavg 0
visibilityavg 0
windavg 0
Rainfall 0
dtype: int64

x=ds.iloc[:,:7].values #Assigning variables to the dependent and independent attiributes


y=ds.iloc[:,7].values

array([[2011, 21, 13, ..., 1009, 3, 5],


[2011, 21, 18, ..., 1009, 3, 5],
[2011, 22, 18, ..., 1010, 3, 10],
...,
[2017, 23, 16, ..., 1014, 4, 5],
[2017, 23, 17, ..., 1013, 4, 5],
[2017, 24, 18, ..., 1012, 4, 2]], dtype=int64)

array([0., 0., 0., ..., 0., 0., 0.])

from sklearn.model_selection import train_test_split #performing train-test split.


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20,random_state=0)

https://colab.research.google.com/drive/1KKIA-THucYMImdBcdRoucGtcC479M2c9#printMode=true 1/5
6/5/24, 6:48 PM Rainfall Prediction.ipynb - Colab
x_train

array([[2015, 30, 27, ..., 1004, 6, 14],


[2015, 29, 26, ..., 1008, 6, 13],
[2017, 25, 22, ..., 1012, 4, 14],
...,
[2013, 28, 26, ..., 1003, 5, 10],
[2013, 23, 22, ..., 1007, 5, 11],
[2016, 18, 14, ..., 1017, 2, 13]], dtype=int64)

from sklearn.ensemble import RandomForestRegressor #Model building using Random Forest


regressor = RandomForestRegressor(n_estimators=100, random_state = 0)
regressor.fit(x_train, y_train)

RandomForestRegressor(random_state=0)

ypred =regressor.predict(x_test) #Predicting using testing data on trained data.

ypred

array([7.18700000e-01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,


0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
7.67110000e+00, 1.51170000e+00, 2.18380000e+00, 8.64940000e+00,
2.54000000e-01, 0.00000000e+00, 6.47110000e+00, 5.10000000e-03,
4.97400000e+00, 7.31500000e-01, 1.17830000e+00, 1.77600000e-01,
0.00000000e+00, 4.55722000e+01, 6.93180000e+00, 1.01308000e+01,
2.02310000e+00, 2.84500000e-01, 0.00000000e+00, 7.50000000e-03,
1.22150000e+00, 0.00000000e+00, 6.98600000e-01, 1.26900000e-01,
0.00000000e+00, 2.12630000e+00, 6.16950000e+00, 0.00000000e+00,
3.81320000e+00, 0.00000000e+00, 1.51974000e+01, 0.00000000e+00,
4.14550000e+00, 0.00000000e+00, 5.60000000e-02, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 1.01400000e-01, 6.14700000e-01,
9.55020000e+00, 5.05300000e-01, 2.03000000e-02, 9.43910000e+00,
6.08290000e+00, 3.09360000e+00, 1.33870000e+00, 2.94513000e+01,
0.00000000e+00, 9.95500000e-01, 6.55400000e-01, 8.57960000e+00,
3.60130000e+00, 1.42480000e+00, 2.77840000e+00, 0.00000000e+00,
1.46090000e+00, 1.60000000e-01, 0.00000000e+00, 0.00000000e+00,
2.47860000e+00, 2.23640000e+00, 3.98800000e-01, 3.45400000e-01,
2.31398000e+01, 3.27900000e+00, 2.15570000e+00, 3.19250000e+00,
0.00000000e+00, 2.37780000e+00, 3.07300000e-01, 0.00000000e+00,
1.84660000e+00, 0.00000000e+00, 0.00000000e+00, 1.53540000e+00,
4.67000000e-01, 1.87410000e+00, 2.28620000e+00, 4.37100000e-01,
1.43740000e+00, 3.68300000e-01, 0.00000000e+00, 1.31850000e+00,
7.22250000e+00, 2.35240000e+00, 1.49050500e+01, 5.89100000e-01,
0.00000000e+00, 2.51170000e+00, 2.31910000e+00, 0.00000000e+00,
5.39380000e+00, 3.38500000e+00, 1.31810000e+00, 3.16980000e+00,
3.46400000e+00, 8.36000000e-02, 0.00000000e+00, 0.00000000e+00,
2.50000000e-03, 5.05400000e-01, 1.62500000e-01, 0.00000000e+00,
1.39480000e+00, 9.85600000e-01, 1.04330000e+00, 1.67121000e+01,
9.21700000e-01, 7.64720000e+00, 0.00000000e+00, 1.49800000e-01,
3.25000000e-01, 8.78800000e-01, 6.35200000e-01, 4.36700000e-01,
0.00000000e+00, 1.43642000e+01, 0.00000000e+00, 1.64656000e+01,
6.42900000e-01, 7.87000000e-02, 1.02000000e-02, 1.19994000e+01,
1.59750000e+00, 4.24190000e+00, 1.57210000e+00, 5.80493333e+00,
1.52150000e+00, 3.49246000e+01, 0.00000000e+00, 0.00000000e+00,
3.62710000e+00, 7.64300000e-01, 2.32464000e+01, 7.38300000e+00,
0.00000000e+00, 2.18500000e+00, 3.49630000e+00, 1.31700000e-01,
8.96600000e+00, 3.19030000e+00, 0.00000000e+00, 3.84190000e+00,
1.24500000e-01, 3.34880000e+00, 1.00000000e-02, 0.00000000e+00,
7.92500000e-01, 0.00000000e+00, 3.32807500e+01, 8.63000000e-02,
0.00000000e+00, 3.16640000e+00, 0.00000000e+00, 1.45615000e+01,
0.00000000e+00, 2.35470000e+00, 1.02000000e-02, 5.71600000e-01,
6.10000000e-02, 7.62100000e-01, 3.03730000e+00, 0.00000000e+00,
1.26738000e+01, 2.03200000e-01, 1.12570000e+00, 1.56670000e+00,
4.78750000e+00, 2.74300000e-01, 1.02000000e-02, 0.00000000e+00,
4.09200000e-01, 3.26920000e+00, 5.97660000e+00, 5.43400000e-01,
6.27500000e-01, 1.22911000e+01, 0.00000000e+00, 2.13830000e+00,
6.40100000e-01, 2.46400000e-01, 4.27930000e+00, 9.71710000e+00,
1.12976000e+01, 1.01200000e+00, 0.00000000e+00, 7.28900000e-01,
1.49150000e+00, 1.63888000e+01, 1.73708000e+01, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 9.11800000e-01, 0.00000000e+00,
0.00000000e+00, 1.07690000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 3.66520000e+00, 2.87780000e+00,
0.00000000e+00, 1.05742000e+01, 4.84900000e-01, 1.64717000e+01,
6.83300000e-01, 4.04360000e+00, 9.09100000e-01, 0.00000000e+00,
1.52000000e-02, 0.00000000e+00, 0.00000000e+00, 1.71421000e+01,
6.37010000e+00, 1.50467000e+01, 4.02470000e+00, 0.00000000e+00,
1.56076000e+01, 0.00000000e+00, 8.48800000e-01, 1.16005000e+01,

https://colab.research.google.com/drive/1KKIA-THucYMImdBcdRoucGtcC479M2c9#printMode=true 2/5
6/5/24, 6:48 PM Rainfall Prediction.ipynb - Colab
y_test.shape

(473,)

from sklearn.metrics import r2_score #Testing accuracy


r2_score(y_test,ypred)

0.2728975259726836

x_train.shape

(1889, 7)

y_train.shape

(1889,)

plt.scatter(x_train[:,1],y_train,color='blue') #Displaying relation b/w temp and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('Temperature')
plt.ylabel('Rainfall')
plt.show()

plt.scatter(x_train[:,2],y_train,color='blue') #Displaying relation b/w dp avg and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('DP avg')
plt.ylabel('Rainfall')
plt.show()

plt.scatter(x_train[:,3],y_train,color='blue') #Displaying relation b/w humidity and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('Humidity')
plt.ylabel('Rainfall')
plt.show()

https://colab.research.google.com/drive/1KKIA-THucYMImdBcdRoucGtcC479M2c9#printMode=true 3/5
6/5/24, 6:48 PM Rainfall Prediction.ipynb - Colab

plt.scatter(x_train[:,4],y_train,color='blue') #Displaying relation SLP and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('SLP')
plt.ylabel('Rainfall')
plt.show()

plt.scatter(x_train[:,5],y_train,color='blue') #Displaying relation b/w visibility and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('visibility')
plt.ylabel('Rainfall')
plt.show()

plt.scatter(x_train[:,6],y_train,color='blue') #Displaying relation b/w Wind and rainfall


plt.title('Rainfall prediction (Training set)')
plt.xlabel('Wind')
plt.ylabel('Rainfall')
plt.show()

https://colab.research.google.com/drive/1KKIA-THucYMImdBcdRoucGtcC479M2c9#printMode=true 4/5
6/5/24, 6:48 PM Rainfall Prediction.ipynb - Colab

ds.corr() #Co-relation b/w attributes

humidity
year tempavg DPavg SLPavg visibilityavg windavg
avg

year 1.000000 0.090248 0.044044 -0.091357 0.085676 -0.082626 0.110007

tempavg 0.090248 1.000000 0.863829 0.340977 -0.674395 0.704721 0.233133

DPavg 0.044044 0.863829 1.000000 0.731021 -0.758224 0.604098 0.213889

humidity
-0.091357 0.340977 0.731021 1.000000 -0.574413 0.173502 0.101864
avg

SLPavg 0.085676 -0.674395 -0.758224 -0.574413 1.000000 -0.557946 -0.378632

visibilityavg -0.082626 0.704721 0.604098 0.173502 -0.557946 1.000000 0.244426

windavg 0 110007 0 233133 0 213889 0 101864 0 378632 0 244426 1 000000

import seaborn as sns #importing seaborn Library

sns.heatmap(ds.corr(),annot=True) #Displaying Co-relation b/w attributes using Heatmap

<matplotlib.axes._subplots.AxesSubplot at 0x219ec56fd30>

ypred1= regressor.predict([[2020,18,16,65,1013,6,8]]) #Predicting new data record.

ypred1 #prediction

array([0.5184])

Done

https://colab.research.google.com/drive/1KKIA-THucYMImdBcdRoucGtcC479M2c9#printMode=true 5/5

You might also like