机器学习项目实战——12集成学习算法之乳腺癌预测

和11差不多,对其进行了代码改进

整体代码:

import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

df = pd.read_csv("data.csv")

print(df.shape)
# (569, 32)

df = df.drop('id', axis=1)

print(df.diagnosis.unique())

df['diagnosis'] = df['diagnosis'].map({'M':1,'B':0})

print(df.describe())

# 画热力图,数值为两个变量之间的相关系数
plt.figure(figsize=(20,20))
p=sns.heatmap(df.corr(), annot=True ,square=True)
plt.show()

# 查看标签分布
print(df.diagnosis.value_counts())
# 使用柱状图的方式画出标签个数统计
p=df.diagnosis.value_counts().plot(kind="bar")
plt.show()




# 获取训练数据和标签
x_data  = df.drop(['diagnosis'], axis=1)
y_data = df['diagnosis']

from sklearn.model_selection import train_test_split
# 切分数据集,stratify=y表示切分后训练集和测试集中的数据类型的比例跟切分前y中的比例一致
# 比如切分前y中0和1的比例为1:2,切分后y_train和y_test中0和1的比例也都是1:2
x_train,x_test,y_train,y_test = train_test_split(x_data, y_data, test_size=0.3, stratify=y_data)

from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, BaggingClassifier

classifiers = [
    KNeighborsClassifier(3),
    LogisticRegression(),
    MLPClassifier(hidden_layer_sizes=(20, 50), max_iter=10000),
    DecisionTreeClassifier(),
    RandomForestClassifier(max_depth=9, min_samples_split=3),
    AdaBoostClassifier(),
    BaggingClassifier(),
]

log = []
for clf in classifiers:
    clf.fit(x_train, y_train)
    name = clf.__class__.__name__

    print("=" * 30)
    print(name)

    print('****Results****')
    test_predictions = clf.predict(x_test)
    acc = accuracy_score(y_test, test_predictions)
    print("Accuracy: {:.4%}".format(acc))

    log.append([name, acc * 100])

print("=" * 30)


log = pd.DataFrame(log)
print(log)

log.rename(columns={0: 'Classifier', 1:'Accuracy'}, inplace=True)

sns.barplot(x='Accuracy', y='Classifier', data=log, color="b")

plt.xlabel('Accuracy %')
plt.title('Classifier Accuracy')
plt.show()

你可能感兴趣的:(机器学习,机器学习)