<jupyter_start><jupyter_text>Breast Cancer Wisconsin (Diagnostic) Prediction*Predict whether the cancer is benign or malignant*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. n the 3-dimensional space is that described in: [<NAME> and <NAME>: "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/Also can be found on UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29**Attribute Information:***1) ID number 2) Diagnosis (M = malignant, B = benign) 3-32)***Ten real-valued features are computed for each cell nucleus:***a) radius (mean of distances from center to points on the perimeter) b) texture (standard deviation of gray-scale values) c) perimeter d) area e) smoothness (local variation in radius lengths) f) compactness (perimeter^2 / area - 1.0) g) concavity (severity of concave portions of the contour) h) concave points (number of concave portions of the contour) i) symmetry j) fractal dimension ("coastline approximation" - 1)**he 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.*All feature values are recoded with four significant digits.Missing attribute values: noneClass distribution: 357 benign, 212 malignant<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
import seaborn as sns # used for plot interactive graph. I like it most for plot
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.cross_validation import KFold # use for cross validation
from sklearn.model_selection import GridSearchCV# for tuning parameter
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 # for the check the error and accuracy of the model
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>#df is dataframe and its self a variable & here i import the dataset into this variable
df=pd.read_csv("cancer.csv")
#it will show top 5 data rows
df.head()
#deleting the useless columns
df.drop(['id'], 1, inplace=True)
df.drop(['Unnamed: 32'], 1, inplace=True)
df.info()
# lets now start with features_mean 
# now as ou know our diagnosis column is a object type so we can map it to integer value
df['diagnosis']=df['diagnosis'].map({'M':1,'B':0})
y=df['diagnosis']
y.head()
# it will describe the all statistical function of our data
df.describe()<jupyter_output><empty_output><jupyter_text>Data Analysis<jupyter_code># plotting the diagonisis result 
sns.countplot(df['diagnosis'],label="Count")
feat_mean= list(df.columns[1:11])
feat_se= list(df.columns[11:20])
feat_worst=list(df.columns[21:31])

corr = df[feat_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= feat_mean, yticklabels= feat_mean,
           cmap= 'coolwarm') # for more on heatmap you can visit Link(http://seaborn.pydata.org/generated/seaborn.heatmap.html)
#Box Plot for the feature texture_mean
sns.boxplot(x = 'diagnosis', y ='texture_mean', data = df)
plt.show()

#Box Plot for the feature perimeter_mean
sns.boxplot(x = 'diagnosis',y = 'perimeter_mean', data = df)
plt.show()

#Box Plot for the feature smoothness_mean
sns.boxplot(x = 'diagnosis', y = 'smoothness_mean', data = df)
plt.show()

#Box Plot for the feature compactness_mean
sns.boxplot(x = 'diagnosis', y = 'compactness_mean', data = df)
plt.show()

#Box Plot for the feature symmetry_mean
sns.boxplot(x = 'diagnosis', y = 'symmetry_mean', data = df)
plt.show()
#Violin Plots for texture_mean
sns.violinplot(x = 'diagnosis', y ='texture_mean', data = df, size = 8)
plt.show()

#Violin Plots for compactness_mean
sns.violinplot(x = 'diagnosis', y = 'compactness_mean', data = df, size = 8)
plt.show()
#Histogram of Axillary_Nodes
sns.FacetGrid(df, hue = "diagnosis", size=5).map(sns.distplot, "symmetry_worst").add_legend();
plt.show();
#taking the main parameters in a single variable 
main_pred_var = ['texture_mean','perimeter_mean','smoothness_mean','compactness_mean','symmetry_mean']<jupyter_output><empty_output><jupyter_text>Spliting the Dataset into two part<jupyter_code># spliting the dataset into two part
train_set,test_set=train_test_split(df, test_size=0.2)
#printing the data shape
print(train_set.shape)
print(test_set.shape)<jupyter_output>(455, 31)
(114, 31)<jupyter_text>**Training Set :**<jupyter_code>x_train=train_set[main_pred_var]
y_train=train_set.diagnosis
print(y_train.shape)
print(x_train.shape)<jupyter_output>(455,)
(455, 5)<jupyter_text>**Test Set :**<jupyter_code>x_test=test_set[main_pred_var]
y_test=test_set.diagnosis
print(y_test.shape)
print(x_test.shape)<jupyter_output>(114,)
(114, 5)<jupyter_text>Various AlgorithmNow i will train this Breast cancer dataset using various Algorithm from scratch see how each of them behaves with respect to one another.    Random Forest    SVM  RandomForst Algorithm<jupyter_code>#define the algorithm class into the algo_one variable
algo_one=RandomForestClassifier()
algo_one.fit(x_train,y_train)
#predicting the algorithm into the non trained dataset that is test set 
prediction = algo_one.predict(x_test)
metrics.accuracy_score(prediction,y_test)<jupyter_output><empty_output><jupyter_text>SupportVector Machine Algorithm (SVM)<jupyter_code>algo_two=svm.SVC()
algo_two.fit(x_train,y_train)
#predicting the algorithm into the non trained dataset that is test set 
prediction = algo_two.predict(x_test)
metrics.accuracy_score(prediction,y_test)<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier Algorithm<jupyter_code>algo_three=DecisionTreeClassifier()
algo_three.fit(x_train,y_train)
#predicting the algorithm into the non trained dataset that is test set 
prediction = algo_three.predict(x_test)
metrics.accuracy_score(prediction,y_test)<jupyter_output><empty_output><jupyter_text>K-Nearest NeighborsClassifier Algorithm<jupyter_code>algo_four=KNeighborsClassifier()
algo_four.fit(x_train,y_train)
#predicting the algorithm into the non trained dataset that is test set 
prediction = algo_four.predict(x_test)
metrics.accuracy_score(prediction,y_test)<jupyter_output><empty_output><jupyter_text>GaussianNB Algorithm<jupyter_code>algo_five=GaussianNB()
algo_five.fit(x_train,y_train)
prediction = algo_five.predict(x_test)
metrics.accuracy_score(prediction,y_test)<jupyter_output><empty_output><jupyter_text>Tuning Parameters using grid search CVLets Start with Random Forest Classifier Tuning the parameters means using the best parameter for predict there are many parameters need to model a Machine learning Algorithm for RandomForestClassifier.<jupyter_code>pred_var = ['radius_mean','perimeter_mean','area_mean','compactness_mean','concave points_mean']
#creating new variable
x_grid= df[pred_var]
y_grid= df["diagnosis"]
# lets Make a function for Grid Search CV
def Classification_model_gridsearchCV(model,param_grid,x_grid,y_grid):
    clf = GridSearchCV(model,param_grid,cv=10,scoring="accuracy")
    # this is how we use grid serch CV we are giving our model
    # the we gave parameters those we want to tune
    # Cv is for cross validation
    # scoring means to score the classifier
    
    clf.fit(x_train,y_train)
    print("The best parameter found on development set is :")
    # this will gie us our best parameter to use
    print(clf.best_params_)
    print("the bset estimator is ")
    print(clf.best_estimator_)
    print("The best score is ")
    # this is the best score that we can achieve using these parameters#
    print(clf.best_score_)
# you will understand these terms once you follow the link above
param_grid = {'max_features': ['auto', 'sqrt', 'log2'],
              'min_samples_split': [2,3,4,5,6,7,8,9,10], 
              'min_samples_leaf':[2,3,4,5,6,7,8,9,10] }
# here our gridasearchCV will take all combinations of these parameter and apply it to model 
# and then it will find the best parameter for model
model= RandomForestClassifier()
Classification_model_gridsearchCV(model,param_grid,x_grid,y_grid)<jupyter_output>The best parameter found on development set is :
{'max_features': 'log2', 'min_samples_leaf': 2, 'min_samples_split': 6}
the bset estimator is 
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='log2', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=2, min_samples_split=6,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False)
The best score is 
0.9384615384615385
{'fraction_non_alphanumeric': 0.09362632784836493, 'fraction_numerical': 0.014996875650906061, 'mean_word_length': 6.2040510127531885, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '667466', 'license': 'MIT', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3025, 'n_words': 933, 'path': 'Breast Cancer .ipynb', 'repo_name': 'suvhradipghosh07/Breast-Cancer-prediction-using-Machine-Learning-Various-Algorithms', 'size': '9620'}