楼主: Lisrelchen
1186 4

【GitHub】Time-Series-Forcasting-with-Neural-Networks [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-4-26 10:12:34 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. Usage:

  2. Import forcasting module

  3. from Forecast import create_series
  4. create_series() function has following parameters:

  5. in_array : time series data (numpy array or list of values)
  6. window_size : window size for feed forward network
  7. period : number of periods to predict
  8. minV : assumed minimum value for the time series
  9. maxV : assumed maximum value for the time series
  10. layer_nodes : list of values for the number of node for each layer in the neural network(Should be more than or equal to 2 values in the list)
  11. sigmoid : name of the sigmoid function('tanh' or 'logistic')
  12. epochs : number of iterations in the neural network
  13. you can either edit these values in the code itself

  14. time_series = [some series] create_series(time_series, 10, 15, 0, 100, [3,5,7], 'tanh', 700000)
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:time-series FORCASTING Networks Casting network

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2017-4-26 10:13:06
  1. from __future__ import division
  2. import numpy as np
  3. from collections import deque
  4. from NeuralNet import NeuralNetwork

  5. def _scale_to_binary(e, minV, maxV):
  6.     result = ((e-minV)/(maxV-minV))*(1-0)+0
  7.     return result

  8. def rescale_from_binary(e, minV, maxV):
  9.     result = e*(maxV-minV) + minV
  10.     return result


  11. def create_series(in_array,window_size,period, minV, maxV, layer_nodes = [2,3], sigmoid = 'tanh', epochs = 50000):
  12.     global_max = maxV
  13.     global_min = minV
  14.    
  15.    
  16.             
  17.     X_train = []
  18.     y_train = []
  19.     for i in range(len(in_array)-window_size):
  20.         X = []
  21.         for j in range(window_size):
  22.             X.append(_scale_to_binary(in_array[i+j],global_min,global_max))
  23.         X_train.append(X)
  24.         y_train.append(_scale_to_binary(in_array[i+window_size],global_min,global_max))
  25.         
  26.     X_train = np.array(X_train)
  27.     y_train = np.array(y_train)

  28.         
  29.     layers = []
  30.     layers.append(window_size)
  31.     for i in range(len(layer_nodes)):
  32.         layers.append(layer_nodes[i])
  33.    
  34.                      
  35.         
  36.     n = NeuralNetwork(layers,sigmoid)
  37.       
  38.     n.fit(X_train,y_train, epochs)
  39.         
  40.       
  41.         
  42.     X_test = in_array[-window_size:]

  43.     for i in range(len(X_test)):
  44.         X_test[i]=_scale_to_binary(X_test[i],global_min,global_max)

  45.     preds = []   
  46.     X_test = deque(X_test)
  47.          
  48.     for i in range(period):
  49.         val = n.predict(X_test)
  50.         preds.append(rescale_from_binary(val[0], global_min, global_max))
  51.             
  52.         X_test.rotate(-1)
  53.         X_test[window_size-1] = val[0]
  54.         
  55.               
  56.     return preds
复制代码

藤椅
Lisrelchen 发表于 2017-4-26 10:14:41
  1. import numpy as np

  2. def tanh(x):
  3.     return np.tanh(x)

  4. def tanh_deriv(x):
  5.     return 1.0 - x**2

  6. def logistic(x):
  7.     return 1/(1 + np.exp(-x))

  8. def logistic_derivative(x):
  9.     return logistic(x)*(1-logistic(x))
  10.    
  11.    
  12. class NeuralNetwork:

  13.     def __init__(self, layers, activation='tanh'):
  14.         """
  15.         :param layers: A list containing the number of units in each layer. Should be at least two values
  16.         :param activation: The activation function to be used. Can be "logistic" or "tanh"
  17.         """
  18.         if activation == 'logistic':
  19.             self.activation = logistic
  20.             self.activation_deriv = logistic_derivative
  21.         elif activation == 'tanh':
  22.             self.activation = tanh
  23.             self.activation_deriv = tanh_deriv
  24.         

  25.         self.weights = []
  26.         for i in range(1, len(layers) - 1):
  27.             self.weights.append((2*np.random.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)
  28.         self.weights.append((2*np.random.random((layers[i] + 1, layers[i + 1]))-1)*0.25)
  29.         
  30.         
  31.     def fit(self, X, y, learning_rate=0.25, epochs=10000):
  32.         X = np.atleast_2d(X)
  33.         temp = np.ones([X.shape[0], X.shape[1]+1])
  34.         temp[:, 0:-1] = X  # adding the bias unit to the input layer
  35.         X = temp
  36.         y = np.array(y)
  37.    
  38.         for k in range(epochs):
  39.             i = np.random.randint(X.shape[0])
  40.             a = [X[i]]
  41.    
  42.             for l in range(len(self.weights)):
  43.                     a.append(self.activation(np.dot(a[l], self.weights[l])))
  44.             error = y[i] - a[-1]
  45.             deltas = [error * self.activation_deriv(a[-1])]
  46.    
  47.             for l in range(len(a) - 2, 0, -1): # we need to begin at the second to last layer
  48.                 deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l]))
  49.             deltas.reverse()
  50.             for i in range(len(self.weights)):
  51.                 layer = np.atleast_2d(a[i])
  52.                 delta = np.atleast_2d(deltas[i])
  53.                 self.weights[i] += learning_rate * layer.T.dot(delta)
  54.                                        
  55.                                        
  56.     def predict(self, x):
  57.         x = np.array(x)        
  58.         temp = np.ones(x.shape[0]+1)
  59.         temp[0:-1] = x
  60.         a = temp
  61.         for l in range(0, len(self.weights)):
  62.             a = self.activation(np.dot(a, self.weights[l]))
  63.         return a
  64.                
复制代码

板凳
MouJack007 发表于 2017-4-26 12:21:49
谢谢楼主分享!

报纸
MouJack007 发表于 2017-4-26 12:22:08

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-9 14:02