Exemple #1
0
 def test_rmse(self):
     actual = np.array([0, 1, 2, 3])
     predicted = np.array([3, 2, 1, 0])
     self.assertEqual(
         asfloat(np.sqrt(5)),
         estimators.rmse(actual, predicted)
     )
Exemple #2
0
 def test_rmse(self):
     actual = np.array([0, 1, 2, 3])
     predicted = np.array([3, 2, 1, 0])
     self.assertEqual(
         asfloat(np.sqrt(5)),
         estimators.rmse(actual, predicted)
     )
def analyze(feature):
    """ returns the 4 regression analysis results"""
    dataset, _ = read_dataset()

    if feature == "maxtemp":
        X, Y = get_features_max_temp(dataset)
    elif feature == "mintemp":
        X, Y = get_features_min_temp(dataset)
    elif feature == "wind":
        X, Y = get_features_wind_speed(dataset)
    elif feature == "percipitation":
        X, Y = get_features_percipitation(dataset)
    else:
        raise Exception("Invalid feature option")

    # split dataset into train and test using sklearn
    x_train, x_test, y_train, y_test = split_dataset(X, Y)

    # train GRNN
    nw = train_GRNN(x_train, y_train)
    y_predicted = nw.predict(x_test)
    rmse = estimators.rmse(y_predicted, y_test)

    return rmse
Exemple #4
0
    layers.join(
        layers.Input(58),
        layers.Softmax(22),
        layers.Softmax(1),
    ),
    layers.join(
        layers.Input(58),
        layers.Relu(60),
        layers.Relu(40),
        layers.Softmax(22),
        layers.Softmax(1),
    ),
    layers.join(
        layers.Input(58),
        layers.Tanh(12),
        layers.Tanh(25),
        layers.Tanh(1),
    ),
])

network
gdnet = algorithms.Adam(network, verbose=True)
print("Start moe training")

gdnet.train(x_train, y_train, epochs=500)

gresult = gdnet.predict(x_test)

gerror = estimators.rmse(gresult, y_test)
print(gerror)
Exemple #5
0
def scorer(actual, predicted):
    return estimators.rmse(predicted, actual)
Exemple #6
0
# best_error = 99
# for std_in in np.array([0.01, 0.1, 0.5, 0.8, 1.0, 1.2, 1.4, 2.0, 5.0, 10.0]):
#     grnnet = algorithms.GRNN(std=std_in, verbose=True)
#     grnnet.train(training_X, training_Y)
#     predicted = grnnet.predict(testing_X)
#     error = scorer(testing_Y, predicted)
#     print("GRNN RMSE = {:.5f}\n".format(error))
#     if error < best_error:
#         print("New Best Error Found: " + str(error))
#         best_error = error
#         best_model = grnnet

grnnet2 = algorithms.GRNN(std=0.2, verbose=True)
grnnet2.train(training_X, training_Y)
y_predicted = grnnet2.predict(testing_X)
print("RMSE = " + str(estimators.rmse(y_predicted, testing_Y.ravel())))
print("MAE = " + str(estimators.mae(y_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(
    estimators.mae(y_predicted, testing_Y))
print("MAE (no. of shares) = " + str(actual_mae.squeeze()))

# Save the best GRNN model
import _pickle
with open(
        '/home/pier/Machine_Learning/KE5206NN/regression/regression_models/grnn.pkl',
        'wb') as fid:
    _pickle.dump(grnnet2, fid)

# GRNN Best values
#
# RMSE = 0.019625235702837703
Exemple #7
0
                                    y_train2,
                                    batch_size=128,
                                    epochs=3,
                                    validation_split=0.05)


def plot_predict(predicted_data, true_data):
    fig = plt.figure(facecolor='white')
    ax = fig.add_subplot(111)
    ax.plot(true_data, label='True Data')
    plt.plot(predicted_data, label='Prediction')
    plt.title('LSTM model loss')
    plt.legend()
    plt.show()
    #plot train and validation for LSTM model


lstm_model_preds = lstm_model.predict(X_test2)
lstm_model_rmse = np.sqrt(mean_squared_error(lstm_model_preds, y_test2))
plot_predict(lstm_model_preds, y_test2)
plotHistory(lstm_model_history, "LSTM Model", "loss", "epochs")
plotHistory(ann_model_history, "ANN Model", "lose", "epochs")
from neupy import algorithms, estimators, environment
environment.reproducible()
grnn_model = algorithms.GRNN(std=0.1, verbose=False)
grnn_model.train(X_train, y_train)
grnn_predicted = grnn_model.predict(X_test)
grnn_model_rmse = estimators.rmse(grnn_predicted, y_test)
print('ANN RMSE', ann_model_rmse)
print('LSTM RMSE', lstm_model_rmse)
print('GRNN RMSE', grnn_model_rmse)
Exemple #8
0
X1 = reduced_X_combine[0:10778, :]
X2 = reduced_X_combine[10778:, :]

print(X1.shape)
print(X2.shape)
print(pca.explained_variance_ratio_)

nw = algorithms.GRNN(std=0.013, verbose=False)
nw.train(X1, y)
prediction = nw.predict(X2)
error = scorer(nw, X2, y_valid)
print("GRNN RMSLE = {:.3f}\n".format(error))

real = y_valid.flatten()
pred = prediction.flatten()

rmse = estimators.rmse(pred, real)
print("the rmse is :", rmse)

for i in range(len(y_valid)):
    print('real      :', real[i])
    print('prediction:', pred[i])
    print('*' * 30)

diff = abs(real - pred)
right = diff[diff <= 0.001]
acc = len(right) / len(y_valid)

print("准确率为:")
print(acc)
Exemple #9
0
#remove recent news (less than 2 months)
df = df[df[' timedelta'] > 60]

#Conduct PCA

data = df[df.columns[2:60]]

target = df[' shares'].ravel()

data_norm = StandardScaler().fit_transform(data)

x_train, x_test, y_train, y_test = train_test_split(data_norm,
                                                    target,
                                                    test_size=0.3)

grnnet = algorithms.GRNN(std=0.5, verbose=True)
grnnet.train(x_train, y_train)

result = grnnet.predict(x_test)

if np.isnan(result).any():
    clean = np.nan_to_num(result)

error = estimators.rmse(clean, y_test)

print("GRNN RMSE = {}\n".format(error))

r2_score = metrics.r2_score(clean, y_test)

print("GRNN R_SCORE = {}\n".format(r2_score))
Exemple #10
0
from sklearn.grid_search import RandomizedSearchCV
from neupy import estimators
import _pickle

with open(
        '/Users/pierlim/PycharmProjects/KE5206NN/regression/regression_models/multi_layer_perceptron.pkl',
        'rb') as fid:
    mlp = _pickle.load(fid)

with open(
        '/Users/pierlim/PycharmProjects/KE5206NN/regression/regression_models/grnn.pkl',
        'rb') as fid:
    grnn = _pickle.load(fid)

y_mlp_predicted = mlp.predict(testing_X)
print("MLP RMSE = " + str(estimators.rmse(y_mlp_predicted, testing_Y.ravel())))
mlp_mae = estimators.mae(y_mlp_predicted, testing_Y.ravel())
print("MLP MAE = " + str(estimators.mae(y_mlp_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(
    estimators.mae(y_mlp_predicted, testing_Y))
print("MLP MAE (no. of shares) = " + str(actual_mae.squeeze()))

y_grnn_predicted = grnn.predict(testing_X)
y_grnn_predicted = y_grnn_predicted.ravel().transpose()
grnn_mae = estimators.mae(y_grnn_predicted, testing_Y.ravel())
print("GRNN RMSE = " +
      str(estimators.rmse(y_grnn_predicted, testing_Y.ravel())))
print("GRNN MAE = " + str(estimators.mae(y_grnn_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(
    estimators.mae(y_grnn_predicted, testing_Y))
print("GRNN MAE (no. of shares) = " + str(actual_mae.squeeze()))
Exemple #11
0
        layers.Softmax(22),
        layers.Softmax(1),
    ),
    layers.join(
        layers.Input(58),
        layers.Relu(60),
        layers.Relu(40),
        layers.Softmax(22),
        layers.Softmax(1),
    ),
    layers.join(
        layers.Input(58),
        layers.Tanh(12),
        layers.Tanh(25),
        layers.Tanh(1),
    ),
])
network
gdnet = algorithms.Adam(network, verbose=True)
gdnet.fit(data_norm, target, epochs=500)

predicted = cross_val_predict(gdnet, data_norm, target, cv=5)

error = estimators.rmse(target, predicted)

print("MOE RMSE = {}\n".format(error))

r2_score = metrics.r2_score(target, predicted)

print("MOE R_SCORE = {}\n".format(r2_score))
Exemple #12
0
#Conduct PCA

data = df[df.columns[2:60]]

target = df[' shares'].ravel()

data_norm = StandardScaler().fit_transform(data)

grnnet = algorithms.GRNN(std=0.5, verbose=True)
scoring = ['precision_macro', 'recall_macro']

predicted = cross_val_predict(grnnet, data_norm, target, cv=5)
clean = np.nan_to_num(predicted)

error = estimators.rmse(target, clean)

error
'''
if np.isnan(result).any():
    clean = np.nan_to_num(result)


error = estimators.rmse(clean, y_test)

print("GRNN RMSE = {}\n".format(error))

r2_score = metrics.r2_score(clean, y_test)

print("GRNN R_SCORE = {}\n".format(r2_score))
print(grnn_nw)

# In[10]:

grnn_nw.train(X1_train, y1_train)

# In[11]:

y1_predicted = grnn_nw.predict(X1_test).round()

y1_predicted[0]

# In[12]:

#accuracy
estimators.rmse(y1_predicted, y1_test)

# In[13]:

#confusion matrix
confusion_matrix(y1_test, y1_predicted)

# In[14]:

from sklearn.metrics import accuracy_score
grnn_acc_score = accuracy_score(y1_test, y1_predicted)
print("Grnn accuracy score ", grnn_acc_score)

#  ## 1b.  PNN Network - [ Diabities Problem ]
#