SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randint
  2. import requests
  3. import json
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import tensorflow as tf
  7. from math import floor, ceil
  8.  
  9. class InvestmentPredictor(object):
  10.  
  11.     def __init__(self, **kwargs):
  12.         self.DataUrl = 'https://api.blockchain.info/charts/market-price?format=json'
  13.         with open('market-price.json', 'r') as f:
  14.             self.data = json.load(f)
  15.         self.rng = 8
  16.         #self.data = json.loads(requests.get(self.DataUrl).text)
  17.         #with open('market-price.json', 'w') as f:
  18.             #json.dump(self.data, f, indent=4)
  19.  
  20.     def _train(self):
  21.         pass
  22.  
  23.     def parse_data(self):
  24.         values = self.data['values']
  25.         x = [round(p['x'], 2) for p in values]
  26.         y = [round(p['y'], 2) for p in values]
  27.         return x, y
  28.  
  29.     def _get_point(self, **kwargs):
  30.         x, y = self.parse_data()
  31.         if kwargs['r'] is True:
  32.             return randint(0, len(x))
  33.         else:
  34.             axis = kwargs['axis']
  35.             p = kwargs['p']
  36.             rng = kwargs['rng']
  37.             if axis == 'y':
  38.                 return y[p-rng:p+1]
  39.             elif axis == 'b':
  40.                 return x[p-rng:p+1], y[p-rng:p+1]
  41.             else:
  42.                 return x[p-rng:p+1]
  43.  
  44.     def _get_trends(self, array, per=False):
  45.         trends = []
  46.         for v in range(len(array)):
  47.             if v != 0:
  48.                 avg = ((array[v]+array[v-1])/2)
  49.                 if per is True:
  50.                     trends.append(round(((array[v]-array[v-1])/avg)*100, 5))
  51.                 else:
  52.                     trends.append(round((array[v]-array[v-1]), 5))
  53.         return trends
  54.  
  55.     def _certify(self, p=None, h=1):
  56.         if p is None: p = self._get_point(r=True)
  57.         sub_y = self._get_point(r=False, p=p, axis='y', rng=self.rng)
  58.         div = len(sub_y) - 2
  59.         test_y = sub_y[div]
  60.         pre_y = sub_y[0: div+1]
  61.         post_y = sub_y[div::]
  62.         pre_trends_p = self._get_trends(pre_y, per=True)
  63.         post_trends_d = self._get_trends(post_y, per=False)
  64.         post_trends_p = self._get_trends(post_y, per=True)
  65.         pre_overall_trend_p = sum(pre_trends_p)/len(pre_trends_p)
  66.         post_overall_trend_d = sum(post_trends_d)/len(post_trends_d)
  67.         post_overall_trend_p = sum(post_trends_p)/len(post_trends_p)
  68.         pre_trends_d = self._get_trends(pre_y, per=False)
  69.         pre_overall_trend_d = sum(pre_trends_d)/len(pre_trends_d)
  70.         worth = 0
  71.         out_set = [pre_overall_trend_d + pre_overall_trend_p + test_y] + pre_trends_d + pre_trends_p
  72.         if post_overall_trend_d > 0 and post_overall_trend_p > h:
  73.             worth = 1
  74.         return out_set, worth
  75.  
  76.     def _get_training_set(self):
  77.         x_set = []
  78.         y_set = []
  79.         for i in range(len(self.data['values'][0:300])):
  80.             if i > self.rng-1:
  81.                 x, y = self._certify(p=i)
  82.                 x_set.append(x)
  83.                 y_set.append(y)
  84.         return x_set, y_set
  85.  
  86.     def _get_testing_set(self):
  87.         x_set = []
  88.         y_set = []
  89.         for i in range(len(self.data['values'][300::])):
  90.             if i > self.rng-1:
  91.                 x, y = self._certify(p=i)
  92.                 x_set.append(x)
  93.                 y_set.append(y)
  94.         return x_set, y_set
  95.  
  96. class NeuralNetwork(object):
  97.  
  98.     def __init__(self):
  99.  
  100.         print(tf.__version__)
  101.         inp = InvestmentPredictor(nn=False)
  102.         x, y = inp._get_training_set()
  103.         x_t, y_t = inp._get_testing_set()
  104.         self.x_train = tf.keras.utils.normalize(x, axis=1)
  105.         self.x_test = tf.keras.utils.normalize(x_t, axis=1)
  106.         self.y_train = np.array(y)
  107.         self.y_test = np.array(y_t)
  108.         self.model = tf.keras.models.Sequential()
  109.  
  110.     def train(self, e=4):
  111.         self.model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
  112.         self.model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
  113.         self.model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
  114.         self.model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
  115.         self.model.fit(self.x_train, self.y_train, epochs=e)
  116.  
  117.     def test(self):
  118.         val_loss, val_acc = self.model.evaluate(self.x_test, self.y_test)
  119.         print('Values Lost: {}, Accuracy: {}'.format(val_loss, val_acc))
  120.  
  121.  
  122. nn = NeuralNetwork()
  123. nn.train()
  124. nn.test()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top