def vectorize_and_dot_dataset(dataset):
dots = numpy.zeros(len(dataset))
labels = numpy.zeros(len(dataset))
i = 0
for row in dataset:
#Get the vectors for each word in the body and headline
split_headline = hf.split_words(row['headline'])
split_body = hf.split_words_special(row['body'], split_code)
headline_vectors = vectorize_wordlist(split_headline)
body_vectors = vectorize_wordlist(split_body)
#Sum the words in the body, sum the words in the headline
summed_headline_vector = numpy.sum(headline_vectors, axis=0)
summed_body_vector = numpy.sum(body_vectors, axis=0)
#Normalize
normalized_headline_vector = normalize(summed_headline_vector.reshape(1,-1))
normalized_body_vector = normalize(summed_body_vector.reshape(1,-1))
#Save the row vector
row['row_vector'] = numpy.concatenate( (normalized_headline_vector[0], normalized_body_vector[0]), axis=0)
row['isRelated'] = 0 if row['stance'] == 'unrelated' else 1
# Data relating to the relationship between the headline/body can be appended to row['row_vector']
if True:
extra_nodes = []
#Save the dot product
dot = numpy.vdot(normalized_headline_vector, normalized_body_vector)
extra_nodes.append(dot)
#Jaccard distance
jaccard = jaccard_distance(set(split_headline), set(split_body))
extra_nodes.append(jaccard)
#Sentiment analysis
extra_nodes.append( sentiment_analyzer.polarity_scores(row['headline'])['compound'] )
extra_nodes.append( sentiment_analyzer.polarity_scores(" ".join(split_body))['compound'] )
row['row_vector'] = numpy.append(row['row_vector'], extra_nodes)
# return dots, labels
python类vdot()的实例源码
def test_refcount_vdot(self, level=rlevel):
# Changeset #3443
_assert_valid_refcount(np.vdot)
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order='C')
b = np.array([[1, 2], [3, 4]], order='F')
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def _calculate(self):
min_, max_ = self._bounds
n = np.prod(self.reference.shape)
f = n * (max_ - min_)**2
diff = self.other - self.reference
value = np.vdot(diff, diff) / f
# calculate the gradient only when needed
self._g_diff = diff
self._g_f = f
gradient = None
return value, gradient
model2.py 文件源码
项目:movie-recommendation-using-RBM
作者: pinkeshbadjatiya
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def calc_cost(self):
cost = sum(self.E[key] * self.E[key] for key in self.E)
cost += self.l*(np.vdot(self.B, self.B) + np.vdot(self.C, self.C))
return cost
test_regression.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_refcount_vdot(self, level=rlevel):
# Changeset #3443
_assert_valid_refcount(np.vdot)
test_multiarray.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order='C')
b = np.array([[1, 2], [3, 4]], order='F')
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def test_blas_dot(backend, n):
b = backend()
x = (np.random.rand(n) + 1j * np.random.rand(n))
y = (np.random.rand(n) + 1j * np.random.rand(n))
x = np.require(x, dtype=np.dtype('complex64'), requirements='F')
y = np.require(y, dtype=np.dtype('complex64'), requirements='F')
x_d = b.copy_array(x)
y_d = b.copy_array(y)
y_exp = np.vdot(x, y).real
y_act = b.dot(x_d, y_d)
np.testing.assert_allclose(y_exp, y_act, atol=1e-5)
def dot(self, x, y):
""" returns x^T * y """
assert isinstance(x, self.dndarray)
assert isinstance(y, self.dndarray)
return np.vdot( x._arr, y._arr ).real
def test_refcount_vdot(self, level=rlevel):
# Changeset #3443
_assert_valid_refcount(np.vdot)
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order='C')
b = np.array([[1, 2], [3, 4]], order='F')
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def test_refcount_vdot(self, level=rlevel):
# Changeset #3443
_assert_valid_refcount(np.vdot)
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order='C')
b = np.array([[1, 2], [3, 4]], order='F')
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def test_refcount_vdot(self, level=rlevel):
# Changeset #3443
_assert_valid_refcount(np.vdot)
def test_vdot_array_order(self):
a = np.array([[1, 2], [3, 4]], order='C')
b = np.array([[1, 2], [3, 4]], order='F')
res = np.vdot(a, a)
# integer arrays are exact
assert_equal(np.vdot(a, b), res)
assert_equal(np.vdot(b, a), res)
assert_equal(np.vdot(b, b), res)
def Tdot(self, fn, dtype):
from pyculib.blas.binding import cuBlas
x = np.random.random(10).astype(dtype)
y = np.random.random(10).astype(dtype)
d_x = cuda.to_device(x)
d_y = cuda.to_device(y)
blas = cuBlas()
got = getattr(blas, fn)(x.size, d_x, 1, d_y, 1)
if fn.endswith('c'):
exp = np.vdot(x, y)
else:
exp = np.dot(x, y)
self.assertTrue(np.allclose(got, exp))
def Tdotc(self, fn, dtype):
x = np.random.random(10).astype(dtype)
y = np.random.random(10).astype(dtype)
got = self.blas.dotc(x, y)
exp = np.vdot(x, y)
self.assertTrue(np.allclose(got, exp))
def compute_correlation(matrix_a, matrix_b):
correlation = numpy.vdot(matrix_a, matrix_b)
correlation /= numpy.linalg.norm(matrix_a)*numpy.linalg.norm(matrix_b)
correlation = (correlation + 1)/2
return correlation
def berry_phase(H,path,ns):
'''
Calculate the Berry phase of some bands of a Hamiltonian along a certain path.
Parameters
----------
H : callable
Input function which returns the Hamiltonian as a 2D array.
path : iterable
The path along which to calculate the Berry phase.
ns : iterable of int
The sequences of bands whose Berry phases are wanted.
Returns
-------
1d ndarray
The wanted Berry phase of the selected bands.
'''
ns=np.array(ns)
for i,parameters in enumerate(path):
new=eigh(H(**parameters))[1][:,ns]
if i==0:
result=np.ones(len(ns),new.dtype)
evs=new
else:
for j in xrange(len(ns)):
result[j]*=np.vdot(old[:,j],new[:,j])
old=new
else:
for j in xrange(len(ns)):
result[j]*=np.vdot(old[:,j],evs[:,j])
return np.angle(result)/np.pi
def iter(self):
'''
The Lanczos iteration.
'''
while len(self.candidates)>0:
v=self.candidates.pop(0)
norm=nl.norm(v)
if norm>self.dtol:
break
elif self.niter>=self.nc:
self.deflations.append(self.niter-self.nc)
else:
self.stop=True
if not self.stop:
self.vectors[self.niter]=v/norm
if self.niter-self.nc-1>=0:
self._T_[self.niter,self.niter-self.nc-1]=norm
else:
self.P[self.niter,self.niter-self.nc-1+self.nv0]=norm
for k,vc in enumerate(self.candidates):
overlap=np.vdot(self.vectors[self.niter],vc)
if k+self.niter>=self.nc:
self._T_[self.niter,k+self.niter-self.nc]=overlap
else:
self.P[self.niter,self.niter-self.nc+k+self.nv0]=overlap
vc-=overlap*self.vectors[self.niter]
v=self.matrix.dot(self.vectors[self.niter])
for k in xrange(max(self.niter-self.nc-1,0),self.niter):
self._T_[k,self.niter]=np.conjugate(self._T_[self.niter,k])
v-=self._T_[k,self.niter]*self.vectors[k]
for k in it.chain(self.deflations,[self.niter]):
overlap=np.vdot(self.vectors[k],v)
if k in set(self.deflations)|{self.niter}:
self._T_[k,self.niter]=overlap
self._T_[self.niter,k]=np.conjugate(overlap)
v-=overlap*self.vectors[k]
self.candidates.append(v)
if not self.keepstate and self.niter>=self.nc and self.niter-self.nc not in self.deflations: self.vectors[self.niter-self.nc]=None
self.niter+=1