数据挖掘实验报告

  • 格式:doc
  • 大小:493.16 KB
  • 文档页数:29

中科大数据挖掘实验报告姓名樊涛声班级软设一班学号SA15226248实验一K邻近算法实验一实验内容使用k近邻算法改进约会网站的配对效果。

海伦使用约会网址寻找适合自己的约会对象,约会网站会推荐不同的人选。

她将曾经交往过的的人总结为三种类型:(1)不喜欢的人(2)魅力一般的人(3)极具魅力的人尽管发现了这些规律,但依然无法将约会网站提供的人归入恰当的分类。

使用KNN算法,更好的帮助她将匹配对象划分到确切的分类中。

二实验要求(1)独立完成kNN实验,基本实现可预测的效果(2)实验报告(3)开放性:可以自己增加数据或修改算法,实现更好的分类效果三实验步骤(1)数据源说明实验给出的数据源为datingTestSet.txt,共有4列,每一列的属性分别为:①percentage of time spenting playing vedio games;②frequent flied miles earned per year;③liters of ice cream consumed per year;④your attitude towars this people。

通过分析数据源中的数据,得到规律,从而判断一个人的前三项属性来得出划分海伦对他的态度。

(2)KNN算法原理对未知属性的某数据集中的每个点一次执行以下操作①计算已知类别数据集中的每一个点和当前点的距离②按照距离递增依次排序③选取与当前点距离最小的k个点④确定k个点所在类别的出现频率⑤返回k个点出现频率最高的点作为当前点的分类(3)KNN算法实现①利用python实现构造分类器首先计算欧式距离然后选取距离最小的K个点代码如下:def classify(inMat,dataSet,labels,k):dataSetSize=dataSet.shape[0]#KNN的算法核心就是欧式距离的计算,一下三行是计算待分类的点和训练集中的任一点的欧式距离diffMat=tile(inMat,(dataSetSize,1))-dataSetsqDiffMat=diffMat**2distance=sqDiffMat.sum(axis=1)**0.5#接下来是一些统计工作sortedDistIndicies=distance.argsort()classCount={}for i in range(k):labelName=labels[sortedDistIndicies[i]]classCount[labelName]=classCount.get(labelName,0)+1;sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse= True)return sortedClassCount[0][0]②解析数据输入文件名,将文件中的数据转化为样本矩阵,方便处理代码如下:def file2Mat(testFileName,parammterNumber):fr=open(testFileName)lines=fr.readlines()lineNums=len(lines)resultMat=zeros((lineNums,parammterNumber))classLabelVector=[]for i in range(lineNums):line=lines[i].strip()itemMat=line.split('\t')resultMat[i,:]=itemMat[0:parammterNumber]classLabelVector.append(itemMat[-1])fr.close()return resultMat,classLabelVector;返回值为前三列属性被写入到resultMat二维数组中,第四列属性作为标签写入到classLableVector中③归一化数据不同评价指标往往具有不同的量纲和量纲单位,这样的情况会影响到数据分析的结果,为了消除指标之间的量纲影响,需要进行数据标准化处理,使各指标处于同一数量级。

处理过程如下:def autoNorm(dataSet):minVals=dataSet.min(0)maxVals=dataSet.max(0)ranges=maxVals-minValsnormMat=zeros(shape(dataSet))size=normMat.shape[0]normMat=dataSet-tile(minVals,(size,1))normMat=normMat/tile(ranges,(size,1))return normMat,minVals,ranges④测试数据在利用KNN算法预测之前,通常只提供已有数据的90%作为训练样本,使用其余的10%数据去测试分类器。

注意10%测试数据是随机选择的,采用错误率来检测分类器的性能。

错误率太高说明数据源出现问题,此时需要重新考虑数据源的合理性。

def test(trainigSetFileName,testFileName):trianingMat,classLabel=file2Mat(trainigSetFileName,3)trianingMat,minVals,ranges=autoNorm(trianingMat)testMat,testLabel=file2Mat(testFileName,3)testSize=testMat.shape[0]errorCount=0.0for i in range(testSize):result=classify((testMat[i]-minVals)/ranges,trianingMat,classLabel,3)if(result!=testLabel[i]):errorCount+=1.0errorRate=errorCount/(float)(len(testLabel))return errorRate;⑤ 使用KNN算法进行预测如果第四步中的错误率在课接受范围内,表示可以利用此数据源进行预测。

