本文整理汇总了Python中numpy.alen函数的典型用法代码示例。如果您正苦于以下问题:Python alen函数的具体用法?Python alen怎么用?Python alen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了alen函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: get_data
def get_data(self, orig):
data = self.serializer.clipboard_data
data_len = np.alen(data)
orig_len = np.alen(orig)
if data_len > orig_len > 1:
data_len = orig_len
return data[0:data_len]
开发者ID:robmcmullen,项目名称:omnivore,代码行数:7,代码来源:clipboard_commands.py
示例2: eccentricity
def eccentricity(data, exponent=1., metricpar={}, callback=None):
if data.ndim==1:
assert metricpar=={}, 'No optional parameter is allowed for a dissimilarity matrix.'
ds = squareform(data, force='tomatrix')
if exponent in (np.inf, 'Inf', 'inf'):
return ds.max(axis=0)
elif exponent==1.:
ds = np.power(ds, exponent)
return ds.sum(axis=0)/float(np.alen(ds))
else:
ds = np.power(ds, exponent)
return np.power(ds.sum(axis=0)/float(np.alen(ds)), 1./exponent)
else:
progress = progressreporter(callback)
N = np.alen(data)
ecc = np.empty(N)
if exponent in (np.inf, 'Inf', 'inf'):
for i in range(N):
ecc[i] = cdist(data[(i,),:], data, **metricpar).max()
progress((i+1)*100//N)
elif exponent==1.:
for i in range(N):
ecc[i] = cdist(data[(i,),:], data, **metricpar).sum()/float(N)
progress((i+1)*100//N)
else:
for i in range(N):
dsum = np.power(cdist(data[(i,),:], data, **metricpar),
exponent).sum()
ecc[i] = np.power(dsum/float(N), 1./exponent)
progress((i+1)*100//N)
return ecc
开发者ID:Sandy4321,项目名称:mapper,代码行数:31,代码来源:filters.py
示例3: candlestick_trades
def candlestick_trades(samplet, lookback, t, px, sz):
#requires = ["CONTIGUOUS", "ALIGNED"]
lib = _load_candlestick_lib()
lib.c_candlestick.restype = None
lib.c_candlestick.argtypes = [np.ctypeslib.c_intp,
np.ctypeslib.ndpointer(float,
flags="aligned, contiguous"),
ctypes.c_double,
np.ctypeslib.c_intp,
np.ctypeslib.ndpointer(float, ndim=1,
flags="aligned, contiguous"),
np.ctypeslib.ndpointer(float, ndim=1,
flags="aligned, contiguous"),
np.ctypeslib.ndpointer(float, ndim=1,
flags="aligned, contiguous"),
np.ctypeslib.ndpointer(float, ndim=1,
flags="aligned, contiguous,"
"writeable")]
# samplet = np.require(samplet, float, requires)
# c = np.empty_like(a)
samplelen = np.alen(samplet)
datalen = np.alen(t)
res = np.empty(6*samplelen)
lib.c_candlestick(samplelen, samplet, lookback, datalen, t, px, sz, res)
return res
开发者ID:tambu-j,项目名称:signals,代码行数:27,代码来源:candlestick.py
示例4: _do_problem
def _do_problem(self, problem, integrator, old_api=True, **integrator_params):
jac = None
if hasattr(problem, 'jac'):
jac = problem.jac
res = problem.res
ig = dae(integrator, res, jacfn=jac, old_api=old_api)
ig.set_options(old_api=old_api, **integrator_params)
z = empty((1+len(problem.stop_t),alen(problem.z0)), float)
zprime = empty((1+len(problem.stop_t),alen(problem.z0)), float)
ist = ig.init_step(0., problem.z0, problem.zprime0, z[0], zprime[0])
i=1
for time in problem.stop_t:
soln = ig.step(time, z[i], zprime[i])
if old_api:
flag, rt = soln
else:
flag = soln.flag
rt = soln.values.t
i += 1
if integrator == 'ida':
assert flag==0, (problem.info(), flag)
else:
assert flag > 0, (problem.info(), flag)
assert problem.verify(array(z), array(zprime), [0.]+problem.stop_t), \
(problem.info(),)
开发者ID:adhalanay,项目名称:odes,代码行数:27,代码来源:test_dae.py
示例5: competence
def competence(stochastic):
"""
The competence function for TWalk.
"""
if stochastic.dtype in float_dtypes and np.alen(stochastic.value) > 4:
if np.alen(stochastic.value) >=10:
return 2
return 1
return 0
开发者ID:along1x,项目名称:pymc,代码行数:9,代码来源:TWalk.py
示例6: tableSum
def tableSum(self):
"""docstring for tableSum"""
self.fsum = 0
for i in xrange(numpy.alen(self.newx)):
for j in xrange(numpy.alen(self._angles)):
self.fsum += self.newx[i]*1.15*self._angles[j]*math.fabs(self.interpedgrid[i,j])
return self.fsum
开发者ID:jlerche,项目名称:muon_inflight_decay,代码行数:9,代码来源:muonsim3.py
示例7: create_binary
def create_binary(filename, num, outfile, options):
"""Create patterned binary data with the first 7 characters of the filename
interleaved with a byte ramp, e.g. A128 \x00A128 \x01A128 \x02 etc.
"""
root, _ = outfile.split(".")
prefix = ("%s " % root)[0:8]
a = np.fromstring(prefix, dtype=np.uint8)
b = np.tile(a, (num / np.alen(a)) + 1)[0:num]
b[7::8] = np.arange(np.alen(b) / 8, dtype=np.uint8)
with open(filename, "wb") as fh:
fh.write(b.tostring())
开发者ID:pombredanne,项目名称:atrcopy,代码行数:11,代码来源:create_binary.py
示例8: add_xexboot_header
def add_xexboot_header(bytes, bootcode=None, title="DEMO", author="an atari user"):
sec_size = 128
xex_size = len(bytes)
num_sectors = (xex_size + sec_size - 1) / sec_size
padded_size = num_sectors * sec_size
if xex_size < padded_size:
bytes = np.append(bytes, np.zeros([padded_size - xex_size], dtype=np.uint8))
paragraphs = padded_size / 16
if bootcode is None:
bootcode = np.fromstring(xexboot_header, dtype=np.uint8)
else:
# don't insert title or author in user supplied bootcode; would have to
# assume that the user supplied everything desired in their own code!
title = ""
author = ""
bootsize = np.alen(bootcode)
v = bootcode[9:11].view(dtype="<u2")
v[0] = xex_size
bootsectors = np.zeros([384], dtype=np.uint8)
bootsectors[0:bootsize] = bootcode
insert_string(bootsectors, 268, title, 0b11000000)
insert_string(bootsectors, 308, author, 0b01000000)
image = np.append(bootsectors, bytes)
return image
开发者ID:pombredanne,项目名称:atrcopy,代码行数:28,代码来源:kboot.py
示例9: classify
def classify(data, trueclass, traindata, final_set,a):
X=np.vstack(data[traindata[:,1],:])
#np.savetxt("parkinsons/foo.csv",x, fmt='%0.5f',delimiter=",")
b=[]
b.append(traindata[:,1])
C = np.searchsorted(a, b)
D = np.delete(np.arange(np.alen(a)), C)
D= np.array(D)
D=D.reshape(D.size,-1)
true_labels = np.ravel(np.vstack(trueclass[D[:,0],0]))
test_data = np.vstack(data[D[:,0],:])
#print test_data.shape
#np.savetxt("parkinsons/foo.csv",test_data, fmt='%0.6s')
y=np.ravel(np.vstack(traindata[:,0]))
clf=svm.SVC(kernel='linear')
clf.fit(X,y)
labels=clf.predict(test_data) #predicting true labels for the remaining rows
predicted_labels = labels.reshape(labels.size,-1)
np.savetxt("parkinsons/foo%d.csv"%final_set, np.concatenate((test_data, predicted_labels,np.vstack(trueclass[D[:,0],0])), axis=1),fmt='%0.5f',delimiter=",")
print true_labels
print labels
misclassify_rate = 1-accuracy_score(true_labels,labels)
print "Misclassification rate = %f" %misclassify_rate
return misclassify_rate
开发者ID:pranithasurya,项目名称:MachineLearning,代码行数:29,代码来源:ClassifyParkinsons.py
示例10: distance_to_measure
def distance_to_measure(data, k, metricpar={}, callback=None):
r'''.. math::
\mathit{distance\_to\_measure}(x) = \sqrt{\frac 1k\sum^k_{j=1}d(x,\nu_j(x))^2},
where :math:`\nu_1(x),\ldots,\nu_k(x)` are the :math:`k` nearest neighbors of :math:`x` in the data set. Again, the first nearest neighbor is :math:`x` itself with distance 0.
Reference: [R4]_.
'''
if data.ndim==1:
assert metricpar=={}, ('No optional parameter is allowed for a '
'dissimilarity matrix.')
# dm data
ds = squareform(data, force='tomatrix')
N = np.alen(ds)
r = np.empty(N)
for i in range(N):
s = np.sort(ds[i,:])
assert s[0]==0.
d = s[1:k]
r[i] = np.sqrt((d*d).sum()/float(k))
return r
else:
# vector data
if metricpar=={} or metricpar['metric']=='euclidean':
from scipy.spatial import cKDTree
T = cKDTree(data)
d, j = T.query(data, k+1)
d = d[:,1:k]
return np.sqrt((d*d).sum(axis=1)/k)
else:
print(kwargs)
raise ValueError('Not implemented')
开发者ID:Sandy4321,项目名称:mapper,代码行数:33,代码来源:filters.py
示例11: kNN_distance
def kNN_distance(data, k, metricpar={}, callback=None):
r'''The distance to the :math:`k`-th nearest neighbor as an (inverse) measure of density.
Note how the number of nearest neighbors is understood: :math:`k=1`, the first neighbor, makes no sense for a filter function since the first nearest neighbor of a data point is always the point itself, and hence this filter function is constantly zero. The parameter :math:`k=2` measures the distance from :math:`x_i` to the nearest data point other than :math:`x_i` itself.
'''
if data.ndim==1:
assert metricpar=={}, ('No optional parameter is allowed for a '
'dissimilarity matrix.')
# dm data
ds = squareform(data, force='tomatrix')
N = np.alen(ds)
r = np.empty(N)
for i in range(N):
s = np.sort(ds[i,:])
assert s[0]==0.
r[i] = s[k]
return r
else:
# vector data
if metricpar=={} or metricpar['metric']=='euclidean':
from scipy.spatial import cKDTree
T = cKDTree(data)
d, j = T.query(data, k+1)
return d[:,k]
else:
print(metricpar)
raise ValueError('Not implemented')
开发者ID:Sandy4321,项目名称:mapper,代码行数:27,代码来源:filters.py
示例12: availability
def availability(self):
availability={}
for key in self.magnet_sets:
availability[key]=range(np.alen(self.magnet_sets[key]))
return availability
开发者ID:DiamondLightSource,项目名称:Opt-ID,代码行数:7,代码来源:magnets.py
示例13: testLBP
def testLBP (format, formatMask, path, output) :
dataset = pd.read_csv(path)
idxCls = dataset['idx']
# cnts = dataset['Cnt']
fnList = dataset['path']
# out = open(output, 'w')
lbps = list(map(lambda x: local_binary_pattern(cv2.bitwise_and(imread(format.format(x)),imread(formatMask.format(x))), lbpP, lbpR, lbpMethod), fnList))
histograms = list(map(lambda x: np.histogram(x, bins=range(int(np.max(lbps)) + 1))[0], lbps))
distances = prw.pairwise_distances(histograms, metric='l1')
np.fill_diagonal(distances, math.inf)
guessedClasses = np.apply_along_axis(lambda x: np.argmin(x), 1, distances)
scores = np.apply_along_axis(lambda x: np.min(x), 1, distances)
correct = list(map(lambda i: idxCls[guessedClasses[i]] == idxCls[i], range(0, np.alen(idxCls))))
# out.write(str(np.average(correct)))
# fpr, tpr, thresholds = roc_curve(correct, scores, pos_label=1)
# pyplot.plot(tpr, fpr)
# pyplot.show()
with open(output + 'lbp_distances.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
a.writerows(distances)
with open(output + 'lbp_guessedClasses.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
a.writerow(guessedClasses)
with open(output + 'lbp_correct.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
a.writerow(correct)
with open(output + 'lbp_real.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
a.writerow(idxCls)
开发者ID:mariaTatarintseva,项目名称:Diploma,代码行数:32,代码来源:LBPSpeedUp.py
示例14: fit_model
def fit_model(self):
if self.similarity_matrix is None:
self._init_similarity_matrix()
self.means = []
for i in xrange(self.dataset.n_items):
i_ = self.item_user_matrix[i][self.item_user_matrix[i] > 0]
self.means.append(np.mean(i_) if not np.alen(i_) == 0 else 0)
开发者ID:cenaka,项目名称:board-game-recommend,代码行数:7,代码来源:item_based_knn.py
示例15: recall_gain
def recall_gain(tp, fn, fp, tn):
"""Calculates Recall Gain from the contingency table
This function calculates Recall Gain from the entries of the contingency
table: number of true positives (TP), false negatives (FN), false positives
(FP), and true negatives (TN). More information on Precision-Recall-Gain
curves and how to cite this work is available at
http://www.cs.bris.ac.uk/~flach/PRGcurves/.
Args:
tp (float) or ([float]): True Positives
fn (float) or ([float]): False Negatives
fp (float) or ([float]): False Positives
tn (float) or ([float]): True Negatives
Returns:
(float) or ([float])
"""
n_pos = tp + fn
n_neg = fp + tn
with np.errstate(divide='ignore', invalid='ignore'):
rg = 1. - (n_pos/n_neg) * (fn/tp)
if np.alen(rg) > 1:
rg[tn + fn == 0] = 1
elif tn + fn == 0:
rg = 1
return rg
开发者ID:JohnReid,项目名称:prg,代码行数:26,代码来源:prg.py
示例16: __init__
def __init__(self, pos):
vals = np.array([np.float(val) for val in pos.split(";")])
numOfVariables = (np.alen(vals) - 3) / 4
"""
self.particlePosition = [np.float64(val) for val in vals[0].split(",")];
self.velocity = [np.float64(val) for val in vals[1].split(",")];
self.fitness = [np.float64(val) for val in vals[2].split(",")];
self.persBestPos = [np.float64(val) for val in vals[3].split(",")];
self.persBestVal = [np.float64(val) for val in vals[4].split(",")];
self.globalBestPos = [np.float64(val) for val in vals[5].split(",")];
self.globalBestVal = [np.float64(val) for val in vals[6].split(",")];
"""
index = 0
self.particlePosition = vals[index : index + numOfVariables]
index += numOfVariables
self.velocity = vals[index : index + numOfVariables]
index += numOfVariables
self.fitness = vals[index : index + 1]
index += 1
self.persBestPos = vals[index : index + numOfVariables]
index += numOfVariables
self.persBestVal = vals[index : index + 1]
index += 1
self.globalBestPos = vals[index : index + numOfVariables]
index += numOfVariables
self.globalBestVal = vals[index : index + 1]
开发者ID:ocatak,项目名称:Hadoop-Particle-Swarm-Optimization,代码行数:27,代码来源:Particle.py
示例17: learn_option
def learn_option(option, environment_name, num_episodes, max_steps):
"""
:param source: the source community
:type source: int
:param target: the target community
:param target: int
"""
from pyrl.agents.sarsa_lambda import sarsa_lambda
from pyrl.rlglue import RLGlueLocal as RLGlueLocal
from pyrl.environments.pinball import PinballRLGlue
import numpy as np
import logging
import pyflann
import options
import cPickle
import random
import csv
prefix = 'option-%d-to-%d'%(option.label, option.target)
score_file = csv.writer(open(prefix + '-score.csv', 'wb'))
# Create agent and environments
agent = sarsa_lambda(epsilon=0.01, alpha=0.001, gamma=0.9, lmbda=0.9,
params={'name':'fourier', 'order':4})
# Wrap the environment with the option's pseudo-reward
environment = options.TrajectoryRecorder(options.PseudoRewardEnvironment(PinballRLGlue(environment_name), option, 10000), prefix + '-trajectory')
# Connect to RL-Glue
rlglue = RLGlueLocal.LocalGlue(environment, agent)
rlglue.RL_init()
# Execute episodes
if not num_episodes:
num_episodes = np.alen(option.initial_states)
print 'Learning %d episodes'%(num_episodes,)
for i in xrange(num_episodes):
initial_state = option.initial_state()
rlglue.RL_env_message('set-start-state %f %f %f %f'
%(initial_state[0], initial_state[1], initial_state[2], initial_state[3]))
terminated = rlglue.RL_episode(max_steps)
total_steps = rlglue.RL_num_steps()
total_reward = rlglue.RL_return()
with open(prefix + '-score.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([i, total_steps, total_reward, terminated])
rlglue.RL_cleanup()
# Save function approximation
option.basis = agent.basis
option.weights = agent.weights[0,:,:]
cPickle.dump(option, open(prefix + '-policy.pl', 'wb'))
return option
开发者ID:gandalfvn,项目名称:skill-acquisition,代码行数:60,代码来源:learn-options.py
示例18: ExpectationMaximization
def ExpectationMaximization(dataset):
# dimension of the space
N = np.alen(dataset[0])
m = 10
minw = 0.01
minsigma = 0.01
# mu: esperance
# sigma2: variance
# w: mixing weight
mu, sigma2, w = initParameters(m, N)
epsi = 0.1
conv = False
while not conv:
Elikelihood = 0
# for each mixture component
for j in range(m):
# Expectation
# gamma: responsibility values
gamma = w[j] * gaussian2(dataset, mu[j], sigma2[j], N)
Nwj = np.sum(gamma)
gamma = gamma/Nwj
# Maximization (of the likelihood)
gammat = np.array([gamma]).T
mu[j] = np.sum( gammat * dataset, 0 ) / Nwj
sigma2[j] = np.sum( gammat * ((dataset - mu[j]) ** 2), 0 ) / Nwj
w[j] = Nwj/N
# prevent variances from reaching 0
sigma2[j] = map(lambda sig2: sig2 * (sig2 >= minsigma) or minsigma, sigma2[j])
# prevent mixin coefficient from reaching 0
if w[j] < minw:
w[j] = minw
Elikelihood -= np.log(Nwj)
print Elikelihood
conv = np.abs(Elikelihood) < epsi
return (w, mu, sigma2)
开发者ID:Tug,项目名称:mnist,代码行数:35,代码来源:gmm.py
示例19: plot_counts
def plot_counts(ax, dictorigin, x_locator, x_formatter, bin_edges_in, snum, enum):
# compute all data needed
time = dictorigin["time"]
cumcounts = np.arange(1, np.alen(time) + 1)
if len(bin_edges_in) < 2:
return
binsize = bin_edges_in[1] - bin_edges_in[0]
binsize_str = binsizelabel(binsize)
# plot
counts, bin_edges_out, patches = ax.hist(
time, bin_edges_in, cumulative=False, histtype="bar", color="black", edgecolor=None
)
ax.grid(True)
ax.xaxis_date()
plt.setp(ax.get_xticklabels(), rotation=90, horizontalalignment="center", fontsize=7)
ax.set_ylabel("# Earthquakes\n%s" % binsize_str, fontsize=8)
ax.xaxis.set_major_locator(x_locator)
ax.xaxis.set_major_formatter(x_formatter)
if snum and enum:
ax.set_xlim(snum, enum)
ax2 = ax.twinx()
p2, = ax2.plot(time, cumcounts, "g", lw=2.5)
ax2.yaxis.get_label().set_color(p2.get_color())
ytl_obj = plt.getp(ax2, "yticklabels") # get the properties for yticklabels
# plt.getp(ytl_obj) # print out a list of properties
plt.setp(ytl_obj, color="g") # set the color of yticks to red
plt.setp(plt.getp(ax2, "yticklabels"), color="g") # xticklabels: same
ax2.set_ylabel("Cumulative\n# Earthquakes", fontsize=8)
ax2.xaxis.set_major_locator(x_locator)
ax2.xaxis.set_major_formatter(x_formatter)
if snum and enum:
ax2.set_xlim(snum, enum)
return
开发者ID:gthompson,项目名称:python_gt,代码行数:35,代码来源:modgiseis.py
示例20: get_bitplanes
def get_bitplanes(self, segment_viewer, bytes_per_row, nr, count, byte_values, style, colors):
bitplanes = self.bitplanes
_, rem = divmod(np.alen(byte_values), bitplanes)
if rem > 0:
byte_values = np.append(byte_values, np.zeros(rem, dtype=np.uint8))
style = np.append(style, np.zeros(rem, dtype=np.uint8))
pixels_per_row = 8 * bytes_per_row // bitplanes
bits = np.unpackbits(byte_values).reshape((-1, 8))
pixels = np.empty((nr * bytes_per_row // bitplanes, pixels_per_row), dtype=np.uint8)
self.get_bitplane_pixels(bits, pixels, bytes_per_row, pixels_per_row)
pixels = pixels.reshape((nr, pixels_per_row))
s = self.get_bitplane_style(style)
style_per_pixel = s.repeat(8).reshape((-1, pixels_per_row))
normal = (style_per_pixel & self.ignore_mask) == 0
highlight = (style_per_pixel & style_bits.selected_bit_mask) == style_bits.selected_bit_mask
data = (style_per_pixel & style_bits.data_bit_mask) == style_bits.data_bit_mask
comment = (style_per_pixel & style_bits.comment_bit_mask) == style_bits.comment_bit_mask
match = (style_per_pixel & style_bits.match_bit_mask) == style_bits.match_bit_mask
color_registers, h_colors, m_colors, c_colors, d_colors = colors
bitimage = np.empty((nr, pixels_per_row, 3), dtype=np.uint8)
for i in range(2**bitplanes):
color_is_set = (pixels == i)
bitimage[color_is_set & normal] = color_registers[i]
bitimage[color_is_set & data] = d_colors[i]
bitimage[color_is_set & comment] = c_colors[i]
bitimage[color_is_set & match] = m_colors[i]
bitimage[color_is_set & highlight] = h_colors[i]
bitimage[count:,:,:] = segment_viewer.preferences.empty_background_color.Get(False)
return bitimage
开发者ID:robmcmullen,项目名称:omnivore,代码行数:30,代码来源:bitmap_renderers.py
注:本文中的numpy.alen函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论