Skip to content

Instantly share code, notes, and snippets.

@lgmoneda
Created June 25, 2017 03:28
Show Gist options
  • Save lgmoneda/af32cb2d759d47c4b72bae641afc5014 to your computer and use it in GitHub Desktop.
Save lgmoneda/af32cb2d759d47c4b72bae641afc5014 to your computer and use it in GitHub Desktop.
Variance with Neural Network
import numpy as np
import random
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
def gen_variance_dataset(n_elements, n_features):
n_rows = int(n_elements / n_features)
data_matrix = np.random.randint(1, 100, n_rows * n_features).reshape((n_rows, n_features))
data = pd.DataFrame(data_matrix, columns=["feature_" + str(i) for i in range(n_features)])
data["target"] = np.std(data_matrix, axis=1)
return data
def build_model(n_features, n_hidden=10):
model = Sequential()
model.add(Dense(n_features, input_dim=n_features, kernel_initializer='normal', activation='relu'))
for i in range(n_hidden):
model.add(Dense(n_features,
activation="linear",
kernel_initializer="normal"))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mean_squared_error', optimizer='adam')
return model
data = gen_variance_dataset(100000, 5)
data.to_csv("variance_data.csv")
features = [col for col in data.columns if col != "target"]
model = build_model(len(features), 12)
X_train, X_test, y_train, y_test = train_test_split(data[features],
data["target"],
test_size=0.2)
model.fit(X_train.as_matrix(),
y_train.as_matrix(),
epochs=1000,
batch_size=256,
verbose=1)
evaluate = model.evaluate(X_test.as_matrix(),
y_test.as_matrix())
print("\n\nEvaluation {}".format(evaluate))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment