def _cascade_evaluation(self, X_test, y_test):
""" Evaluate the accuracy of the cascade using X and y.
:param X_test: np.array
Array containing the test input samples.
Must be of the same shape as training data.
:param y_test: np.array
Test target values.
:return: float
the cascade accuracy.
"""
casc_pred_prob = np.mean(self.cascade_forest(X_test), axis=0)
casc_pred = np.argmax(casc_pred_prob, axis=1)
casc_accuracy = accuracy_score(y_true=y_test, y_pred=casc_pred)
print('Layer validation accuracy = {}'.format(casc_accuracy))
return casc_accuracy
python类shape()的实例源码
def _create_feat_arr(self, X, prf_crf_pred):
""" Concatenate the original feature vector with the predicition probabilities
of a cascade layer.
:param X: np.array
Array containing the input samples.
Must be of shape [n_samples, data] where data is a 1D array.
:param prf_crf_pred: list
Prediction probabilities by a cascade layer for X.
:return: np.array
Concatenation of X and the predicted probabilities.
To be used for the next layer in a cascade forest.
"""
swap_pred = np.swapaxes(prf_crf_pred, 0, 1)
add_feat = swap_pred.reshape([np.shape(X)[0], -1])
feat_arr = np.concatenate([add_feat, X], axis=1)
return feat_arr
def fit(self, X, y):
""" Training the gcForest on input data X and associated target y.
:param X: np.array
Array containing the input samples.
Must be of shape [n_samples, data] where data is a 1D array.
:param y: np.array
1D array containing the target values.
Must be of shape [n_samples]
"""
if np.shape(X)[0] != len(y):
raise ValueError('Sizes of y and X do not match.')
mgs_X = self.mg_scanning(X, y)
_ = self.cascade_forest(mgs_X, y)
def postProcess(PDFeatures1,which):
PDFeatures2 = np.copy(PDFeatures1)
cols = np.shape(PDFeatures2)[1]
for x in xrange(cols):
indinf = np.where(np.isinf(PDFeatures2[:,x])==True)[0]
if len(indinf) > 0:
PDFeatures2[indinf,x] = 0
indnan = np.where(np.isnan(PDFeatures2[:,x])==True)[0]
if len(indnan) > 0:
PDFeatures2[indnan,x] = 0
indLN = np.where(PDFeatures2[:,0] < -1)[0]
for x in indLN:
PDFeatures2[x,0] = np.random.uniform(-0.75,-0.99,1)
term1 = (PDFeatures2[:,2]+PDFeatures2[:,3]+PDFeatures2[:,5])/3.
print term1
PDFeatures2[:,1] = 1.-term1
print "PDF",PDFeatures2[:,1]
return PDFeatures2
def get_batch():
ran = random.randint(600, data_size)
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(batch_size * n_steps):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
frame_0 = np.array(frame_0).reshape(-1)
image.append(frame_0)
#print(np.shape(image))
for i in range(batch_size):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
frame_1 = np.array(frame_1).reshape(-1)
label.append(frame_1)
for i in range(batch_size):
frame_2 = cv2.imread('./cropedoriginalUS2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_2 = cv2.resize(frame_2, (LONGITUDE, LONGITUDE))
frame_2 = np.array(frame_2).reshape(-1)
label_0.append(frame_2)
return image , label , label_0
def get_train_batch(noise=0):
ran = random.randint(600, data_size)
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(batch_size ):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
frame_0 = np.array(frame_0).reshape(-1)
image.append(frame_0)
#print(np.shape(image))
for i in range(batch_size):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
frame_1 = np.array(frame_1).reshape(-1)
label.append(frame_1)
return image , label
def get_train_batch(noise=500):
ran = np.random.randint(600,5800,size=10,dtype='int')
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(10):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
frame_0 = frame_0 / 255.0
image.append(frame_0)
#print(np.shape(image))
for i in range(10):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
label.append(frame_1)
return np.array(image,dtype='float') , np.array(label,dtype='float')
def get_test_batch(noise=500):
ran = np.random.randint(5800,6000,size=10,dtype='int')
#print(ran)
image = []
label = []
label_0 = []
n_pic = ran
# print(n_pic)
for i in range(10):
frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_0 = add_noise(frame_0, n = noise)
frame_0 = cv2.resize(frame_0, (24, 24))
frame_0 = np.array(frame_0).reshape(-1)
frame_0 = frame_0 / 255.0
image.append(frame_0)
#print(np.shape(image))
for i in range(10):
frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
frame_1 = cv2.resize(frame_1, (24, 24))
frame_1 = np.array(frame_1).reshape(-1)
frame_1 = gray2binary(frame_1)
label.append(frame_1)
return np.array(image,dtype='float') , np.array(label,dtype='float')
def get_data(datadir):
#datadir = args.data
# assume each image is 512x256 split to left and right
imgs = glob.glob(os.path.join(datadir, '*.jpg'))
data_X = np.zeros((len(imgs),3,img_cols,img_rows))
data_Y = np.zeros((len(imgs),3,img_cols,img_rows))
i = 0
for file in imgs:
img = cv2.imread(file,cv2.IMREAD_COLOR)
img = cv2.resize(img, (img_cols*2, img_rows))
#print('{} {},{}'.format(i,np.shape(img)[0],np.shape(img)[1]))
img = np.swapaxes(img,0,2)
X, Y = split_input(img)
data_X[i,:,:,:] = X
data_Y[i,:,:,:] = Y
i = i+1
return data_X, data_Y
def load_solar_data():
with open('solar label.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
labels = np.array(rows, dtype=int)
print(shape(labels))
with open('solar.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
rows = np.array(rows, dtype=float)
rows=rows[:104832,:]
print(shape(rows))
trX = np.reshape(rows.T,(-1,576))
print(shape(trX))
m = np.ndarray.max(rows)
print("maximum value of solar power", m)
trY=np.tile(labels,(32,1))
trX=trX/m
return trX,trY
def _set_x0(self, x0):
if utils.is_str(x0):
if type(x0) is not str:
print(type(x0), x0)
x0 = eval(x0)
self.x0 = array(x0, dtype=float, copy=True) # should not have column or row, is just 1-D
if self.x0.ndim == 2 and 1 in self.x0.shape:
utils.print_warning('input x0 should be a list or 1-D array, trying to flatten ' +
str(self.x0.shape) + '-array')
if self.x0.shape[0] == 1:
self.x0 = self.x0[0]
elif self.x0.shape[1] == 1:
self.x0 = array([x[0] for x in self.x0])
if self.x0.ndim != 1:
raise ValueError('x0 must be 1-D array')
if len(self.x0) <= 1:
raise ValueError('optimization in 1-D is not supported (code was never tested)')
try:
self.x0.resize(self.x0.shape[0]) # 1-D array, not really necessary?!
except NotImplementedError:
pass
# ____________________________________________________________
# ____________________________________________________________
def fCauchy(ftrue, alpha, p):
"""Returns Cauchy model noisy value
Cauchy with median 1e3*alpha and with p=0.2, zero otherwise
P(Cauchy > 1,10,100,1000) = 0.25, 0.032, 0.0032, 0.00032
"""
# expects ftrue to be a np.array
popsi = np.shape(ftrue)
fval = ftrue + alpha * np.maximum(0., 1e3 + (_rand(popsi) < p) *
_randn(popsi) / (np.abs(_randn(popsi)) + 1e-199))
tol = 1e-8
fval = fval + 1.01 * tol
idx = ftrue < tol
try:
fval[idx] = ftrue[idx]
except IndexError: # fval is a scalar
if idx:
fval = ftrue
return fval
### CLASS DEFINITION ###
def __read_nsx_data_variant_a(self, nsx_nb):
"""
Extract nsx data from a 2.1 .nsx file
"""
filename = '.'.join([self._filenames['nsx'], 'ns%i' % nsx_nb])
# get shape of data
shape = (
self.__nsx_databl_param['2.1']('nb_data_points', nsx_nb),
self.__nsx_basic_header[nsx_nb]['channel_count'])
offset = self.__nsx_params['2.1']('bytes_in_headers', nsx_nb)
# read nsx data
# store as dict for compatibility with higher file specs
data = {1: np.memmap(
filename, dtype='int16', shape=shape, offset=offset)}
return data
def __read_nsx_data_variant_b(self, nsx_nb):
"""
Extract nsx data (blocks) from a 2.2 or 2.3 .nsx file. Blocks can arise
if the recording was paused by the user.
"""
filename = '.'.join([self._filenames['nsx'], 'ns%i' % nsx_nb])
data = {}
for data_bl in self.__nsx_data_header[nsx_nb].keys():
# get shape and offset of data
shape = (
self.__nsx_data_header[nsx_nb][data_bl]['nb_data_points'],
self.__nsx_basic_header[nsx_nb]['channel_count'])
offset = \
self.__nsx_data_header[nsx_nb][data_bl]['offset_to_data_block']
# read data
data[data_bl] = np.memmap(
filename, dtype='int16', shape=shape, offset=offset)
return data
def __read_nsx_data_variant_b(self, nsx_nb):
"""
Extract nsx data (blocks) from a 2.2 or 2.3 .nsx file. Blocks can arise
if the recording was paused by the user.
"""
filename = '.'.join([self._filenames['nsx'], 'ns%i' % nsx_nb])
data = {}
for data_bl in self.__nsx_data_header[nsx_nb].keys():
# get shape and offset of data
shape = (
self.__nsx_data_header[nsx_nb][data_bl]['nb_data_points'],
self.__nsx_basic_header[nsx_nb]['channel_count'])
offset = \
self.__nsx_data_header[nsx_nb][data_bl]['offset_to_data_block']
# read data
data[data_bl] = np.memmap(
filename, dtype='int16', shape=shape, offset=offset)
return data
def unscentedTransform(X, Wm, Wc, f):
Y = None
Ymean = None
fdim = None
N = np.shape(X)[1]
for j in range(0,N):
fImage = f(X[:,j])
if Y is None:
fdim = np.size(fImage)
Y = np.zeros((fdim, np.shape(X)[1]))
Ymean = np.zeros(fdim)
Y[:,j] = fImage
Ymean += Wm[j] * Y[:,j]
Ycov = np.zeros((fdim, fdim))
for j in range(0, N):
meanAdjustedYj = Y[:,j] - Ymean
Ycov += np.outer(Wc[j] * meanAdjustedYj, meanAdjustedYj)
return Y, Ymean, Ycov
def predict(self):
try:
X, Wm, Wc = sigmaPoints(self.xa, self.Pa)
except:
warnings.warn('Encountered a matrix that is not positive definite in the sigma points calculation at the predict step')
self.Pa = nearpd(self.Pa)
X, Wm, Wc = sigmaPoints(self.xa, self.Pa)
fX, x, Pxx = unscentedTransform(X, Wm, Wc, self.fa)
x = np.asscalar(x)
Pxx = np.asscalar(Pxx)
Pxv = 0.
N = np.shape(X)[1]
for j in range(0, N):
Pxv += Wc[j] * fX[0,j] * X[3,j]
self.xa = np.array( ((x,), (0.,), (0.,), (0.,)) )
self.Pa = np.array( ((Pxx, Pxv , 0. , 0. ),
(Pxv, self.R, 0. , 0. ),
(0. , 0. , self.Q , self.cor),
(0. , 0. , self.cor, self.R )) )
def precompute(self):
# CSR_W = cuda_cffi.cusparse.CSR.to_CSR(self.st['W_gpu'],diag_type=True)
# Dia_W_cpu = scipy.sparse.dia_matrix( (self.st['M'], self.st['M']),dtype=dtype)
# Dia_W_cpu = scipy.sparse.dia_matrix( ( self.st['W'], 0 ), shape=(self.st['M'], self.st['M']) )
# Dia_W_cpu = scipy.sparse.diags(self.st['W'], format="csr", dtype=dtype)
# CSR_W = cuda_cffi.cusparse.CSR.to_CSR(Dia_W_cpu)
self.st['pHp_gpu'] = self.CSRH.gemm(self.CSR)
self.st['pHp']=self.st['pHp_gpu'].get()
print('untrimmed',self.st['pHp'].nnz)
self.truncate_selfadjoint(1e-5)
print('trimmed', self.st['pHp'].nnz)
self.st['pHp_gpu'] = cuda_cffi.cusparse.CSR.to_CSR(self.st['pHp'])
# self.st['pHWp_gpu'] = self.CSR.conj().gemm(CSR_W,transA=cuda_cffi.cusparse.CUSPARSE_OPERATION_TRANSPOSE)
# self.st['pHWp_gpu'] = self.st['pHWp_gpu'].gemm(self.CSR, transA=cuda_cffi.cusparse.CUSPARSE_OPERATION_NON_TRANSPOSE)
def plan(self, om, Nd, Kd, Jd):
self.debug = 0 # debug
n_shift = tuple(0*x for x in Nd)
self.st = plan(om, Nd, Kd, Jd)
self.Nd = self.st['Nd'] # backup
self.sn = self.st['sn'] # backup
self.ndims=len(self.st['Nd']) # dimension
self.linear_phase(n_shift)
# calculate the linear phase thing
self.st['pH'] = self.st['p'].getH().tocsr()
self.st['pHp']= self.st['pH'].dot(self.st['p'])
self.NdCPUorder, self.KdCPUorder, self.nelem = preindex_copy(self.st['Nd'], self.st['Kd'])
# self.st['W'] = self.pipe_density()
self.shape = (self.st['M'], numpy.prod(self.st['Nd']))
# print('untrimmed',self.st['pHp'].nnz)
# self.truncate_selfadjoint(1e-1)
# print('trimmed', self.st['pHp'].nnz)
def create_dummy_data(self):
self.datasetname = 'sherlock'
self.read_metadata_json(self.dataset.getMetadata())
self.worddict, self.lenwords, self.randwords = self.dataset.loadVocabulary()
# normalized probability matrix, words in a topic
#self.wordprob = self.dataset.getWordsInTopicMatrix()
#self.numtopics = numpy.shape(self.wordprob)[0]
self.email_prob = self.dataset.getWordsInTopicMatrix()
self.numtopics = numpy.shape(self.email_prob)[0]
print(self.numtopics)
# normalized probability matrix, emails in a topic
self.num_emails = len(self.metadata)
#self.email_prob = self.dataset.getDocsInTopicMatrix()
self.wordprob = self.dataset.getDocsInTopicMatrix()
#import pdb; pdb.set_trace()
# distance matrix between topics
self.distance_matrix = self.dataset.getTopicDistanceMatrix(self.wordprob)
def generateWekaFile(X,Y,features,path,name):
f = open(path + name + '.arff', 'w')
f.write("@relation '" + name + "'\n\n")
for feat in features:
f.write("@attribute " + feat + " numeric\n")
f.write("@attribute cluster {True,False}\n\n")
f.write("@data\n\n")
for i in range(X.shape[0]):
for j in range(X.shape[1]):
if np.isnan(X[i,j]):
f.write("?,")
else:
f.write(str(X[i,j]) + ",")
if Y[i] == 1.0 or Y[i] == True:
f.write("True\n")
else:
f.write("False\n")
f.close()
def mahalanobis_distance(difference, num_random_features):
num_samples, _ = np.shape(difference)
sigma = np.cov(np.transpose(difference))
mu = np.mean(difference, 0)
if num_random_features == 1:
stat = float(num_samples * mu ** 2) / float(sigma)
else:
try:
linalg.inv(sigma)
except LinAlgError:
print('covariance matrix is singular. Pvalue returned is 1.1')
warnings.warn('covariance matrix is singular. Pvalue returned is 1.1')
return 0
stat = num_samples * mu.dot(linalg.solve(sigma, np.transpose(mu)))
return chi2.sf(stat, num_random_features)
def compute_pvalue(self, samples):
samples = self._make_two_dimensional(samples)
self.shape = samples.shape[1]
stein_statistics = []
for f in range(self.number_of_random_frequencies):
# This is a little bit of a bug , but th holds even for this choice
random_frequency = np.random.randn()
matrix_of_stats = self.stein_stat(random_frequency=random_frequency, samples=samples)
stein_statistics.append(matrix_of_stats)
normal_under_null = np.hstack(stein_statistics)
normal_under_null = self._make_two_dimensional(normal_under_null)
return mahalanobis_distance(normal_under_null, normal_under_null.shape[1])
def extractOuterGrid(img):
rows,cols = np.shape(img)
maxArea = 0
point = [0,0]
imgOriginal = img.copy()
for i in range(rows):
for j in range(cols):
if img[i][j] == 255:
img,area,dummy = customFloodFill(img,[i,j],100,0)
if area > maxArea:
maxArea = area
point = [i,j]
img = imgOriginal
img,area,dummy = customFloodFill(img,[point[0],point[1]],100,0)
for i in range(rows):
for j in range(cols):
if img[i][j] == 100:
img[i][j] = 255
else: img[i][j] = 0
return img,point
# Draws a line on the image given its parameters in normal form
def centerDigit(img):
xMean,yMean,count = 0,0,0
(x,y) = np.shape(img)
for i in range(x):
for j in range(y):
if img[i][j] == 255:
xMean,yMean,count = (xMean+i),(yMean+j),(count+1)
if count == 0:
return img
xMean,yMean = (xMean / count),(yMean / count)
xDisp,yDisp = (xMean - (x/2)),(yMean - (y/2))
newImg = np.zeros((x,y),np.uint8)
for i in range(x):
for j in range(y):
if img[i][j] == 255:
newImg[i-xDisp][j-yDisp] = 255
return newImg
# Given the cropped out digit, places it on a black background for matching with templates
two_sigma_financial_modelling.py 文件源码
项目:PortfolioTimeSeriesAnalysis
作者: MizioAnd
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def outlier_identification(self, model, x_train, y_train):
# Split the training data into an extra set of test
x_train_split, x_test_split, y_train_split, y_test_split = train_test_split(x_train, y_train)
print('\nOutlier shapes')
print(np.shape(x_train_split), np.shape(x_test_split), np.shape(y_train_split), np.shape(y_test_split))
model.fit(x_train_split, y_train_split)
y_predicted = model.predict(x_test_split)
residuals = np.absolute(y_predicted - y_test_split)
rmse_pred_vs_actual = self.rmse(y_predicted, y_test_split)
outliers_mask = residuals >= rmse_pred_vs_actual
outliers_mask = np.concatenate([np.zeros((np.shape(y_train_split)[0],), dtype=bool), outliers_mask])
not_an_outlier = outliers_mask == 0
# Resample the training set from split, since the set was randomly split
x_out = np.insert(x_train_split, np.shape(x_train_split)[0], x_test_split, axis=0)
y_out = np.insert(y_train_split, np.shape(y_train_split)[0], y_test_split, axis=0)
return x_out[not_an_outlier, ], y_out[not_an_outlier, ]
def __init__(self,to_plot = True):
self.state = np.array([0,0])
self.observation_shape = np.shape(self.get_state())[0]
if to_plot:
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(111,aspect='equal')
#ax1.axis('off')
plt.xlim([-0.5,5.5])
plt.ylim([-0.5,5.5])
self.g1 = ax1.add_artist(plt.Circle((self.state[0],self.state[1]),0.1,color='red'))
self.fig = fig
self.ax1 = ax1
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def Dreamzs_finalize(MCMCPar,Sequences,Z,outDiag,fx,iteration,iloc,pCR,m_z,m_func):
# Start with CR
outDiag.CR = outDiag.CR[0:iteration-1,0:pCR.shape[1]+1]
# Then R_stat
outDiag.R_stat = outDiag.R_stat[0:iteration-1,0:MCMCPar.n+1]
# Then AR
outDiag.AR = outDiag.AR[0:iteration-1,0:2]
# Adjust last value (due to possible sudden end of for loop)
# Then Sequences
Sequences = Sequences[0:iloc+1,0:MCMCPar.n+2,0:MCMCPar.seq]
# Then the archive Z
Z = Z[0:m_z,0:MCMCPar.n+2]
if MCMCPar.savemodout==True:
# remove zeros
fx = fx[:,0:m_func]
return Sequences,Z, outDiag, fx
operations.py 文件源码
项目:Saliency_Detection_Convolutional_Autoencoder
作者: arthurmeyer
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def load_weights(model, sess, weight_file):
"""
Load weights from given weight file (used to load pretrain weight of vgg model)
Args:
model : model to restore variable to
sess : tensorflow session
weight_file : weight file name
"""
weights = np.load(weight_file)
keys = sorted(weights.keys())
for i, k in enumerate(keys):
if i <= 29:
print('-- %s %s --' % (i,k))
print(np.shape(weights[k]))
sess.run(model.parameters_conv[i].assign(weights[k]))
def update_canvas(widget=None):
global r, Z, res, rects, painted_rects
if widget is None:
widget = w
# Update display values
r = np.repeat(np.repeat(Z,r.shape[0]//Z.shape[0],0),r.shape[1]//Z.shape[1],1)
# If we're letting freeform painting happen, delete the painted rectangles
for p in painted_rects:
w.delete(p)
painted_rects = []
for i in range(Z.shape[0]):
for j in range(Z.shape[1]):
w.itemconfig(int(rects[i,j]),fill = rb(255*Z[i,j]),outline = rb(255*Z[i,j]))
# Function to move the paintbrush