티스토리 뷰

2장_07_유방암데이터

유방암 데이터 활용

  • scikit-learn 에는 유방암 데이터가 기본적으로 들어있다. 머신러닝 학습에 많이 사용되는 데이터 이므로 익숙해지자.
  • 총 30개의 속성과 malignant(악성), benign(양성) 의 두가지 타겟값을 가지고 있다.
In [1]:
%pylab inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
Populating the interactive namespace from numpy and matplotlib
In [2]:
from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()
In [3]:
type(cancer)
Out[3]:
sklearn.utils.Bunch
In [4]:
dir(cancer)
Out[4]:
['DESCR', 'data', 'feature_names', 'target', 'target_names']
In [5]:
cancer.data.shape
Out[5]:
(569, 30)
In [6]:
cancer.feature_names
Out[6]:
array(['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'], dtype='<U23')
In [7]:
cancer.target_names
Out[7]:
array(['malignant', 'benign'], dtype='<U9')
In [8]:
cancer.target
Out[8]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
       1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
       1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
       1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
       0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
       1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
       0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
       1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
       1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0,
       0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
       1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
       1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
       1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
       1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
       1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
       1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1])
In [9]:
np.bincount(cancer.target) # cancer.target[cancer.target==0].shape
Out[9]:
array([212, 357], dtype=int64)
In [10]:
print(cancer.DESCR)
Breast Cancer Wisconsin (Diagnostic) Database
=============================================

Notes
-----
Data Set Characteristics:
    :Number of Instances: 569

    :Number of Attributes: 30 numeric, predictive attributes and the class

    :Attribute Information:
        - radius (mean of distances from center to points on the perimeter)
        - texture (standard deviation of gray-scale values)
        - perimeter
        - area
        - smoothness (local variation in radius lengths)
        - compactness (perimeter^2 / area - 1.0)
        - concavity (severity of concave portions of the contour)
        - concave points (number of concave portions of the contour)
        - symmetry 
        - fractal dimension ("coastline approximation" - 1)

        The mean, standard error, and "worst" or largest (mean of the three
        largest values) of these features were computed for each image,
        resulting in 30 features.  For instance, field 3 is Mean Radius, field
        13 is Radius SE, field 23 is Worst Radius.

        - class:
                - WDBC-Malignant
                - WDBC-Benign

    :Summary Statistics:

    ===================================== ====== ======
                                           Min    Max
    ===================================== ====== ======
    radius (mean):                        6.981  28.11
    texture (mean):                       9.71   39.28
    perimeter (mean):                     43.79  188.5
    area (mean):                          143.5  2501.0
    smoothness (mean):                    0.053  0.163
    compactness (mean):                   0.019  0.345
    concavity (mean):                     0.0    0.427
    concave points (mean):                0.0    0.201
    symmetry (mean):                      0.106  0.304
    fractal dimension (mean):             0.05   0.097
    radius (standard error):              0.112  2.873
    texture (standard error):             0.36   4.885
    perimeter (standard error):           0.757  21.98
    area (standard error):                6.802  542.2
    smoothness (standard error):          0.002  0.031
    compactness (standard error):         0.002  0.135
    concavity (standard error):           0.0    0.396
    concave points (standard error):      0.0    0.053
    symmetry (standard error):            0.008  0.079
    fractal dimension (standard error):   0.001  0.03
    radius (worst):                       7.93   36.04
    texture (worst):                      12.02  49.54
    perimeter (worst):                    50.41  251.2
    area (worst):                         185.2  4254.0
    smoothness (worst):                   0.071  0.223
    compactness (worst):                  0.027  1.058
    concavity (worst):                    0.0    1.252
    concave points (worst):               0.0    0.291
    symmetry (worst):                     0.156  0.664
    fractal dimension (worst):            0.055  0.208
    ===================================== ====== ======

    :Missing Attribute Values: None

    :Class Distribution: 212 - Malignant, 357 - Benign

    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian

    :Donor: Nick Street

    :Date: November, 1995

This is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.
https://goo.gl/U2Uwz2

Features are computed from a digitized image of a fine needle
aspirate (FNA) of a breast mass.  They describe
characteristics of the cell nuclei present in the image.

Separating plane described above was obtained using
Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree
Construction Via Linear Programming." Proceedings of the 4th
Midwest Artificial Intelligence and Cognitive Science Society,
pp. 97-101, 1992], a classification method which uses linear
programming to construct a decision tree.  Relevant features
were selected using an exhaustive search in the space of 1-4
features and 1-3 separating planes.

The actual linear program used to obtain the separating plane
in the 3-dimensional space is that described in:
[K. P. Bennett and O. L. Mangasarian: "Robust Linear
Programming Discrimination of Two Linearly Inseparable Sets",
Optimization Methods and Software 1, 1992, 23-34].

This database is also available through the UW CS ftp server:

ftp ftp.cs.wisc.edu
cd math-prog/cpo-dataset/machine-learn/WDBC/

References
----------
   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction 
     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on 
     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,
     San Jose, CA, 1993.
   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and 
     prognosis via linear programming. Operations Research, 43(4), pages 570-577, 
     July-August 1995.
   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques
     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) 
     163-171.

In [11]:
for i,name in enumerate(cancer.feature_names):
    print('%02d : %s' %(i,name)) # 0을 안붙이면 앞에 0을 안붙이고, 02를 안붙이면 숫자를 앞으로 땡겨쓴다.
00 : mean radius
01 : mean texture
02 : mean perimeter
03 : mean area
04 : mean smoothness
05 : mean compactness
06 : mean concavity
07 : mean concave points
08 : mean symmetry
09 : mean fractal dimension
10 : radius error
11 : texture error
12 : perimeter error
13 : area error
14 : smoothness error
15 : compactness error
16 : concavity error
17 : concave points error
18 : symmetry error
19 : fractal dimension error
20 : worst radius
21 : worst texture
22 : worst perimeter
23 : worst area
24 : worst smoothness
25 : worst compactness
26 : worst concavity
27 : worst concave points
28 : worst symmetry
29 : worst fractal dimension
In [12]:
cancer.target_names # malignant(악성), benign(양성)
Out[12]:
array(['malignant', 'benign'], dtype='<U9')
In [13]:
print('data =>',cancer.data.shape)
print('target =>',cancer.target.shape)

malignant = cancer.data[cancer.target==0]
benign = cancer.data[cancer.target==1]

print('malignant(악성) =>',malignant.shape)
print('benign(양성) =>',benign.shape)
data => (569, 30)
target => (569,)
malignant(악성) => (212, 30)
benign(양성) => (357, 30)
In [14]:
_, bins=np.histogram(cancer.data[:,0], bins=20)
np.histogram(cancer.data[:,0], bins=20)
Out[14]:
(array([ 4, 15, 31, 48, 93, 92, 71, 58, 32, 23, 22, 28, 27, 11,  2,  5,  2,
         2,  0,  3], dtype=int64),
 array([ 6.981  ,  8.03745,  9.0939 , 10.15035, 11.2068 , 12.26325,
        13.3197 , 14.37615, 15.4326 , 16.48905, 17.5455 , 18.60195,
        19.6584 , 20.71485, 21.7713 , 22.82775, 23.8842 , 24.94065,
        25.9971 , 27.05355, 28.11   ]))
In [15]:
plt.hist(malignant[:,0],bins=bins, alpha=0.3)
plt.hist(benign[:,0], bins=bins ,alpha=0.3)
plt.title(cancer.feature_names[0])
Out[15]:
Text(0.5,1,'mean radius')
In [16]:
plt.figure(figsize=[20,15])

for col in range(30):
    plt.subplot(8,4,col+1)
    _, bins=np.histogram(cancer.data[:,col], bins=20)

    plt.hist(malignant[:,col],bins=bins, alpha=0.3)
    plt.hist(benign[:,col], bins=bins ,alpha=0.3)
    plt.title(cancer.feature_names[col])
    if col==0: plt.legend(cancer.target_names)
    plt.xticks([])
  • 10번 반복해서 Logistic Regression 과 선형 SVM 을 적용해 보자. train_test_split() 함수에서 랜덤하게 데이터를 나누기 때문에 매번 점수가 달라진다.
  • 선형 SVM 의 결과가 좋지 못하다. 이것은 데이터 정규화를 하지 않았기 때문인데 뒤에서 다루겠다.
In [17]:
from sklearn.linear_model import LogisticRegression

scores = []

for i in range(10):
    X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target)

    model = LogisticRegression()
    model.fit(X_train,y_train)

    score = model.score(X_test,y_test)
    scores.append(score)

print('scores =', scores)
scores = [0.965034965034965, 0.9370629370629371, 0.9370629370629371, 0.9300699300699301, 0.9300699300699301, 0.9440559440559441, 0.9300699300699301, 0.972027972027972, 0.951048951048951, 0.9020979020979021]
In [18]:
from sklearn.svm import LinearSVC

scores = []

for i in range(10):
    X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target)

    model = LinearSVC()
    model.fit(X_train,y_train)

    score = model.score(X_test,y_test)
    scores.append(score)

print('scores =', scores)
scores = [0.916083916083916, 0.9020979020979021, 0.9440559440559441, 0.9230769230769231, 0.8461538461538461, 0.8321678321678322, 0.916083916083916, 0.8811188811188811, 0.916083916083916, 0.7622377622377622]
In [19]:
fig=plt.figure(figsize=[14,14])
fig.suptitle('Breast Cancer - feature analysis', fontsize=20)

for col in range(cancer.feature_names.shape[0]): # 30 features
    plt.subplot(8,4,col+1)
    _,bins=np.histogram(cancer.data[:,col],bins=50)
    plt.hist(malignant[:,col], bins=bins, alpha=0.5, label='malignant', color='red')
    plt.hist(benign[:,col], bins=bins, alpha=0.5, label='benign', color='green')
    
    plt.title(cancer.feature_names[col]+('(%d)' % col))
    plt.xticks([])
    plt.yticks([])
    if col==0: plt.legend()
