python-文本挖掘

# 聚类
import pandas as pd
import jieba
import matplotlib.pyplot as plt
from sklearn import metrics
from wordcloud import WordCloud
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.cluster import AgglomerativeClustering
from sklearn.feature_extraction.text import CountVectorizer

def get_stop():
    with open("data/stopword.txt","r",encoding="GBK") as f:
        stop=[line.strip() for line in f.readlines()]  #遍历每一行并读取第一个f.readlines()读一行
    return stop

def rm_stops(words,stops):
    words_no_stop=[]
    for i in words:
        if i not in stops:
            words_no_stop.append(i)
    return words_no_stop
stops=get_stop()

def preprocess_text(docs,sentences,label):
    for doc in docs:
        words=jieba.lcut(doc)
        words_new = rm_stops(words,stops)
        sentences.append((" ".join(words_new),label))

# 数据可视化
def Seedata(cv_x,clf):
    # 7.1 降维聚类
    p = PCA(n_components=2)  # 降为2维
    new_x = p.fit_transform(cv_x)
    clf = KMeans(n_clusters=4)
    y_pre = clf.fit_predict(new_x)
    result = list(y_pre)
    # 7.2 绘制样本点
    plt.figure(2)
    Lab = [[] for i in range(4)]
    index = 0
    for labi in result:
        Lab[labi].append(index)
        index += 1
    color = ['oy', 'ob', 'og', 'cs']
    for i in range(4):
        x1 = []
        y1 = []
        for ind1 in new_x[Lab[i]]:
            x1.append(ind1[0])
            y1.append(ind1[1])
        plt.plot(x1, y1, color[i])
    # 7.3 绘制聚类中心点
    x1 = []
    y1 = []
    for ind1 in clf.cluster_centers_:
        x1.append(ind1[0])
        y1.append(ind1[1])
    plt.plot(x1, y1, "rv")
    plt.show()
    return Lab

#  词云图
def Wc(Lab):
    for i in range(num_cluster):
        s = " "
        for j in Lab[i]:
            s = s+x[j]
        w = WordCloud(font_path="SIMLI.TTF",  # 微软雅黑 msyh.ttc
                      background_color='white',  # 背景颜色
                      max_words=30,  # 词语个数
                      min_font_size=8,  # 最小字体
                      random_state=12,  # 颜色随机
                      mask=plt.imread("data/heart.jpg")  # 指定图形样式
                      )  # 配置参数
        w.generate(s)  # 加载
        plt.imshow(w)
        plt.show()

if __name__ == '__main__':
    #1.文本收集
    doc = pd.read_csv("data/protein2.csv")
    doc.dropna(inplace=True)
    ym = doc["segment"].values.tolist()
    print(ym)

    #2.分词
    sentences=[]
    preprocess_text(ym,sentences,1)
    # print(sentences)
    # random.shuffle(sentences)

    #3、向量化  Count Tfidf 等等
    #训练测试数据分离
    x=[ a for a,b in sentences]
    y=[ b for a,b in sentences]
    cv = CountVectorizer()
    cv_x = cv.fit_transform(x).toarray()

    # 4、模型建立 Kmeans 层次聚类
    num_cluster = 3
    clt = KMeans(n_clusters=num_cluster)
    # clt = AgglomerativeClustering(n_clusters=num_cluster)
    clt.fit(cv_x)
    # print(clt.inertia_)
    print(clt.labels_)

    # 5、模型评价
    lk = metrics.silhouette_score(cv_x,clt.labels_)  # 轮廓系数
    print("轮廓系数:",lk)
    ars = metrics.adjusted_rand_score(y,clt.labels_) #  兰德指数:计算两个聚类之间的相似性度量
    print("兰德指数:",ars)

    # 6、模型输出
    li = []
    for i in range(1,30,1):
        clf = KMeans(n_clusters=i)
        s = clf.fit(cv_x)
        li.append(clf.inertia_)
    import matplotlib.pyplot as plt
    plt.plot(range(1,30,1),li)
    plt.show()

    # 7、数据可视化
    L = Seedata(cv_x,clt)
    # 8. 词云图
    Wc(L)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,模糊文本挖掘是一种处理文本数据的技术,用于从文本中提取有用的信息。在上面的引用中,介绍了如何使用scikit-learn包进行模糊文本挖掘的一些基本步骤。 首先,需要构建一个语料库,即将要处理的文本数据集。可以使用os库来遍历文件夹和文件,读取文件内容,并将其添加到一个数据框中。 接下来,需要对文本进行分词处理。在这里,使用了jieba库来进行中文分词。通过遍历语料库中的每个文件内容,使用jieba.cut函数对文本进行分词,并过滤掉非中文字符。 然后,可以处理停用词。停用词是在文本处理中需要被过滤掉的常见词语,例如“的”、“是”、“在”等。在上述引用中,使用了sklearn的CountVectorizer和TfidfTransformer类来进行停用词过滤。通过读取停用词文件,并将其传递给CountVectorizer的stop_words参数,就可以直接过滤掉停用词。 总结起来,要进行模糊文本挖掘,需要以下几个步骤: 1. 构建语料库,将要处理的文本数据存储到一个数据框中。 2. 对文本进行分词处理,使用合适的分词库,如jieba,将文本切分成词汇。 3. 处理停用词,通过读取停用词文件,并将其传递给相应的文本处理工具,过滤掉停用词。 以上是一个简单的示例,实际的模糊文本挖掘过程可能会更加复杂。具体的实施取决于具体的任务和需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值