数据挖掘作业

简介: 数据挖掘作业

第4章 算法设计


4.1 实现方式1:欧式距离


实验原理如下图:


16.png

图 1 实验原理


4.1.1 步骤1:数据预处理


这一部分对应实验代码1的preprocess函数,首选将文本大写转为小写,然后利用正则表达式去除标点符号,接下来使用nltk包去除句子中的停用词,比如the、a等。经过以上去除一些冗余信息和无关特征,有利于提高模型准确率。


4.2.2 步骤2:获取文档单词列表


实现函数为get_words,主要步骤为读取文档内容,然后行读取句子,将句子按空格分词,最后进行去重,返回单词列表。


4.2.3 步骤三:获取单词词频矩阵


这块分为两部分,一个是为文档单词词频设计的get_Xdw,一个是为查询语句设计的get_Yw,主要步骤是通过get_wors获取文本中的单词列表,然后遍历列表中每一个单词,统计文本中含有该单词的个数。


4.2.4 步骤四:计算查询语句与文档的欧式距离


给定一个查询语句,通过get_Yw获取单词词频矩阵,然后遍历所有的文档,利用下图公式,计算查询语句与文档之间的欧式距离,最后结果按从大到小进行排序。


17.png

图2 欧氏距离公式


第5章 程序实现


5.1 实验代码1 计算query与document之间的欧式距离(Euclidean Metric)

# -*- coding: utf-8 -*-
# @Time    : 2018/4/10 15:11
# @Author  : quincyqiang
# @File    : main.py
import os
import math
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import matplotlib.pyplot as plt
import numpy as np
# pre-process
def preprocess(words=None):
    print("text propressing..")
    # lower 大写转小写
    words=[word.lower() for word in words]
    # 去除标点符号
    words=[word.replace('[^\w\s]','') for word in words]
    # 去除停用词
    stop=stopwords.words('english')
    temp=[]
    for word in words:
        if word not in stop:
            temp.append(word)
    # 提取词干
    st=PorterStemmer()
    words=[st.stem(word) for word in words]
    return words
# get word list
def get_words():
    words=[]
    for txt in os.listdir('data'):
        with open('data/'+txt,'r') as txt_file:
            for line in txt_file.readlines():
                if line!='\n':
                    for word in line.split():
                        words.append(word)
    new_words=list(set(words))
    # print(new_words)
    new_words=preprocess(new_words)
    return new_words
# get X-dw(document word vector)
def get_Xdw():
    Xdw=[]
    words=get_words()
    # print(words)
    # print(len(words))
    for txt in os.listdir('data'):
        with open('data/'+txt,'r') as txt_file:
            content=txt_file.read()
            Xdw.append((txt,[content.count(word) for word in words]))
    # print(Xdw[0],len(Xdw))
    return Xdw
# get Yw(query word vector)
def get_Yw(sentence):
    words=get_words()
    sentence_words=sentence.split()
    sentence_words=preprocess(sentence_words)
    sentence=' '.join(sentence_words)
    Yw=[sentence.count(word) for word in words]
    # print(Yw)
    return Yw
def evaluate(sentence):
    scores=[]
    Yw=get_Yw(sentence)
    Xdw=get_Xdw()
    for X in Xdw:
        sum=0
        for data in zip(X[1],Yw):
            sum+=(data[0]-data[1])*(data[0]-data[1])
        score=math.sqrt(sum)
        scores.append(round(score,2))
    result=[]
    for data in zip(os.listdir('data'),scores):
        result.append(data)
    result.sort(key=lambda result:result[-1],reverse=True)
    print(result)
    return result
# evaluate('awarded  as a mathematician about approximate measure')
result=evaluate('awarded as a statistician about data model and inference')
x=np.linspace(1,13,13)
y=np.array([score[1] for score in result])
plt.plot(x,y)
plt.show()


第六章 实现结果


6.1 实验结果1:

给定查询语句“awarded as a statistician about data model and inference”,得出如下结果,欧氏距离最小的为'leobrieman.txt'。


18.png

图3 查询结果


19.png

图4 查询语句与文档相似曲线

相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10262 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1546 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2696 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2375 0