In [20]:
fig=plt.figure(figsize=[14,14])
fig.suptitle('Breast Cancer - feature analysis', fontsize=20)

for col in range(cancer.feature_names.shape[0]): # 30 features
    plt.subplot(8,4,col+1)
#     f_,bins=np.histogram(cancer.data[:,col],bins=50)
#     plt.hist(malignant[:,col], bins=bins, alpha=0.5, label='malignant', color='red')
#     plt.hist(benign[:,col], bins=bins, alpha=0.5, label='benign', color='green')
    plt.scatter(cancer.data[:,col], cancer.target, c=cancer.target, alpha=0.5)
    
    
    plt.title(cancer.feature_names[col]+('(%d)' % col))
    plt.xticks([])
    plt.yticks([])
#     if col==0: plt.legend()
In [21]:
col1= 15
col2= 28

plt.scatter(cancer.data[:,0], cancer.data[:,1], c=cancer.target, alpha=0.3)
Out[21]:
<matplotlib.collections.PathCollection at 0x1bd46117dd8>
In [22]:
fig,axes = plt.subplots(5,6,figsize=[12,20])
fig.suptitle('mean radius vs others', fontsize=20)

for i in range(30):
    ax=axes.ravel()[i]
    ax.scatter(cancer.data[:,0],cancer.data[:,i], c=cancer.target, cmap='winter', alpha=0.1)
    ax.set_title(cancer.feature_names[i]+('\n(%d)' % i))
    ax.set_axis_off()
In [23]:
#모든 속성에 대해서 한번에 그래프를 그릴수 없으므로 상관관계를 수치를 통해 데이터를 파악한다.
mat=np.corrcoef(cancer.data.T)  # 열로 읽으므로 Transpose 시켜줘야 한다.
mat

