import os os.environ['CUDA_VISIBLE_DEVICES'] = "0" from mlp_clf import MLPClassifier import numpy as np import tensorflow as tf from sklearn.datasets import load_svmlight_file #from scipy.sparse import csr_matrix def mean(numbers): #计算平均值 s = 0.0 for num in numbers: s = s + num return s/len(numbers) def dev(numbers, mean): #计算方差 sdev = 0.0 for num in numbers: sdev = sdev + (num - mean)**2 return pow(sdev / (len(numbers)-1), 0.5) if __name__ == '__main__': mze_array=[] mae_array=[] mse_array=[] trainfile = "test_housing.0" testfile = "train_housing.0" X_train0, y_train = load_svmlight_file(trainfile) X_test0 , y_test = load_svmlight_file(testfile) X_train = X_train0.todense() X_test = X_test0.todense() attribute = np.shape(X_train)[-1]#tf.shape(X_train) n_class = tf.reduce_max(y_train) print("n_class:", n_class) y_train = y_train - 1 y_test = y_test - 1 clf = MLPClassifier(attribute, 5, [100]*3) log = clf.fit(X_train, y_train, n_epoch=1000, val_data=(X_test, y_test)) Y_pred = clf.predict(X_test) mze = 1- (Y_pred == y_test).mean() mae = np.abs(Y_pred.ravel()-y_test.ravel()).astype(float).mean() mse = np.power(Y_pred.ravel()-y_test.ravel(),2).astype(float).mean() print("mze: %.4f" % mze) print("mae:", mae) print("mse:", mse)