In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
In [2]:
from sklearn.datasets import make_moons
X, y = make_moons(n_samples = 1000, noise=0.1, random_state = 0)
plt.plot(X[y == 0, 0], X[y == 0, 1], 'ob', alpha = 0.5)
plt.plot(X[y == 1, 0], X[y == 1, 1], 'xr', alpha = 0.5)
plt.legend(['0', '1'])
Out[2]:
In [3]:
X.shape
Out[3]:
In [4]:
from sklearn.model_selection import train_test_split
In [5]:
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size = 0.3,
random_state = 42)
In [6]:
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam
In [7]:
model = Sequential()
model.add(Dense(1,
input_shape=(2,),
activation='sigmoid'))
model.compile(Adam(lr = 0.05),
loss = 'binary_crossentropy',
metrics = ['accuracy'])
In [8]:
model.fit(X_train, y_train, epochs = 200, verbose=0)
Out[8]:
In [9]:
results = model.evaluate(X_test, y_test)
In [10]:
results
Out[10]:
In [11]:
print("The Accuracy score on the Train set is:\t{:0.3f}".format(results[1]))
In [12]:
def plot_decision_boundary(model, X, y):
amin, bmin = X.min(axis = 0) - 0.1
amax, bmax = X.max(axis = 0) + 0.1
hticks = np.linspace(amin, amax, 101)
vticks = np.linspace(bmin, bmax, 101)
aa, bb = np.meshgrid(hticks, vticks)
ab = np.c_[aa.ravel(), bb.ravel()]
c = model.predict(ab)
cc = c.reshape(aa.shape)
plt.figure(figsize = (12, 8))
plt.contourf(aa, bb, cc, cmap = 'bwr', alpha = 0.2)
plt.plot(X[y == 0, 0], X[y == 0, 1], 'ob', alpha = 0.5)
plt.plot(X[y == 1, 0], X[y == 1, 1], 'xr', alpha = 0.5)
plt.legend(['0', '1'])
plot_decision_boundary(model, X, y)
In [13]:
model = Sequential()
model.add(Dense(4,
input_shape=(2,),
activation = 'tanh'))
model.add(Dense(2,
activation = 'tanh'))
model.add(Dense(1,
activation='sigmoid'))
model.compile(Adam(lr = 0.05),
loss = 'binary_crossentropy',
metrics=['accuracy'])
In [14]:
model.fit(X_train, y_train, epochs = 100, verbose=0)
Out[14]:
In [15]:
model.evaluate(X_test, y_test)
Out[15]:
In [16]:
from sklearn.metrics import accuracy_score, confusion_matrix
In [17]:
y_train_pred = model.predict_classes(X_train)
y_test_pred = model.predict_classes(X_test)
print("The Accuracy score on the Train set is:\t{:0.3f}".format(accuracy_score(y_train, y_train_pred)))
print("The Accuracy score on the Test set is:\t{:0.3f}".format(accuracy_score(y_test, y_test_pred)))
In [18]:
plot_decision_boundary(model, X, y)
In [19]:
df = pd.read_csv('./data/iris.csv')
In [20]:
import notebook
import seaborn as sns
sns.pairplot(df,
hue = "species")
Out[20]:
In [21]:
df.head()
Out[21]:
In [22]:
X = df.drop('species', axis = 1)
X.head()
Out[22]:
In [23]:
target_names = df['species'].unique()
target_names
Out[23]:
In [24]:
target_dict = {n:i for i, n in enumerate(target_names)}
target_dict
Out[24]:
In [25]:
y = df['species'].map(target_dict)
y.head()
Out[25]:
In [26]:
from keras.utils.np_utils import to_categorical
In [27]:
y_cat = to_categorical(y)
In [28]:
y_cat[:10]
Out[28]:
In [29]:
X_train, X_test, y_train, y_test = train_test_split(X.values,
y_cat,
test_size = 0.2,
random_state = 42)
In [30]:
model = Sequential()
model.add(Dense(3,
input_shape = (4,),
activation = 'softmax'))
model.compile(Adam(lr = 0.1),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
In [31]:
model.fit(X_train,
y_train,
epochs = 20,
validation_split = 0.1)
Out[31]:
In [32]:
y_pred = model.predict(X_test)
In [33]:
y_pred[:5]
Out[33]:
In [34]:
y_test_class = np.argmax(y_test, axis = 1)
y_pred_class = np.argmax(y_pred, axis = 1)
In [35]:
from sklearn.metrics import classification_report
In [36]:
print(classification_report(y_test_class, y_pred_class))
In [37]:
confusion_matrix(y_test_class, y_pred_class)
Out[37]:
The Pima Indians dataset is a very famous dataset distributed by UCI and originally collected from the National Institute of Diabetes and Digestive and Kidney Diseases. It contains data from clinical exams for women age 21 and above of Pima indian origins. The objective is to predict based on diagnostic measurements whether a patient has diabetes.
It has the following features:
The last colum is the outcome, and it is a binary variable.
In this first exercise we will explore it through the following steps:
sns.pairplot
we used above or drawing a heatmap of the correlations.X
and y
variables to be used by a ML model. Make sure you define your target variable well. Will you need dummy columns?
In [38]:
ex1 = pd.read_csv('./data/diabetes.csv')
In [39]:
ex1.hist(figsize = (12, 7))
plt.tight_layout()
In [40]:
import seaborn as sns
In [41]:
sns.pairplot(ex1,
hue = "Outcome")
Out[41]:
In [42]:
ex1.info()
In [43]:
ex1.describe()
Out[43]:
In [44]:
from sklearn.preprocessing import MinMaxScaler
In [45]:
minmax = MinMaxScaler()
In [46]:
X = ex1.drop('Outcome', axis = 1)
In [47]:
Y = ex1['Outcome']
In [48]:
X.head()
Out[48]:
In [49]:
X = pd.DataFrame(minmax.fit_transform(X),
columns = ['Pregnancies', 'Glucose', 'BloodPressure',
'SkinThickness','Insulin', 'BMI',
'DiabetesPedigreeFunction', 'Age'])
In [50]:
X.head()
Out[50]:
In [51]:
Y.head()
Out[51]:
In [52]:
X = X.values
Y = Y.values
In [53]:
from keras.utils import to_categorical
In [54]:
Y_cat = to_categorical(Y)
In [55]:
Y_cat
Out[55]:
Build a fully connected NN model that predicts diabetes. Follow these steps:
random_state = 22
In [56]:
from sklearn.model_selection import train_test_split
In [57]:
X_train, X_test, Y_train, Y_test = train_test_split(X,
Y_cat,
test_size = 0.2,
random_state = 22)
In [58]:
X_train.shape
Out[58]:
In [59]:
X_test.shape
Out[59]:
In [60]:
Y_train.shape
Out[60]:
In [61]:
Y_test.shape
Out[61]:
In [62]:
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
In [63]:
model = Sequential()
model.add(Dense(units = 64,
input_shape = (8, ),
activation = 'tanh'))
model.add(Dense(units = 32,
activation = 'tanh'))
model.add(Dense(units = 2,
activation = 'softmax'))
model.compile(optimizer = Adam(lr = 0.01),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
In [64]:
model.fit(X_train,
Y_train,
validation_split = 0.1,
epochs = 50,
verbose = 2)
Out[64]:
In [65]:
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
In [66]:
Y_pred = model.predict(X_test)
In [67]:
Y_pred
Out[67]:
In [68]:
Y_pred = np.argmax(Y_pred, axis = 1)
Y_test = np.argmax(Y_test, axis = 1)
In [69]:
pd.Series(Y_test).value_counts() / len(Y_test)
Out[69]:
In [70]:
pd.Series(Y_pred).value_counts() / len(Y_test)
Out[70]:
In [71]:
accuracy_score(Y_test, Y_pred)
Out[71]:
In [72]:
print(classification_report(Y_test, Y_pred))
In [73]:
confusion_matrix(Y_test, Y_pred)
Out[73]:
Compare your work with the results presented in this notebook. Are your Neural Network results better or worse than the results obtained by traditional Machine Learning techniques?
In [74]:
X_train, X_test, Y_train, Y_test = train_test_split(X,
Y,
test_size = 0.2,
random_state = 22)
In [75]:
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
In [76]:
models = [SVC(), RandomForestClassifier()]
for model in models:
model.fit(X_train, Y_train)
print('*' * 50)
print(model)
Y_pred = model.predict(X_test)
accuracy = accuracy_score(Y_test, Y_pred)
print('Accuracy: {:0.3f}'.format(accuracy))
print('Confusion matrix: \n', confusion_matrix(Y_test, Y_pred))
In [77]:
ex1.head()
Out[77]:
In [78]:
outcome_corr = ex1.corr()['Outcome']
outcome_corr
Out[78]:
In [79]:
outcome_corr_sorted = outcome_corr.sort_values(ascending = False)
outcome_corr_sorted
Out[79]:
In [80]:
outcome_corr_sorted.index[0]
Out[80]:
In [81]:
chosen_features = outcome_corr_sorted.index[1:5]
chosen_features
Out[81]:
In [82]:
X = ex1[chosen_features]
X = minmax.fit_transform(X)
In [83]:
pd.DataFrame(X, columns = chosen_features).head()
Out[83]:
In [84]:
Y = ex1['Outcome']
In [85]:
Y.head()
Out[85]:
In [86]:
X_train, X_test, Y_train, Y_test = train_test_split(X,
Y,
test_size = 0.2,
random_state = 22)
In [87]:
models = [SVC(), RandomForestClassifier()]
for model in models:
model.fit(X_train, Y_train)
print('*' * 100)
print(model)
Y_pred = model.predict(X_test)
accuracy = accuracy_score(Y_test, Y_pred)
print('Accuracy: {:0.3f}'.format(accuracy))
print('Confusion matrix: \n', confusion_matrix(Y_test, Y_pred))