本文整理汇总了Python中numpy.ndarray函数的典型用法代码示例。如果您正苦于以下问题:Python ndarray函数的具体用法?Python ndarray怎么用?Python ndarray使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ndarray函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: produce_optimization_sets
def produce_optimization_sets(self, train, test_samples=None):
if test_samples == 0:
return [train, numpy.ndarray([0, 0]), 0]
test_size = int(round(train.shape[0] / 10))
if test_samples is None:
test_samples = random.sample(xrange(0, train.shape[0] - 1), test_size)
train_index = 0
test_index = 0
train_result = numpy.ndarray([train.shape[0] - test_size, train.shape[1]], dtype=theano.config.floatX)
test_result = numpy.ndarray([test_size, train.shape[1]], dtype=theano.config.floatX)
for i in xrange(train.shape[0]):
if i in test_samples:
test_result[test_index, :] = train[i, :]
test_index += 1
else:
train_result[train_index, :] = train[i, :]
train_index += 1
return [train_result, test_result, test_samples]
示例2: __init__
def __init__(self, n_in, n_out, weights=None,
activation='sigmoid', is_classifier_layer=False):
# Get activation function from string
self.activation_string = activation
self.activation = Activation.get_activation(self.activation_string)
self.activation_derivative = Activation.get_derivative(
self.activation_string)
self.n_in = n_in
self.n_out = n_out
self.inp = np.ndarray(n_in + 1)
self.inp[0] = 1
self.outp = np.ndarray(n_out)
self.deltas = np.zeros(n_out)
# You can have better initialization here
if weights is None:
self.weights = np.random.rand(n_in + 1, n_out) / 10 - 0.05
# Adjust weights to zero mean
for i in range(n_out):
self.weights[:][i] -= (sum(self.weights[:][i]) / len(self.weights[:][i]))
else:
assert(weights.shape == (n_in + 1, n_out))
self.weights = weights
self.is_classifier_layer = is_classifier_layer
# Some handy properties of the layers
self.size = self.n_out
self.shape = self.weights.shape
示例3: generate_performance_results
def generate_performance_results(self, data, configs):
num_folds = len(self.fold_data)
num_pred = len(self.fold_data[0].test_predicted_values)
num_actual = len(self.fold_data[0].test_actual_values)
train_perf = np.ndarray(num_folds)
test_perf = np.ndarray(num_folds)
test_predicted = np.ones((num_folds,num_pred,))
test_actual = np.ones((num_folds,num_actual,))
for index, fold in enumerate(self.fold_data):
# train_targets = self.train_targets[index]
# test_targets = self.test_targets[index]
train_predicted = fold.train_predicted_values
train_actual = fold.train_actual_values
test_predicted[index] = fold.test_predicted_values
test_actual[index] = fold.test_actual_values
results_loss_function = configs.results_loss_function
train_target_ids = data.get_target_ids(fold.train_inds)
test_target_ids = data.get_target_ids(fold.test_inds)
train_perf[index] = LossFunction.compute_loss_function(train_predicted,
train_actual,
train_target_ids,
results_loss_function)
test_perf[index] = LossFunction.compute_loss_function(test_predicted[index],
test_actual[index],
test_target_ids,
results_loss_function)
self.fold_data[index].train_error = train_perf[index]
self.fold_data[index].test_error = test_perf[index]
self.test_actual = test_actual
self.train_actual = train_actual
self.test_predicted = test_predicted
self.train_predicted = train_predicted
示例4: get_synthetic_warped_circle
def get_synthetic_warped_circle(nslices):
#get a subsampled circle
fname_cicle = get_data('reg_o')
circle = np.load(fname_cicle)[::4,::4].astype(floating)
#create a synthetic invertible map and warp the circle
d, dinv = vfu.create_harmonic_fields_2d(64, 64, 0.1, 4)
d = np.asarray(d, dtype=floating)
dinv = np.asarray(dinv, dtype=floating)
mapping = DiffeomorphicMap(2, (64, 64))
mapping.forward, mapping.backward = d, dinv
wcircle = mapping.transform(circle)
if(nslices == 1):
return circle, wcircle
#normalize and form the 3d by piling slices
circle = (circle-circle.min())/(circle.max() - circle.min())
circle_3d = np.ndarray(circle.shape + (nslices,), dtype=floating)
circle_3d[...] = circle[...,None]
circle_3d[...,0] = 0
circle_3d[...,-1] = 0
#do the same with the warped circle
wcircle = (wcircle-wcircle.min())/(wcircle.max() - wcircle.min())
wcircle_3d = np.ndarray(wcircle.shape + (nslices,), dtype=floating)
wcircle_3d[...] = wcircle[...,None]
wcircle_3d[...,0] = 0
wcircle_3d[...,-1] = 0
return circle_3d, wcircle_3d
示例5: AllocateRAM
def AllocateRAM (self, verbose = True):
ramsz = np.prod(self.dims) * self.ds / MB
if (verbose):
print ' Allocating %.1f MB of RAM' % ramsz
print ' Data (dims: %(a)d %(b)d %(c)d %(d)d %(e)d %(f)d %(g)d %(h)d %(i)d %(j)d %(k)d %(l)d %(m)d %(n)d %(o)d %(p)d)' % {
"a": self.dims[ 0], "b": self.dims[ 1], "c": self.dims[ 2], "d": self.dims[ 3], "e": self.dims[ 4], "f": self.dims[ 5],
"g": self.dims[ 6], "h": self.dims[ 7], "i": self.dims[ 8], "j": self.dims[ 9], "k": self.dims[10], "l": self.dims[11],
"m": self.dims[12], "n": self.dims[13], "o": self.dims[14], "p": self.dims[15]}
print ' Noise (dims: %(a)d %(b)d %(c)d %(d)d %(e)d %(f)d %(g)d %(h)d %(i)d %(j)d %(k)d %(l)d %(m)d %(n)d %(o)d %(p)d)' % {
"a": self.noisedims[ 0], "b": self.noisedims[ 1], "c": self.noisedims[ 2], "d": self.noisedims[ 3], "e": self.noisedims[ 4], "f": self.noisedims[ 5],
"g": self.noisedims[ 6], "h": self.noisedims[ 7], "i": self.noisedims[ 8], "j": self.noisedims[ 9], "k": self.noisedims[10], "l": self.noisedims[11],
"m": self.noisedims[12], "n": self.noisedims[13], "o": self.noisedims[14], "p": self.noisedims[15]}
if (self.syncdims[0]):
print ' SyncData (dims: %(a)d %(b)d )' % {"a": self.syncdims[0], "b": self.syncdims[1]}
self.data = np.ndarray(shape=self.dims, dtype=self.dt)
self.sync = np.ndarray(shape=self.syncdims, dtype=self.sddt)
self.noise = np.ndarray(shape=filter(lambda x:x>0,self.noisedims), dtype=self.nddt)
if (verbose):
print (" ... done.\n" % ramsz)
return
示例6: load
def load(data_folders, min_num_images, max_num_images):
dataset = np.ndarray(
shape=(max_num_images, image_size, image_size), dtype=np.float32)
labels = np.ndarray(shape=(max_num_images), dtype=np.int32)
label_index = 0
image_index = 0
for folder in data_folders:
print(folder)
for image in os.listdir(folder):
if image_index >= max_num_images:
raise Exception('More images than expected: %d >= %d' % (
num_images, max_num_images))
image_file = os.path.join(folder, image)
try:
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[image_index, :, :] = image_data
labels[image_index] = label_index
image_index += 1
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
label_index += 1
num_images = image_index
dataset = dataset[0:num_images, :, :]
labels = labels[0:num_images]
if num_images < min_num_images:
raise Exception('Many fewer images than expected: %d < %d' % (
num_images, min_num_images))
print('Full dataset tensor:', dataset.shape)
print('Mean:', np.mean(dataset))
print('Standard deviation:', np.std(dataset))
print('Labels:', labels.shape)
return dataset, labels
示例7: dual_plot
def dual_plot(self, n):
fig = plt.figure()
ax1 = fig.add_subplot(121, axisbg='k')
ax1.set_xlabel("Theta")
ax1.set_ylabel("R")
ax1.imshow(self.data, cmap=cm.hot,origin="lower", \
extent=[0,2*np.pi,self.rmin,self.rmax])
r = np.arange(self.rmin,self.rmax,(self.rmax-self.rmin)/self.nrad)
t = np.arange(0.,2.*np.pi,2.*np.pi/self.nsec)
x = np.ndarray([self.nrad*self.nsec], dtype = float)
y = np.ndarray([self.nrad*self.nsec], dtype = float)
z = np.ndarray([self.nrad*self.nsec], dtype = float)
k = 0
for i in range(self.nrad):
for j in range(self.nsec):
x[k] = r[i]*np.cos(t[j])
y[k] = r[i]*np.sin(t[j])
z[k] = self.data[i,j]
k +=1
xx = np.arange(-self.rmax, self.rmax, (self.rmax-self.rmin)/n)
yy = np.arange(-self.rmax, self.rmax, (self.rmax-self.rmin)/n)
zz = griddata(x,y,z,xx,yy)
ax2 = fig.add_subplot(122, axisbg='k')
ax2.set_xlabel("X")
ax2.set_ylabel("Y")
ax2.imshow(zz, cmap=cm.hot,origin="lower" \
, extent=[-self.rmax,self.rmax,-self.rmax,self.rmax])
plt.show()
示例8: get_label_voxels
def get_label_voxels(self):
#the voxel coordinates of fg and bg labels
if not self.opLabelArray.NonzeroBlocks.ready():
return (None,None)
nonzeroSlicings = self.opLabelArray.NonzeroBlocks[:].wait()[0]
coors1 = [[], [], []]
coors2 = [[], [], []]
for sl in nonzeroSlicings:
a = self.opLabelArray.Output[sl].wait()
w1 = numpy.where(a == 1)
w2 = numpy.where(a == 2)
w1 = [w1[i] + sl[i].start for i in range(1,4)]
w2 = [w2[i] + sl[i].start for i in range(1,4)]
for i in range(3):
coors1[i].append( w1[i] )
coors2[i].append( w2[i] )
for i in range(3):
if len(coors1[i]) > 0:
coors1[i] = numpy.concatenate(coors1[i],0)
else:
coors1[i] = numpy.ndarray((0,), numpy.int32)
if len(coors2[i]) > 0:
coors2[i] = numpy.concatenate(coors2[i],0)
else:
coors2[i] = numpy.ndarray((0,), numpy.int32)
return (coors2, coors1)
示例9: shape_from_header
def shape_from_header(self, hdr):
'''Read the shape of the array described by the header.
The file position after this call is unspecified.
'''
mclass = hdr.mclass
if mclass == mxFULL_CLASS:
shape = tuple(map(int, hdr.dims))
elif mclass == mxCHAR_CLASS:
shape = tuple(map(int, hdr.dims))
if self.chars_as_strings:
shape = shape[:-1]
elif mclass == mxSPARSE_CLASS:
dt = hdr.dtype
dims = hdr.dims
if not (len(dims) == 2 and dims[0] >= 1 and dims[1] >= 1):
return ()
# Read only the row and column counts
self.mat_stream.seek(dt.itemsize * (dims[0] - 1), 1)
rows = np.ndarray(shape=(1,), dtype=dt,
buffer=self.mat_stream.read(dt.itemsize))
self.mat_stream.seek(dt.itemsize * (dims[0] - 1), 1)
cols = np.ndarray(shape=(1,), dtype=dt,
buffer=self.mat_stream.read(dt.itemsize))
shape = (int(rows), int(cols))
else:
raise TypeError('No reader for class code %s' % mclass)
if self.squeeze_me:
shape = tuple([x for x in shape if x != 1])
return shape
示例10: generate_batch
def generate_batch(config, data, unique_neg_data):
global batch_idx, data_idx
batch_size = config['batch_size']
neg_sample_size = config['neg_sample_size']
batch = data.values()[batch_idx]
neg_batch = unique_neg_data.values()[batch_idx]
idx = data_idx[batch_idx]
data_pos_x = np.ones(batch_size) * batch_idx
data_pos_y = np.ndarray(shape=batch_size, dtype=np.int32)
data_neg_y = np.ndarray(shape=neg_sample_size, dtype=np.int32)
for i in xrange(batch_size):
data_pos_y[i] = batch[idx]
idx = (idx + 1) % len(batch)
for i, neg_y_idx in enumerate(random.sample(set(neg_batch), neg_sample_size)):
data_neg_y[i] = neg_y_idx
data_idx[batch_idx] = idx
batch_idx = (batch_idx + 1) % len(data)
return data_pos_x, data_pos_y, data_neg_y
示例11: load
def load(data_folders, min_num_images, max_num_images):
dataset = np.ndarray(
shape=(max_num_images, image_size, image_size), dtype=np.float32)
labels = np.ndarray(shape=(max_num_images), dtype=np.int32)
label_index = 0
image_index = 0
for folder in data_folders:
print folder
for image in os.listdir(folder):
if image_index >= max_num_images:
raise Exception('More images than expected: %d > %d'
(num_images, max_num_images))
image_file = os.path.join(folder, image)
try:
# Loads the file. There seems to be some sort of translation on it.
image_data = (ndimage.imread(image_file).astype(float) - pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[image_index, :, :] = image_data
labels[image_index] = label_index
image_index += 1
except IOError as e:
print 'Could not read: ', image_file, ': ', e, '- skipped.'
label_index += 1
num_images = image_index
dataset = dataset[0:num_images, :, :]
labels = labels[0:num_images]
if num_images < min_num_images:
raise Exception('Many fewer images than expected: %d < %d' %
(num_images, min_num_images))
print 'Full dataset tensor: ', dataset.shape
print 'Mean: ', np.mean(dataset)
print 'Stdev: ', np.std(dataset)
print 'Labels: ', labels.shape
return dataset, labels
示例12: cv
def cv(xs,ys):
errorsums = np.zeros(11)
bestdeg = 0
def sqerror(y, yhat):
return (y-yhat)**2
segment = len(xs)/10
for d in xrange(0,11): #each K
for i in xrange(0,10): #each segment
trp = 0 #points next position in train arrays
tep = 0 #points next position in test arrays
tsi = segment*i #Testdata start index
xtrain = np.ndarray(len(xs)-segment)
ytrain = np.ndarray(len(xs)-segment)
xtest = np.ndarray(segment)
ytest = np.ndarray(segment)
for j in xrange(0,len(xs)): #divide data
if j<tsi or j>=tsi+segment:
xtrain[trp] = xs[j]
ytrain[trp] = ys[j]
trp+=1
else:
xtest[tep] = xs[j]
ytest[tep] = ys[j]
tep+=1
polynomial = fitPolynomial(d, xtrain, ytrain)
for k in xrange(0, len(ytest)):
errorsums[d] += sqerror(ytest[k], polynomial(xtest[k]))
if errorsums[d] < errorsums[bestdeg]:
bestdeg = d
return [bestdeg,errorsums]
示例13: load_gl_buffers
def load_gl_buffers(self):
num = self.n_frags
pos = np.ndarray((num, 4), dtype=np.float32)
seed = np.random.rand(2,num)
pos[:,0] = seed[0,:]
pos[:,1] = 0.0
pos[:,2] = seed[1,:] # z pos
pos[:,3] = 1. # velocity
# pos[:,1] = np.sin(np.arange(0., num) * 2.001 * np.pi / (10*num))
# pos[:,1] *= np.random.random_sample((num,)) / 3. - 0.2
# pos[:,2] = np.cos(np.arange(0., num) * 2.001 * np.pi /(10* num))
# pos[:,2] *= np.random.random_sample((num,)) / 3. - 0.2
# pos[:,0] = 0. # z pos
# pos[:,3] = 1. # velocity
self.pos = pos
self.pos_vbo = vbo.VBO(data=self.pos, usage=GL_DYNAMIC_DRAW, target=GL_ARRAY_BUFFER)
# self.pos_vbo = vbo.VBO(data=self.pos_vect_frags_4_GL, usage=GL_DYNAMIC_DRAW, target=GL_ARRAY_BUFFER)
self.pos_vbo.bind()
self.col_vbo = vbo.VBO(data=self.col_vect_frags_4_GL, usage=GL_DYNAMIC_DRAW, target=GL_ARRAY_BUFFER)
self.col_vbo.bind()
self.vel = np.ndarray((self.n_frags, 4), dtype=np.float32)
self.vel[:,2] = self.pos[:,2] * 2.
self.vel[:,1] = self.pos[:,1] * 2.
self.vel[:,0] = 3.
self.vel[:,3] = np.random.random_sample((self.n_frags, ))
示例14: ch
def ch(X, cIDX, distance="euclidean"):
Nclusters = cIDX.max() + 1
Npoints = len(X)
n = np.ndarray(shape=(Nclusters), dtype=float)
j = 0
for i in range(cIDX.min(), cIDX.max() + 1):
aux = np.asarray([float(b) for b in (cIDX == i)])
n[j] = aux.sum()
j = j + 1
# Clusters
A = np.array([X[np.where(cIDX == i)] for i in range(Nclusters)])
# Centroids
v = np.array([np.sum(Ai, axis=0) / float(Ai.shape[0]) for Ai in A])
ssb = 0
for i in range(Nclusters):
ssb = n[i] * (cdist([v[i]], [np.mean(X, axis=0)], metric=distance)[0][0] ** 2) + ssb
z = np.ndarray(shape=(Nclusters), dtype=float)
for i in range(cIDX.min(), cIDX.max() + 1):
aux = np.array([(cdist([x], [v[i]], metric=distance)[0][0] ** 2) for x in X[cIDX == i]])
z[i] = aux.sum()
ssw = z.sum()
return (ssb / (Nclusters - 1)) / (ssw / (Npoints - Nclusters))
示例15: extract
def extract(self, image, segments):
fs = self.feature_size
bg = 255
regions = numpy.ndarray(shape=(0, fs), dtype=FEATURE_DATATYPE)
for segment in segments:
region = region_from_segment(image, segment)
if self.stretch:
region = cv2.resize(region, (fs, fs))
else:
x, y, w, h = segment
proportion = float(min(h, w)) / max(w, h)
new_size = (fs, int(fs * proportion)) if min(w, h) == h else (int(fs * proportion), fs)
region = cv2.resize(region, new_size)
s = region.shape
new_region = numpy.ndarray((fs, fs), dtype=region.dtype)
new_region[:, :] = bg
new_region[:s[0], :s[1]] = region
region = new_region
regions = numpy.append(regions, region, axis=0)
regions.shape = (len(segments), fs**2)
return regions