Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource



Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:


Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO



from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from pandas import read_sql_query
from pandas import datetime
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
import numpy
import MySQLdb
import sys

#definiciones db
sql_hn      = "********"
sql_p       = 3306
sql_uid     = "********"
sql_pwd     = "********"
sql_db      = "********"

#conexión db
conn = MySQLdb.connect(
            host    = sql_hn,
            port    = sql_p,
            user    = sql_uid,
            passwd  = sql_pwd,
            db      = sql_db

cursor = conn.cursor()

def imprimir(text):

# date-time parsing function for loading the dataset
def parser(x):
	return datetime.strptime(x, '%Y-%m-%d %H:%M:%S')

# frame a sequence as a supervised learning problem
def timeseries_to_supervised(data, lag=1):
	df = DataFrame(data)
	columns = [df.shift(i) for i in range(1, lag+1)]
	df = concat(columns, axis=1)
	df.fillna(0, inplace=True)
	return df

# create a differenced series
def difference(dataset, interval=1):
	diff = list()
	for i in range(interval, len(dataset)):
		value = dataset[i] - dataset[i - interval]
	return Series(diff)

# invert differenced value
def inverse_difference(history, yhat, interval=1):
	return yhat + history[-interval]

# scale train and test data to [-1, 1]
def scale(train, test):
	# fit scaler
	scaler = MinMaxScaler(feature_range=(-1, 1))
	scaler =
	# transform train
	train = train.reshape(train.shape[0], train.shape[1])
	train_scaled = scaler.transform(train)
	# transform test
	test = test.reshape(test.shape[0], test.shape[1])
	test_scaled = scaler.transform(test)
	return scaler, train_scaled, test_scaled

# inverse scaling for a forecasted value
def invert_scale(scaler, X, value):
	new_row = [x for x in X] + [value]
	array = numpy.array(new_row)
	array = array.reshape(1, len(array))
	inverted = scaler.inverse_transform(array)
	return inverted[0, -1]

# fit an LSTM network to training data
def fit_lstm(train, batch_size, nb_epoch, neurons):
	X, y = train[:, 0:-1], train[:, -1]
	X = X.reshape(X.shape[0], 1, X.shape[1])
	model = Sequential()
	model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
	model.compile(loss='mean_squared_error', optimizer='adam')
	for i in range(nb_epoch):, y, epochs=1, batch_size=batch_size, verbose=1, shuffle=False)
	return model

# make a one-step forecast
def forecast_lstm(model, batch_size, X):
	X = X.reshape(1, 1, len(X))
	yhat = model.predict(X, batch_size=batch_size)
	return yhat[0,0]

seleccion = " \
SELECT  creacion,  \
        menores  \
    SELECT  creacion, \
            COUNT(*) as maximo, \
            (SUM(CASE WHEN value < 2 THEN 1 ELSE 0 END)) as menores, \
            (SUM(CASE WHEN value >= 2 THEN 1 ELSE 0 END)) as mayores, \
            ((SUM(CASE WHEN value < 2 THEN 1 ELSE 0 END)) - (SUM(CASE WHEN value >= 2 THEN 1 ELSE 0 END))) as diferencia, \
            FLOOR(UNIX_TIMESTAMP(creacion)/(2 * 60)) AS lapso, \
            DATE_FORMAT(creacion, '%T') as sub_creacion \
    FROM crawler \
    WHERE round > 500000 \
    GROUP BY lapso \
) as sub \
ORDER BY creacion DESC"    
# load dataset
#series = read_csv('shampoo.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
#series = read_csv('numeros.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
series = read_sql_query(seleccion, conn, parse_dates=['creacion'], index_col=['creacion'])

# transform data to be stationary
raw_values  = series.values
diff_values = difference(raw_values, 1)

# transform data to be supervised learning
supervised = timeseries_to_supervised(diff_values, 1)
supervised_values = supervised.values

# split data into train and test-sets
#train, test = supervised_values[0:-12], supervised_values[-12:]

#dividimos la información
train_size        = int(len(supervised_values) * 0.50)
test_size         = len(supervised_values) - train_size
train, test       = supervised_values[0:train_size,:], supervised_values[train_size:len(supervised_values),:]

# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)

# repeat experiment
repeats = 1
epochos = 1
error_scores = list()
for r in range(repeats):
	# fit the model
	lstm_model = fit_lstm(train_scaled, 1, epochos, 16)
	# forecast the entire training dataset to build up state for forecasting
	train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
	lstm_model.predict(train_reshaped, batch_size=1)
	# walk-forward validation on the test data
	predictions = list()
	for i in range(len(test_scaled)):
		# make one-step forecast
		X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
		yhat = forecast_lstm(lstm_model, 1, X)
		# invert scaling
		yhat = invert_scale(scaler, X, yhat)
		# invert differencing
		yhat = inverse_difference(raw_values, yhat, len(test_scaled)+1-i)
		# store forecast
	# report performance
	rmse = sqrt(mean_squared_error(raw_values[-test_size:], predictions))
	print('%d) Test RMSE: %.3f' % (r+1, rmse))

# summarize results
#results = DataFrame()
#results['rmse'] = error_scores

# report performance
#rmse = sqrt(mean_squared_error(raw_values[-test_size:], predictions))
#print('Test RMSE: %.3f' % rmse)
# line plot of observed vs predicted

print("*******************  PREDICCIONES  *******************")

while True:

    #pedimos la informacion
    seleccion = " \
    SELECT  creacion,  \
            menores  \
    FROM( \
        SELECT  creacion, \
                COUNT(*) as maximo, \
                (SUM(CASE WHEN value < 2 THEN 1 ELSE 0 END)) as menores, \
                (SUM(CASE WHEN value >= 2 THEN 1 ELSE 0 END)) as mayores, \
                ((SUM(CASE WHEN value < 2 THEN 1 ELSE 0 END)) - (SUM(CASE WHEN value >= 2 THEN 1 ELSE 0 END))) as diferencia, \
                FLOOR(UNIX_TIMESTAMP(creacion)/(2 * 60)) AS lapso, \
                DATE_FORMAT(creacion, '%T') as sub_creacion \
        FROM crawler \
        GROUP BY lapso \
        ORDER BY creacion DESC \
        LIMIT 0, 300 \
    ) as sub \
    ORDER BY creacion DESC"
    #seleccionamos la información para hacer la predicción
    predecir = read_sql_query(seleccion, conn, parse_dates=['creacion'], index_col=['creacion'])
    #limpiamos la cache
    yhat = forecast_lstm(lstm_model, 1, predecir)
    #ynew = ynew[0]




Keep friends close and formatters closer.