输入前三项属性之后较为准确的预测了分类。

代码如下:def classifyPerson():input a person , decide like or not, then update the DBresultlist = ['not at all','little doses','large doses']percentTats = float(raw_input('input the person\' percentage of time playing video games:'))ffMiles = float(raw_input('flier miles in a year:'))iceCream = float(raw_input('amount of iceCream consumed per year:'))datingDataMat,datingLabels = file2matrix('datingTestSet.txt')normMat, ranges, minVals = autoNorm(datingDataMat)normPerson = (array([ffMiles,percentTats,iceCream])-minVals)/rangesresult = classify0(normPerson, normMat, datingLabels, 3)print 'you will probably like this guy in:' ,result#resultlist[result -1]#update the datingTestSetprint 'update dating DB'tmp = '\t'.join([repr(ffMiles),repr(percentTats),repr(iceCream),repr(result)])+'\n'with open('datingTestSet2.txt','a') as fr:fr.write(tmp)四实验结果及分析本次实验结果截图如下:在终端输入python KNN.py命令开始执行KNN.py,分别得到了样本测试的错误率以及输入数据后KNN算法的预测结果:从实验结果来看,本数据集的一共检测的数据有200个,其中预测的和实际不相符的有16个,错误率为8%,在可接受范围之内。

由于检测的数据集是随机选取的,因此该数据比较可信。

当输入数据分别为900,40,80时,分类结果为didntlike,与数据集中给出的类似数据的分类一致。

实验二分组实验一实验内容本次实验的实验内容为利用数据挖掘的聚类算法实现对DBLP合作者的数据挖掘。

DBLP收录了国内外学者发表的绝大多数论文,其收录的论文按照文章类型等分类存储在DBLP.xml文件中。

通过聚类算法发现频繁项集就可以很好的发掘出有哪些作者经常在一起发表论文。

二实验要求(1)完成对DBLP数据集的采集和预处理,能从中提取出作者以及合作者的姓名(2)利用聚类算法完成对合作者的挖掘(3)实验报告三实验步骤(1)从DBLP数据集中提取作者信息首先从官网下载DBLP数据集dblp.xml.gz 解压后得到dblp.xml文件。

用vim打开dblp.xml发现所有的作者信息分布在以下这些属性中:'article','inproceedings','proceedings','book','incollection','phdthesis','mastersthesis','www'。

在这里使用python自带的xml分析器解析该文件。

代码如下:(其核心思想为,分析器在进入上面那些属性中的某一个时,标记flag=1,然后将author属性的内容输出到文件,退出时再标记flag = 0,最后得到authors.txt 文件)‘’’G etauthor.pyimport codecsfrom xml.sax import handler, make_parserpaper_tag = ('article','inproceedings','proceedings','book','incollection','phdthesis','mastersthesis','www')class mHandler(handler.ContentHandler):def __init__(self,result):self.result = resultself.flag = 0def startDocument(self):print 'Document Start'def endDocument(self):print 'Document End'def startElement(self, name, attrs):if name == 'author':self.flag = 1def endElement(self, name):if name == 'author':self.result.write(',')self.flag = 0if (name in paper_tag) :self.result.write('\r\n')def characters(self, chrs):if self.flag:self.result.write(chrs)def parserDblpXml(source,result):handler = mHandler(result)parser = make_parser()parser.setContentHandler(handler)parser.parse(source)if __name__ == '__main__':source = codecs.open('dblp.xml','r','utf-8')result = codecs.open('authors.txt','w','utf-8')parserDblpXml(source,result)result.close()source.close()(2)建立索引作者ID读取步骤1中得到的authors.txt文件,将其中不同的人名按照人名出现的次序编码,存储到文件authors_index.txt中,同时将编码后的合作者列表写入authors_encoded.txt文件。