每次精度都很高,但最终的预测是错误的

问题描述 投票:0回答:1

我正在尝试通过输入温度,土壤湿度,pH和平均降雨量来预测农作物的名称。而且准确率始终很高,即每次的准确率范围从88%到94%。但是经过预测的最终结果总是错误的。这是代码:

#importing the required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

#Reading the csv file
data=pd.read_csv('cpdata.csv')

#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))
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.3)

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()

#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)

ah=89.41
atemp=26.98
shum=28
pH=6.26
rain=58.54


l=[]
l.append(atemp)
l.append(ah)
l.append(pH)
l.append(rain)
predictcrop=[l]

# Putting the names of crop in a single list
crops=['rice','wheat','mungbean','Tea','millet','maize','lentil','jute','cofee','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','pomegranate','watermelon']
cr='rice'

#Predicting the crop
predictions = clf.predict(predictcrop)
count=0
for i in range(0,31):
    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)

我得到的输出是-

The accuracy of this model is:  90.43010752688173
The predicted crop is apple

尽管我输入了其他任何作物的确切值,但每次都会得到苹果或芒果。

请帮助。

python machine-learning prediction predict
1个回答
1
投票

还将定标器也应用于新数据以进行预测。如果没有您的数据,我将无法测试它,但是它看起来应该像这样:

datascaled = sc.transform(predictcrop)
predictions = clf.predict(datascaled)

为了以后将缩放器也应用于新数据,需要保存它:

from sklearn.externals.joblib import dump, load
dump(sc, 'scaler.bin', compress=True)

及以后:

sc=load('scaler.bin')
© www.soinside.com 2019 - 2024. All rights reserved.