【问题标题】:Accuracy is high everytime but the resulting prediction is wrong每次准确率都很高,但结果预测是错误的
【发布时间】:2020-05-17 04:43:11
【问题描述】:

我正在尝试通过输入温度、土壤湿度、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

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

请帮忙。

【问题讨论】:

  • 您的数据集中有多少示例?我有点困惑如何将数据集拆分为训练集和测试集,通常应该是这样的:train_x, train_y, test_x, test_y = train_test_split(data,labels,test_size=0.3)

标签: python machine-learning prediction predict


【解决方案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')

【讨论】:

  • 非常感谢!!你帮了大忙!
猜你喜欢
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 2020-09-28
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多