-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainfallprediction.py
115 lines (78 loc) · 3.07 KB
/
rainfallprediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""RainfallPrediction.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/14JI6oNeuwBzQ1y27eoMqtk7ZGTTXn0mq
**Importing libraries**
"""
import numpy as np
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
"""**import dataset**"""
dataset=pd.read_csv('weatherAUS.csv')
X=dataset.iloc[:,[1,2,3,4,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]].values
# here : represent all the rows , numbers represents the columns
Y=dataset.iloc[:,-1].values
# here this command returns one dimensional array
# Y=dataset.iloc[:,22:].values
# here this command returns two dimensional array
X
Y
Y=Y.reshape(-1,1)
# Here this command convert one dimensional array to two dimensional array.Because train test splitting algorithm only accept 2 dimensional lists
Y
"""**Cleaning and preprocessing the dataset**"""
imputer =SimpleImputer(missing_values=np.nan,strategy='most_frequent')
# here for all the missing values we substitute most most_frequent(mode) value of each column
X=imputer.fit_transform(X)
Y=imputer.fit_transform(Y)
"""**Label encoding**"""
encoder=LabelEncoder()
X[:,0]=encoder.fit_transform(X[:,0])
X[:,4]=encoder.fit_transform(X[:,4])
X[:,6]=encoder.fit_transform(X[:,6])
X[:,7]=encoder.fit_transform(X[:,7])
X[:,-1]=encoder.fit_transform(X[:,-1])
encoder2=LabelEncoder()
Y=encoder2.fit_transform(Y)
"""**Feature scaling** - feature scaling done for minimize the data range/scale.So we can analyze the data within range(-3 to +3)"""
sc=StandardScaler()
X=sc.fit_transform(X)
X
"""**Splitting data to training and testing**"""
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=0)
X_train
"""**Lets train the model** - Here we must try with several classificationtraining algorithms like SVM,decision tree,Random forest,etc.the best results were produced by random forest algorithm.So lets use it"""
model=RandomForestClassifier(n_estimators=100,random_state=0)
model.fit(X_train,Y_train)
model.score(X_train,Y_train)
"""**Lets evaluate**"""
predictions=model.predict(X_test)
predictions
# score=accuracy_score(Y_test,predictions)
# score
print(predictions)
print(Y_test)
# To graphically compare yes no we will have to convert Y_test and predictions from 1/0 to yes/no again using label encoder function
predictions=encoder2.inverse_transform(predictions)
Y_test=encoder2.inverse_transform(Y_test)
print(predictions)
print(Y_test)
predictions=predictions.reshape(-1,1)
Y_test=Y_test.reshape(-1,1)
# here we convert 1D array to 2D array
print(predictions)
print(Y_test)
df=np.concatenate((Y_test,predictions),axis=1)
print(df)
dataframe=pd.DataFrame(df,columns=['Rain on tommorow','Prediction on tommorow'])
print(dataframe)
score=accuracy_score(Y_test,predictions)
score
"""**Download the results**"""
dataframe.to_csv('result.csv')