python - 我对 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
解决方案
您需要向输出层添加更多神经元。目前,您正在预测是否使用单一作物。但在 Kaggle 数据集中,标签实际上是 22 种不同的作物,您想决定哪种作物最适合手头的土壤。所以你的输出层需要22个神经元,即将你的最后一层更改为
model.add(Dense(22, activation='softmax'))
由此产生的预测将表明哪种作物最适合土壤。
确保这y_train,y_test
是一个热向量。如果还没有,您可以使用 fe 转换它们one_hot_y_train = to_categorical(y_train)
推荐阅读
- fragment - sap.ui.core.Fragment.load 返回未定义
- c# - 如何使用 LINQ 在我的数据库中的调查中找到带有答案的问题数量
- python - 单击表单上的提交后未重定向到 Home.html 页面
- ruby-on-rails - 活动管理员嵌套资源表单生成
- ruby-on-rails - 如何进行安全的 Rails PayPal 支付?
- javascript - 如何使非 Firebase 用户无法访问页面?
- codeigniter - 如何使用 mvc 结构使用 codeigniter 查看更新?
- javascript - 如何将二维数组转换为对象
- unity3d - AudioSource.PlayOneShot 音量增加和削波
- node-red - 如何运行节点红色流客户端?