def kraus_to_choi(kraus_list): """ Takes a list of Kraus operators and returns the Choi matrix for the channel represented by the Kraus operators in `kraus_list` """ kraus_mat_list = list(map(lambda x: matrix(x.data.todense()), kraus_list)) op_len = len(kraus_mat_list[0]) op_rng = range(op_len) choi_blocks = array([[sum([op[:, c_ix] * array([op.H[r_ix, :]]) for op in kraus_mat_list]) for r_ix in op_rng] for c_ix in op_rng]) return Qobj(inpt=hstack(hstack(choi_blocks)), dims=[kraus_list[0].dims, kraus_list[0].dims])
def data_prep(self, open_col, high_col, low_col, clos_col, raw_seq): # Reshape the array to columns and rows open_col = open_col.reshape((len(open_col), 1)) high_col = high_col.reshape((len(high_col), 1)) low_col = low_col.reshape((len(low_col), 1)) clos_col = clos_col.reshape((len(clos_col), 1)) # Stacks arrays side by side in one array raw_seq = hstack((open_col, high_col, low_col, clos_col)) # Splits the data into test and train (data, windows, size of test) X_train, X_test, y_train, y_test = Models.split_data( raw_seq, self.timestep, 0.2) # Splits the data into test and val (data, windows, size of val) X_train, X_val, y_train, y_val = train_test_split( X_train, y_train, test_size=0.2) # Rescales the data for better results scaler = MinMaxScaler() X_train = scaler.fit_transform(X_train.reshape(-1, X_train.shape[-1])).reshape(X_train.shape) X_test = scaler.transform(X_test.reshape(-1, X_test.shape[-1])).reshape(X_test.shape) X_val = scaler.fit_transform(X_val.reshape(-1, X_val.shape[-1])).reshape(X_val.shape) y_val = scaler.transform(y_val.reshape(-1, y_val.shape[-1])).reshape(y_val.shape) y_train = scaler.fit_transform(y_train.reshape(-1, y_train.shape[-1])).reshape(y_train.shape) y_test = scaler.transform(y_test.reshape(-1, y_test.shape[-1])).reshape(y_test.shape) Models.check_model(self, X_train, X_val, y_train, y_val, X_test, y_test, raw_seq)
def data_prep(self, open_col, high_col, low_col, clos_col, raw_seq): # Reshape the array to columns and rows open_col = open_col.reshape((len(open_col), 1)) high_col = high_col.reshape((len(high_col), 1)) low_col = low_col.reshape((len(low_col), 1)) clos_col = clos_col.reshape((len(clos_col), 1)) # Stacks arrays side by side in one array raw_seq = hstack((open_col, high_col, low_col, clos_col)) # scaler = StandardScaler() # scaler.fit(raw_seq)# # scaler.transform(raw_seq) # Splits the data into test and train (data, windows, size of test) X_train, X_test, y_train, y_test = Models.split_data( raw_seq, self.timestep, 0.2) # Splits the data into test and val (data, windows, size of val) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2) Models.check_model(self, X_train, X_val, y_train, y_val, X_test, y_test, raw_seq)
def readSamples(self, fileName, key,recalc=False,samples=None): fn = fileName + ".pre" try: if recalc: raise IOError() with open(fn): pass print "precalculated file present" self.mu, self.cov = hsplit(mat(fromfile(fn).reshape((3,-1))),[1]) except IOError: if samples != None: self._samples = samples print "got samples: " , self._samples else: print "no file present, calculating..." smpls = loadmat(fileName)[key] print "loaded from mat file" self._samples = mat(smpls) print "reshaped into samples" self.mu = sum(self._samples, axis=1) / self._samples.shape[1] print "mu=", str(self.mu) sampdiffmu = self._samples - self.mu self.cov = sampdiffmu*sampdiffmu.T / self._samples.shape[1] print"cov=", str(self.cov) mat(hstack((self.mu,self.cov))).tofile(fn) self._invCov = self.cov.I self._detCov = det(self.cov) self._multConst = 1 / sqrt((2 * pi) ** 3 * self._detCov)
def sample(self, n=1): X = randn(n, 2) X[:, 0] = sqrt(self.V) * X[:, 0] X[:, 1] = X[:, 1] + self.bananicity * (X[:, 0]**2 - self.V) if self.dimension > 2: X = hstack((X, randn(n, self.dimension - 2))) return Sample(X)
def write_submission(filename, h): print 'Saving submission' timed_filename = filename + "-" + time_stamp() ids = numpy.array(range(h.shape[0]), ndmin = 2).T h = hstack((ids, h)) numpy.savetxt(fname = timed_filename, X = h, fmt='%.15e', delimiter=",") functions.add_lineno_and_header(timed_filename)
def sample(self, n=1): X = randn(n, 2) X[:, 0] = sqrt(self.V) * X[:, 0] X[:, 1] = X[:, 1] + self.bananicity * (X[:, 0] ** 2 - self.V) if self.dimension > 2: X = hstack((X, randn(n, self.dimension - 2))) return Sample(X)
def test_stacking_arrays(self): a = array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) b = array([[13,14,15,16],[17,18,19,20],[21,22,23,24]]) c = vstack((a,b)) numpy.testing.assert_array_equal(c, array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20],[21,22,23,24]])) d = hstack((a,b)) numpy.testing.assert_array_equal(d, array([[1,2,3,4,13,14,15,16], [5,6,7,8,17,18,19,20], [9,10,11,12,21,22,23,24]]))
def kraus_to_choi(kraus_list): """ Takes a list of Kraus operators and returns the Choi matrix for the channel represented by the Kraus operators in `kraus_list` """ kraus_mat_list = list(map(lambda x: x.data.toarray(), kraus_list)) op_rng = range(kraus_mat_list[0].shape[0]) choi_blocks = array([[ sum([ np.outer(op[:, c_ix], np.transpose(np.conjugate(op))[r_ix, :]) for op in kraus_mat_list ]) for r_ix in op_rng ] for c_ix in op_rng]) return Qobj(inpt=hstack(hstack(choi_blocks)), dims=[kraus_list[0].dims[::-1], kraus_list[0].dims[::-1]], type='super', superrep='choi')
def next(self): if self._curFold < self._numFolds-1: if self._testData is not None: self._trainData.insert(self._curFold, self._testData) self._trainLabels.insert(self._curFold, self._testLabels) self._curFold += 1 self._testData = self._trainData.pop(self._curFold) self._testLabels = self._trainLabels.pop(self._curFold) ret = (vstack(self._trainData), hstack(self._trainLabels), self._testData, self._testLabels) #print self._curFold, self._numFolds, [x.shape for x in ret] return ret else: raise StopIteration()
def logisticRegression(trainData, trainLabels, testData, testLabels): #adjust the data, adding the 'free parameter' to the train data trainDataWithFreeParam = hstack((trainData.copy(), ones(trainData.shape[0])[:,newaxis])) testDataWithFreeParam = hstack((testData.copy(), ones(testData.shape[0])[:,newaxis])) alpha = 10 oldW = zeros(trainDataWithFreeParam.shape[1]) newW = ones(trainDataWithFreeParam.shape[1]) iteration = 0 trainDataWithFreeParamTranspose = transpose(trainDataWithFreeParam) alphaI = alpha * identity(oldW.shape[0]) while not array_equal(oldW, newW): if iteration == 100: break oldW = newW.copy() yVect = yVector(oldW, trainDataWithFreeParam) r = R(yVect) firstTerm = inv(alphaI + dot(dot(trainDataWithFreeParamTranspose, r), trainDataWithFreeParam)) secondTerm = dot(trainDataWithFreeParamTranspose, (yVect-trainLabels)) + alpha * oldW newW = oldW - dot(firstTerm, secondTerm) iteration += 1 #see how well we did numCorrect = 0 for x,t in izip(testDataWithFreeParam, testLabels): if yScalar(newW, x) >= 0.5: if t == 1: numCorrect += 1 else: if t == 0: numCorrect += 1 return float(numCorrect) / float(len(testLabels))
def crossValidation(numFolds, data, labels, algorithm, accuracyList, learningCurveList, numLearningCurveIterations, learningCurveIndexMod): dataFolds = array_split(data, numFolds) labelFolds = array_split(labels, numFolds) for testIndex in range(numFolds): print testIndex, testData = dataFolds.pop(testIndex) testLabels = labelFolds.pop(testIndex) trainData = vstack(dataFolds) trainLabels = hstack(labelFolds) accuracyList.append(algorithm(trainData, trainLabels, testData, testLabels)) learningCurve(algorithm, learningCurveList, trainData, trainLabels, testData, testLabels, numLearningCurveIterations, learningCurveIndexMod) dataFolds.insert(testIndex, testData) labelFolds.insert(testIndex, testLabels) print ''
def setUp(self): x = arange(0,1,0.1) y = arange(0,1,0.1) z = arange(0,1,0.1) self._line = array(zip(x,y,z)) t = hstack((arange(-1,-0.1,0.005), arange(1, 2, 0.005))) x = map(lambda x: x + gauss(0,0.005), t) y = map(lambda x: x + gauss(0,0.005), t) z = map(lambda x: x + gauss(0,0.005), t) self._line_cluster = array(zip(x,y,z)) t = arange(-1,1,0.001) x = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) y = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) z = map(lambda x: x + gauss(0,0.02)*(1-x*x), t) line = array(zip(x,y,z)) self._line_cluster_2_ = vstack((line, line + 3))
def yVector(w, data): return hstack([yScalar(w,x) for x in data])
def predictions(X): # Makes the prediciton based on the most recent data # Creates a plot comparing the predicted and actual prices # Loads saved model so retraining isn't needed # json_file = open('saved_models/MLP.json', 'r') # loaded_model_json = json_file.read() # json_file.close() # model = model_from_json(loaded_model_json) # model.load_weights('saved_models/MLP.h5') #X = currency[X] # Creates arrays to be used to specify each column open_col, high_col, low_col, clos_col, raw_seq = [], [], [], [], array( []) # Read input from CSV directo = os.path.dirname(__file__) path1 = os.path.join(directo, 'Finance_Data/Raw_Data/') with open(path1 + X, 'r') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) # Assignes each column within CSV to appropriate Array for lines in csv_reader: if 'null' in lines: continue else: # Index is +1 due to CSV indexing if lines[1] != 'null': open_col.append(float(lines[1])) if lines[2] != 'null': high_col.append(float(lines[2])) if lines[3] != 'null': low_col.append(float(lines[3])) if lines[4] != 'null': clos_col.append(float(lines[4])) # Converts list to a Numpy array open_col = array(open_col) high_col = array(high_col) low_col = array(low_col) clos_col = array(clos_col) # Reshape the array to columns and rows open_col = open_col.reshape((len(open_col), 1)) high_col = high_col.reshape((len(high_col), 1)) low_col = low_col.reshape((len(low_col), 1)) clos_col = clos_col.reshape((len(clos_col), 1)) raw_seq = hstack((open_col, high_col, low_col, clos_col)) directo = os.path.dirname(__file__) path1 = os.path.join(directo, '../saved_models/KNN_file') loaded_model = pickle.load(open(path1, 'rb')) res = [] #res.append([1.1212,1.12218,1.12106,1.12188]) #res.append([0.0, 0.0, 0.0, 0.0]) for i in raw_seq: i = [i] result = loaded_model.predict(i) result = result.flatten() result = result.tolist() res.append(result) print(raw_seq[-1]) print(res[-1]) reslen = len(res) rawlen = len(raw_seq) print('\n') no = [NaN, NaN, NaN, NaN] res.insert(0, no) result = pd.DataFrame(res) directo = os.path.dirname(__file__) path1 = os.path.join(directo, 'Finance_Data/Raw_Data/') file_loc = path1 + X data = pd.read_csv(file_loc, parse_dates=False, usecols=['Date']) data = data.values.tolist() data = np.array(data) data = data.flatten() raw_seq = pd.DataFrame(raw_seq) raw_seq['Date'] = data raw_seq.index = pd.DatetimeIndex(raw_seq['Date']) raw_seq.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] raw_seq['Volume'] = 0 print(raw_seq) # data = np.delete(data, 0) data = np.append(data, '2021-05-10') result['Date'] = data result.index = pd.DatetimeIndex(result['Date']) result.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] result['Volume'] = 0 print(result) #print(len(result)) s = mpf.make_mpf_style(base_mpl_style='seaborn', rc={'axes.grid': False}) fig = mpf.figure(style=s, figsize=(7.5, 5.75)) ax1 = fig.subplot() ax2 = ax1.twinx() mpf.plot(result[reslen - 50:reslen], ax=ax1, type='candle') mpf.plot(raw_seq[rawlen - 50:rawlen], ax=ax2, type='candle', style='yahoo') plt.show()
def __classify__(self, data): t=dot(hstack((data, ones((data.shape[0],1)))),self.w) return t>0
def _followx( self, x, way = 'one', last_eigenvector = None, weights = 1.): '''Generates a single lpc curve, from the start point, x. Proceeds in forward ('one'), backward ('back') or both ('two') directions from this point. Parameters ---------- x : 1-dim numpy.array of floats containing the start point for the lpc algorithm way : one of 'one'/'back'/'two', defines the orientation of the lpc propagation last_eigenvector: see _followXSingleDirection weights: see _followXSingleDirection Returns ------- curve : a dictionary comprising a single lpc curve in m-dim feature space, with keys, values as self._followxSingleDirection with the addition of start_point, 1-dim numpy.array of floats of length m; start_point_index, index of start_point in save_xd; For way == 'two', the forward and backward curves are stitched together. save_xd, eigen_vecd, cos_neu_neu, rho and c0 are formed by concatenating the reversed 'back' curve (with start_point removed) with the 'one' curve. high_rho_points are the union of forward and backward high_rho_points. lamb is the cumulative segment distance along the stitched together save_xd with, as before, lamb[0] = 0.0. TODO, should farm this out to an 'lpcCurve'-type class that knows how to join its instances ''' if way == 'one': curve = self._followxSingleDirection( x, direction = Direction.FORWARD, last_eigenvector = last_eigenvector, weights = weights) curve['start_point'] = x curve['start_point_index'] = 0 return curve elif way == 'back': curve = self._followxSingleDirection( x, direction = Direction.BACK, last_eigenvector = last_eigenvector, weights = weights) curve['start_point'] = x curve['start_point_index'] = 0 return curve elif way == 'two': forward_curve = self._followxSingleDirection( x, direction = Direction.FORWARD, last_eigenvector = last_eigenvector, weights = weights) back_curve = self._followxSingleDirection( x, direction = Direction.BACK, forward_curve = forward_curve, last_eigenvector = last_eigenvector, weights = weights) #Stitching - append forward_curve to the end of the reversed back_curve with initial point of back curve removed #TODO - neaten this up, looks pretty clumsy combined_distance = hstack((-back_curve['lamb'][:0:-1], forward_curve['lamb'])) curve = {'start_point': x, 'start_point_index': len(back_curve['save_xd']) - 1, 'save_xd': vstack((back_curve['save_xd'][:0:-1], forward_curve['save_xd'])), 'eigen_vecd': vstack((back_curve['eigen_vecd'][:0:-1], forward_curve['eigen_vecd'])), 'cos_neu_neu': hstack((back_curve['cos_neu_neu'][:0:-1], forward_curve['cos_neu_neu'])), 'rho': hstack((back_curve['rho'][:0:-1], forward_curve['rho'])), 'high_rho_points': vstack((back_curve['high_rho_points'], forward_curve['high_rho_points'])), 'lamb': combined_distance - min(combined_distance), 'c0': hstack((back_curve['c0'][:0:-1], forward_curve['c0'])), } return curve else: raise ValueError, 'way must be one of one/back/two'
def __train__(self, data, labels): o=ones((data.shape[0],1)) h=hstack((data, o)) pseudoX=pinv(h) self.w=dot(pseudoX, labels.reshape((-1,1)))