#상관계수: 모든 점에 대해서 (c1-c1')(c2-c2')을 나눠준 값의 합을 표준편차1,표준편차2,n으로 나눠준다.
Out[23]:
array([[ 1.00000000e+00,  3.23781891e-01,  9.97855281e-01,
         9.87357170e-01,  1.70581187e-01,  5.06123578e-01,
         6.76763550e-01,  8.22528522e-01,  1.47741242e-01,
        -3.11630826e-01,  6.79090388e-01, -9.73174431e-02,
         6.74171616e-01,  7.35863663e-01, -2.22600125e-01,
         2.05999980e-01,  1.94203623e-01,  3.76168956e-01,
        -1.04320881e-01, -4.26412691e-02,  9.69538973e-01,
         2.97007644e-01,  9.65136514e-01,  9.41082460e-01,
         1.19616140e-01,  4.13462823e-01,  5.26911462e-01,
         7.44214198e-01,  1.63953335e-01,  7.06588569e-03],
       [ 3.23781891e-01,  1.00000000e+00,  3.29533059e-01,
         3.21085696e-01, -2.33885160e-02,  2.36702222e-01,
         3.02417828e-01,  2.93464051e-01,  7.14009805e-02,
        -7.64371834e-02,  2.75868676e-01,  3.86357623e-01,
         2.81673115e-01,  2.59844987e-01,  6.61377735e-03,
         1.91974611e-01,  1.43293077e-01,  1.63851025e-01,
         9.12716776e-03,  5.44575196e-02,  3.52572947e-01,
         9.12044589e-01,  3.58039575e-01,  3.43545947e-01,
         7.75033588e-02,  2.77829592e-01,  3.01025224e-01,
         2.95315843e-01,  1.05007910e-01,  1.19205351e-01],
       [ 9.97855281e-01,  3.29533059e-01,  1.00000000e+00,
         9.86506804e-01,  2.07278164e-01,  5.56936211e-01,
         7.16135650e-01,  8.50977041e-01,  1.83027212e-01,
        -2.61476908e-01,  6.91765014e-01, -8.67610783e-02,
         6.93134890e-01,  7.44982694e-01, -2.02694026e-01,
         2.50743681e-01,  2.28082345e-01,  4.07216916e-01,
        -8.16293270e-02, -5.52339106e-03,  9.69476363e-01,
         3.03038372e-01,  9.70386887e-01,  9.41549808e-01,
         1.50549404e-01,  4.55774228e-01,  5.63879263e-01,
         7.71240789e-01,  1.89115040e-01,  5.10185297e-02],
       [ 9.87357170e-01,  3.21085696e-01,  9.86506804e-01,
         1.00000000e+00,  1.77028377e-01,  4.98501682e-01,
         6.85982829e-01,  8.23268869e-01,  1.51293079e-01,
        -2.83109812e-01,  7.32562227e-01, -6.62802136e-02,
         7.26628328e-01,  8.00085921e-01, -1.66776667e-01,
         2.12582551e-01,  2.07660060e-01,  3.72320282e-01,
        -7.24965882e-02, -1.98869632e-02,  9.62746086e-01,
         2.87488627e-01,  9.59119574e-01,  9.59213326e-01,
         1.23522939e-01,  3.90410309e-01,  5.12605920e-01,
         7.22016626e-01,  1.43569914e-01,  3.73759735e-03],
       [ 1.70581187e-01, -2.33885160e-02,  2.07278164e-01,
         1.77028377e-01,  1.00000000e+00,  6.59123215e-01,
         5.21983768e-01,  5.53695173e-01,  5.57774788e-01,
         5.84792002e-01,  3.01467098e-01,  6.84064482e-02,
         2.96091933e-01,  2.46552432e-01,  3.32375443e-01,
         3.18943297e-01,  2.48395680e-01,  3.80675691e-01,
         2.00774376e-01,  2.83606699e-01,  2.13120142e-01,
         3.60717988e-02,  2.38852626e-01,  2.06718363e-01,
         8.05324195e-01,  4.72468444e-01,  4.34925715e-01,
         5.03053353e-01,  3.94309482e-01,  4.99316369e-01],
       [ 5.06123578e-01,  2.36702222e-01,  5.56936211e-01,
         4.98501682e-01,  6.59123215e-01,  1.00000000e+00,
         8.83120670e-01,  8.31135043e-01,  6.02641048e-01,
         5.65368663e-01,  4.97473446e-01,  4.62048307e-02,
         5.48905265e-01,  4.55652852e-01,  1.35299268e-01,
         7.38721790e-01,  5.70516871e-01,  6.42261851e-01,
         2.29976591e-01,  5.07318127e-01,  5.35315398e-01,
         2.48132833e-01,  5.90210428e-01,  5.09603806e-01,
         5.65541166e-01,  8.65809040e-01,  8.16275250e-01,
         8.15573224e-01,  5.10223430e-01,  6.87382323e-01],
       [ 6.76763550e-01,  3.02417828e-01,  7.16135650e-01,
         6.85982829e-01,  5.21983768e-01,  8.83120670e-01,
         1.00000000e+00,  9.21391026e-01,  5.00666617e-01,
         3.36783359e-01,  6.31924822e-01,  7.62183481e-02,
         6.60390787e-01,  6.17426806e-01,  9.85637458e-02,
         6.70278825e-01,  6.91270208e-01,  6.83259917e-01,
         1.78009208e-01,  4.49300749e-01,  6.88236408e-01,
         2.99878889e-01,  7.29564917e-01,  6.75987231e-01,
         4.48822038e-01,  7.54968016e-01,  8.84102639e-01,
         8.61323034e-01,  4.09464127e-01,  5.14929891e-01],
       [ 8.22528522e-01,  2.93464051e-01,  8.50977041e-01,
         8.23268869e-01,  5.53695173e-01,  8.31135043e-01,
         9.21391026e-01,  1.00000000e+00,  4.62497388e-01,
         1.66917383e-01,  6.98049834e-01,  2.14795819e-02,
         7.10649872e-01,  6.90298540e-01,  2.76533084e-02,
         4.90424250e-01,  4.39167068e-01,  6.15634131e-01,
         9.53507869e-02,  2.57583746e-01,  8.30317631e-01,
         2.92751713e-01,  8.55923128e-01,  8.09629620e-01,
         4.52753054e-01,  6.67453677e-01,  7.52399498e-01,
         9.10155314e-01,  3.75744145e-01,  3.68661134e-01],
       [ 1.47741242e-01,  7.14009805e-02,  1.83027212e-01,
         1.51293079e-01,  5.57774788e-01,  6.02641048e-01,
         5.00666617e-01,  4.62497388e-01,  1.00000000e+00,
         4.79921330e-01,  3.03379263e-01,  1.28052926e-01,
         3.13892761e-01,  2.23970219e-01,  1.87321165e-01,
         4.21659147e-01,  3.42627023e-01,  3.93297872e-01,
         4.49136542e-01,  3.31786146e-01,  1.85727753e-01,
         9.06506881e-02,  2.19168559e-01,  1.77193377e-01,
         4.26675026e-01,  4.73200013e-01,  4.33721008e-01,
         4.30296611e-01,  6.99825798e-01,  4.38413498e-01],
       [-3.11630826e-01, -7.64371834e-02, -2.61476908e-01,
        -2.83109812e-01,  5.84792002e-01,  5.65368663e-01,
         3.36783359e-01,  1.66917383e-01,  4.79921330e-01,
         1.00000000e+00,  1.10995073e-04,  1.64173966e-01,
         3.98299316e-02, -9.01702475e-02,  4.01964425e-01,
         5.59836691e-01,  4.46630322e-01,  3.41198044e-01,
         3.45007397e-01,  6.88131577e-01, -2.53691495e-01,
        -5.12692020e-02, -2.05151211e-01, -2.31854451e-01,
         5.04942075e-01,  4.58798157e-01,  3.46233876e-01,
         1.75325449e-01,  3.34018684e-01,  7.67296779e-01],
       [ 6.79090388e-01,  2.75868676e-01,  6.91765014e-01,
         7.32562227e-01,  3.01467098e-01,  4.97473446e-01,
         6.31924822e-01,  6.98049834e-01,  3.03379263e-01,
         1.10995073e-04,  1.00000000e+00,  2.13247337e-01,
         9.72793677e-01,  9.51830112e-01,  1.64514220e-01,
         3.56064576e-01,  3.32357538e-01,  5.13346441e-01,
         2.40567362e-01,  2.27753533e-01,  7.15065195e-01,
         1.94798557e-01,  7.19683804e-01,  7.51548476e-01,
         1.41918553e-01,  2.87103166e-01,  3.80584635e-01,
         5.31062328e-01,  9.45428304e-02,  4.95594325e-02],
       [-9.73174431e-02,  3.86357623e-01, -8.67610783e-02,
        -6.62802136e-02,  6.84064482e-02,  4.62048307e-02,
         7.62183481e-02,  2.14795819e-02,  1.28052926e-01,
         1.64173966e-01,  2.13247337e-01,  1.00000000e+00,
         2.23170729e-01,  1.11567247e-01,  3.97242853e-01,
         2.31699699e-01,  1.94998464e-01,  2.30283400e-01,
         4.11620680e-01,  2.79722748e-01, -1.11690313e-01,
         4.09002766e-01, -1.02241922e-01, -8.31949886e-02,
        -7.36576591e-02, -9.24393536e-02, -6.89562205e-02,
        -1.19637523e-01, -1.28214758e-01, -4.56545686e-02],
       [ 6.74171616e-01,  2.81673115e-01,  6.93134890e-01,
         7.26628328e-01,  2.96091933e-01,  5.48905265e-01,
         6.60390787e-01,  7.10649872e-01,  3.13892761e-01,
         3.98299316e-02,  9.72793677e-01,  2.23170729e-01,
         1.00000000e+00,  9.37655407e-01,  1.51075331e-01,
         4.16322368e-01,  3.62481582e-01,  5.56264084e-01,
         2.66487092e-01,  2.44142773e-01,  6.97200593e-01,
         2.00370854e-01,  7.21031310e-01,  7.30712973e-01,
         1.30054392e-01,  3.41919445e-01,  4.18898816e-01,
         5.54897231e-01,  1.09930434e-01,  8.54325721e-02],
       [ 7.35863663e-01,  2.59844987e-01,  7.44982694e-01,
         8.00085921e-01,  2.46552432e-01,  4.55652852e-01,
         6.17426806e-01,  6.90298540e-01,  2.23970219e-01,
        -9.01702475e-02,  9.51830112e-01,  1.11567247e-01,
         9.37655407e-01,  1.00000000e+00,  7.51503381e-02,
         2.84840057e-01,  2.70894726e-01,  4.15729568e-01,
         1.34108980e-01,  1.27070903e-01,  7.57373189e-01,
         1.96496649e-01,  7.61212636e-01,  8.11407961e-01,
         1.25389431e-01,  2.83256538e-01,  3.85100136e-01,
         5.38166314e-01,  7.41262916e-02,  1.75392951e-02],
       [-2.22600125e-01,  6.61377735e-03, -2.02694026e-01,
        -1.66776667e-01,  3.32375443e-01,  1.35299268e-01,
         9.85637458e-02,  2.76533084e-02,  1.87321165e-01,
         4.01964425e-01,  1.64514220e-01,  3.97242853e-01,
         1.51075331e-01,  7.51503381e-02,  1.00000000e+00,
         3.36696081e-01,  2.68684760e-01,  3.28429499e-01,
         4.13506125e-01,  4.27374207e-01, -2.30690710e-01,
        -7.47429649e-02, -2.17303755e-01, -1.82195478e-01,
         3.14457456e-01, -5.55581387e-02, -5.82983867e-02,
        -1.02006796e-01, -1.07342098e-01,  1.01480315e-01],
       [ 2.05999980e-01,  1.91974611e-01,  2.50743681e-01,
         2.12582551e-01,  3.18943297e-01,  7.38721790e-01,
         6.70278825e-01,  4.90424250e-01,  4.21659147e-01,
         5.59836691e-01,  3.56064576e-01,  2.31699699e-01,
         4.16322368e-01,  2.84840057e-01,  3.36696081e-01,
         1.00000000e+00,  8.01268343e-01,  7.44082668e-01,
         3.94712835e-01,  8.03268818e-01,  2.04607166e-01,
         1.43002583e-01,  2.60515840e-01,  1.99371329e-01,
         2.27394232e-01,  6.78780354e-01,  6.39146698e-01,
         4.83208327e-01,  2.77878431e-01,  5.90972763e-01],
       [ 1.94203623e-01,  1.43293077e-01,  2.28082345e-01,
         2.07660060e-01,  2.48395680e-01,  5.70516871e-01,
         6.91270208e-01,  4.39167068e-01,  3.42627023e-01,
         4.46630322e-01,  3.32357538e-01,  1.94998464e-01,
         3.62481582e-01,  2.70894726e-01,  2.68684760e-01,
         8.01268343e-01,  1.00000000e+00,  7.71803995e-01,
         3.09428578e-01,  7.27372184e-01,  1.86903516e-01,
         1.00240984e-01,  2.26680426e-01,  1.88352652e-01,
         1.68481321e-01,  4.84857801e-01,  6.62564134e-01,
         4.40472263e-01,  1.97787822e-01,  4.39329269e-01],
       [ 3.76168956e-01,  1.63851025e-01,  4.07216916e-01,
         3.72320282e-01,  3.80675691e-01,  6.42261851e-01,
         6.83259917e-01,  6.15634131e-01,  3.93297872e-01,
         3.41198044e-01,  5.13346441e-01,  2.30283400e-01,
         5.56264084e-01,  4.15729568e-01,  3.28429499e-01,
         7.44082668e-01,  7.71803995e-01,  1.00000000e+00,
         3.12780223e-01,  6.11044139e-01,  3.58126669e-01,
         8.67412099e-02,  3.94999252e-01,  3.42271162e-01,
         2.15350597e-01,  4.52888377e-01,  5.49592375e-01,
         6.02449606e-01,  1.43115669e-01,  3.10654551e-01],
       [-1.04320881e-01,  9.12716776e-03, -8.16293270e-02,
        -7.24965882e-02,  2.00774376e-01,  2.29976591e-01,
         1.78009208e-01,  9.53507869e-02,  4.49136542e-01,
         3.45007397e-01,  2.40567362e-01,  4.11620680e-01,
         2.66487092e-01,  1.34108980e-01,  4.13506125e-01,
         3.94712835e-01,  3.09428578e-01,  3.12780223e-01,
         1.00000000e+00,  3.69078083e-01, -1.28120769e-01,
        -7.74734202e-02, -1.03753044e-01, -1.10342743e-01,
        -1.26617996e-02,  6.02548795e-02,  3.71190486e-02,
        -3.04133964e-02,  3.89402485e-01,  7.80794763e-02],
       [-4.26412691e-02,  5.44575196e-02, -5.52339106e-03,
        -1.98869632e-02,  2.83606699e-01,  5.07318127e-01,
         4.49300749e-01,  2.57583746e-01,  3.31786146e-01,
         6.88131577e-01,  2.27753533e-01,  2.79722748e-01,
         2.44142773e-01,  1.27070903e-01,  4.27374207e-01,
         8.03268818e-01,  7.27372184e-01,  6.11044139e-01,
         3.69078083e-01,  1.00000000e+00, -3.74876179e-02,
        -3.19502887e-03, -1.00039763e-03, -2.27361473e-02,
         1.70568316e-01,  3.90158842e-01,  3.79974661e-01,
         2.15204013e-01,  1.11093956e-01,  5.91328066e-01],
       [ 9.69538973e-01,  3.52572947e-01,  9.69476363e-01,
         9.62746086e-01,  2.13120142e-01,  5.35315398e-01,
         6.88236408e-01,  8.30317631e-01,  1.85727753e-01,
        -2.53691495e-01,  7.15065195e-01, -1.11690313e-01,
         6.97200593e-01,  7.57373189e-01, -2.30690710e-01,
         2.04607166e-01,  1.86903516e-01,  3.58126669e-01,
        -1.28120769e-01, -3.74876179e-02,  1.00000000e+00,
         3.59920754e-01,  9.93707916e-01,  9.84014564e-01,
         2.16574431e-01,  4.75820037e-01,  5.73974708e-01,
         7.87423852e-01,  2.43529204e-01,  9.34919787e-02],
       [ 2.97007644e-01,  9.12044589e-01,  3.03038372e-01,
         2.87488627e-01,  3.60717988e-02,  2.48132833e-01,
         2.99878889e-01,  2.92751713e-01,  9.06506881e-02,
        -5.12692020e-02,  1.94798557e-01,  4.09002766e-01,
         2.00370854e-01,  1.96496649e-01, -7.47429649e-02,
         1.43002583e-01,  1.00240984e-01,  8.67412099e-02,
        -7.74734202e-02, -3.19502887e-03,  3.59920754e-01,
         1.00000000e+00,  3.65098245e-01,  3.45842283e-01,
         2.25429415e-01,  3.60832339e-01,  3.68365607e-01,
         3.59754610e-01,  2.33027461e-01,  2.19122425e-01],
       [ 9.65136514e-01,  3.58039575e-01,  9.70386887e-01,
         9.59119574e-01,  2.38852626e-01,  5.90210428e-01,
         7.29564917e-01,  8.55923128e-01,  2.19168559e-01,
        -2.05151211e-01,  7.19683804e-01, -1.02241922e-01,
         7.21031310e-01,  7.61212636e-01, -2.17303755e-01,
         2.60515840e-01,  2.26680426e-01,  3.94999252e-01,
        -1.03753044e-01, -1.00039763e-03,  9.93707916e-01,
         3.65098245e-01,  1.00000000e+00,  9.77578091e-01,
         2.36774604e-01,  5.29407690e-01,  6.18344080e-01,
         8.16322102e-01,  2.69492769e-01,  1.38956862e-01],
       [ 9.41082460e-01,  3.43545947e-01,  9.41549808e-01,
         9.59213326e-01,  2.06718363e-01,  5.09603806e-01,
         6.75987231e-01,  8.09629620e-01,  1.77193377e-01,
        -2.31854451e-01,  7.51548476e-01, -8.31949886e-02,
         7.30712973e-01,  8.11407961e-01, -1.82195478e-01,
         1.99371329e-01,  1.88352652e-01,  3.42271162e-01,
        -1.10342743e-01, -2.27361473e-02,  9.84014564e-01,
         3.45842283e-01,  9.77578091e-01,  1.00000000e+00,
         2.09145334e-01,  4.38296284e-01,  5.43330525e-01,
         7.47418802e-01,  2.09145508e-01,  7.96470340e-02],
       [ 1.19616140e-01,  7.75033588e-02,  1.50549404e-01,
         1.23522939e-01,  8.05324195e-01,  5.65541166e-01,
         4.48822038e-01,  4.52753054e-01,  4.26675026e-01,
         5.04942075e-01,  1.41918553e-01, -7.36576591e-02,
         1.30054392e-01,  1.25389431e-01,  3.14457456e-01,
         2.27394232e-01,  1.68481321e-01,  2.15350597e-01,
        -1.26617996e-02,  1.70568316e-01,  2.16574431e-01,
         2.25429415e-01,  2.36774604e-01,  2.09145334e-01,
         1.00000000e+00,  5.68186517e-01,  5.18523293e-01,
         5.47690903e-01,  4.93838330e-01,  6.17624192e-01],
       [ 4.13462823e-01,  2.77829592e-01,  4.55774228e-01,
         3.90410309e-01,  4.72468444e-01,  8.65809040e-01,
         7.54968016e-01,  6.67453677e-01,  4.73200013e-01,
         4.58798157e-01,  2.87103166e-01, -9.24393536e-02,
         3.41919445e-01,  2.83256538e-01, -5.55581387e-02,
         6.78780354e-01,  4.84857801e-01,  4.52888377e-01,
         6.02548795e-02,  3.90158842e-01,  4.75820037e-01,
         3.60832339e-01,  5.29407690e-01,  4.38296284e-01,
         5.68186517e-01,  1.00000000e+00,  8.92260899e-01,
         8.01080365e-01,  6.14440501e-01,  8.10454856e-01],
       [ 5.26911462e-01,  3.01025224e-01,  5.63879263e-01,
         5.12605920e-01,  4.34925715e-01,  8.16275250e-01,
         8.84102639e-01,  7.52399498e-01,  4.33721008e-01,
         3.46233876e-01,  3.80584635e-01, -6.89562205e-02,
         4.18898816e-01,  3.85100136e-01, -5.82983867e-02,
         6.39146698e-01,  6.62564134e-01,  5.49592375e-01,
         3.71190486e-02,  3.79974661e-01,  5.73974708e-01,
         3.68365607e-01,  6.18344080e-01,  5.43330525e-01,
         5.18523293e-01,  8.92260899e-01,  1.00000000e+00,
         8.55433860e-01,  5.32519729e-01,  6.86510921e-01],
       [ 7.44214198e-01,  2.95315843e-01,  7.71240789e-01,
         7.22016626e-01,  5.03053353e-01,  8.15573224e-01,
         8.61323034e-01,  9.10155314e-01,  4.30296611e-01,
         1.75325449e-01,  5.31062328e-01, -1.19637523e-01,
         5.54897231e-01,  5.38166314e-01, -1.02006796e-01,
         4.83208327e-01,  4.40472263e-01,  6.02449606e-01,
        -3.04133964e-02,  2.15204013e-01,  7.87423852e-01,
         3.59754610e-01,  8.16322102e-01,  7.47418802e-01,
         5.47690903e-01,  8.01080365e-01,  8.55433860e-01,
         1.00000000e+00,  5.02528494e-01,  5.11114146e-01],
       [ 1.63953335e-01,  1.05007910e-01,  1.89115040e-01,
         1.43569914e-01,  3.94309482e-01,  5.10223430e-01,
         4.09464127e-01,  3.75744145e-01,  6.99825798e-01,
         3.34018684e-01,  9.45428304e-02, -1.28214758e-01,
         1.09930434e-01,  7.41262916e-02, -1.07342098e-01,
         2.77878431e-01,  1.97787822e-01,  1.43115669e-01,
         3.89402485e-01,  1.11093956e-01,  2.43529204e-01,
         2.33027461e-01,  2.69492769e-01,  2.09145508e-01,
         4.93838330e-01,  6.14440501e-01,  5.32519729e-01,
         5.02528494e-01,  1.00000000e+00,  5.37848206e-01],
       [ 7.06588569e-03,  1.19205351e-01,  5.10185297e-02,
         3.73759735e-03,  4.99316369e-01,  6.87382323e-01,
         5.14929891e-01,  3.68661134e-01,  4.38413498e-01,
         7.67296779e-01,  4.95594325e-02, -4.56545686e-02,
         8.54325721e-02,  1.75392951e-02,  1.01480315e-01,
         5.90972763e-01,  4.39329269e-01,  3.10654551e-01,
         7.80794763e-02,  5.91328066e-01,  9.34919787e-02,
         2.19122425e-01,  1.38956862e-01,  7.96470340e-02,
         6.17624192e-01,  8.10454856e-01,  6.86510921e-01,
         5.11114146e-01,  5.37848206e-01,  1.00000000e+00]])
