css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              #https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/

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):
    sys.stdout.write(str(text))
    sys.stdout.flush()

# 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)]
	columns.append(df)
	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]
		diff.append(value)
	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 = scaler.fit(train)
	# 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.add(Dense(1))
	model.compile(loss='mean_squared_error', optimizer='adam')
	for i in range(nb_epoch):
		model.fit(X, y, epochs=1, batch_size=batch_size, verbose=1, shuffle=False)
		model.reset_states()
	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  \
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 \
    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
		predictions.append(yhat)
	# report performance
	rmse = sqrt(mean_squared_error(raw_values[-test_size:], predictions))
	print('%d) Test RMSE: %.3f' % (r+1, rmse))
	error_scores.append(rmse)

# summarize results
#results = DataFrame()
#results['rmse'] = error_scores
#print(results.describe())
#results.boxplot()
#pyplot.show()

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



    
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
    conn.commit()
    
    imprimir(predecir)
    
    yhat = forecast_lstm(lstm_model, 1, predecir)
    #ynew = ynew[0]
    
    imprimir(yhat)
    
    
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console