首页 > 解决方案 > 我对 ANN 的准确性并不完美

问题描述

crop: object = CropData(path= '')    
crop.dataset: pd.DataFrame = CropAnalysis.rename_columns(dataset= 
crop.dataset)
crop.dataset.head()


data_charaterictics: Generator = 
CropAnalysis.data_characteristics(dataset= crop.dataset)
while True:
try:
    print('-'*100)
    print(data_charaterictics.__next__())
except StopIteration: break


unique_values: Generator = 
CropAnalysis.data_unique_values(dataset= crop.dataset)
while True:
try:
    print('-'*100)
    print(unique_values.__next__())
except StopIteration: break

target_classification: Generator = 
CropAnalysis.target_classification_count(dataset= crop.dataset, 
target= 'crop') 
while True:
try:
    print('-'*100)
    print(target_classification.__next__())
except StopIteration: break

crop.dataset.keys()

numeric_histoplots: Generator = 
CropAnalysis.histograms_numeric_features(dataset= crop.dataset,
                                                                     
numeric_features= [
                                                                         
'Nitrogen', 'Phosphorus',
                                                                         
'Potassium', 'Temp','hum', 'PH','Rain'
                                                                        
]    )
while True:
try: numeric_histoplots.__next__()
except AttributeError: break

crop.dataset: pd.DataFrame = 
CropPreprocess.change_object_to_str(dataset= crop.dataset, cols= 
['crop'])
crop.dataset: pd.DataFrame = 
CropPreprocess.encode_features(dataset= crop.dataset)
crop.dataset

X, y = crop.dataset.drop('crop', axis= 1), crop.dataset['crop']
from sklearn import preprocessing
normalizer = preprocessing.Normalizer()
normalized_train_X = normalizer.fit_transform(X_train)
normalized_train_X

from tensorflow.keras import utils
from tensorflow.keras.utils import to_categorical
one_hot_y_train = to_categorical(y_train)
one_hot_y_test = to_categorical(y_test)


from sklearn.model_selection import train_test_split as tts
X_train,X_test,y_train,y_test=tts(X,y,test_size=0.2)

 from tensorflow import keras

 from tensorflow.keras import layers , Sequential 

` 从 keras.layers 导入密集

import keras
from keras.models import Sequential
from keras.layers import Dense
# Neural network
model = Sequential()
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(22, activation='softmax'))


model.compile(optimizer = 'adam', loss = 
'categorical_crossentropy', metrics = ['accuracy'])

model.fit(X_train, y_train, batch_size = 25, epochs = 100)

准确率和损失的值小到0.04,这与机器学习算法不一样,数据的大小也很大,比如2200*8,不小。帮我找出数据有什么问题这里提供了数据集https://www.kaggle.com/atharvaingle/crop-recommendation-dataset

标签: pythontensorflowkeras

解决方案


您需要向输出层添加更多神经元。目前,您正在预测是否使用单一作物。但在 Kaggle 数据集中,标签实际上是 22 种不同的作物,您想决定哪种作物最适合手头的土壤。所以你的输出层需要22个神经元,即将你的最后一层更改为

model.add(Dense(22, activation='softmax'))

由此产生的预测将表明哪种作物最适合土壤。

确保这y_train,y_test是一个热向量。如果还没有,您可以使用 fe 转换它们one_hot_y_train = to_categorical(y_train)


推荐阅读