Professional Documents
Culture Documents
ipynb - Colaboratory
from google.colab import files
uploaded = files.upload()
import pandas as pd
import io
df = pd.read_csv(io.BytesIO(uploaded['Diabetes.csv']))
print(df)
0 6 148 ... 50 1
1 1 85 ... 31 0
2 8 183 ... 32 1
3 1 89 ... 21 0
4 0 137 ... 33 1
767 1 93 ... 23 0
df.head()
0 6 148 72 35 0 33.6
1 1 85 66 29 0 26.6
2 8 183 64 0 0 23.3
3 1 89 66 23 94 28.1
df.dtypes
Pregnancies int64
Glucose int64
BloodPressure int64
SkinThickness int64
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 1/7
8/31/2021 Untitled6.ipynb - Colaboratory
Insulin int64
BMI float64
DiabetesPedigreeFunction float64
Age int64
Outcome int64
dtype: object
correlation=df.corr()
correlation.style.background_gradient(cmap='coolwarm')
correlation.style.background_gradient(cmap='coolwarm').set_precision(2)
import matplotlib.pyplot as plt
plt.matshow(df.corr())
plt.show()
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 2/7
8/31/2021 Untitled6.ipynb - Colaboratory
age=df['Age']
out=df['Outcome']
import matplotlib.pyplot as plt
plt.bar(age,out)
plt.show()
df.boxplot(by ='Outcome', column =['Insulin'], grid = False)
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 3/7
8/31/2021 Untitled6.ipynb - Colaboratory
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationW
return array(a, dtype, copy=False, order=order)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdd06376050>
for i in df.columns:
print(i,":",df[i][df[i]==0].count())
Pregnancies : 111
Glucose : 5
BloodPressure : 35
SkinThickness : 227
Insulin : 374
BMI : 11
DiabetesPedigreeFunction : 0
Age : 0
Outcome : 500
for col in df.columns:
val=df[col].mean()
df[col]=df[col].replace(0,val)
df.head(10)
df.boxplot(by ='Outcome', column =['Insulin'], grid = False)
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 4/7
8/31/2021 Untitled6.ipynb - Colaboratory
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationW
return array(a, dtype, copy=False, order=order)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdd085f80d0>
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
type(X)
numpy.ndarray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
# determine the number of input features
n_features = X_train.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_feat
model.add(Dense(8, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# fit the model
model.fit(X_train, y_train, epochs=150, batch_size=32, verbose=0)
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 5/7
8/31/2021 Untitled6.ipynb - Colaboratory
<keras.callbacks.History at 0x7fdd06210110>
# evaluate the model
loss, acc = model.evaluate(X_test, y_test, verbose=0)
print('Test Accuracy: %.3f' % acc)
# make a prediction
import numpy as np
row = np.array([[1,0,0.99539,-0.05889,0.85243,0.02306,0.83398,-0.37708]])
yhat = model.predict([row])
print('Predicted: %.3f' % yhat)
Predicted: 0.204
import numpy as np
row1=np.array([[1,0,0.99539,-0.05889,0.85243,0.02306,0.83398,-0.37708]])
row1.shape
(1, 8)
yhat = model.predict([row1])
print('Predicted: %.3f' % yhat)
Predicted: 0.204
model.summary()
Model: "sequential_1"
_________________________________________________________________
=================================================================
_________________________________________________________________
_________________________________________________________________
=================================================================
Non-trainable params: 0
_________________________________________________________________
df.boxplot(by ='Outcome', column =['Insulin'], grid = False)
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 6/7
8/31/2021 Untitled6.ipynb - Colaboratory
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationW
return array(a, dtype, copy=False, order=order)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdd086bc450>
https://colab.research.google.com/drive/1IAYPbv5kKKV04u8-wWsy6TvzPIXQplNv#scrollTo=FctYEwzZ-0zM&printMode=true 7/7