我是神经网络和keras的完全新手,我正在尝试使用Keras构建神经网络分类器,以根据高度预测性别。当我运行以下代码时,每次迭代的准确性都没有提高,预测的类总是为0.
由于这是一项家庭作业,教授选择将数据保密,但我的数据仅包含一个带有两列的pandas DataFrame:性别和身高,其中性别被编码为一串女性&# 39;和男性'和高度是一个以英寸为单位的数字。
from __future__ import division
import pandas as pd
import numpy as np
import operator
import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from sklearn.preprocessing import LabelEncoder
# encode class values as integers
def encode_Y(Y):
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
return(encoded_Y)
def NN(X_data):
seed = 7
numpy.random.seed(seed)
X = X_data.values.astype(float)
Y = train["gender"].values
#change Y from ['female', 'male'] format to 0, 1 encoding
encoded_Y = encode_Y(Y)
# create model
model = Sequential()
model.add(Dense(1, input_dim=1, init='uniform', activation='relu'))
model.add(Dense(10, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
opt = SGD(lr=0.001)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics ['accuracy'])
model.fit(X, encoded_Y, batch_size=10, nb_epoch=150, verbose=0)
return model
height_model = NN(train["height"])
def acc_rate(model, dataX, dataY):
train_pred = model.predict_classes(dataX.values.astype(float)).flatten()
encoded_Y = encode_Y(dataY)
return sum(train_pred == encoded_Y)/len(train_pred)
acc_rate(height_model, train["height"], train["gender"])
我尝试过的可能的问题/方法: