<jupyter_start><jupyter_text>IntroductionIn this project, I will build and test several machine learning classification models on breast cancer data. The dataset used comes from the [UCI Machine Learning Repository for breast cancer](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)). The goal is of the project is to help detect early diagnosis of breat cancer using ML to drastically improve the prognosis and chance of survival. Please note that this project is in its early stages of development. Information on the DatasetThis dataset is publicly available online, and was created by Dr. <NAME>, a physician at the University Of Wisconsin Hospital.Attribute Information:1. ID number 2) Diagnosis (M = malignant, B = benign) 3–32)Ten real-valued features are computed for each cell nucleus:1. radius (mean of distances from center to points on the perimeter)2. texture (standard deviation of gray-scale values)3. perimeter4. area5. smoothness (local variation in radius lengths)6. compactness (perimeter² / area — 1.0)7. concavity (severity of concave portions of the contour)8. concave points (number of concave portions of the contour)9. symmetry10. fractal dimension (“coastline approximation” — 1) Importing Necessary Libraries & Files<jupyter_code>#importing EDA libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set() # Making sns as default for plots 
#importing scikit-learn libraries
from sklearn.linear_model import LogisticRegression # to apply the Logistic regression
from sklearn.model_selection import train_test_split # to split the data into two parts
from sklearn.model_selection import KFold # use for cross validation
from sklearn.ensemble import RandomForestClassifier # for random forest classifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import svm # for Support Vector Machine
from sklearn import metrics

#importing breast cancer dataset
data = pd.read_csv('./drive/My Drive/breastcancerdata.csv')
X = data.iloc[:, 1:31].values
Y = data.iloc[:, 31].values
data.head()
data.shape
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 33 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   id                       569 non-null    int64  
 1   diagnosis                569 non-null    object 
 2   radius_mean              569 non-null    float64
 3   texture_mean             569 non-null    float64
 4   perimeter_mean           569 non-null    float64
 5   area_mean                569 non-null    float64
 6   smoothness_mean          569 non-null    float64
 7   compactness_mean         569 non-null    float64
 8   concavity_mean           569 non-null    float64
 9   concave points_mean      569 non-null    float64
 10  symmetry_mean            569 non-null    float64
 11  fractal_dimension_mean   569 non-null    float64
 12  radius_se                569 non-null    float64
 13  texture_se               569 non-null    float64
 14  perimeter_se             5[...]<jupyter_text>Checking for NaN or Null Values<jupyter_code>data = data.drop(labels="Unnamed: 32", axis=1)
data = data.drop(labels="id", axis=1)
data.isnull().sum()
data.isna().sum()<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>data.describe()
features_mean= list(data.columns[1:11])
features_se= list(data.columns[11:20])
features_worst=list(data.columns[21:31])
print(features_mean)
print("-----------------------------------")
print(features_se)
print("------------------------------------")
print(features_worst)
data['diagnosis']=data['diagnosis'].map({'M':1,'B':0})
sns.countplot(data['diagnosis'],label="Count")<jupyter_output>/usr/local/lib/python3.6/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning<jupyter_text>From this plot, I can see that there is a more number of bengin stages of cancer, which can be cured.<jupyter_code>corr = data[features_mean].corr() # .corr is used for find corelation
plt.figure(figsize=(14,14))
sns.heatmap(corr, cbar = True,  square = True, annot=True, fmt= '.2f',annot_kws={'size': 15},
           xticklabels= features_mean, yticklabels= features_mean,
           cmap= 'coolwarm')<jupyter_output><empty_output><jupyter_text>Splitting Data<jupyter_code>prediction_var = ['texture_mean','perimeter_mean','smoothness_mean','compactness_mean','symmetry_mean'] #variables which I will use for prediction
train, test = train_test_split(data, test_size = 0.3)
print(train.shape)
print(test.shape)
train_X = train[prediction_var] #taking the training data input 
train_y=train.diagnosis #output of our training data
test_X= test[prediction_var] #taking test data inputs
test_y =test.diagnosis #output value of test data<jupyter_output><empty_output><jupyter_text>Building and Testing Appropriate Classifer Models<jupyter_code>def classification_model(model,data,prediction_input,output):
    # here the model means the model 
    # data is used for the data 
    #prediction_input means the inputs used for prediction
    # output mean the value which are to be predicted
    # here we will try to find out the Accuarcy of model by using same data for fiiting and 
    #comparison for same data
    #Fit the model:
    model.fit(data[prediction_input],data[output]) #Here we fit the model using training set
  
    #Make predictions on training set:
    predictions = model.predict(data[prediction_input])
  
    #Print accuracy
    # now checkin accuracy for same data
    accuracy = metrics.accuracy_score(predictions,data[output])
    print("Accuracy : %s" % "{0:.3%}".format(accuracy))
 
    
    kf = KFold(data.shape[0], n_splits=5)
    #I am going to use cross validation
    #data.shape[0] means number of rows in data
    #n_splits is for number of folds (updated library, those who code with this are probably used to n_folds)
    error = []
    for train, test in kf:
        # The data will be divided into train and test using KFold
        # I am going to fit model
        #in the cross validation, the data in train and test will change for every iteration
        train_X = (data[prediction_input].iloc[train,:])
        train_y = data[output].iloc[train]
        # Training the algorithm using the predictors and target.
        model.fit(train_X, train_y)
    
        #for test data
        test_X=data[prediction_input].iloc[test,:]
        test_y=data[output].iloc[test]
        error.append(model.score(test_X,test_y))
        # printing the score 
        print("Cross-Validation Score : %s" % "{0:.3%}".format(np.mean(error)))<jupyter_output><empty_output><jupyter_text>***Random Forest Classifier***<jupyter_code>model=RandomForestClassifier(n_estimators=100)# a simple random forest model
model.fit(train_X,train_y)# fit model for training data
prediction=model.predict(test_X)
metrics.accuracy_score(prediction,test_y) # to check the accuracy<jupyter_output><empty_output><jupyter_text>The Random Forest Classifer Model predicts with an accuracy of 93.57% ***Support Vector Machine***<jupyter_code>model = svm.SVC()
model.fit(train_X,train_y)
prediction=model.predict(test_X)
metrics.accuracy_score(prediction,test_y)<jupyter_output><empty_output><jupyter_text>Support Vector Machine Model predicts with an accuracy of 88.3% ***K-Nearest Neighbors Algorithm***<jupyter_code>model = KNeighborsClassifier()
classification_model(model,data,prediction_var,outcome_var)<jupyter_output>Accuracy : 92.970%<jupyter_text>Not sure if this is an error in the updated Library?Anyways, the K-Nearest Neighbors Algorithm predicts with an accuracy of 92.97% ***Logistic Regression***<jupyter_code>model=LogisticRegression()
classification_model(model,data,prediction_var,outcome_var)<jupyter_output>Accuracy : 89.807%
{'fraction_non_alphanumeric': 0.09616792775201367, 'fraction_numerical': 0.022699536246033682, 'mean_word_length': 4.096393034825871, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '383903', 'license': 'CC0-1.0', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2412, 'n_words': 775, 'path': 'Breast Cancer Model_1.ipynb', 'repo_name': 'EOHFA-GOAT/machine-learning-projects', 'size': '8206'}