def fit(self, x, y): self.random_val = None self.x_copy = None self.y_copy = None # perform training with multiple random starts # get the initial state of the RNG mae = 100000000 for i in range(10): st0 = np.random.get_state() temp_reg = NN(**self.get_params()).fit(x, y) mae_ = np.abs(y - temp_reg.predict(x)).mean() if mae > mae_: mae = mae_ self.random_val = st0 self.x_copy = x self.y_copy = y self.optim_model = temp_reg self.set_params(**self.optim_model.get_params()) self.MSE = mean_squared_error(self.y_copy, self.predict(self.x_copy)) self.MAE = np.abs(self.y_copy - self.predict(self.x_copy)).mean() self.R2 = self.optim_model.score(self.x_copy, self.y_copy) print('MSE: ', self.MSE) print('R2: ', self.R2) print('MAE: ', mae) return self
def __init__(self, randomize=True, params={}): if randomize == True: self.params = self.Random_individual() else: self.params = params self.model = NN(hidden_layer_sizes=self.params["hidden_layer_sizes"], activation=self.params["activation"], solver=self.params["solver"], alpha=self.params["alpha"], learning_rate=self.params["learning_rate"], learning_rate_init=self.params["learning_rate_init"], max_iter=self.params["max_iter"])
] #[0.0002, 0.0003, 0.0004, 0.0005, # for evaluating against train size train_performance = [] val_performance = [] test_performance = [] features = None encodings = None for train_frac in train_fracs: acc = [] clfs = [] for hid_layer_specific in hid_layers: clf = NN(hid_layer_specific, activation='relu') if hid_layer_specific == hid_layers[0] and train_frac == train_fracs[0]: features, encodings, ((x_train, y_train), (x_val, y_val), (x_test, y_test)) = process(all_data, train_frac, val_frac, test_frac, modify=True) else: _, _, ((x_train, y_train), (x_val, y_val), (x_test, y_test)) = process(all_data, train_frac, val_frac, test_frac, features=features, encodings=encodings)
img = trans.resize(img, (300, 400), mode='constant') img_converts.append(img) X = np.array(img_converts) # Split into train and test vars trainX, testX, trainY, testY = train_test_split(X, Y, test_size=0.17) # Reshape the image arrays into 2-D arrays so it will fit the model xa, xb, xc, xd = trainX.shape d2_trainX = np.asarray(trainX.reshape((xa, xb * xc * xd)), dtype=np.float) xe, xf, xg, xh = testX.shape d2_testX = np.asarray(testX.reshape((xe, xf * xg * xh)), dtype=np.float) clf = NN(solver='lbfgs', hidden_layer_sizes=(5, 5, 5), random_state=42, verbose=True) # Recast the Y data so the fit won't get a label mismatch trainY = np.asarray(trainY, dtype=np.integer) testY = np.asarray(testY, dtype=np.integer) print('The machine is learning...') clf.fit(d2_trainX, trainY) print('Predicting...') count = 1, for line in clf.predict(d2_testX): for i in range(len(line)): if int(line[i]) == 1:
Y = np.genfromtxt('data/Y_train.txt') X = np.genfromtxt('data/X_train.txt') Xte = np.genfromtxt('data/X_test.txt') X, Y = ml.shuffleData(X, Y) scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) Xte = scaler.transform(Xte) Xtr = X[0:40000] Ytr = Y[0:40000] Xtest = X[180000:] Ytest = Y[180000:] clf = NN(activation='tanh', alpha=0.1, hidden_layer_sizes=(80, )) gbm0 = RandomForestClassifier(n_estimators=300, max_depth=15, max_features=3) clf.fit(Xtr, Ytr) gbm0.fit(Xtr, Ytr) YteNN = clf.predict_proba(Xtest)[:, 1] YteRF = gbm0.predict_proba(Xtest)[:, 1] wt = [0.6, 0.65, 0.7, 0.75, 0.8] auc = [] for i, k in enumerate(wt): Yfinal = k * YteNN + (1 - k) * YteRF temp = roc_auc_score(Ytest, Yfinal) auc.append(temp) print auc plt.plot(wt, auc, marker='o')
trainingLabels) # Creates LDA Model ldaModel = LDA().fit(training, trainingLabels) # Creates LDA Model using PCA ldaModelPca = LDA().fit(pcaTraining, trainingLabels) # Creates SVM Model svmModel = LinearSVC().fit(training, trainingLabels) # Creates SVM Model using PCA svmModelPca = LinearSVC().fit(pcaTraining, trainingLabels) # Creates NN Model nnModel = NN(activation="identity").fit(training, trainingLabels) #nnModelPca = NN(activation="identity", solver='adam').fit(pcaTraining, trainingLabels) # Creates confusion matrix table def evaluate(predictions, correct): tableCategories = { 1: 'airplane', 2: 'automobile', 3: 'bird', 4: 'cat', 5: 'deer', 6: 'dog', 7: 'frog', 8: 'horse',
test_frac = 0.2 hid_layers = (5, ) train_frac = 0.7 #[0.0002, 0.0003, 0.0004, 0.0005, # for evaluating against train size train_performance = [] val_performance = [] test_performance = [] features = None encodings = None clf = NN(hid_layers, activation='relu') features, encodings, ((x_train, y_train), (x_val, y_val), (x_test, y_test)) = process(all_data, train_frac, val_frac, test_frac, modify=True) print(x_train.shape, y_train.shape) clf.fit(x_train, y_train) print("Simulated annealing:") acc_anneal = [] test_anneal = [] clf.coefs_ = [] clf.intercepts_ = []