第一讲 数据挖掘初探
什么是数据挖掘
- 数据挖掘的定义:
从大量数据中自动化(或者半自动化)地发现有价值的知识的过程
- 数据库的知识发现(Knowledge discovery in database, KDD)指的是, 将为加工的数据转化为知识的整个过程. 数据挖掘是KDD的一部分.
- 数据挖掘不同于信息检索.
(1) 信息检索包括:
- 使用数据库管理系统查找记录
- 通过搜索引擎查找特定的资源
(2) 可以使用数据挖掘技术增强信息检索的能力
- 与数据挖掘内涵相同或相似的名词:
商业智能BI、人工智能AI、数据分析、模式识别、决策支持… - 数据挖掘的衍生名词:
云计算、大数据…
数据挖掘面临的挑战
- 海量、高纬数据
- 数据种类的复杂性: 要能够处理数字、文本、音频、视频等不同类型的数据
- 数据质量问题: 真实的数据可能存在数据缺失、异常值、极端值、数据分布不是正态分布等问题
- 数据的所有权和分布
- 隐私问题
数据挖掘的工作领域
- 数据分析师:
在拥有行业数据的电商、金融、电信、咨询等行业里做业务咨询,商务智能,从数据库中提取数据、清洗数据,出分析报告。 - 数据挖掘工程师:
在多媒体、电商、搜索、社交等大数据相关行业里做机器学习算法实现和分析。 - 科学研究方向:
在高校、科研单位、企业研究院等科研机构研究新算法效率改进及未来应用。
数据挖掘工具
- 编程语言:
Java、Python、C++、R等,适用于快速开发、快速迭代要求高的场合 - 软件:
- SAS的Enterprise Miner模块、 IBM的SPSS Modeler
- SQL Server、Excel、Weka、Orange、Oracle的Darwin、IBM的Intelligent Miner
- python 与 Anacoda
- Python编写的代码具有简洁、易读和易维护等优点
Python中包含丰富的第三方模块,用户可以利用这些模块完成数据科学中的工作任务 - Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。当下载并安装好Anaconda后,它就已经集成了上百个第三方模块的安装。例如,数据挖掘中将使用的numpy、pandas、matplotlib、sklearn等。用户需要使用这些模块时,直接导入即可,不用再去下载.
- Anaconda的下载地址: https://www.anaconda.com/distribution/#download-section
- 通过conda命令可以进行模块的安装与卸载。
conda install ××× # ×××为模块名
conda uninstall ××× - Anaconda中查看python版本
python --version
数据挖掘任务
- 预测建模
- 分类(Classification)
- 回归(Regression)
- 关联分析(Association Analysis)
- 聚类(Clustering)
- 异常检测(Anomaly Detection)
1. 预测建模
- 分类:预测结果是一个类别(离散变量)
(1) 二分类:
- 判断一只动物是猫还是狗(图像识别)
- 判断是否是垃圾邮件;
- 判断病患是否为良性肿瘤;
- 判断某支股票涨或跌;
(2) 多分类:
- 手写数字识别;
- 预测一朵鸢尾花的种类;
- 判断发放给客户信用卡的风险评级
- 回归: 预测结果是一个连续的值而非一个类别,例如房屋的价格、学生的成绩、股票的价格等。
【例1】预测鸢尾花卉属于(Setosa(山鸢尾),Versicolour(杂色鸢尾),Virginica(维吉尼亚鸢尾))三个种类中的哪一类。
【说明】 iris 鸢尾花数据集内包含 3 类共 150 条记录,每类各 50 个数据,每条记录都有 4 项特征:花萼(sepal)长度、花萼宽度、花瓣(petal)长度、花瓣宽度,可以通过这4个特征预测鸢尾花属于哪一品种。
1.认识iris数据集
from sklearn.datasets import load_iris
iris = load_iris() #载入数据集
print(iris) # 查看iris数据集
print(iris.feature_names) #特征名称
print(iris.target_names) #标签名称
print(iris.data) # 读取iris数据 萼片长度;萼片宽度;花瓣长度;花瓣宽度;种类
print(iris.data.shape) # 读取矩阵的形状
print(iris.target)
print(iris.target.shape)
结果:
{'data': array([[5.1, 3.5, 1.4, 0.2], # array
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9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2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), 'frame': None, 'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'), 'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n ============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83 0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n ============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n :Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n.. topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n - Many, many more ...', 'feature_names': ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], 'filename': '/Users/eve/opt/anaconda3/lib/python3.8/site-packages/sklearn/datasets/data/iris.csv'} # 查看iris数据集
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] #特征名称
['setosa' 'versicolor' 'virginica'] #标签名称
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
[5.7 3.8 1.7 0.3]
[5.1 3.8 1.5 0.3]
[5.4 3.4 1.7 0.2]
[5.1 3.7 1.5 0.4]
[4.6 3.6 1. 0.2]
[5.1 3.3 1.7 0.5]
[4.8 3.4 1.9 0.2]
[5. 3. 1.6 0.2]
[5. 3.4 1.6 0.4]
[5.2 3.5 1.5 0.2]
[5.2 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5.7 2.5]
[6.7 3. 5.2 2.3]
[6.3 2.5 5. 1.9]
[6.5 3. 5.2 2. ]
[6.2 3.4 5.4 2.3]
[5.9 3. 5.1 1.8]] # print(iris.data)
(150, 4) # print(iris.data.shape) 读取矩阵的形状 150行 4列
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2] # print(iris.target) 种类
(150,) # print(iris.target.shape) 读取的为 1行 共150个
numpy.array函数详解
numpy.array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)
作用:创建一个数组
参数说明
object:数组
公开数组接口的任何对象,__array__
方法返回数组的对象,或任何(嵌套)序列
dtype:数据类型,可选
数组所需的数据类型。如果没有给出,那么类型将被确定为保持序列中的对象所需的最小类型。此参数只能用于“upcast”数组。对于向下转换,请使用.astype(t)方法。
copy : bool,可选
如果为true(默认值),则复制对象。否则,只有当__array__
返回副本,obj是嵌套序列,或者需要副本来满足任何其他要求(dtype,顺序等)时,才会进行复制。
order : {'K','A','C','F'},可选
指定阵列的内存布局。如果object不是数组,则新创建的数组将按C顺序排列(行主要),除非指定了’F’,在这种情况下,它将采用Fortran顺序(专业列)。如果object是一个数组,则以下成立。
当copy=False出于其他原因而复制时,结果copy=True与对A的一些例外情况相同,请参阅“注释”部分。默认顺序为“K”。
subok : bool,可选
如果为True,则子类将被传递,否则返回的数组将被强制为基类数组(默认)。
ndmin : int,可选
指定结果数组应具有的最小维数。根据需要,将根据需要预先设置形状。
返回值:out:ndarray
满足要求的数组对象
Examples:
>>> np.array([1, 2, 3])
array([1, 2, 3])
>>> np.array([[1, 2], [3, 4]]) # 二维
array([[1, 2], [3, 4]])
>>> np.array([1, 2, 3], ndmin=2) # 最小维度为2
array([[1, 2, 3]])
>>> np.array([1, 2, 3], dtype=complex) # 提供类型
array([ 1.+0.j, 2.+0.j, 3.+0.j])
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) # 由多个元素组成的数据类型:
>>> x['a']
array([1, 3])
>>> np.array(np.mat('1 2; 3 4')) # 从子类创建数组:
array([[1, 2], [3, 4]])
>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2], [3, 4]])
2.训练集和测试集
想要利用iris数据集构建一个数据挖掘(机器学习)模型,用于预测新的鸢尾花的品种。
在将模型应用于新的数据之前,需要知道模型是否有效。
通常的做法是将收集好的带标签数据(此例中是 150 朵花的数据)分成训练集(train set)和测试集(test set).训练集用于训练模型,测试集用于评估模型的好坏。
python代码:
from sklearn.model_selection import train_test_split
# 拆分训练集与测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,train_size=0.75, random_state=1234)
# 一般用大写的X表示数据特征,用小写的y表示数据对应的标签。因为X是一个二维数组(矩阵),y是一维数组(向量)
# 通过固定random_state的值,保证每次生成的伪随机数相同;若缺省设置,则每次生成的伪随机数不同。
print("X_train shape:", X_train.shape)
print("y_train shape:", y_train.shape)
print("X_test shape:", X_test.shape)
print("y_test shape:", y_test.shape)
结果:
X_train shape: (112, 4)
y_train shape: (112,)
X_test shape: (38, 4)
y_test shape: (38,)
- 训练模型
构建真实的数据挖掘(机器学习)模型。
scikit-learn 中有许多可用的分类算法。本例使用决策树算法,建立特征与类别的关系。
# 使用决策树算法进行训练
from sklearn.tree import DecisionTreeClassifier
# 定义一个决策树对象
model = DecisionTreeClassifier(random_state=1234)
# 训练模型
model.fit(X_train, y_train)
- 评估模型
# 所得模型的准确性
print(model.score(X_test,y_test))
结果:
0.9736842105263158
5.根据特征, 预测鸢尾花的分类
# 预测三个样本点的分类结果
print(model.predict([[5.1, 3.5, 1.4, 0.2],
[7, 3.2, 4.7, 1.4],
[5.9, 3, 5.1, 1.8 ]]))
结果:
[0 1 2]
注意:
- 实际工作中,通常需要对比多个模型,最终选择一个最优模型或者组合模型。
- 上面使用的决策树模型直接调用了第三方模块,并且是基于默认参数的模型构建。但默认参数往往不能得到最佳的拟合效果。通常,还需要不停地调整模型参数。然后,基于这些不同的参数值,验证哪种组合的参数会得到效果最佳的模型。
PyCharm配置Anaconda环境
打开PyCharm
接下来都ok就行了.
2. 关联分析
关联分析:用来发现描述数据中强关联特征的模式。
3. 聚类
- 将物理或抽象对象的集合分成由类似的对象组成的多个类的过程被称为聚类。
- 由聚类所生成的簇是一组数据对象的集合,这些对象与同一个簇中的对象彼此相似,与其他簇中的对象相异。
e.g. 基于聚类分析的案例:零售客户细分
客户细分的目的在于识别不同的客户群体,然后针对不同的客户群体,精准地进行产品设计和推送,从而节约营销成本,提高营销效率。
例如,针对商业银行的零售客户进行细分,基于零售客户的特征变量(人口特征、资产特征、负债特征、结算特征),计算客户之间的距离。然后,按照距离的远近,把相似的客户聚集为一类,从而有效的细分客户。将全体客户划分为诸如,理财偏好者、基金偏好者、活期偏好者、国债偏好者、风险均衡者、渠道偏好者等。
分类与聚类的区别
- 分类:类别是已知的,通过对已知分类的数据进行训练和学习,找到这些不同类的特征,再对未分类的数据进行分类。
- 聚类:事先没有给出类别的“标签”,而是通过数据之间的相似度进行类别划分。
4. 异常检测
- 异常检测:识别其特征显著不同于其他数据的样本点。这样的点被称作异常点或者离群点。
e.g. 基于异常检测的案例:支付中的交易欺诈侦测
采用支付宝或者刷信用卡支付时,系统会实时判断这笔刷卡行为是否属于盗刷。
异常值的判断,可能包含两类规则,即事件类规则和模型类规则。第一,事件类规则,例如刷卡的时间、刷卡的地点、刷卡的商户、刷卡金额、刷卡频次是否异常。第二,模型类规则,则是通过算法判定交易是否属于欺诈。一般通过支付数据、卖家数据、结算数据,构建模型进行分类问题的判断。
机器学习与数据挖掘
机器学习与数据挖掘的关系
- 机器学习关注的问题:计算机程序如何随着经验积累自动提高性能。
- 数据挖掘是使用了包括机器学习算法在内的众多知识的一门应用学科。
- 数据挖掘的处理过程一般包括数据预处理(数据清洗、数据集成等),数据仓库(可以是DBMS、大型数据仓库以及分布式存储系统)与OLAP,使用各种算法(主要是机器学习的算法)进行挖掘以及最后的评估工作。
机器学习与传统算法的区别
- 传统算法的思路:定义规则,编写代码,让机器执行。
- 机器学习的解决思路:输入学习资料,通过机器学习算法,得到一个可以执行任务的算法(模型)。