本次 DataWhale 第二十三期组队学习,其开源内容的链接为:https://github.com/datawhalechina/team-learning-data-mining/tree/master/EnsembleLearning
1.导论
1.机器学习的一个重要的目标就是利用数学模型来理解数据,发现数据中的规律,用作数据的分析和预测。
2.数据通常由一组向量组成,这组向量中的每个向量都是一个样本,同时每个样本包含特征,但不一定包含因变量。根据有无因变量,可以将机器学习分为有监督学习和无监督学习。
- 根据因变量是否连续,有监督学习又可以分为回归和分类。
约定数据的表达形式:
第i个样本:xi=(xi1,xi2,...,xip,yi)T,i=1,2,...,Nx_i=(x_{i1},x_{i2},...,x_{ip},y_i)^T,i=1,2,...,Nxi=(xi1,xi2,...,xip,yi)T,i=1,2,...,N
因变量:y=(y1,y2,...,yN)Ty=(y_1,y_2,...,y_N)^Ty=(y1,y2,...,yN)T
第k个特征:x(k)=(x1k,x2k,...,xNk)Tx^{(k)}=(x_{1k},x_{2k},...,x_{Nk})^Tx(k)=(x1k,x2k,...,xNk)T
特征矩阵:X=(x1,x2,...,xN)TX=(x_1,x_2,...,x_N)^TX=(x1,x2,...,xN)T
# 引入相关科学计算包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use("ggplot")
import seaborn as sns
1.1 回归
使用 sklearn 内置的 Boston 房价数据集。
对于 sklearn 中所有的内置数据集都封装在 datasets 对象内,其返回的对象有:
- data:特征X的矩阵(ndarray)
- target:因变量的向量(ndarray)
- feature_names:特征名称(ndarray)
- 该对象中filename等属性,但是一般不会用到。
# 1.1回归
from sklearn import datasets
# 返回一个类似于字典的类
boston = datasets.load_boston()
X = boston.data
y = boston.target
features = boston.feature_names
boston_data = pd.DataFrame(X, columns = features)
boston_data['Price'] = y
boston_data.head()
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | Price | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.00632 | 18.0 | 2.31 | 0.0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1.0 | 296.0 | 15.3 | 396.90 | 4.98 | 24.0 |
1 | 0.02731 | 0.0 | 7.07 | 0.0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2.0 | 242.0 | 17.8 | 396.90 | 9.14 | 21.6 |
2 | 0.02729 | 0.0 | 7.07 | 0.0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2.0 | 242.0 | 17.8 | 392.83 | 4.03 | 34.7 |
3 | 0.03237 | 0.0 | 2.18 | 0.0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3.0 | 222.0 | 18.7 | 394.63 | 2.94 | 33.4 |
4 | 0.06905 | 0.0 | 2.18 | 0.0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3.0 | 222.0 | 18.7 | 396.90 | 5.33 | 36.2 |
其中 feature_names 返回的结果是:array([‘CRIM’, ‘ZN’, ‘INDUS’, ‘CHAS’, ‘NOX’, ‘RM’, ‘AGE’, ‘DIS’, ‘RAD’,‘TAX’, ‘PTRATIO’, ‘B’, ‘LSTAT’], dtype=’<U7’)
可以看出,这些都是数据集中属性的名字。
我们可以看出,该任务的因变量 Price 是一个连续变量,因此是一个回归问题
有关波士顿房价分析的文章:
- https://blog.csdn.net/weixin_39789646/article/details/111662291
- https://www.jianshu.com/p/f36bdecbbd9a
1.2 分类
以 iris 数据集为例
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
features = iris.feature_names
iris_data = pd.DataFrame(X,columns=features)
iris_data['target'] = y
iris_data.head()
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | 0 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | 0 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | 0 |
# 可视化特征
marker = ['s', 'x', 'o']
for index,c in enumerate(np.unique(y)):
plt.scatter(x=iris_data.loc[y==c,"sepal length (cm)"],y=iris_data.loc[y==c,"sepal width (cm)"],alpha=0.8,label=c,marker=marker[c])
plt.xlabel("sepal length (cm)")
plt.ylabel("sepal width (cm)")
plt.legend()
plt.show()
可以看出鸢尾花数据集的因变量是类别变量,因此,这是一次分类问题。
# enumerate 小例子
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
0 one
1 two
2 three
# 此处绘制散点图就是对各个类别进行绘制的
for index,c in enumerate(np.unique(y)):
print(index, c)
0 0
1 1
2 2
1.3 无监督学习
我们可以使用sklearn生成符合自身需求的数据集,下面我们用其中几个函数例子来生成无因变量的数据集:
https://scikit-learn.org/stable/modules/classes.html?highlight=datasets#module-sklearn.datasets
# 生成月牙型数据集
from sklearn import datasets
x, y = datasets.make_moons(n_samples = 3000, shuffle = True,
noise = 0.05, random_state = 2021)
for index, c in enumerate(np.unique(y)):
plt.scatter(x[y == c, 0], x[y == c, 1], s = 7)
plt.show()