In [24]:
from sklearn.datasets import load_iris 

iris = load_iris()

mat_iris=np.corrcoef(iris.data.T)
plt.imshow(mat_iris, vmin=-1, vmax=1)
colorbar()
Out[24]:
<matplotlib.colorbar.Colorbar at 0x1bd448821d0>
In [25]:
mat.shape, mat[4,24]
Out[25]:
((30, 30), 0.8053241954943631)
In [26]:
plt.scatter(cancer.data[:,4], cancer.data[:,24], alpha=0.1)
Out[26]:
<matplotlib.collections.PathCollection at 0x1bd462d5ef0>
In [27]:
# 속성간의 관계를 한 눈에 파악
fig=plt.figure(figsize=[14,14])
plt.title('Breast Cancer - Correlation Coefficient',fontsize=20)
plt.imshow(mat, interpolation='none', vmin=-1, vmax=1)
plt.colorbar(shrink=0.7)
plt.xticks(range(30),cancer.feature_names,rotation=90,ha='center')
plt.yticks(range(30))
print('')

In [28]:
fig=plt.figure(figsize=[10,8])
plt.title('Cancer - boxplot for features',fontsize=15)
plt.boxplot(cancer.data)
plt.xticks(np.arange(30)+1,cancer.feature_names,rotation=90)
#plt.ylim(0,1) # y축 길이를 바꿔보자
plt.xlabel('features')
plt.ylabel('scale')
print('')

