关于机器学习:一机器学习基础与特征工程

2次阅读

共计 7024 个字符,预计需要花费 18 分钟才能阅读完成。

1. 机器学习根底

1.1 数学根底

须要的数学知识:
高等数学、线性代数、概率与统计。

当然一开始不必深刻进去,能够在学习过程中逐渐积攒。

1.2 编程语言

人工智能畛域很火的畛域的天然是 Python,门槛也低,能够作为机器学习入门的首选语言。
有精力的话,再学习 C /C++,多一门语言傍身不是好事。

1.3 机器学习的开发流程


1)从公司原有的数据库或者爬虫等路径取得原始数据
咱们所拿到的数据,大抵分为两种类型:离散型数据和连续型数据。
2)对原始数据进行数据处理,python 中能够应用 pandas 库。
pandas 库 参考文档 https://pandas.pydata.org/pandas-docs/stable/
3)特色解决:十分重要的一步,间接影响模型的预测后果
4)而后抉择适合的算法,构建模式
5)对模型进行评估,是否合乎预期,如何不现实,须要从新抉择算法,或者对数据重做特色解决
6)评估 OK 后,模型间接利用

2.scikit-learn 库

scikit-learn 是基于 Python 语言的机器学习工具:
简略高效的数据挖掘和数据分析工具
可供大家在各种环境中重复使用
建设在 NumPy,SciPy 和 matplotlib 上
开源,可商业应用 – BSD 许可证

通过这个库来逐渐揭开机器学习的面纱.

2.1 特色工程

2.1.1 特色抽取

scikit-learn 库提供了特色抽取的 API:sklearn.feature_extraction

  • 字典特色抽取 sklearn.feature_extraction.DictVectorizer
from sklearn.feature_extraction import DictVectorizer

def dict_ex():
    """
    字典数据抽取
    :return:
    """list_demo = [{'city':' 美国 ','new_add': 24500},
                 {'city': '俄罗斯', 'new_add': 4000},
                 {'city': '英国', 'new_add': 5600}]

    dict_ins = DictVectorizer(sparse=False) 

    data = dict_ins.fit_transform(list_demo)

    print(dict_ins.get_feature_names()) 
    print(data.astype('int')) 

if __name__ == '__main__':
    dict_ex()

后果

['city= 俄罗斯', 'city= 美国', 'city= 英国', 'new_add']
[[0     1     0 24500]
 [1     0     0  4000]
 [0     0     1  5600]]
  • 文本特色抽取 sklearn.feature_extraction.text.CountVectorizer
    限度英文,能够应用分词工具
from sklearn.feature_extraction.text import CountVectorizer

def text_ex():
    """
    文本特色抽取
    :return:
    """text_demo = ["""
        Beautiful is better than ugly.
        Explicit is better than implicit.
        Simple is better than complex.
        Complex is better than complicated.
        Flat is better than nested.
        Sparse is better than dense.
        Readability counts.
        Special cases aren't special enough to break the rules.
        Although practicality beats purity.
        Errors should never pass silently.
        Unless explicitly silenced.
        In the face of ambiguity, refuse the temptation to guess.
        There should be one-- and preferably only one --obvious way to do it.
        Although that way may not be obvious at first unless you're Dutch.
        Now is better than never.
        Although never is often better than *right* now.
        If the implementation is hard to explain, it's a bad idea.
        If the implementation is easy to explain, it may be a good idea.
        Namespaces are one honking great idea -- let's do more of those!"""]

    cv = CountVectorizer()
    data = cv.fit_transform(text_demo)
    print(cv.get_feature_names())
    print(data) # 默认为 sparse 格局
    print(data.toarray()) # 转化为数组
  
if __name__ == '__main__':
    text_ex()  

后果

['although', 'ambiguity', 'and', 'are', 'aren', 'at', 'bad', 'be', 'beats', 'beautiful', 'better', 'break', 'cases', 'complex', 'complicated', 'counts', 'dense', 'do', 'dutch', 'easy', 'enough', 'errors', 'explain', 'explicit', 'explicitly', 'face', 'first', 'flat', 'good', 'great', 'guess', 'hard', 'honking', 'idea', 'if', 'implementation', 'implicit', 'in', 'is', 'it', 'let', 'may', 'more', 'namespaces', 'nested', 'never', 'not', 'now', 'obvious', 'of', 'often', 'one', 'only', 'pass', 'practicality', 'preferably', 'purity', 're', 'readability', 'refuse', 'right', 'rules', 'should', 'silenced', 'silently', 'simple', 'sparse', 'special', 'temptation', 'than', 'that', 'the', 'there', 'those', 'to', 'ugly', 'unless', 'way', 'you']
  (0, 9)    1
  (0, 38)    10
  (0, 10)    8
  :    :
  (0, 40)    1
  (0, 42)    1
  (0, 73)    1
[[ 3  1  1  1  1  1  1  3  1  1  8  1  1  2  1  1  1  2  1  1  1  1  2  1
   1  1  1  1  1  1  1  1  1  3  2  2  1  1 10  3  1  2  1  1  1  3  1  2
   2  2  1  3  1  1  1  1  1  1  1  1  1  1  2  1  1  1  1  2  1  8  1  5
   1  1  5  1  2  2  1]]

汉字的特征提取示例

from sklearn.feature_extraction.text import CountVectorizer
import jieba

def zh_exc():
    """
    中文特征值
    :return:
    """text_demo1 ="""
     我与父亲不相见已二年余了,我最不能遗记的是他的背影。那年冬天,祖母死了,父亲的差使也交卸了,正是洪水猛兽的日子,我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的货色,又想起祖母,不禁簌簌地流下眼泪。父亲说,“事已如此,不用惆怅,好在天无绝人之路!"""text_demo2 ="""
    沿着荷塘,是一条波折的小煤屑路。这是一条幽僻的路;白天也少人走,夜晚更加寂寞。荷塘四面,长着许多树,蓊蓊郁郁的。路的一旁,是些杨柳,和一些不晓得名字的树。没有月光的早晨,这路上阴森森的,有些怕人。今晚却很好,尽管月光也还是淡淡的。"""

    c1 = jieba.cut(text_demo1)
    c2 = jieba.cut(text_demo2)

    # 转换成字符串
    text1 = ' '.join(list(c1))
    text2 = ' '.join(list(c2))

    cv = CountVectorizer()
    data = cv.fit_transform([text1, text2])
    print(cv.get_feature_names())
    print(data.toarray())
 
 if __name__ == '__main__':
    zh_exc()

后果

['一些', '一旁', '一条', '不用', '不禁', '不能', '货色', '事已如此', '二年', '交卸', '今晚', '冬天', '北京', '名字', '四面', '回家', '夜晚', '天无绝人之路', '奔丧', '寂寞', '少人', '差使', '幽僻', '徐州', '遗记', '怕人', '想起', '打算', '日子', '早晨', '波折', '更加', '月光', '有些', '杨柳', '正是', '没有', '沿着', '流下', '淡淡的', '满院', '煤屑', '父亲', '狼藉', '白天', '相见', '看见', '眼泪', '晓得', '祖母', '洪水猛兽', '簌簌', '背影', '荷塘', '蓊蓊郁郁', '尽管', '许多', '跟着', '路上', '还是', '这是', '那年', '长着', '阴森森', '惆怅']
[[0 0 0 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 1 0 0 1 0 2 1 0 1 1 1 0 0 0 0 0 0 1
  0 0 1 0 1 0 5 1 0 1 1 1 0 2 1 1 1 0 0 0 0 1 0 0 0 1 0 0 1]
 [1 1 2 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 0 0 0 1 1 1 2 1 1 0
  1 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 2 1 1 1 0 1 1 1 0 1 1 0]]
  • TF-IDF 核心内容就是评估一字词的重要水平
    对应的 API sklearn.feature_extraction.text.TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
import jieba


def tfidf_ext():
    text_demo1 = """
         我与父亲不相见已二年余了,我最不能遗记的是他的背影。那年冬天,祖母死了,父亲的差使也交卸了,正是洪水猛兽的日子,我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的货色,又想起祖母,不禁簌簌地流下眼泪。父亲说,“事已如此,不用惆怅,好在天无绝人之路!"""text_demo2 ="""
        沿着荷塘,是一条波折的小煤屑路。这是一条幽僻的路;白天也少人走,夜晚更加寂寞。荷塘四面,长着许多树,蓊蓊郁郁的。路的一旁,是些杨柳,和一些不晓得名字的树。没有月光的早晨,这路上阴森森的,有些怕人。今晚却很好,尽管月光也还是淡淡的。"""

    c1 = jieba.cut(text_demo1)
    c2 = jieba.cut(text_demo2)

    # 转换成字符串
    text1 = ' '.join(list(c1))
    text2 = ' '.join(list(c2))

    tf = TfidfVectorizer()
    data = tf.fit_transform([text1, text2])
    print(tf.get_feature_names())
    print(data.toarray())
   
 if __name__ == '__main__':
    tfidf_ext()

后果

['一些', '一旁', '一条', '不用', '不禁', '不能', '货色', '事已如此', '二年', '交卸', '今晚', '冬天', '北京', '名字', '四面', '回家', '夜晚', '天无绝人之路', '奔丧', '寂寞', '少人', '差使', '幽僻', '徐州', '遗记', '怕人', '想起', '打算', '日子', '早晨', '波折', '更加', '月光', '有些', '杨柳', '正是', '没有', '沿着', '流下', '淡淡的', '满院', '煤屑', '父亲', '狼藉', '白天', '相见', '看见', '眼泪', '晓得', '祖母', '洪水猛兽', '簌簌', '背影', '荷塘', '蓊蓊郁郁', '尽管', '许多', '跟着', '路上', '还是', '这是', '那年', '长着', '阴森森', '惆怅']
[[0.         0.         0.         0.12598816 0.12598816 0.12598816
  0.12598816 0.12598816 0.12598816 0.12598816 0.         0.12598816
  0.12598816 0.         0.         0.12598816 0.         0.12598816
  0.12598816 0.         0.         0.12598816 0.         0.25197632
  0.12598816 0.         0.12598816 0.12598816 0.12598816 0.
  0.         0.         0.         0.         0.         0.12598816
  0.         0.         0.12598816 0.         0.12598816 0.
  0.62994079 0.12598816 0.         0.12598816 0.12598816 0.12598816
  0.         0.25197632 0.12598816 0.12598816 0.12598816 0.
  0.         0.         0.         0.12598816 0.         0.
  0.         0.12598816 0.         0.         0.12598816]
 [0.15617376 0.15617376 0.31234752 0.         0.         0.
  0.         0.         0.         0.         0.15617376 0.
  0.         0.15617376 0.15617376 0.         0.15617376 0.
  0.         0.15617376 0.15617376 0.         0.15617376 0.
  0.         0.15617376 0.         0.         0.         0.15617376
  0.15617376 0.15617376 0.31234752 0.15617376 0.15617376 0.
  0.15617376 0.15617376 0.         0.15617376 0.         0.15617376
  0.         0.         0.15617376 0.         0.         0.
  0.15617376 0.         0.         0.         0.         0.31234752
  0.15617376 0.15617376 0.15617376 0.         0.15617376 0.15617376
  0.15617376 0.         0.15617376 0.15617376 0.        ]]

2.1.2 特色解决

什么是特色解决?就是通过特定的数学方法将数据转换成咱们算法要求的数据。

  • 归一化
    min-max 归一化的公式为:

    mean 归一化的公式为:

    ​​其中 mean(x)、min(x) 和 max(x) 别离是样本数据的平均值、最小值和最大值。
    再求 X “


其中 mx,mi 别离为指定区间[mi, mx],个别 mx 为 1,mi 为 0。

归一化的目标:通过对原始数据进行变换把数据映射到指定区间之间,个别是[0,1],那就是省去求 X “, 这步能够省略。

sklearn 库提供了归一化 API:sklearn.preprocessing.MinMaxScaler
代码示例:

from sklearn.preprocessing import MinMaxScaler

def mm_ex():
    """
    归一化示例
    :return:
    """
    test_dict = [[99,1,18,1002],
                [88,4,18,1400],
                [89, 4,25,1201],
                [97,2,19,2800]]
    mm = MinMaxScaler()
    data = mm.fit_transform(test_dict)
    print(data)
    
if __name__ == '__main__':
    mm_ex()

后果

[[1.         0.         0.         0.]
 [0.         1.         0.         0.22135706]
 [0.09090909 1.         1.         0.11067853]
 [0.81818182 0.33333333 0.14285714 1.]]

归一化的后果因最大值和最小值而变动,所以容易特异点数据影响,如

    test_dict = [[99,1,18,200000],
                [88,4,18,1400],
                [89, 4,25,1201],
                [97,2,19,2800]]

200000 这个数值就对后果有很大影响。

  • 标准化
    公式

    u 为平均值,σ 为标准差

sklearn 提供了标准化 API: scikit-learn.preprocessing.StandardScaler
代码示例

from sklearn.preprocessing import StandardScaler

def standard_ex():
    """
    标准化 API 示例
    :return:
    """
    test_dict = [[99, 1, 18, 1002],
                 [88, 4, 18, 1400],
                 [89, 4, 25, 1201],
                 [97, 2, 19, 2800]]
    
    ss = StandardScaler()
    data = ss.fit_transform(test_dict)
    print(data)


if __name__ == '__main__':
    standard_ex()

后果

[[1.1941005  -1.34715063 -0.68599434 -0.84743801]
 [-1.09026568  0.96225045 -0.68599434 -0.28413057]
 [-0.88259602  0.96225045  1.71498585 -0.56578429]
 [0.7787612  -0.57735027 -0.34299717  1.69735287]]
正文完
 0