我的代码在泰坦尼克号数据集上,这是我的代码。 执行预测后的形状,我什至试图在密函数的第一层中更改input_dim。而且有必要对Y_train进行缩放,因为很有可能也会对预测中的数据进行缩放。
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.preprocessing import StandardScaler
import time
from datetime import timedelta#combination of date and time
dataset_train=pd.read_csv("train.csv")
dataset_test=pd.read_csv("test.csv")
#Filling the missing values
def data(df):
df=df.drop(["Name","Ticket","Cabin"],axis=1)
df['Age']=df['Age'].fillna(value=df['Age'].mean())
df['Fare']=df['Fare'].fillna(value=df['Fare'].mean())
df['Embarked'] =
df['Embarked'].fillna(value=df['Embarked'].
value_counts().idxmax())
#Mapping male and female
df['Sex']=df['Sex'].map({'female':0,'male':1}).astype(int
one_hot_vector=pd.get_dummies(df['Embarked'],prefix='Embarked')
df=df.drop('Embarked',axis=1)
df=df.join(one_hot_vector)
return
dataset_train=dat
a(dataset_train)
#Splitting the dataset
X=dataset_train.drop(['Survived'],axis=1).values
scale=StandardScaler()
X=scale.fit_transform(X)
Y=dataset_train['Survived'].values
def create_model(optimizer="adam",init="uniform"):
model=Sequential()
model.add(Dense(20,input_dim=X.shape[1],
kernel_initializer='normal',activation="relu"))
model.add(Dense(8,kernel_initializer='normal',activation="relu"))
model.add(Dense(4,kernel_initializer='normal',activation="relu"))
model.add(Dense(1,kernel_initializer='normal',
activation="sigmoid"))
#Compiling the model
model.compile(loss="binary_crossentropy",
optimizer=optimizer,metric=["accuracy"])
return model
estimator=KerasClassifier(build_fn=create_model,
epochs=200,batch_size=5,verbose=1,
init='glorot_uniform',optimizer='rmsprop')
estimator.fit(X,Y)
test_df=data(dataset_test)
validation_df=scale.fit_transform(test_df)
data=validation_df[:,0]
prediction=estimator.predict(data)