단위가 서로 다르다.

cancer.data를 평균은 0 표준편차를 1로 변환하자

In [29]:
# 속성별 평균
m = cancer.data.mean(axis=0)

# 속성별 표준편차
s = cancer.data.std(axis=0)

# 정규확인 데이터
data2 = (cancer.data - m)/s
In [30]:
fig=plt.figure(figsize=[20,15])
plt.boxplot(data2)
pass
In [31]:
m1 = cancer.data.max(axis=0)
m2 = cancer.data.min(axis=0)

data3 = (cancer.data-m2)/(m1-m2)

fig=plt.figure(figsize=[20,15])
plt.boxplot(data3)
pass

크기를 맞췄다.

kNN, LinearSVM, Logistic Regression을 적용

  • train_test_split() 적용 후 score 확인
  • 원본 데이터와 정규화된 데이터 비교
In [32]:
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target)

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train,y_train)

score = model.score(X_test,y_test)
score
Out[32]:
0.9300699300699301
In [49]:
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

X2_train,X2_test,y_train,y_test = train_test_split(data2,cancer.target)

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X2_train,y_train)

score = model.score(X2_test,y_test)
score
Out[49]:
0.9790209790209791
In [34]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=1)
model.fit(X_train, y_train)

score = model.score(X_test, y_test)
print(score)
0.38461538461538464
In [35]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=1)
model.fit(X2_train, y_train)

score = model.score(X2_test, y_test)
print(score)
0.9440559440559441
In [36]:
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X_train,y_train)
score = model. score(X_test,y_test)
score
Out[36]:
0.6013986013986014
In [50]:
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X2_train,y_train)
score = model.score(X2_test,y_test)
score
Out[50]:
0.972027972027972
In [38]:
def sigmoid(x):
    return 1/(1+np.exp(-10*x))
In [39]:
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target)

model = LogisticRegression()
model.fit(X_train, y_train)

score_train = model.score(X_train, y_train)
score_test = model.score(X_test, y_test)

print(score_train, score_test)
0.9507042253521126 0.9440559440559441
In [47]:
from sklearn.svm import SVC

model = SVC(C=1)
model.fit(X_train, y_train)

score = model.score(X_test, y_test)
print(score)
0.6433566433566433
In [51]:
from sklearn.svm import SVC

model = SVC(C=1)
model.fit(X2_train, y_train)

score = model.score(X2_test, y_test)
print(score)
0.972027972027972
In [44]:
X_mean = X_train.mean(axis=0)
X_std = X_train.std(axis=0)
X_train_norm = (X_train - X_mean)/X_std
X_test_norm = (X_test - X_mean)/X_std

plt.boxplot(X_train_norm)
print('')

In [45]:
from sklearn.svm import SVC

model = SVC(C=1)
model.fit(X_train_norm, y_train)

score = model.score(X_test_norm, y_test)
print(score)
0.986013986013986

'beginner > 파이썬 머신러닝 기초' 카테고리의 다른 글

유방암 데이터 분석 by SVM  (0) 2019.03.12
지도학습 - kernel SVM  (0) 2019.03.07
지도학습 - 로지스틱회귀  (0) 2019.03.05
지도학습 - LinearSVM_2  (0) 2019.03.05
지도학습 - LinearSVM_1  (0) 2019.03.05
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함