机器学习高斯贝叶斯算法实战:判断肿瘤是良性还是恶性

概述
我们使用威斯康星乳腺肿瘤数据集,来构建一个机器学习模型,用来判断患者的肿瘤是良性还是恶性。

数据分析
威斯康星乳腺肿瘤数据集,包括569个病例的数据样本,每个样本具有30个特征值。

样本分为两类:恶性Malignant和良性Benign。

查看key:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
data.keys()
1
2
3
4
输出:

dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])
1
查看分类和特征:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()

print(data["target_names"])
print(data["feature_names"])
1
2
3
4
5
6
输出:

['malignant' 'benign']
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']
1
2
3
4
5
6
7
8
9
10
使用高斯贝叶斯进行建模
完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

print(clf.score(X_test, y_test))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
输出:

0.951048951048951
1
高斯贝叶斯算法还是比较靠谱的,在真实的数据集上,也能够获得如此高的分数。

不过这恰好说明,这个癌症的数据集是符合正态分布的。

使用模型进行预测
这里我们使用随机的一条数据进行预测即可。

在真实的使用中,只需要传入的参数符合和本案例中的预测数据参数保持一致即可。

完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 一个预测点
point = X[33]
print(point)

# 使用这个点进行预测
# 注意:这里的参数应该是个二位数组,而point是一维数组
print(clf.predict([point]))
print(y[33])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
输出:

[1.927e+01 2.647e+01 1.279e+02 1.162e+03 9.401e-02 1.719e-01 1.657e-01
 7.593e-02 1.853e-01 6.261e-02 5.558e-01 6.062e-01 3.528e+00 6.817e+01
 5.015e-03 3.318e-02 3.497e-02 9.643e-03 1.543e-02 3.896e-03 2.415e+01
 3.090e+01 1.614e+02 1.813e+03 1.509e-01 6.590e-01 6.091e-01 1.785e-01
 3.672e-01 1.123e-01]
[0]
0
1
2
3
4
5
6
7
从输出可以看到,预测结果和真实的结果是一样的。

绘制高斯贝叶斯的学习曲线
在机器学习中,有一个概念叫做学习曲线learning curve,指的是随着数据集样本的增加,模型得分的变化情况。

下面,我们绘制基于这个乳腺癌数据集的高斯贝叶斯算法的学习曲线:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, learning_curve, ShuffleSplit
from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """绘制学习曲线"""
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    
    train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)

    plt.grid()
    plt.plot(train_sizes, train_scores_mean, "o-", color="r", label="Training score")
    plt.plot(train_sizes, test_scores_mean, "o-", color="g", label="Cross-validation score")
    plt.legend(loc="lower right")
    return plt

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 绘图
title = "Learning Curves (GaussianNB)"
cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
estimator = clf
plt = plot_learning_curve(estimator, title, X, y, ylim=(.9, 1.01), cv=cv, n_jobs=4)
plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
输出:


从结果来看,数据越多,模型的分数越来越低,但是在测试集上的预测分数则一直比较稳定。

分数低是因为数据量越多,模型需要拟合的的数据也会越多,难度也会越来越。

这说明,高斯贝叶斯算法对于样本数量的要求没有那么苛刻。如果我们要建立机器学习模型的样本数量比较少,又符合高斯分布的话,则可以优先考虑高斯贝叶斯算法来进行建模!!!
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/qq_37703224/article/details/138963132

概述
我们使用威斯康星乳腺肿瘤数据集,来构建一个机器学习模型,用来判断患者的肿瘤是良性还是恶性。

数据分析
威斯康星乳腺肿瘤数据集,包括569个病例的数据样本,每个样本具有30个特征值。

样本分为两类:恶性Malignant和良性Benign。

查看key:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
data.keys()
1
2
3
4
输出:

dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])
1
查看分类和特征:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()

print(data["target_names"])
print(data["feature_names"])
1
2
3
4
5
6
输出:

['malignant' 'benign']
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']
1
2
3
4
5
6
7
8
9
10
使用高斯贝叶斯进行建模
完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

print(clf.score(X_test, y_test))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
输出:

0.951048951048951
1
高斯贝叶斯算法还是比较靠谱的,在真实的数据集上,也能够获得如此高的分数。

不过这恰好说明,这个癌症的数据集是符合正态分布的。

使用模型进行预测
这里我们使用随机的一条数据进行预测即可。

在真实的使用中,只需要传入的参数符合和本案例中的预测数据参数保持一致即可。

完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 一个预测点
point = X[33]
print(point)

# 使用这个点进行预测
# 注意:这里的参数应该是个二位数组,而point是一维数组
print(clf.predict([point]))
print(y[33])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
输出:

[1.927e+01 2.647e+01 1.279e+02 1.162e+03 9.401e-02 1.719e-01 1.657e-01
 7.593e-02 1.853e-01 6.261e-02 5.558e-01 6.062e-01 3.528e+00 6.817e+01
 5.015e-03 3.318e-02 3.497e-02 9.643e-03 1.543e-02 3.896e-03 2.415e+01
 3.090e+01 1.614e+02 1.813e+03 1.509e-01 6.590e-01 6.091e-01 1.785e-01
 3.672e-01 1.123e-01]
[0]
0
1
2
3
4
5
6
7
从输出可以看到,预测结果和真实的结果是一样的。

绘制高斯贝叶斯的学习曲线
在机器学习中,有一个概念叫做学习曲线learning curve,指的是随着数据集样本的增加,模型得分的变化情况。

下面,我们绘制基于这个乳腺癌数据集的高斯贝叶斯算法的学习曲线:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, learning_curve, ShuffleSplit
from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """绘制学习曲线"""
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    
    train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)

    plt.grid()
    plt.plot(train_sizes, train_scores_mean, "o-", color="r", label="Training score")
    plt.plot(train_sizes, test_scores_mean, "o-", color="g", label="Cross-validation score")
    plt.legend(loc="lower right")
    return plt

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 绘图
title = "Learning Curves (GaussianNB)"
cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
estimator = clf
plt = plot_learning_curve(estimator, title, X, y, ylim=(.9, 1.01), cv=cv, n_jobs=4)
plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
输出:


从结果来看,数据越多,模型的分数越来越低,但是在测试集上的预测分数则一直比较稳定。

分数低是因为数据量越多,模型需要拟合的的数据也会越多,难度也会越来越。

这说明,高斯贝叶斯算法对于样本数量的要求没有那么苛刻。如果我们要建立机器学习模型的样本数量比较少,又符合高斯分布的话,则可以优先考虑高斯贝叶斯算法来进行建模!!!
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/qq_37703224/article/details/138963132

概述
我们使用威斯康星乳腺肿瘤数据集,来构建一个机器学习模型,用来判断患者的肿瘤是良性还是恶性。

数据分析
威斯康星乳腺肿瘤数据集,包括569个病例的数据样本,每个样本具有30个特征值。

样本分为两类:恶性Malignant和良性Benign。

查看key:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
data.keys()
1
2
3
4
输出:

dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])
1
查看分类和特征:

from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()

print(data["target_names"])
print(data["feature_names"])
1
2
3
4
5
6
输出:

['malignant' 'benign']
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
 'mean smoothness' 'mean compactness' 'mean concavity'
 'mean concave points' 'mean symmetry' 'mean fractal dimension'
 'radius error' 'texture error' 'perimeter error' 'area error'
 'smoothness error' 'compactness error' 'concavity error'
 'concave points error' 'symmetry error' 'fractal dimension error'
 'worst radius' 'worst texture' 'worst perimeter' 'worst area'
 'worst smoothness' 'worst compactness' 'worst concavity'
 'worst concave points' 'worst symmetry' 'worst fractal dimension']
1
2
3
4
5
6
7
8
9
10
使用高斯贝叶斯进行建模
完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

print(clf.score(X_test, y_test))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
输出:

0.951048951048951
1
高斯贝叶斯算法还是比较靠谱的,在真实的数据集上,也能够获得如此高的分数。

不过这恰好说明,这个癌症的数据集是符合正态分布的。

使用模型进行预测
这里我们使用随机的一条数据进行预测即可。

在真实的使用中,只需要传入的参数符合和本案例中的预测数据参数保持一致即可。

完整代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 一个预测点
point = X[33]
print(point)

# 使用这个点进行预测
# 注意:这里的参数应该是个二位数组,而point是一维数组
print(clf.predict([point]))
print(y[33])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
输出:

[1.927e+01 2.647e+01 1.279e+02 1.162e+03 9.401e-02 1.719e-01 1.657e-01
 7.593e-02 1.853e-01 6.261e-02 5.558e-01 6.062e-01 3.528e+00 6.817e+01
 5.015e-03 3.318e-02 3.497e-02 9.643e-03 1.543e-02 3.896e-03 2.415e+01
 3.090e+01 1.614e+02 1.813e+03 1.509e-01 6.590e-01 6.091e-01 1.785e-01
 3.672e-01 1.123e-01]
[0]
0
1
2
3
4
5
6
7
从输出可以看到,预测结果和真实的结果是一样的。

绘制高斯贝叶斯的学习曲线
在机器学习中,有一个概念叫做学习曲线learning curve,指的是随着数据集样本的增加,模型得分的变化情况。

下面,我们绘制基于这个乳腺癌数据集的高斯贝叶斯算法的学习曲线:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, learning_curve, ShuffleSplit
from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt

def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None, n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """绘制学习曲线"""
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    
    train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)

    plt.grid()
    plt.plot(train_sizes, train_scores_mean, "o-", color="r", label="Training score")
    plt.plot(train_sizes, test_scores_mean, "o-", color="g", label="Cross-validation score")
    plt.legend(loc="lower right")
    return plt

# 加载数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

# 训练模型
clf = GaussianNB()
clf.fit(X_train, y_train)

# 绘图
title = "Learning Curves (GaussianNB)"
cv = ShuffleSplit(n_splits=100, test_size=0.2, random_state=0)
estimator = clf
plt = plot_learning_curve(estimator, title, X, y, ylim=(.9, 1.01), cv=cv, n_jobs=4)
plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
输出:


从结果来看,数据越多,模型的分数越来越低,但是在测试集上的预测分数则一直比较稳定。

分数低是因为数据量越多,模型需要拟合的的数据也会越多,难度也会越来越。

这说明,高斯贝叶斯算法对于样本数量的要求没有那么苛刻。如果我们要建立机器学习模型的样本数量比较少,又符合高斯分布的话,则可以优先考虑高斯贝叶斯算法来进行建模!!!
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/qq_37703224/article/details/138963132

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值