本文整理汇总了Python中scipy.product函数的典型用法代码示例。如果您正苦于以下问题:Python product函数的具体用法?Python product怎么用?Python product使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了product函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tag_images_with_color_value
def tag_images_with_color_value(NUM_CLUSTERS = 4, INPUT_FOLDER = './data/covers/'):
isbn = list()
cover_color = list()
files = os.listdir(INPUT_FOLDER)
for eachFile in files:
print eachFile
im = Image.open(INPUT_FOLDER + eachFile)
im = im.resize((50, 50)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
print len(shape)
if len(shape) == 2:
ar = ar.reshape(scipy.product(shape[:1]), shape[1])
else:
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
# finding clusters
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
# cluster centres:\n', codes
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
index_max = scipy.argmax(counts) # find most frequent
peak = codes[index_max]
colour = ''.join(chr(c) for c in peak).encode('hex')
isbn.append(eachFile[:-4])
cover_color.append(colour)
result = zip(isbn, cover_color)
return result
示例2: prblm_8
def prblm_8():
"""Find the largest product of 13 adjacent digits in the following series"""
from scipy import product
series = """73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450""".replace('\n','')
serilist = [int(x) for x in series]
heighestprod = 0
for ii in range(0, len(serilist) - 13):
if product(serilist[ii:ii + 13]) > heighestprod:
heighestprod = product(serilist[ii:ii+13])
heighestset = serilist[ii:ii+13]
return heighestprod, heighestset
示例3: test
def test():
intlist = load_test()
triads = get_groups(intlist, cachefile=TESTCACHE)
x = itergroups(triads).next()
print 'group 1: {}'.format(x)
print 'QE : {}'.format(scipy.product(list(x[0])))
quads = get_groups(intlist, num=4, cachefile=TESTCACHE)
x = itergroups(quads, num=4).next()
print 'group 1: {}'.format(x)
print 'QE : {}'.format(scipy.product(list(x[0])))
示例4: stats
def stats(self, startdate, enddate, mktbasket, avdate, output=False, mappingoverride=None):
"""
Calculates statistics for a fund over a period.
Parameters
----------
startdate : datetime
beginning of statistic period
enddate : datetime
end of statistic period
mktbasket : dict
dictionary of market streams
output : bool
if True, output results to db
mappingoverride : None or mapping dictionary
whether to override the db mapping
Returns
-------
stats : dict
dictionary of statistics
"""
actualstream, projstream = self.project(mktbasket, mappingoverride)
if actualstream[startdate:enddate] is None: return None
if projstream[startdate:enddate] is None: return None
actual = actualstream[startdate:enddate].returns
projected = projstream[startdate:enddate].returns
diff = actual - projected
outdata = {
'TE' : scipy.std(diff) * 100.0 * 100.0,
'BETA' : scipy.cov(projected, actual, bias=1)[1, 0] / scipy.var(projected),
'ALPHA' : (scipy.product(diff + 1.0)) ** (1.0 / diff.size) - 1.0,
'VOL' : scipy.std(actual) * scipy.sqrt(252.0),
'PROJ' : scipy.product(1.0 + projected) - 1.0,
'ACT' : scipy.product(1.0 + actual) - 1.0,
'R2' : 0.0 if scipy.all(actual == 0.0) else scipy.corrcoef(projected, actual)[1, 0] ** 2.0,
'AV' : self.av(avdate),
'DELTA' : self.deltaestimate(avdate)
}
outdata['DIFF'] = outdata['ACT'] - outdata['PROJ']
outdata['PL'] = outdata['DELTA'] * outdata['DIFF'] * 100.0
if output:
cnxn = pyodbc.connect(ORACLESTRING)
cursor = cnxn.cursor()
sql = 'INSERT INTO FUNDOUTPUT VALUES ({0!s},{1!s},{2!s},{3!s},{4!s},{5!s},{6},{7},{8!s},{9!s},{10!s},{11!s},{12!s},{13!s});'
sql = sql.format(self.fundcode, outdata['PROJ'], outdata['ACT'], outdata['DIFF'],
outdata['DELTA'], outdata['PL'], oracledatebuilder(startdate),
oracledatebuilder(enddate), outdata['TE'], outdata['R2'], outdata['BETA'],
outdata['ALPHA'], outdata['VOL'], outdata['AV'])
cursor.execute(sql)
cnxn.commit()
cnxn.close()
return outdata
示例5: stats
self.mapping[indexes[i]] = finalbeta[i]
return self.mapping
def stats(self, startdate, enddate, mktbasket, output = False):
"""
Calculates statistics for a fund over a period.
Parameters
----------
startdate : datetime
beginning of statistic period
enddate : datetime
end of statistic period
mktbasket : dict
dictionary of market streams
output : bool
if True, output results to db
Returns
-------
stats : dict
dictionary of statistics
"""
inputmatrix, fundreturns, indexes, daterange = self.align(startdate, enddate, mktbasket)
if self.mapping and not(inputmatrix is None):
weights = scipy.array([self.mapping[mykey] if mykey in self.mapping else 0.0 for mykey in mktbasket.keys()])
projected = scipy.dot(inputmatrix,weights.reshape(len(indexes),1)).flatten()
actual = fundreturns.flatten()
diff = actual-projected
outdata = {
'TE' : scipy.std(diff)*100.0*100.0,
'BETA' : scipy.cov(projected,actual)[1,0]/scipy.var(projected),
'ALPHA' : (scipy.product(diff+1.0))**(1.0/diff.size)-1.0,
'VOL' : scipy.std(actual)*scipy.sqrt(252.0),
'PROJ' : scipy.product(1.0+projected)-1.0,
'ACT' : scipy.product(1.0+actual)-1.0,
'R2' : 0.0 if scipy.all(actual==0.0) else scipy.corrcoef(projected,actual)[1,0]**2.0,
'AV' : self.av(startdate),
'DELTA' : self.deltaestimate(startdate)
}
outdata['DIFF'] = outdata['ACT']-outdata['PROJ']
outdata['PL'] = outdata['DELTA']*outdata['DIFF']*100.0
if output:
cnxn = pyodbc.connect(ORACLESTRING)
cursor = cnxn.cursor()
sql = 'INSERT INTO FUNDOUTPUT VALUES ({0!s},{1!s},{2!s},{3!s},{4!s},{5!s},{6},{7},{8!s},{9!s},{10!s},{11!s},{12!s},{13!s});'
sql = sql.format(self.fundcode,outdata['PROJ'],outdata['ACT'],outdata['DIFF'],
outdata['DELTA'],outdata['PL'],oracledatebuilder(startdate),
oracledatebuilder(enddate),outdata['TE'],outdata['R2'],outdata['BETA'],
outdata['ALPHA'],outdata['VOL'],outdata['AV'])
cursor.execute(sql)
cnxn.commit()
cnxn.close()
示例6: getDominantColor
def getDominantColor(img_url):
if r.exists(img_url):
cache_result = r.hmget(img_url, ['r', 'g', 'b'])
return cache_result
NUM_CLUSTERS = 5
im = Image.open(StringIO.StringIO(urllib2.urlopen(img_url).read()))
img_arr = scipy.misc.fromimage(im)
img_shape = img_arr.shape
if len(img_shape) > 2:
img_arr = img_arr.reshape(scipy.product(img_shape[:2]), img_shape[2])
codes, _ = scipy.cluster.vq.kmeans(img_arr, NUM_CLUSTERS)
original_codes = codes
for low, hi in [(60, 200), (35, 230), (10, 250)]:
codes = scipy.array([code for code in codes if not (all([c < low for c in code]) or all([c > hi for c in code]))])
if not len(codes):
codes = original_codes
else:
break
vecs, _ = scipy.cluster.vq.vq(img_arr, codes)
counts, bins = scipy.histogram(vecs, len(codes))
index_max = scipy.argmax(counts)
peak = codes[index_max]
color = [int(c) for c in peak[:3]]
r.hmset(img_url, {'r':color[0], 'g':color[1], 'b':color[2]})
#r.expire(img_url, 86400)
return color
示例7: maskLowStddVoxels
def maskLowStddVoxels(self, dds, nMeanDds, nStddDds):
unique = np.unique(sp.where(nStddDds.subd.asarray() <= 1.0/3, dds.subd.asarray(), dds.mtype.maskValue()))
unique = unique[sp.where(unique != dds.mtype.maskValue())]
if (dds.mpi.comm != None):
unique = dds.mpi.comm.allreduce(unique.tolist(), op=mpi.SUM)
unique = np.unique(unique)
rootLogger.info("Unique constant stdd values = %s" % (unique,))
rootLogger.info("Creating mask from unique constant values...")
mskDds = mango.zeros_like(dds, mtype="segmented")
for uVal in unique:
mskDds.asarray()[...] = sp.where(dds.asarray() == uVal, 1, mskDds.asarray())
rootLogger.info("Done creating mask from unique constant values.")
rootLogger.info("Labeling connected constant zero-stdd regions...")
mskDds.updateHaloRegions()
mskDds.mirrorOuterLayersToBorder(False)
self.writeIntermediateDds("_000ZeroStddForLabeling", mskDds)
lblDds = mango.image.label(mskDds, 1)
rootLogger.info("Done labeling connected constant stdd regions.")
self.writeIntermediateDds("_000ZeroStdd", lblDds)
countThresh = 0.01 * sp.product(lblDds.shape)
rootLogger.info("Eliminating large clusters...")
lblDds = mango.image.eliminate_labels_by_size(lblDds, minsz=int(countThresh), val=lblDds.mtype.maskValue())
self.writeIntermediateDds("_000ZeroStddLargeEliminated", lblDds)
rootLogger.info("Assigning mask values...")
mskDds.subd.asarray()[...] = \
sp.where(lblDds.subd.asarray() == lblDds.mtype.maskValue(), True, False)
self.writeIntermediateDds("_000ZeroStddMskd", mskDds)
del lblDds
for tmpDds in [dds, nMeanDds, nStddDds]:
tmpDds.subd.asarray()[...] = \
sp.where(mskDds.subd.asarray(), tmpDds.mtype.maskValue(), tmpDds.subd.asarray())
示例8: testGaussianPValue
def testGaussianPValue(self):
for typePair in [(None, "float32"), ("tomo", None)]:
mtype = typePair[0]
dtype = typePair[1]
mean = 32000.0
stdd = 1000.0
noisDds = mango.data.gaussian_noise(shape=(105,223,240), mean=mean, stdd=stdd, mtype=mtype, dtype=dtype)
pvalDds = \
mango.fmm.gaussian_pvalue(
noisDds,
mean=mean,
stdd=stdd,
sidedness=mango.fmm.PValueSidednessType.RIGHT_SIDEDNESS
)
alpha = 0.05
count = sp.sum(sp.where(pvalDds.asarray() <= alpha, 1, 0))
if (pvalDds.mpi.comm != None):
count = pvalDds.mpi.comm.allreduce(count)
expCount = sp.product(noisDds.shape)*alpha
count = float(count)
relErr = sp.absolute(expCount-float(count))/sp.absolute(max(expCount,count))
rootLogger.info("relErr = %s" % relErr)
self.assertTrue(relErr < 0.10)
示例9: get_dominant_color
def get_dominant_color(image_path):
'''
Parse image and return dominant color in image.
@param image_path: Image path to parse.
@return: Return dominant color, format as hexadecimal number.
'''
# print 'reading image'
im = Image.open(image_path)
im = im.resize((150, 150)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
# print 'finding clusters'
NUM_CLUSTERS = 5
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
# print 'cluster centres:\n', codes
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
index_max = scipy.argmax(counts) # find most frequent
peak = codes[index_max]
colour = ''.join(chr(c) for c in peak).encode('hex')
# print 'most frequent is %s (#%s)' % (peak, colour)
return "#%s" % (colour[0:6])
示例10: determine_dominant_color_in_image
def determine_dominant_color_in_image(self, image):
NUM_CLUSTERS = 5
ar = scipy.misc.fromimage(image)
shape = ar.shape
if len(shape) > 2:
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
codes, _ = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
# print "Before: %s" % codes
original_codes = codes
for low, hi in [(60, 200), (35, 230), (10, 250)]:
codes = scipy.array([code for code in codes
if not ((code[0] < low and code[1] < low and code[2] < low) or
(code[0] > hi and code[1] > hi and code[2] > hi))])
if not len(codes): codes = original_codes
else: break
# print "After: %s" % codes
vecs, _ = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
# colors = [''.join(chr(c) for c in code).encode('hex') for code in codes]
# total = scipy.sum(counts)
# print dict(zip(colors, [count/float(total) for count in counts]))
index_max = scipy.argmax(counts) # find most frequent
peak = codes[index_max]
color = ''.join(chr(c) for c in peak).encode('hex')
# print 'most frequent is %s (#%s)' % (peak, color)
return color[:6]
示例11: calculate_dominant_colors
def calculate_dominant_colors(image, num_clusters):
ar = scipy.misc.fromimage(image)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
codes, _ = scipy.cluster.vq.kmeans(ar.astype(float), num_clusters)
vecs, _ = scipy.cluster.vq.vq(ar, codes)
return ar, shape, codes, vecs
示例12: find_a_dominant_color
def find_a_dominant_color(image):
# K-mean clustering to find the k most dominant color, from:
# http://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image
n_clusters = 5
# Get image into a workable form
im = image.copy()
im = im.resize((150, 150)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
im_shape = ar.shape
ar = ar.reshape(scipy.product(im_shape[:2]), im_shape[2])
ar = np.float_(ar)
# Compute clusters
codes, dist = scipy.cluster.vq.kmeans(ar, n_clusters)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
# Get the indexes of the most frequent, 2nd most frequent, 3rd, ...
sorted_idxs = np.argsort(counts)
# Get the color
peak = codes[sorted_idxs[1]] # get second most frequent color
return [int(i) for i in peak.tolist()] # list comprehension to quickly cast everything to int
示例13: getPredominantColor
def getPredominantColor(filename):
im = Image.open(filename).convert('RGB')
# Convert to numpy array
ar = scipy.misc.fromimage(im)
# Get dimensions
shape = ar.shape
# Convert to bidimensional array of width x height rows and 3 columns (RGB)
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
# Find cluster centers and their distortions
# codes contains the RGB value of the centers
codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS)
# Maps all the pixels in the image to their respective centers
vecs, dist = scipy.cluster.vq.vq(ar, codes)
# Counts the occurances of each color (NUM_CLUSTER different colors after the mapping)
counts, bins = scipy.histogram(vecs, len(codes))
# Find most frequent color
index_max = scipy.argmax(counts)
peak = codes[index_max]
return peak.astype(int)
示例14: getDomIMAGEColor
def getDomIMAGEColor( imName ):
# Reference:
# http://stackoverflow.com/questions/3241929/
# python-find-dominant-most-common-color-in-an-image
# number of k-means clusters
NUM_CLUSTERS = 4
# Open target image
im = imName
im = im.resize((150, 150)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
ar = ar.astype(float)
# Find clusters
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
# Find most frequent
index_max = scipy.argmax(counts)
peak = codes[index_max]
color = ''.join(chr(int(c)) for c in peak).encode('hex')
return (peak, color)
示例15: cluster_colors
def cluster_colors(image_url, num_clusters=5):
"""
Return the most clustered colors of an image.
Use scipy's k-means clustering algorithm.
"""
print 'Reading image...'
response = requests.get(image_url)
im = Image.open(StringIO(response.content))
im = im.resize((150, 150)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
ar = ar.astype(float)
print 'Finding clusters...'
# k-means clustering
codes, dist = scipy.cluster.vq.kmeans(ar, num_clusters)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
sorted_index = sorted(range(len(counts)), key=lambda index: counts[index], reverse=True)
most_common_colors = []
for index in sorted_index:
peak = codes[index]
peak = peak.astype(int)
colour = ''.join(format(c, '02x') for c in peak)
most_common_colors.append('#' + colour)
return most_common_colors