Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sureshgorakala/61ad5c5975321537374691d64aba8810 to your computer and use it in GitHub Desktop.
Save sureshgorakala/61ad5c5975321537374691d64aba8810 to your computer and use it in GitHub Desktop.
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Running first neural network model on google colaboratory",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"metadata": {
"id": "UDddsi5G-wDJ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## **Neural Network classification example using Sklearn** "
]
},
{
"metadata": {
"id": "F2sIFGWX8KoH",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Load required libraries\n"
]
},
{
"metadata": {
"id": "ne5mS-4B8SUI",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.datasets import load_breast_cancer\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "DRM5E1BCB6qA",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
""
]
},
{
"metadata": {
"id": "-OROLmIdBF26",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Load breast cancer dataset which comes with sklearn package"
]
},
{
"metadata": {
"id": "GyOHpZ_GBJg7",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "df8a5b10-b694-4e37-cb22-4e3b245feef4"
},
"cell_type": "code",
"source": [
"cancer = load_breast_cancer()\n",
"print(type(cancer))\n"
],
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"text": [
"<class 'sklearn.utils.Bunch'>\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "6By97F3BCNx9",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"cancer dataset loaded is a dictionary like object containing description of the data, input features and targets."
]
},
{
"metadata": {
"id": "2w6HnYWfCTfn",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "8b5a319a-a6b9-4556-e31b-4680b05b2aa3"
},
"cell_type": "code",
"source": [
"cancer.keys()"
],
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 20
}
]
},
{
"metadata": {
"id": "YvmP9GlcEaZW",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Run below code to see the description of the data"
]
},
{
"metadata": {
"id": "TvN4eZLXCrqz",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"print(cancer['DESCR'])"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "OLyAd4ytEgns",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Run below code to check the shape of the dataset, input features and target variables"
]
},
{
"metadata": {
"id": "GQ0F9n70ERYr",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"print(cancer['data'].shape)\n",
"print(cancer['target'])\n",
"print(cancer['target_names'])\n",
"print(cancer['feature_names'])\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "3XIbXwGFFUtN",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Let's create input and target data that will be used for running a neural network model"
]
},
{
"metadata": {
"id": "S13WdvoeE08b",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "54ee4d01-041e-4479-d254-3a779d32e39b"
},
"cell_type": "code",
"source": [
"X = cancer['data']\n",
"Y = cancer['target']\n",
"\n",
"print(type(X))\n",
"print(type(Y))\n"
],
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"text": [
"<class 'numpy.ndarray'>\n",
"<class 'numpy.ndarray'>\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "v77vKorRFe6Z",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"In this step, let's create train and test sets"
]
},
{
"metadata": {
"id": "XUAmq3X_FeiK",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"from sklearn.model_selection import train_test_split\n",
"X_train, X_test, y_train, y_test = train_test_split(X, Y)\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "TGq1S9yeF9RU",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Now that we have created the train and test sets, lets create a basic neural network classification model by importing the MLPClassifier estimator object from Sklean package"
]
},
{
"metadata": {
"id": "zKPyrPQ2GMa-",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"from sklearn.neural_network import MLPClassifier\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "Ua7MU4d1GoYL",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"After we loaded the required classifier object, let us create an instance of MLPClassifier with number of hidden layers defined. \n",
"In our example, we shall create a 3-layered neural network with 30 neurons in each of 3 layers using below code"
]
},
{
"metadata": {
"id": "Y09NrYGIHNvX",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"mlp = MLPClassifier(hidden_layer_sizes=(30,30,30))"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "um49FFZIHQK5",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"As a next step, we now fit the model created in the previous step with training samples\n",
"\n",
"After running the fit function, we can see the model summary with set parameters and default parameters"
]
},
{
"metadata": {
"id": "Gb79xh63Hjyh",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"outputId": "b9471ed1-d54e-4142-8b3d-44bdca826b2d"
},
"cell_type": "code",
"source": [
"mlp.fit(X_train,y_train)"
],
"execution_count": 43,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,\n",
" beta_2=0.999, early_stopping=False, epsilon=1e-08,\n",
" hidden_layer_sizes=(30, 30, 30), learning_rate='constant',\n",
" learning_rate_init=0.001, max_iter=200, momentum=0.9,\n",
" nesterovs_momentum=True, power_t=0.5, random_state=None,\n",
" shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1,\n",
" verbose=False, warm_start=False)"
]
},
"metadata": {
"tags": []
},
"execution_count": 43
}
]
},
{
"metadata": {
"id": "VcSiDkEwHn8P",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Wow, we have created our first basic multi layer neural network model on Google Colab. Its time make predictions as below:"
]
},
{
"metadata": {
"id": "ZaVITT6xIILH",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "3ca2ec14-c51f-4bdd-fc5d-3f2125dc7697"
},
"cell_type": "code",
"source": [
"predictions = mlp.predict(X_test)\n",
"\n",
"print(predictions[1:10])"
],
"execution_count": 45,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 1 0 1 1 1 1 1 0]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "w6uhwaj3IKTr",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"We can evaluate the model accuracy using sklearns built in evaluation metrics as below"
]
},
{
"metadata": {
"id": "5JblXOB8IYKs",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 170
},
"outputId": "17bfefc4-199f-4dda-947e-5b21b7c88bb2"
},
"cell_type": "code",
"source": [
"from sklearn.metrics import classification_report,confusion_matrix\n",
"print(confusion_matrix(y_test,predictions))\n",
"\n",
"print(classification_report(y_test,predictions))"
],
"execution_count": 50,
"outputs": [
{
"output_type": "stream",
"text": [
"[[39 12]\n",
" [ 0 92]]\n",
" precision recall f1-score support\n",
"\n",
" 0 1.00 0.76 0.87 51\n",
" 1 0.88 1.00 0.94 92\n",
"\n",
"avg / total 0.93 0.92 0.91 143\n",
"\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment