In [ ]:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report
import seaborn as sns
import matplotlib.pyplot as plt
In [ ]:
df = pd.read_csv('cancer.csv')
df.head()
Out[ ]:
| diagnosis | radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | compactness_mean | |
|---|---|---|---|---|---|---|---|
| 0 | M | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | 0.27760 |
| 1 | M | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | 0.07864 |
| 2 | M | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | 0.15990 |
| 3 | M | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | 0.28390 |
| 4 | M | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | 0.13280 |
In [ ]:
# look at diagnisis category with data exploration
len(df)
Out[ ]:
570
In [ ]:
df['diagnosis'].value_counts()
Out[ ]:
| count | |
|---|---|
| diagnosis | |
| B | 357 |
| M | 213 |
In [ ]:
df.isnull().sum()
Out[ ]:
| 0 | |
|---|---|
| diagnosis | 0 |
| radius_mean | 0 |
| texture_mean | 0 |
| perimeter_mean | 0 |
| area_mean | 0 |
| smoothness_mean | 0 |
| compactness_mean | 0 |
In [ ]:
#radius_mean
sns.kdeplot(data=df, x='radius_mean', hue= 'diagnosis', common_norm= False,
fill=True)
plt.show()
In [ ]:
features = df.iloc[:,1:7]
features.head()
Out[ ]:
| radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | compactness_mean | |
|---|---|---|---|---|---|---|
| 0 | 17.99 | 10.38 | 122.80 | 1001.0 | 0.11840 | 0.27760 |
| 1 | 20.57 | 17.77 | 132.90 | 1326.0 | 0.08474 | 0.07864 |
| 2 | 19.69 | 21.25 | 130.00 | 1203.0 | 0.10960 | 0.15990 |
| 3 | 11.42 | 20.38 | 77.58 | 386.1 | 0.14250 | 0.28390 |
| 4 | 20.29 | 14.34 | 135.10 | 1297.0 | 0.10030 | 0.13280 |
In [ ]:
label = df.iloc[:,0]
label.head()
Out[ ]:
| diagnosis | |
|---|---|
| 0 | M |
| 1 | M |
| 2 | M |
| 3 | M |
| 4 | M |
In [ ]:
#data normalization (standard)
std_scaler = StandardScaler()
#normalize data
features_scaled = std_scaler.fit_transform(features)
features_scaled = pd.DataFrame(features_scaled, columns= features.columns, index= features.index)
features_scaled.head()
Out[ ]:
| radius_mean | texture_mean | perimeter_mean | area_mean | smoothness_mean | compactness_mean | |
|---|---|---|---|---|---|---|
| 0 | 1.096320 | -2.072462 | 1.269364 | 0.983936 | 1.570347 | 3.286462 |
| 1 | 1.829403 | -0.356089 | 1.685585 | 1.908811 | -0.825701 | -0.487431 |
| 2 | 1.579360 | 0.452162 | 1.566076 | 1.558781 | 0.943929 | 1.053917 |
| 3 | -0.770485 | 0.250099 | -0.594153 | -0.765926 | 3.285878 | 3.405961 |
| 4 | 1.749844 | -1.152728 | 1.776248 | 1.826283 | 0.281919 | 0.539881 |
In [ ]:
train_x, test_x, train_y, test_y = train_test_split(features_scaled, label,
test_size=0.2,
random_state=1)
In [ ]:
#classifier
clf = MLPClassifier(hidden_layer_sizes=(4, 3), solver= 'lbfgs',
alpha=1e-5, random_state=1)
#create model
clf.fit(train_x, train_y)
Out[ ]:
MLPClassifier(alpha=1e-05, hidden_layer_sizes=(4, 3), random_state=1,
solver='lbfgs')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MLPClassifier(alpha=1e-05, hidden_layer_sizes=(4, 3), random_state=1,
solver='lbfgs')In [ ]:
#predict
pred_y = clf.predict(test_x)
In [ ]:
rp = classification_report(test_y, pred_y)
print(rp)
precision recall f1-score support
B 0.84 0.95 0.89 65
M 0.93 0.76 0.83 49
accuracy 0.87 114
macro avg 0.88 0.85 0.86 114
weighted avg 0.88 0.87 0.87 114