Commit f01001dd authored by Anurag Kumar's avatar Anurag Kumar

Upload New File

parent ccce059a
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "3h3Bvs88G-mA"
},
"source": [
"## **Nueral Network as Regresser**\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "i8Rml3gU1BtA"
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "snez6-o51LPA",
"outputId": "9aabb98d-52c8-4e9c-cedb-ab933d129692"
},
"outputs": [
{
"data": {
"text/plain": [
"(4, 2)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_value = np.array([[0,0],[0,1],[1,1],[1,0]])\n",
"input_value.shape\n",
"# input_value"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "rHWrrMEm1imh"
},
"outputs": [],
"source": [
"# assign output values"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pS42RZo91mag",
"outputId": "876e4903-d1ce-4f4d-9067-950eeda31331"
},
"outputs": [
{
"data": {
"text/plain": [
"(4, 1)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output = np.array([0,1, 1, 0])\n",
"output = output.reshape(4, 1)\n",
"output.shape"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "u62jT8EW19zy"
},
"outputs": [],
"source": [
"# assign weight"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "k5GempR-1_9S",
"outputId": "ed14f4e7-6f95-4f19-b0f5-318ce5ea4548"
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.1],\n",
" [0.2]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"weight = np.array([[0.1],[0.2]])\n",
"weight"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "4dOOWpDQ2E8I"
},
"outputs": [],
"source": [
"# add bias"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "2QLpAT4s2MmY"
},
"outputs": [],
"source": [
"bias = 0.3"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "Ddt-btsY2OB6"
},
"outputs": [],
"source": [
"# activation function"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "evVFkquB2QfP"
},
"outputs": [],
"source": [
"def sigmoid_fun(x):\n",
" return 1/(1 + np.exp(-x))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "xAP0Ope_2ZPy"
},
"outputs": [],
"source": [
"# derivative of sigmoid function"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "bP-cbIrS2djV"
},
"outputs": [],
"source": [
"def dr_sigmoid(x):\n",
" return sigmoid_fun(x) * (1 - sigmoid_fun(x))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "Ps8f-d4N2nZN"
},
"outputs": [],
"source": [
"# updating weights"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "R-HrpTG_2rm2"
},
"outputs": [],
"source": [
"# bias = 0.3\n",
"for epochs in range(1000):\n",
" input_arr = input_value\n",
"\n",
" weighted_sum = np.dot(input_arr, weight) + bias\n",
" first_output = sigmoid_fun(weighted_sum)\n",
"\n",
" error = first_output - output\n",
" total_error = np.square(np.subtract(first_output, output)).mean()\n",
"\n",
" first_der = error\n",
" second_der = dr_sigmoid(first_output)\n",
" derivative = first_der * second_der\n",
"\n",
"\n",
" t_input = input_value.T\n",
" final_der = np.dot(t_input, derivative)\n",
"\n",
"\n",
" # update weight and bias\n",
" weight = weight - 0.05 * final_der\n",
"\n",
" for i in derivative:\n",
" bias = bias - 0.05 * i"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CigLg9Am4RQN",
"outputId": "6dacdb6c-9288-41d8-e016-4edb33fd5e1e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.4204469]\n",
" [ 4.2597748]]\n"
]
}
],
"source": [
"print(weight)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KbqBhvML4OJv",
"outputId": "5bc6eae0-f33c-41f4-93d2-7ff2b70bd5a0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1.7964881]\n"
]
}
],
"source": [
"print(bias)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KTVyGAyD52Qy",
"outputId": "de99e641-5589-4b80-a300-1fd03d8ca10d"
},
"outputs": [
{
"data": {
"text/plain": [
"array([0.09824])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pred = np.array([1, 0])\n",
"result = np.dot(pred, weight) + bias\n",
"res = sigmoid_fun(result)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fwL99ihN6GKY"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "xKt3UcWrHU2n"
},
"source": [
"## **NN as Classifier**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XMP9aSDJ6xAf"
},
"source": [
"**`Nueral network for Breast cancer data`**"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "SjS_pMlA7JUy"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 240
},
"id": "PUAxhcbJ7PH4",
"outputId": "feb6e5eb-2c37-45a2-d445-fcc8c71112e9"
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>diagnosis</th>\n",
" <th>radius_mean</th>\n",
" <th>texture_mean</th>\n",
" <th>perimeter_mean</th>\n",
" <th>area_mean</th>\n",
" <th>smoothness_mean</th>\n",
" <th>compactness_mean</th>\n",
" <th>concavity_mean</th>\n",
" <th>concave points_mean</th>\n",
" <th>...</th>\n",
" <th>texture_worst</th>\n",
" <th>perimeter_worst</th>\n",
" <th>area_worst</th>\n",
" <th>smoothness_worst</th>\n",
" <th>compactness_worst</th>\n",
" <th>concavity_worst</th>\n",
" <th>concave points_worst</th>\n",
" <th>symmetry_worst</th>\n",
" <th>fractal_dimension_worst</th>\n",
" <th>Unnamed: 32</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>842302</td>\n",
" <td>M</td>\n",
" <td>17.99</td>\n",
" <td>10.38</td>\n",
" <td>122.80</td>\n",
" <td>1001.0</td>\n",
" <td>0.11840</td>\n",
" <td>0.27760</td>\n",
" <td>0.3001</td>\n",
" <td>0.14710</td>\n",
" <td>...</td>\n",
" <td>17.33</td>\n",
" <td>184.60</td>\n",
" <td>2019.0</td>\n",
" <td>0.1622</td>\n",
" <td>0.6656</td>\n",
" <td>0.7119</td>\n",
" <td>0.2654</td>\n",
" <td>0.4601</td>\n",
" <td>0.11890</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>842517</td>\n",
" <td>M</td>\n",
" <td>20.57</td>\n",
" <td>17.77</td>\n",
" <td>132.90</td>\n",
" <td>1326.0</td>\n",
" <td>0.08474</td>\n",
" <td>0.07864</td>\n",
" <td>0.0869</td>\n",
" <td>0.07017</td>\n",
" <td>...</td>\n",
" <td>23.41</td>\n",
" <td>158.80</td>\n",
" <td>1956.0</td>\n",
" <td>0.1238</td>\n",
" <td>0.1866</td>\n",
" <td>0.2416</td>\n",
" <td>0.1860</td>\n",
" <td>0.2750</td>\n",
" <td>0.08902</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>84300903</td>\n",
" <td>M</td>\n",
" <td>19.69</td>\n",
" <td>21.25</td>\n",
" <td>130.00</td>\n",
" <td>1203.0</td>\n",
" <td>0.10960</td>\n",
" <td>0.15990</td>\n",
" <td>0.1974</td>\n",
" <td>0.12790</td>\n",
" <td>...</td>\n",
" <td>25.53</td>\n",
" <td>152.50</td>\n",
" <td>1709.0</td>\n",
" <td>0.1444</td>\n",
" <td>0.4245</td>\n",
" <td>0.4504</td>\n",
" <td>0.2430</td>\n",
" <td>0.3613</td>\n",
" <td>0.08758</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>84348301</td>\n",
" <td>M</td>\n",
" <td>11.42</td>\n",
" <td>20.38</td>\n",
" <td>77.58</td>\n",
" <td>386.1</td>\n",
" <td>0.14250</td>\n",
" <td>0.28390</td>\n",
" <td>0.2414</td>\n",
" <td>0.10520</td>\n",
" <td>...</td>\n",
" <td>26.50</td>\n",
" <td>98.87</td>\n",
" <td>567.7</td>\n",
" <td>0.2098</td>\n",
" <td>0.8663</td>\n",
" <td>0.6869</td>\n",
" <td>0.2575</td>\n",
" <td>0.6638</td>\n",
" <td>0.17300</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>84358402</td>\n",
" <td>M</td>\n",
" <td>20.29</td>\n",
" <td>14.34</td>\n",
" <td>135.10</td>\n",
" <td>1297.0</td>\n",
" <td>0.10030</td>\n",
" <td>0.13280</td>\n",
" <td>0.1980</td>\n",
" <td>0.10430</td>\n",
" <td>...</td>\n",
" <td>16.67</td>\n",
" <td>152.20</td>\n",
" <td>1575.0</td>\n",
" <td>0.1374</td>\n",
" <td>0.2050</td>\n",
" <td>0.4000</td>\n",
" <td>0.1625</td>\n",
" <td>0.2364</td>\n",
" <td>0.07678</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 33 columns</p>\n",
"</div>"
],
"text/plain": [
" id diagnosis radius_mean texture_mean perimeter_mean area_mean \\\n",
"0 842302 M 17.99 10.38 122.80 1001.0 \n",
"1 842517 M 20.57 17.77 132.90 1326.0 \n",
"2 84300903 M 19.69 21.25 130.00 1203.0 \n",
"3 84348301 M 11.42 20.38 77.58 386.1 \n",
"4 84358402 M 20.29 14.34 135.10 1297.0 \n",
"\n",
" smoothness_mean compactness_mean concavity_mean concave points_mean \\\n",
"0 0.11840 0.27760 0.3001 0.14710 \n",
"1 0.08474 0.07864 0.0869 0.07017 \n",
"2 0.10960 0.15990 0.1974 0.12790 \n",
"3 0.14250 0.28390 0.2414 0.10520 \n",
"4 0.10030 0.13280 0.1980 0.10430 \n",
"\n",
" ... texture_worst perimeter_worst area_worst smoothness_worst \\\n",
"0 ... 17.33 184.60 2019.0 0.1622 \n",
"1 ... 23.41 158.80 1956.0 0.1238 \n",
"2 ... 25.53 152.50 1709.0 0.1444 \n",
"3 ... 26.50 98.87 567.7 0.2098 \n",
"4 ... 16.67 152.20 1575.0 0.1374 \n",
"\n",
" compactness_worst concavity_worst concave points_worst symmetry_worst \\\n",
"0 0.6656 0.7119 0.2654 0.4601 \n",
"1 0.1866 0.2416 0.1860 0.2750 \n",
"2 0.4245 0.4504 0.2430 0.3613 \n",
"3 0.8663 0.6869 0.2575 0.6638 \n",
"4 0.2050 0.4000 0.1625 0.2364 \n",
"\n",
" fractal_dimension_worst Unnamed: 32 \n",
"0 0.11890 NaN \n",
"1 0.08902 NaN \n",
"2 0.08758 NaN \n",
"3 0.17300 NaN \n",
"4 0.07678 NaN \n",
"\n",
"[5 rows x 33 columns]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv(\"dataset/breast_cancer.csv\")\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "kVNcPTKMBCFM"
},
"outputs": [],
"source": [
"# converting text value into classifier (or, number)\n",
"from sklearn import preprocessing\n",
"Label = preprocessing.LabelEncoder()\n",
"\n",
"diagnosis = Label.fit_transform(df['diagnosis'])\n",
"# diagnosis"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"id": "cjFky7jBB35X"
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"train, test = train_test_split(df, random_state = 40)\n",
"x_train = train[train.columns[2:30]]\n",
"y_train = train['diagnosis']\n",
"\n",
"x_test = test[test.columns[2:30]]\n",
"y_test = test['diagnosis']"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"id": "1GcS6-c-Cdaw"
},
"outputs": [],
"source": [
"# standardisation of data"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"id": "qFGvRfAuCd5S"
},
"outputs": [],
"source": [
"from sklearn.preprocessing import StandardScaler\n",
"scaler = StandardScaler()\n",
"\n",
"scaler.fit(x_train)\n",
"\n",
"x_train = scaler.transform(x_train)\n",
"x_test = scaler.transform(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "khIL2u3MC4Cd",
"outputId": "17333d83-dd64-4e39-dd34-d45fc07c2a28"
},
"outputs": [
{
"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=(20, 20, 20), learning_rate='constant',\n",
" learning_rate_init=0.001, max_iter=10000, 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)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.neural_network import MLPClassifier\n",
"MLP = MLPClassifier(hidden_layer_sizes = (20, 20, 20), max_iter = 10000)\n",
"MLP.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"id": "TNaAIwboDP2e"
},
"outputs": [],
"source": [
"pred = MLP.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "q2yjRi-CDbYc",
"outputId": "d1934ccc-c52e-45c5-81ec-02d3ba20822c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[97 1]\n",
" [ 3 42]]\n",
" precision recall f1-score support\n",
"\n",
" B 0.97 0.99 0.98 98\n",
" M 0.98 0.93 0.95 45\n",
"\n",
"avg / total 0.97 0.97 0.97 143\n",
"\n"
]
}
],
"source": [
"# accuracy\n",
"from sklearn.metrics import confusion_matrix, classification_report\n",
"print(confusion_matrix(y_test, pred))\n",
"print(classification_report(y_test, pred))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "v1N28PsLFwkU",
"outputId": "503eeacb-8589-4cbb-fcbf-5c7e8284d3ae"
},
"outputs": [
{
"data": {
"text/plain": [
"array(['M'], dtype='<U1')"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# predicting one perticular output\n",
"x_test1 = scaler.transform(df.iloc[0:1, 2:30])\n",
"pred = MLP.predict(x_test1)\n",
"pred "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "nueral network.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment