![](/img/trans.png)
[英]How to pass list from one function to another without using global variable?
[英]Pass from one file to another a constant variable, without using function parameters
我有以下代码在生成的表格数据帧上生成概率标签。
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV, cross_val_score
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import Adam
from sklearn.metrics import accuracy_score
def baseline_model(optimizer='adam', learn_rate=0.1):
model = Sequential()
model.add(Dense(100, input_dim=X.shape[1], activation='relu'))
model.add(Dense(50, activation='relu')) # 8 is the dim/ the number of hidden units (units are the kernel)
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
def get_probability_labels(x, y, optimizer='adam'):
all_predictions = []
cv_5 = StratifiedKFold(n_splits=5, random_state=None, shuffle=False)
estimator = KerasClassifier(optimizer=optimizer, batch_size=32, epochs=100, build_fn=baseline_model, verbose=0)
for train_index, test_index in cv_5.split(x, y):
X_train, X_test = x.iloc[train_index], x.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
estimator.fit(X_train, y_train)
predictions = estimator.predict(X_test)
all_predictions.append(predictions)
a = [j for i in all_predictions for j in i] #remove nested list
return a
def add_labels(real_data, synthetic_data):
# add labels 0 for real and 1 for synthetic
data = pd.concat([real_data, synthetic_data], ignore_index=True)
o_labels = np.zeros((len(real_data)), dtype=int)
s_labels = np.ones((len(synthetic_data)), dtype=int)
labels = np.concatenate([o_labels, s_labels], axis=0)
data['class'] = labels
x = data.drop('class', axis=1)
y = data['class']
return x, y
# other file
def main():
X, Y = add_labels(df, df_synth)
probability_labels = get_probability_labels(X, Y)
print(probability_labels)
我已经根据上述问题修复了以下代码。 错误优化器参数在 Keras function 中不合法
问题是我无法将X
作为参数添加到baseline_model
function(生成上一篇文章中修复的错误)。 但是baseline_model
与X.shape[1]
一起使用。 考虑到 main 在另一个文件中并且我不能将它作为参数传递给baseline_model
function,我怎么能以某种方式接收该值?
使用全局变量 X。
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV, cross_val_score
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.optimizers import Adam
from sklearn.metrics import accuracy_score
X = None
def baseline_model(optimizer='adam', learn_rate=0.1):
model = Sequential()
model.add(Dense(100, input_dim=X.shape[1], activation='relu'))
model.add(Dense(50, activation='relu')) # 8 is the dim/ the number of hidden units (units are the kernel)
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
def get_probability_labels(x, y, optimizer='adam'):
global X
X = x
all_predictions = []
cv_5 = StratifiedKFold(n_splits=5, random_state=None, shuffle=False)
estimator = KerasClassifier(optimizer=optimizer, batch_size=32, epochs=100, build_fn=baseline_model, verbose=0)
for train_index, test_index in cv_5.split(x, y):
X_train, X_test = x.iloc[train_index], x.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
estimator.fit(X_train, y_train)
predictions = estimator.predict(X_test)
all_predictions.append(predictions)
a = [j for i in all_predictions for j in i] #remove nested list
return a
def add_labels(real_data, synthetic_data):
# add labels 0 for real and 1 for synthetic
data = pd.concat([real_data, synthetic_data], ignore_index=True)
o_labels = np.zeros((len(real_data)), dtype=int)
s_labels = np.ones((len(synthetic_data)), dtype=int)
labels = np.concatenate([o_labels, s_labels], axis=0)
data['class'] = labels
x = data.drop('class', axis=1)
y = data['class']
return x, y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.