-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
109 lines (83 loc) · 2.97 KB
/
temp.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
#importing the required libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
#Reading the csv file
data=pd.read_csv("./cpdata.csv")
print(data.head(1))
#Creating dummy variable for target i.e label
label= pd.get_dummies(data.label).iloc[: , 1:]
data= pd.concat([data,label],axis=1)
data.drop('label', axis=1,inplace=True)
print('The data present in one row of the dataset is')
print(data.head(1))
# # Import label encoder
# from sklearn import preprocessing
# # label_encoder object knows how to understand word labels.
# label_encoder = preprocessing.LabelEncoder()
# #storing targetvariables
# target_variables=data['label'].unique()
# print(target_variables)
# # Encode labels in column 'label'.
# data['label']= label_encoder.fit_transform(data['label'])
# print(data['label'].unique())
train=data.iloc[:, 0:4].values
test=data.iloc[: ,4:].values
#Dividing the data into training and test set
X_train,X_test,y_train,y_test=train_test_split(train,test,test_size=0.2)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# # Importing Decision Tree classifier
# from sklearn.tree import DecisionTreeRegressor
# clf=DecisionTreeRegressor()
#importing KNeighboursClassifier
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=30)
clf.fit(X_train, y_train)
#Fitting the classifier into training set
clf.fit(X_train,y_train)
pred=clf.predict(X_test)
from sklearn.metrics import accuracy_score
# Finding the accuracy of the model
a=accuracy_score(y_test,pred)
print("The accuracy of this model is: ", a*100)
l=[[23,36,6.9,280],[36,80,6.8,150]]
print(clf.predict(l))
# #Using firebase to import data to be tested
# from firebase import firebase
# firebase =firebase.FirebaseApplication('https://cropit-eb156.firebaseio.com/')
# tp=firebase.get('/Realtime',None)
#importing pickle
import pickle
pickle.dump(clf,open('models/final_prediction.pickle','wb'))
# ah=tp['Air Humidity']
# atemp=tp['Air Temp']
# shum=tp['Soil Humidity']
# pH=tp['Soil pH']
# rain=tp['Rainfall']
# l=[]
# l.append(ah)
# l.append(atemp)
# l.append(pH)
# l.append(rain)
# predictcrop=[l]
# # Putting the names of crop in a single list
# crops=['wheat','mungbean','Tea','millet','maize','lentil','jute','coffee','cotton','ground nut','peas','rubber','sugarcane','tobacco','kidney beans','moth beans','coconut','blackgram','adzuki beans','pigeon peas','chick peas','banana','grapes','apple','mango','muskmelon','orange','papaya','watermelon','pomegranate']
# cr='rice'
# #Predicting the crop
# predictions = clf.predict(predictcrop)
# count=0
# for i in range(0,30):
# if(predictions[0][i]==1):
# c=crops[i]
# count=count+1
# break
# i=i+1
# if(count==0):
# print('The predicted crop is %s'%cr)
# else:
# print('The predicted crop is %s'%c)
#Sending the predicted crop to database
#cp=firebase.put('/croppredicted','crop',c)