sklearn 入门

传统的机器学习任务从开始到建模的一般流程是 获取数据 -> 数据预处理 -> 训练建模 -> 模型评估 -> 预测,分类。

本文依据传统的机器学习流程,介绍相关的函数及用法。

1.获取数据

1.导入sklearn数据集

要想使用sklearn中的数据集必须先导入datasets模块:

from sklearn import datasets

下图中包含了大量的sklearn数据集及调用方式,这里用iris(鸢尾花数据集)举个例子:

iris=datasets.losd_iris();#导入数据集
X=iris.data #获取特征向量
Y=iris.target #获取样本lebal

2.创建数据集

除了可以使用sklearn自带的数据集还可以自己创建训练样本,这里简介一些sklearn中的samples generator包含的大量创建样本数据的方法:

                 

下面我们拿分类模型的样本生成器举例子:

from sklearn.datasets.samples_generator import make_classification

X, y = make_classification(n_samples=6, n_features=5, n_informative=2, 
    n_redundant=2, n_classes=2, n_clusters_per_class=2, scale=1.0, 
    random_state=20)

# n_samples:指定样本数
# n_features:指定特征数
# n_classes:指定几分类
# random_state:随机种子,使得随机状可重
>>> for x_,y_ in zip(X,y):
    print(y_,end=': ')
    print(x_)

    
0: [-0.6600737  -0.0558978   0.82286793  1.1003977  -0.93493796]
1: [ 0.4113583   0.06249216 -0.90760075 -1.41296696  2.059838  ]
1: [ 1.52452016 -0.01867812  0.20900899  1.34422289 -1.61299022]
0: [-1.25725859  0.02347952 -0.28764782 -1.32091378 -0.88549315]
0: [-3.28323172  0.03899168 -0.43251277 -2.86249859 -1.10457948]
1: [ 1.68841011  0.06754955 -1.02805579 -0.83132182  0.93286635]

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/84885900