机器学习(二)

一、特征工程之文本特征的抽取API——CountVectorizer

(一)、纯英文文本的计数特征抽取方式

这一api主要是起到文本中某些单词出现的次数进行统计,通过统计文本中某些单词出现的次数来判断这一文章的类型。例如 love等词出现过很多次,可以大体猜测出这一文章主要是情感类文章(仅仅举例,不要抬杠,杠就是你对)。
废话不多说,直接上代码:

from sklearn.feature_extraction.text import CountVectorizer


def txt():
    """
    文本特征的抽取
    :return:
    """
    # 需要处理的两段文本信息
    message = [" There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real!",
               "it was our custom to leave the front door on the latch at night"]

    # 实例化
    CV = CountVectorizer()

    # 调用CountVectorizer()
    data = CV.fit_transform(message)

    print(CV.get_feature_names())

    # 将sparce矩阵转化成数字矩阵并且打印
    print(data.toarray())

以上代码运行的结果为:

['and', 'are', 'at', 'custom', 'door', 'dreams', 'for', 'from', 'front', 'hug', 'in', 'it', 'just', 'latch', 'leave', 'life', 'miss', 'moments', 'much', 'night', 'on', 'our', 'pick', 'real', 'so', 'someone', 'that', 'the', 'them', 'there', 'to', 'want', 'was', 'when', 'you', 'your']
[[1 1 0 0 0 1 1 1 0 1 1 0 1 0 0 1 1 1 1 0 0 0 1 1 1 1 1 0 2 1 1 1 0 1 2 1]
 [0 0 1 1 1 0 0 0 1 0 0 1 0 1 1 0 0 0 0 1 1 1 0 0 0 0 0 2 0 0 1 0 1 0 0 0]]

对以上代码运行的结果做出解释,首先是get_feature_names()这一API的输出:
‘and’, ‘are’, ‘at’, ‘custom’, ‘door’, ‘dreams’, ‘for’, ‘from’, ‘front’, ‘hug’, ‘in’, ‘it’, ‘just’, ‘latch’, ‘leave’, ‘life’, ‘miss’, ‘moments’, ‘much’, ‘night’, ‘on’, ‘our’, ‘pick’, ‘real’, ‘so’, ‘someone’, ‘that’, ‘the’, ‘them’, ‘there’, ‘to’, ‘want’, ‘was’, ‘when’, ‘you’, ‘your’
从这一输出可以看出,sklearn.feature_extraction.text.CountVectorizer()这一API主要是统计文章中所有出现的单词,只要某一单词在此文章中出现过,则在对应的数组位置上标注出出现的次数,否则为零。例如‘and’这一词在第一段中出现过一次,则在第一个数组中的第一个数字置1。相反’and’没有在第二段话中出现过,则在对应的位置置0。注意,文章中出现的单个字母不做统计,例如‘I’ love you 中的‘i’。

(二)、含有中文文本的特征抽取

从纯英文文本的特征抽取方式我们可以看出,CountVectorizer这一接口主要是通过空格来进行拆词的,对于英文来说这并不难,但是对于汉语来说,往往词语之间是没有空格的,这时候我们需要借助一种高效的拆词库——jieba,jiaba可以将一句话中的词语拆分出来,进而达到特征抽取的目的,实现代码如下:

import jieba
from sklearn.feature_extraction.text import CountVectorizer

def Chinese():
    """
    中文文本的特征提取
    :return:
    """
    # 将中文文本通过空格的形式分成词语
    T1 = jieba.cut("一叶知秋,是一个孤单的词。")
    T2 = jieba.cut("铅灰的天空,覆雪的原野,冰封的河面,构成了冷色。")

    # 将词语拼接成列表的形式
    word_list1 = list(T1)
    word_list2 = list(T2)

    # 将列表转换成字符串的形式
    string1 = " ".join(word_list1)
    string2 = " ".join(word_list2)
    
    # 实例化
    CV = CountVectorizer()

    # 将中文文本进行特征提取
    data = CV.fit_transform([string1, string2])

    print(CV.get_feature_names())

    # 将sparce矩阵转化成数字矩阵并且打印
    print(data.toarray())

以上代码运行的结果如下:

['一个', '一叶知秋', '冰封', '冷色', '原野', '天空', '孤单', '构成', '河面', '覆雪']
[[1 1 0 0 0 0 1 0 0 0]
 [0 0 1 1 1 1 0 1 1 1]]

结果的含义以及作用与纯英文文本的特征抽取是一样的了。这段代码可以实现含有汉语词语的文本特征抽取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding: utf-8 -*- import numpy as np import librosa import random def extract_power(y, sr, size=3): """ extract log mel spectrogram feature :param y: the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: log-mel spectrogram feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y)) y = y * normalization_factor # random crop start = random.randint(0, len(y) - size * sr) y = y[start: start + size * sr] # extract log mel spectrogram ##### powerspec = np.abs(librosa.stft(y,n_fft=128, hop_length=1024)) ** 2 #logmelspec = librosa.power_to_db(melspectrogram) return powerspec def extract_logmel(y, sr, size=3): """ extract log mel spectrogram feature :param y: the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: log-mel spectrogram feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y)) y = y * normalization_factor # random crop start = random.randint(0, len(y) - size * sr) y = y[start: start + size * sr] # extract log mel spectrogram ##### melspectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024, n_mels=90) logmelspec = librosa.power_to_db(melspectrogram) return logmelspec def extract_mfcc(y, sr, size=3): """ extract MFCC feature :param y: np.ndarray [shape=(n,)], real-valued the input signal (audio time series) :param sr: sample rate of 'y' :param size: the length (seconds) of random crop from original audio, default as 3 seconds :return: MFCC feature """ # normalization y = y.astype(np.float32) normalization_factor = 1 / np.max(np.abs(y))

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值