资源简介
这是我自己的代码,主要用来存储,若能帮到其他人,我也很愿意。
代码片段和文件信息
from numpy import *
import matplotlib.pyplot as plt
import pylab
def loadData(str):
fr = open(str)
sArr = [line.strip().split(“\t“) for line in fr.readlines()]
datArr = [[float(s) for s in line[1][1:-1].split(“ “)] for line in sArr]
matA = mat(datArr)
print(matA.shape)
nameArr = [line[0] for line in sArr]
return matA nameArr
def pca(inputM k):
covM = cov(inputM rowvar=0)
s V = linalg.eig(covM)
paixu = argsort(s)
paixuk = paixu[:-(k+1):-1]
kwei = V[:paixuk]
outputM = inputM * kwei
chonggou = (outputM * kwei.T)
return outputMchonggou
def plotV(a labels):
fig = plt.figure()
ax = fig.add_subplot(111)
print(“aaa“)
font = { ‘fontname‘:‘Tahoma‘ ‘fontsize‘:0.5 ‘verticalalignment‘: ‘top‘ ‘horizontalalignment‘:‘center‘ }
ax.scatter(a[:0] a[:1] marker = ‘ ‘)
ax.set_xlim(-0.80.8)
ax.set_ylim(-0.80.8)
i = 0
for label x y in zip(labels a[: 0] a[: 1]):
i += 1
s = random.uniform(0100)
if i<14951:
if s > 3.1:
continue
else:
if s > 6.7:
continue
ax.annotate(label xy = (x y) xytext = None ha = ‘right‘ va = ‘bottom‘ **font)
plt.title(‘TransE pca2dim‘)
plt.xlabel(‘X‘)
plt.ylabel(‘Y‘)
print(“ddd“)
plt.savefig(‘plot_with_labels‘ dpi = 3000 bbox_inches = ‘tight‘ orientation = ‘landscape‘ papertype = ‘a0‘)
if __name__ == ‘__main__‘:
dirEntity = “c:\\entityVector.txt“
dirRelation = “c:\\relationVector.txt“
matEntity nameEntity = loadData(dirEntity)
matRelation nameRelation = loadData(dirRelation)
mat = row_stack((matEntity matRelation))
print(mat.shape)
nameEntity.extend(nameRelation)
k = 2
a b = pca(mat k)
plotV(a nameEntity)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 603035 2014-09-04 12:37 transE-master\data\entity2id.txt
文件 360 2014-09-04 12:37 transE-master\data\relation2id.txt
文件 168947 2014-09-04 12:37 transE-master\data\test.txt
文件 100 2019-04-15 09:57 transE-master\data\testLittle.txt
文件 4758553 2014-09-04 12:37 transE-master\data\train.txt
文件 1824 2016-04-26 01:31 transE-master\pca.py
文件 412 2016-04-26 01:31 transE-master\README.md
文件 87845336 2019-04-09 10:14 transE-master\result\entityVector.txt
文件 40864 2019-04-09 10:14 transE-master\result\relationVector.txt
文件 341878 2019-04-09 12:11 transE-master\result1\testHeadRaw.txt
文件 402971 2019-04-09 12:11 transE-master\result1\testRelationRaw.txt
文件 341817 2019-04-09 13:31 transE-master\result1\testTailRaw.txt
文件 859 2016-04-26 01:31 transE-master\reTranE.py
文件 6232 2019-04-15 09:12 transE-master\test.py
文件 4618 2019-04-23 15:26 transE-master\testDataTrue.py
文件 24 2019-04-19 09:38 transE-master\testJava.py
文件 11276 2019-04-24 09:11 transE-master\tranE.py
目录 0 2019-04-15 09:58 transE-master\data
目录 0 2019-04-09 10:14 transE-master\result
目录 0 2019-04-09 13:31 transE-master\result1
目录 0 2019-04-24 09:11 transE-master
----------- --------- ---------- ----- ----
94529106 21
- 上一篇:BOOK_软件工程+理论+方法与实践
- 下一篇:基于ssm框架的人事管理系统
评论
共有 条评论