<filename>breast_cancer.py
# -*- coding: utf-8 -*-
"""Breast Cancer.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1QlZl470SwjOnYcv36B-khL7wDFHu5Qka
"""

# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier
import seaborn as sns
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.preprocessing import StandardScaler
# %matplotlib inline
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix

cancer=pd.read_csv('data.csv')
print(cancer.shape)

cancer.isnull().sum()

cancer.drop(['id','Unnamed: 32'], axis=1, inplace=True)

cancer['diagnosis'].value_counts()

cancer['diagnosis']=cancer['diagnosis'].map({'B':0,'M':1})

cancer.head()

cancer.info()

sns.pairplot(cancer, hue='diagnosis', vars=['radius_mean','texture_mean','perimeter_mean','area_mean','smoothness_mean','compactness_mean','compactness_mean','concave points_mean','symmetry_mean','fractal_dimension_se'])

corr=cancer.corr()
plt.figure(figsize=(18,18))
sns.heatmap(corr, cbar=True, square=True,annot=True, cmap='coolwarm')
plt.show()

sns.countplot(cancer['diagnosis'])

x = cancer.drop(['diagnosis'], axis=1)
y = cancer[['diagnosis']]

print(f"'x' shape: {x.shape}")
print(f"'y' shape: {y.shape}")

"""## Logistic Regression"""

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.35)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

log=LogisticRegression()
log.fit(x_train,y_train)

y_pred=log.predict(x_test)

score = log.score(x_test, y_test)
print(score)

conf_mat=confusion_matrix(y_test,y_pred)
print(conf_mat)

"""The accuracy of our model is 94%"""

plt.figure(figsize=(10,10))
sns.heatmap(conf_mat, square=True, annot=True, linewidths=0.5, fmt=".3f", cmap='coolwarm')

"""##  KNN Regression"""

from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler    
st_x= StandardScaler()

x_train= st_x.fit_transform(x_train)    
x_test= st_x.transform(x_test)

knn = KNeighborsRegressor(n_neighbors=1)
knn.fit(x_train, y_train)

y_pred= knn.predict(x_test)

knn.score(x_test, y_test)

k_range = range(1, 31)
values = []
for k in k_range:
    knn = KNeighborsRegressor(n_neighbors=k)
    knn.fit(x_train, y_train)
    y_pred = knn.predict(x_test)
    values.append(knn.score(x_test, y_test))
print(values)

plt.plot(k_range, values)
plt.xlabel('Value of K for KNN')
plt.ylabel('Testing Accuracy')

sns.set_style("darkgrid", {"axes.facecolor": ".9"})
sns.scatterplot(k_range,values)
<filename>breast_cancer.py
# -*- coding: utf-8 -*-
"""Breast Cancer.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1QlZl470SwjOnYcv36B-khL7wDFHu5Qka
"""

# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier
import seaborn as sns
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.preprocessing import StandardScaler
# %matplotlib inline
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix

cancer=pd.read_csv('data.csv')
print(cancer.shape)

cancer.isnull().sum()

cancer.drop(['id','Unnamed: 32'], axis=1, inplace=True)

cancer['diagnosis'].value_counts()

cancer['diagnosis']=cancer['diagnosis'].map({'B':0,'M':1})

cancer.head()

cancer.info()

sns.pairplot(cancer, hue='diagnosis', vars=['radius_mean','texture_mean','perimeter_mean','area_mean','smoothness_mean','compactness_mean','compactness_mean','concave points_mean','symmetry_mean','fractal_dimension_se'])

corr=cancer.corr()
plt.figure(figsize=(18,18))
sns.heatmap(corr, cbar=True, square=True,annot=True, cmap='coolwarm')
plt.show()

sns.countplot(cancer['diagnosis'])

x = cancer.drop(['diagnosis'], axis=1)
y = cancer[['diagnosis']]

print(f"'x' shape: {x.shape}")
print(f"'y' shape: {y.shape}")

"""## Logistic Regression"""

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.35)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

log=LogisticRegression()
log.fit(x_train,y_train)

y_pred=log.predict(x_test)

score = log.score(x_test, y_test)
print(score)

conf_mat=confusion_matrix(y_test,y_pred)
print(conf_mat)

"""The accuracy of our model is 94%"""

plt.figure(figsize=(10,10))
sns.heatmap(conf_mat, square=True, annot=True, linewidths=0.5, fmt=".3f", cmap='coolwarm')

"""##  KNN Regression"""

from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler    
st_x= StandardScaler()

x_train= st_x.fit_transform(x_train)    
x_test= st_x.transform(x_test)

knn = KNeighborsRegressor(n_neighbors=1)
knn.fit(x_train, y_train)

y_pred= knn.predict(x_test)

knn.score(x_test, y_test)

k_range = range(1, 31)
values = []
for k in k_range:
    knn = KNeighborsRegressor(n_neighbors=k)
    knn.fit(x_train, y_train)
    y_pred = knn.predict(x_test)
    values.append(knn.score(x_test, y_test))
print(values)

plt.plot(k_range, values)
plt.xlabel('Value of K for KNN')
plt.ylabel('Testing Accuracy')

sns.set_style("darkgrid", {"axes.facecolor": ".9"})
sns.scatterplot(k_range,values)
# -*- coding: utf-8 -*- Breast Cancer.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1QlZl470SwjOnYcv36B-khL7wDFHu5Qka # Commented out IPython magic to ensure Python compatibility. # %matplotlib inline ## Logistic Regression The accuracy of our model is 94% ##  KNN Regression