Python机器学习(基础篇---监督学习(线性分类器))

监督学习经典模型

机器学习中的监督学习模型的任务重点在于,根据已有的经验知识对未知样本的目标/标记进行预测。根据目标预测变量的类型不同,我们把监督学习任务大体分为分类学习回归预测两类。监督学习任务的基本流程:首先准备训练数据,可以是文本、图像、音频等;然后抽取所需要的特征,形成特征向量,接着把这些特征向量连同对应的标记/目标(Labels)一并送入学习算法中,训练一个预测模型,然后采用同样的特征抽取方法作用于新测试数据,得到用于测试的特征向量,最后使用预测模型对这些待测试的特征向量进行预测并得到结果。

1.分类学习

最基础的是二分类问题,即判断是非,从两个类别中选择一个作为预测结果。多分类问题,即在多余两个类别中选择一个,多标签分类问题,判断一个样本是否同时属于多个不同类别。

1.1线性分类器

模型介绍:线性分类器是一种假设特征与分类结果存在线性关系的模型。通过累加计算每个维度的特征与各自权重的乘积来帮助类别决策。

如果我们定义x=<x1,x2,...,xn>来代表n维特征列向量,同时用n维列向量w=<w1,w2,...wn>来代表对应得权重,避免坐标过坐标原点,假设截距为b。线性关系可表达为:

 f(w,x,b)=wTx+b

我们所要处理的简单二分类问题希望f∈{0,1};因此需要一个函数把原先的f∈R映射到(0,1),逻辑斯蒂函数:

g(z)=1/(1+e-z)

将z替换为f,逻辑斯蒂回归模型:

hw,b(x)=g(f(w,x,b))=1/(1+e-f)=1/(1+e-(wTx+b)

实例1:良/恶性乳腺癌肿瘤预测----------逻辑斯蒂回归分类器

数据描述: 

Number of Instances: 699 (as of 15 July 1992)

Number of Attributes: 10 plus the class attribute
Attribute Information: (class attribute has been moved to last column)

   #  Attribute                     Domain

   -- -----------------------------------------

   1. Sample code number            id number

   2. Clump Thickness               1 - 10

   3. Uniformity of Cell Size       1 - 10

   4. Uniformity of Cell Shape      1 - 10

   5. Marginal Adhesion             1 - 10

   6. Single Epithelial Cell Size   1 - 10

   7. Bare Nuclei                   1 - 10

   8. Bland Chromatin               1 - 10

   9. Normal Nucleoli               1 - 10

  10. Mitoses                       1 - 10

  11. Class:                        (2 for benign, 4 for malignant)

Missing attribute values: 16

   There are 16 instances in Groups 1 to 6 that contain a single missing 

   (i.e., unavailable) attribute value, now denoted by "?".  

 Class distribution:

   Benign: 458 (65.5%)

   Malignant: 241 (34.5%)
 #步骤一:良/恶性乳腺癌肿瘤数据预处理

#导入pandas与numpy工具包

import pandas as pd

import numpy as np

#创建特征列表

column_names=['Sample code number','Clump Thickness','Uniformity of Cell Size',

              'Uniformity of Cell Shape','Marginal Adhesion',

              'Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin',

              'Normal Nucleoli','Mitoses','Class']

#使用pandas.read_csv函数从互联网读取指定数据

data=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data',names=column_names)

# print(data)#[699 rows x 11 columns]

# print(data[:5])

#Sample code number Clump Thickness Uniformity of Cell Size \

#0 1000025 5 1

#1 1002945 5 4

#2 1015425 3 1

#3 1016277 6 8

#4 1017023 4 1

 

#Uniformity of Cell Shape Marginal Adhesion Single Epithelial Cell Size \

#0 1 1 2

#1 4 5 7

#2 1 1 2

#3 8 1 3

#4 1 3 2

 

#Bare Nuclei Bland Chromatin Normal Nucleoli Mitoses Class

#0 1 3 1 1 2

#1 10 3 2 1 2

#2 2 3 1 1 2

#3 4 3 7 1 2

#4 1 3 1 1 2



data=data.replace(to_replace='?',value=np.nan)

data=data.dropna(how='any')

print(data.shape)#(683, 11)
 

#步骤二:准备良/恶性乳腺癌肿瘤训练、测试数据

#使用sklearn.cross_validation里的train_test_split模块用于分割数据

from sklearn.cross_validation import train_test_split

#随机采样25%的数据用于测试,剩下的75%用于构建训练集合

X_train,X_test,y_train,y_test=train_test_split(data[column_names[1:10]],data[column_names[10]],test_size=0.25,random_state=33)

#检查训练样本的数量和类别分布

print(y_train.value_counts())

# 2 344

# 4 168

# Name: Class, dtype: int64

print(y_test.value_counts())

# 2 100

# 4 71

# Name: Class, dtype: int64

 #步骤三:使用线性分类模型从事良/恶性肿瘤预测任务

#从sklearn.preprocessing里导入StandardScaler

from sklearn.preprocessing import StandardScaler

#从sklearn.preprocessing里导入LogisticRegression与SGDClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.linear_model import SGDClassifier

#标准化数据,保证每个维度的特征数据方差为1,均值为0。使得预测结果不会被某些维度过大的特征值而主导

ss=StandardScaler()

X_train=ss.fit_transform(X_train)

X_test=ss.fit_transform(X_test)

#初始化LogisticRegression与SGDClassifier

lr=LogisticRegression()

sgdc=SGDClassifier()

#调用LogisticRegression中的fit函数/模块用来训练模型参数

lr.fit(X_train,y_train)

#使用训练好的模型lr对X_test

lr_y_predict=lr.predict(X_test)

#调用SGDClassifier中的fit函数/模块用来训练模型参数

sgdc.fit(X_train,y_train)

sgdc_y_predict=sgdc.predict(X_test)

#步骤四:使用线性分类模型从事良/恶性肿瘤预测任务的性能分析

#从sklearn.metrics里导入classification_report模块

from sklearn.metrics import classification_report

#使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果

print('Accuracy of LR Classifier:',lr.score(X_test,y_test))

#利用classification_report模块获得LogisticRegression其他三个指标的结果。

print(classification_report(y_test,lr_y_predict,target_names=['Benign','Malignant']))

#使用随机梯度下降模型自带的评分函数score获得模型在测试集上的准确性结果

print('Accuracy of SGD Classifier:',sgdc.score(X_test,y_test))

#利用classification_report模块获得LogisticRegression其他三个指标的结果。

print(classification_report(y_test,sgdc_y_predict,target_names=['Benign','Malignant']))

 

 

转载于:https://www.cnblogs.com/yxllfl/p/10526921.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值