In [ ]:
!pip install xgboost
In [3]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
In [7]:
cancer = load_breast_cancer()
cancer
Out[7]:
{'data': array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01, 1.189e-01], [2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01, 8.902e-02], [1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01, 8.758e-02], ..., [1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01, 7.820e-02], [2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01, 1.240e-01], [7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01, 7.039e-02]]), 'target': 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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 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]), 'target_names': array(['malignant', 'benign'], dtype='<U9'), 'DESCR': '.. _breast_cancer_dataset:\n\nBreast cancer wisconsin (diagnostic) dataset\n--------------------------------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 569\n\n :Number of Attributes: 30 numeric, predictive attributes and the class\n\n :Attribute Information:\n - radius (mean of distances from center to points on the perimeter)\n - texture (standard deviation of gray-scale values)\n - perimeter\n - area\n - smoothness (local variation in radius lengths)\n - compactness (perimeter^2 / area - 1.0)\n - concavity (severity of concave portions of the contour)\n - concave points (number of concave portions of the contour)\n - symmetry \n - fractal dimension ("coastline approximation" - 1)\n\n The mean, standard error, and "worst" or largest (mean of the three\n largest values) of these features were computed for each image,\n resulting in 30 features. For instance, field 3 is Mean Radius, field\n 13 is Radius SE, field 23 is Worst Radius.\n\n - class:\n - WDBC-Malignant\n - WDBC-Benign\n\n :Summary Statistics:\n\n ===================================== ====== ======\n Min Max\n ===================================== ====== ======\n radius (mean): 6.981 28.11\n texture (mean): 9.71 39.28\n perimeter (mean): 43.79 188.5\n area (mean): 143.5 2501.0\n smoothness (mean): 0.053 0.163\n compactness (mean): 0.019 0.345\n concavity (mean): 0.0 0.427\n concave points (mean): 0.0 0.201\n symmetry (mean): 0.106 0.304\n fractal dimension (mean): 0.05 0.097\n radius (standard error): 0.112 2.873\n texture (standard error): 0.36 4.885\n perimeter (standard error): 0.757 21.98\n area (standard error): 6.802 542.2\n smoothness (standard error): 0.002 0.031\n compactness (standard error): 0.002 0.135\n concavity (standard error): 0.0 0.396\n concave points (standard error): 0.0 0.053\n symmetry (standard error): 0.008 0.079\n fractal dimension (standard error): 0.001 0.03\n radius (worst): 7.93 36.04\n texture (worst): 12.02 49.54\n perimeter (worst): 50.41 251.2\n area (worst): 185.2 4254.0\n smoothness (worst): 0.071 0.223\n compactness (worst): 0.027 1.058\n concavity (worst): 0.0 1.252\n concave points (worst): 0.0 0.291\n symmetry (worst): 0.156 0.664\n fractal dimension (worst): 0.055 0.208\n ===================================== ====== ======\n\n :Missing Attribute Values: None\n\n :Class Distribution: 212 - Malignant, 357 - Benign\n\n :Creator: Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian\n\n :Donor: Nick Street\n\n :Date: November, 1995\n\nThis is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.\nhttps://goo.gl/U2Uwz2\n\nFeatures are computed from a digitized image of a fine needle\naspirate (FNA) of a breast mass. They describe\ncharacteristics of the cell nuclei present in the image.\n\nSeparating plane described above was obtained using\nMultisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree\nConstruction Via Linear Programming." Proceedings of the 4th\nMidwest Artificial Intelligence and Cognitive Science Society,\npp. 97-101, 1992], a classification method which uses linear\nprogramming to construct a decision tree. Relevant features\nwere selected using an exhaustive search in the space of 1-4\nfeatures and 1-3 separating planes.\n\nThe actual linear program used to obtain the separating plane\nin the 3-dimensional space is that described in:\n[K. P. Bennett and O. L. Mangasarian: "Robust Linear\nProgramming Discrimination of Two Linearly Inseparable Sets",\nOptimization Methods and Software 1, 1992, 23-34].\n\nThis database is also available through the UW CS ftp server:\n\nftp ftp.cs.wisc.edu\ncd math-prog/cpo-dataset/machine-learn/WDBC/\n\n.. topic:: References\n\n - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction \n for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on \n Electronic Imaging: Science and Technology, volume 1905, pages 861-870,\n San Jose, CA, 1993.\n - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and \n prognosis via linear programming. Operations Research, 43(4), pages 570-577, \n July-August 1995.\n - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques\n to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) \n 163-171.', 'feature_names': 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'), 'filename': 'c:\\users\\karan\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\sklearn\\datasets\\data\\breast_cancer.csv'}
In [8]:
cancer['feature_names']
Out[8]:
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 [9]:
cancer['target_names']
Out[9]:
array(['malignant', 'benign'], dtype='<U9')
In [10]:
X = cancer.data
y = cancer.target
In [11]:
xgb_model = xgb.XGBClassifier(objective="binary:logistic", random_state=42)
xgb_model.fit(X,y)
Out[11]:
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1, importance_type='gain', interaction_constraints='', learning_rate=0.300000012, max_delta_step=0, max_depth=6, min_child_weight=1, missing=nan, monotone_constraints='()', n_estimators=100, n_jobs=0, num_parallel_tree=1, objective='binary:logistic', random_state=42, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method='exact', validate_parameters=1, verbosity=None)
In [12]:
y_pred = xgb_model.predict(X)
y_pred
Out[12]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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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 [13]:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y, y_pred)
cm
Out[13]:
array([[212, 0], [ 0, 357]], dtype=int64)
In [14]:
from sklearn.metrics import accuracy_score
accuracy_score(y,y_pred) * 100
Out[14]:
100.0
In [ ]:
In [15]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [16]:
df = pd.read_csv('datasets/iris.csv')
df
Out[16]:
Unnamed: 0 | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|---|
0 | 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
… | … | … | … | … | … | … |
145 | 146 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 147 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 148 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 149 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 150 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 6 columns
In [17]:
df = df.iloc[:,1:]
df
Out[17]:
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
… | … | … | … | … | … |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [19]:
X = df.iloc[:,:4].values
y = df.iloc[:,4].values
In [20]:
from sklearn.preprocessing import LabelEncoder
Ly = LabelEncoder()
y = Ly.fit_transform(y)
y
Out[20]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
In [21]:
Ly.classes_
Out[21]:
array(['setosa', 'versicolor', 'virginica'], dtype=object)
In [22]:
Ly.inverse_transform([0])
Out[22]:
array(['setosa'], dtype=object)
In [23]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=0)
X_train
Out[23]:
array([[6.4, 3.1, 5.5, 1.8], [5.4, 3. , 4.5, 1.5], [5.2, 3.5, 1.5, 0.2], [6.1, 3. , 4.9, 1.8], [6.4, 2.8, 5.6, 2.2], [5.2, 2.7, 3.9, 1.4], [5.7, 3.8, 1.7, 0.3], [6. , 2.7, 5.1, 1.6], [5.9, 3. , 4.2, 1.5], [5.8, 2.6, 4. , 1.2], [6.8, 3. , 5.5, 2.1], [4.7, 3.2, 1.3, 0.2], [6.9, 3.1, 5.1, 2.3], [5. , 3.5, 1.6, 0.6], [5.4, 3.7, 1.5, 0.2], [5. , 2. , 3.5, 1. ], [6.5, 3. , 5.5, 1.8], [6.7, 3.3, 5.7, 2.5], [6. , 2.2, 5. , 1.5], [6.7, 2.5, 5.8, 1.8], [5.6, 2.5, 3.9, 1.1], [7.7, 3. , 6.1, 2.3], [6.3, 3.3, 4.7, 1.6], [5.5, 2.4, 3.8, 1.1], [6.3, 2.7, 4.9, 1.8], [6.3, 2.8, 5.1, 1.5], [4.9, 2.5, 4.5, 1.7], [6.3, 2.5, 5. , 1.9], [7. , 3.2, 4.7, 1.4], [6.5, 3. , 5.2, 2. ], [6. , 3.4, 4.5, 1.6], [4.8, 3.1, 1.6, 0.2], [5.8, 2.7, 5.1, 1.9], [5.6, 2.7, 4.2, 1.3], [5.6, 2.9, 3.6, 1.3], [5.5, 2.5, 4. , 1.3], [6.1, 3. , 4.6, 1.4], [7.2, 3.2, 6. , 1.8], [5.3, 3.7, 1.5, 0.2], [4.3, 3. , 1.1, 0.1], [6.4, 2.7, 5.3, 1.9], [5.7, 3. , 4.2, 1.2], [5.4, 3.4, 1.7, 0.2], [5.7, 4.4, 1.5, 0.4], [6.9, 3.1, 4.9, 1.5], [4.6, 3.1, 1.5, 0.2], [5.9, 3. , 5.1, 1.8], [5.1, 2.5, 3. , 1.1], [4.6, 3.4, 1.4, 0.3], [6.2, 2.2, 4.5, 1.5], [7.2, 3.6, 6.1, 2.5], [5.7, 2.9, 4.2, 1.3], [4.8, 3. , 1.4, 0.1], [7.1, 3. , 5.9, 2.1], [6.9, 3.2, 5.7, 2.3], [6.5, 3. , 5.8, 2.2], [6.4, 2.8, 5.6, 2.1], [5.1, 3.8, 1.6, 0.2], [4.8, 3.4, 1.6, 0.2], [6.5, 3.2, 5.1, 2. ], [6.7, 3.3, 5.7, 2.1], [4.5, 2.3, 1.3, 0.3], [6.2, 3.4, 5.4, 2.3], [4.9, 3. , 1.4, 0.2], [5.7, 2.5, 5. , 2. ], [6.9, 3.1, 5.4, 2.1], [4.4, 3.2, 1.3, 0.2], [5. , 3.6, 1.4, 0.2], [7.2, 3. , 5.8, 1.6], [5.1, 3.5, 1.4, 0.3], [4.4, 3. , 1.3, 0.2], [5.4, 3.9, 1.7, 0.4], [5.5, 2.3, 4. , 1.3], [6.8, 3.2, 5.9, 2.3], [7.6, 3. , 6.6, 2.1], [5.1, 3.5, 1.4, 0.2], [4.9, 3.1, 1.5, 0.2], [5.2, 3.4, 1.4, 0.2], [5.7, 2.8, 4.5, 1.3], [6.6, 3. , 4.4, 1.4], [5. , 3.2, 1.2, 0.2], [5.1, 3.3, 1.7, 0.5], [6.4, 2.9, 4.3, 1.3], [5.4, 3.4, 1.5, 0.4], [7.7, 2.6, 6.9, 2.3], [4.9, 2.4, 3.3, 1. ], [7.9, 3.8, 6.4, 2. ], [6.7, 3.1, 4.4, 1.4], [5.2, 4.1, 1.5, 0.1], [6. , 3. , 4.8, 1.8], [5.8, 4. , 1.2, 0.2], [7.7, 2.8, 6.7, 2. ], [5.1, 3.8, 1.5, 0.3], [4.7, 3.2, 1.6, 0.2], [7.4, 2.8, 6.1, 1.9], [5. , 3.3, 1.4, 0.2], [6.3, 3.4, 5.6, 2.4], [5.7, 2.8, 4.1, 1.3], [5.8, 2.7, 3.9, 1.2], [5.7, 2.6, 3.5, 1. ], [6.4, 3.2, 5.3, 2.3], [6.7, 3. , 5.2, 2.3], [6.3, 2.5, 4.9, 1.5], [6.7, 3. , 5. , 1.7], [5. , 3. , 1.6, 0.2], [5.5, 2.4, 3.7, 1. ], [6.7, 3.1, 5.6, 2.4], [5.8, 2.7, 5.1, 1.9], [5.1, 3.4, 1.5, 0.2], [6.6, 2.9, 4.6, 1.3], [5.6, 3. , 4.1, 1.3], [5.9, 3.2, 4.8, 1.8], [6.3, 2.3, 4.4, 1.3], [5.5, 3.5, 1.3, 0.2], [5.1, 3.7, 1.5, 0.4], [4.9, 3.1, 1.5, 0.1], [6.3, 2.9, 5.6, 1.8], [5.8, 2.7, 4.1, 1. ], [7.7, 3.8, 6.7, 2.2], [4.6, 3.2, 1.4, 0.2]])
In [24]:
import xgboost as xgb
xgb_model = xgb.XGBClassifier(random_state=0)
xgb_model.fit(X_train,y_train)
Out[24]:
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1, importance_type='gain', interaction_constraints='', learning_rate=0.300000012, max_delta_step=0, max_depth=6, min_child_weight=1, missing=nan, monotone_constraints='()', n_estimators=100, n_jobs=0, num_parallel_tree=1, objective='multi:softprob', random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=None, subsample=1, tree_method='exact', validate_parameters=1, verbosity=None)
In [25]:
Yp = xgb_model.predict(X_test)
Yp
Out[25]:
array([2, 1, 0, 2, 0, 2, 0, 1, 1, 1, 2, 1, 1, 1, 1, 0, 1, 1, 0, 0, 2, 1, 0, 0, 2, 0, 0, 1, 1, 0])
In [26]:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, Yp)
cm
Out[26]:
array([[11, 0, 0], [ 0, 13, 0], [ 0, 0, 6]], dtype=int64)
In [27]:
from sklearn.metrics import accuracy_score
accuracy_score(y_test,Yp) * 100
Out[27]:
100.0
In [ ]: