1. obtain data
wget https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data
Separate raw data with commas:
Attributes of each column:
1. Sample Code Number ID number
2. clump thickness 1-10 Lump Thickness
3. Uniformity of cell size 1-10 cell size uniformity
4. Uniformity of cell shape 1-10 cell shape Uniformity
5. Marginal adhesion 1-10 edge attachment
6. Single epithelial cell size 1-10 single epithelial cell size
7. Bare nucleus 1-10 bare Core
8. bland chromatin 1-10 brenq
9. normal nucleus Oli 1-10 normal nucleus
10. mitoses 1-10 split
11. Class 2 is benign, and 4 is malignant
2. Use LR and SGD
Import pandas as pdimport numpy as npfrom sklearn. model_selection import train_test_splitfrom sklearn. preprocessing import standardscalerfrom sklearn. linear_model import logisticregressionfrom sklearn. linear_model import sgdclassifierfrom sklearn import metrics # the data does not have a title. Therefore, the headerdata parameter is added as PD. read_csv ('https: // example Onsin. data ', header = none) column_names = ['sample code number', 'clump thickness', 'uniformity of cell size', 'uniformity of cell size', 'marginal adhesion ', 'Single epithelial cell size', 'bare route', 'blandchromatin ', 'normal compute', 'docs', 'class'] data. columns = column_names # What is found in the data? Symbol DATA = data. Replace (to_replace = '? ', Value = NP. nan) Data = data. dropna (how = 'any') # generally, 1 indicates malignant, and 0 indicates benign. (In this dataset, 4 indicates malignant. Therefore, 4 indicates 1, and 2 indicates 0) # data ['class'] [DATA ['class'] = 4] = 1 # data ['class'] [DATA ['class'] = 2] = 0data. loc [DATA ['class'] = 4, 'class'] = 1data. loc [DATA ['class'] = 2, 'class'] = 0 # The sample code number feature has no effect on classification. dataset 75% is used as the training set, 25% as test set x_train, x_test, y_train, y_test = train_test_split (data [column_names [], data [column_names [10], test_size = 0.25, random_state = 33) ss = standardscaler () x_train = ss. fit_transform (x_train) x_test = ss. transform (x_test) LR = logisticregression () LR. FIT (x_train, y_train) lr_y_predict = LR. predict (x_test) print ('the LR predict result', metrics. accuracy_score (lr_y_predict, y_test) # LR also comes with scoreprint ("the LR predict result show by LR. score ", LR. score (x_test, y_test) sgdc = sgdclassifier (max_iter = 1000) sgdc. FIT (x_train, y_train) sgdc_y_predict = sgdc. predict (x_test) print ("The sgdc predict result", metrics. accuracy_score (sgdc_y_predict, y_test) # sgdc also comes with scoreprint ("The sgdc predict result show by sgdc. score ", sgdc. score (x_test, y_test) print ("\ n") print ("Performance Analysis: \ n") # performance analysis from sklearn. metrics import classification_report # Use the classification_report module to obtain the LR results (recall rate, accuracy rate, and harmonic mean) print (classification_report (y_test, lr_y_predict, target_names = ['benign ', 'malignant']) # Use the classification_report module to obtain the output print (classification_report (y_test, sgdc_y_predict, target_names = ['benign ', 'malignant']) of the three sgdc indicators ''' Feature Analysis: LR uses a precise resolution method to calculate parameters. The time for calculation is long but the model performance is high. sgdc uses the random gradient rise algorithm to estimate model parameters, the computation time is short, but the output model performance is slightly lower. Generally, sgdc ''' is recommended for data with a training data volume of more than 0.1 million Cus, considering the time consumption '''
Wisconsin Benign Breast Cancer Prediction