Dataset Viewer (First 5GB)
Search is not available for this dataset
text
stringlengths 2.05k
86.5M
| id
stringlengths 22
24
| file_path
stringclasses 46
values |
---|---|---|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
"_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
"execution": {
"iopub.execute_input": "2020-11-21T08:58:50.945230Z",
"iopub.status.busy": "2020-11-21T08:58:50.944132Z",
"iopub.status.idle": "2020-11-21T08:58:50.950673Z",
"shell.execute_reply": "2020-11-21T08:58:50.951348Z"
},
"papermill": {
"duration": 0.026226,
"end_time": "2020-11-21T08:58:50.951553",
"exception": false,
"start_time": "2020-11-21T08:58:50.925327",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nama: Abdillah Husaini\n",
"NPM: 2009020002\n",
"Teknologi Informasi A1 Pagi\n",
"Tugas Algoritma Ke-4\n"
]
}
],
"source": [
"print('Nama: Abdillah Husaini')\n",
"print('NPM: 2009020002')\n",
"print('Teknologi Informasi A1 Pagi')\n",
"print('Tugas Algoritma Ke-4')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:50.981892Z",
"iopub.status.busy": "2020-11-21T08:58:50.981014Z",
"iopub.status.idle": "2020-11-21T08:58:50.986859Z",
"shell.execute_reply": "2020-11-21T08:58:50.985860Z"
},
"papermill": {
"duration": 0.023614,
"end_time": "2020-11-21T08:58:50.987024",
"exception": false,
"start_time": "2020-11-21T08:58:50.963410",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Program diatas menunjukkan program yang mencetak kata hello sebanyak 10 kali secara berulang-ulang. Yang dimaksud dengan For adalah perulangan (perulangan yang terhitung),dan fungsi range(), digunakan untuk mengembalikan deret bulat (integer) secara berurutan pada kisaran (range) yang sudah ditentukan dari start sampai stop.\n"
]
}
],
"source": [
"for i in range(10):\n",
" print('Hello')\n",
"print('Program diatas menunjukkan program yang mencetak kata hello sebanyak 10 kali secara berulang-ulang. Yang dimaksud dengan For adalah perulangan (perulangan yang terhitung),dan fungsi range(), digunakan untuk mengembalikan deret bulat (integer) secara berurutan pada kisaran (range) yang sudah ditentukan dari start sampai stop.')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.022037Z",
"iopub.status.busy": "2020-11-21T08:58:51.020980Z",
"iopub.status.idle": "2020-11-21T08:58:51.062851Z",
"shell.execute_reply": "2020-11-21T08:58:51.062065Z"
},
"papermill": {
"duration": 0.06421,
"end_time": "2020-11-21T08:58:51.063032",
"exception": false,
"start_time": "2020-11-21T08:58:50.998822",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"ename": "StdinNotImplementedError",
"evalue": "raw_input was called, but this frontend does not support input requests.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-844bc765987c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnum\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Enteranumber: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'The square of your number is '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'The loop is now done. '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Kode program diatas adalah program meminta pengguna untuk memasukkan nomor/angka dan mencetak kotaknya, kemudian meminta nomor lain untuk mencetak kotaknya, program tersebut melakukan tiga kali dan kemudian mencetak bahwa perulangan selesai. Perintah modul diatas ialah range(3), bilangan bulat berurutan lalu eval(menguraikan) string ekspresi yang dilewatkan ke dalamnya,lalu kita input,setelah itu cetak hasil dari input tadi, karena kuadrat maka dikali(*) dan cetak perulangan selesai.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 854\u001b[0m raise StdinNotImplementedError(\n\u001b[0;32m--> 855\u001b[0;31m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 856\u001b[0m )\n\u001b[1;32m 857\u001b[0m return self._input_request(str(prompt),\n",
"\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests."
]
}
],
"source": [
"for i in range (3):\n",
" num = eval(input('Enteranumber: '))\n",
" print ('The square of your number is ', num*num)\n",
"print('The loop is now done. ')\n",
"print('Kode program diatas adalah program meminta pengguna untuk memasukkan nomor/angka dan mencetak kotaknya, kemudian meminta nomor lain untuk mencetak kotaknya, program tersebut melakukan tiga kali dan kemudian mencetak bahwa perulangan selesai. Perintah modul diatas ialah range(3), bilangan bulat berurutan lalu eval(menguraikan) string ekspresi yang dilewatkan ke dalamnya,lalu kita input,setelah itu cetak hasil dari input tadi, karena kuadrat maka dikali(*) dan cetak perulangan selesai.')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.096255Z",
"iopub.status.busy": "2020-11-21T08:58:51.095370Z",
"iopub.status.idle": "2020-11-21T08:58:51.101672Z",
"shell.execute_reply": "2020-11-21T08:58:51.100775Z"
},
"papermill": {
"duration": 0.025314,
"end_time": "2020-11-21T08:58:51.101871",
"exception": false,
"start_time": "2020-11-21T08:58:51.076557",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"B\n",
"C\n",
"D\n",
"C\n",
"D\n",
"C\n",
"D\n",
"C\n",
"D\n",
"C\n",
"D\n",
"E\n",
"Kode program diatas mencetak A lalu B, kemudian akan mengganti C dan D lima kali,kemudian selesai dengan huruf E satu kali. Akan tetapi C dan D range(5), berarti di cetak 5 baris tetapi tidak secara berurutan melainkan di cetak secara acak, itulah cara kerja perulangan.\n"
]
}
],
"source": [
"print('A')\n",
"print('B')\n",
"for i in range (5):\n",
" print('C')\n",
" print('D')\n",
"print('E')\n",
"print('Kode program diatas mencetak A lalu B, kemudian akan mengganti C dan D lima kali,kemudian selesai dengan huruf E satu kali. Akan tetapi C dan D range(5), berarti di cetak 5 baris tetapi tidak secara berurutan melainkan di cetak secara acak, itulah cara kerja perulangan.')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.139887Z",
"iopub.status.busy": "2020-11-21T08:58:51.138708Z",
"iopub.status.idle": "2020-11-21T08:58:51.143818Z",
"shell.execute_reply": "2020-11-21T08:58:51.144743Z"
},
"papermill": {
"duration": 0.029542,
"end_time": "2020-11-21T08:58:51.145000",
"exception": false,
"start_time": "2020-11-21T08:58:51.115458",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"B\n",
"C\n",
"C\n",
"C\n",
"C\n",
"C\n",
"D\n",
"D\n",
"D\n",
"D\n",
"D\n",
"E\n",
"Kode program ini Seperti contoh tadi akan tetapi disini range C da D yaitu (5) tetapi kali ini dicetak secara berurutan karena di jelaskan range C (5) dan range D juga (5), dan pada program diatas juga sudah jelas bahwa masing-masing range dicetak secara berurutan.\n"
]
}
],
"source": [
"print('A')\n",
"print('B')\n",
"for i in range (5):\n",
" print('C')\n",
"for i in range (5):\n",
" print('D')\n",
"print('E')\n",
"print('Kode program ini Seperti contoh tadi akan tetapi disini range C da D yaitu (5) tetapi kali ini dicetak secara berurutan karena di jelaskan range C (5) dan range D juga (5), dan pada program diatas juga sudah jelas bahwa masing-masing range dicetak secara berurutan.')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.185178Z",
"iopub.status.busy": "2020-11-21T08:58:51.183682Z",
"iopub.status.idle": "2020-11-21T08:58:51.196550Z",
"shell.execute_reply": "2020-11-21T08:58:51.197158Z"
},
"papermill": {
"duration": 0.036391,
"end_time": "2020-11-21T08:58:51.197356",
"exception": false,
"start_time": "2020-11-21T08:58:51.160965",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"56\n",
"57\n",
"58\n",
"59\n",
"60\n",
"61\n",
"62\n",
"63\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"69\n",
"70\n",
"71\n",
"72\n",
"73\n",
"74\n",
"75\n",
"76\n",
"77\n",
"78\n",
"79\n",
"80\n",
"81\n",
"82\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"98\n",
"99\n",
"Kode program diatas I rangenya(100) lalu dicetak atau hasil dari i maka perintahnya seperti diatas, ketika perulangan pertama dimulai, python menetapkan variabel I ke 0. Setiap kali kita mengulang kembali, python meningkatkan nilai I sebesar 1.\n"
]
}
],
"source": [
"for i in range (100):\n",
" print(i)\n",
"print('Kode program diatas I rangenya(100) lalu dicetak atau hasil dari i maka perintahnya seperti diatas, ketika perulangan pertama dimulai, python menetapkan variabel I ke 0. Setiap kali kita mengulang kembali, python meningkatkan nilai I sebesar 1.')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.234396Z",
"iopub.status.busy": "2020-11-21T08:58:51.233561Z",
"iopub.status.idle": "2020-11-21T08:58:51.240239Z",
"shell.execute_reply": "2020-11-21T08:58:51.239263Z"
},
"papermill": {
"duration": 0.028459,
"end_time": "2020-11-21T08:58:51.240607",
"exception": false,
"start_time": "2020-11-21T08:58:51.212148",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 --Hello \n",
"2 --Hello \n",
"3 --Hello \n",
"Kode program diatas rangenya(3), lalu di cetak i+1,kemudian hello. Kenapa ada 1, 2, dan 3 karena variabel perulangan, i bertambah 1 setiap kali melalui perulangan, itu dapat digunakan untuk dimana kita berada dalam proses perulangan.\n"
]
}
],
"source": [
"for i in range (3):\n",
" print(i+1, '--Hello ')\n",
"print('Kode program diatas rangenya(3), lalu di cetak i+1,kemudian hello. Kenapa ada 1, 2, dan 3 karena variabel perulangan, i bertambah 1 setiap kali melalui perulangan, itu dapat digunakan untuk dimana kita berada dalam proses perulangan.')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.281036Z",
"iopub.status.busy": "2020-11-21T08:58:51.279653Z",
"iopub.status.idle": "2020-11-21T08:58:51.295285Z",
"shell.execute_reply": "2020-11-21T08:58:51.294533Z"
},
"papermill": {
"duration": 0.03868,
"end_time": "2020-11-21T08:58:51.295446",
"exception": false,
"start_time": "2020-11-21T08:58:51.256766",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"56\n",
"57\n",
"58\n",
"59\n",
"60\n",
"61\n",
"62\n",
"63\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"69\n",
"70\n",
"71\n",
"72\n",
"73\n",
"74\n",
"75\n",
"76\n",
"77\n",
"78\n",
"79\n",
"80\n",
"81\n",
"82\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"98\n",
"99\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n",
"26\n",
"27\n",
"28\n",
"29\n",
"30\n",
"31\n",
"32\n",
"33\n",
"34\n",
"35\n",
"36\n",
"37\n",
"38\n",
"39\n",
"40\n",
"41\n",
"42\n",
"43\n",
"44\n",
"45\n",
"46\n",
"47\n",
"48\n",
"49\n",
"50\n",
"51\n",
"52\n",
"53\n",
"54\n",
"55\n",
"56\n",
"57\n",
"58\n",
"59\n",
"60\n",
"61\n",
"62\n",
"63\n",
"64\n",
"65\n",
"66\n",
"67\n",
"68\n",
"69\n",
"70\n",
"71\n",
"72\n",
"73\n",
"74\n",
"75\n",
"76\n",
"77\n",
"78\n",
"79\n",
"80\n",
"81\n",
"82\n",
"83\n",
"84\n",
"85\n",
"86\n",
"87\n",
"88\n",
"89\n",
"90\n",
"91\n",
"92\n",
"93\n",
"94\n",
"95\n",
"96\n",
"97\n",
"98\n",
"99\n",
"Dua Kode program diatas sama seperti contoh tadi,wacky_name range (100) lalu di cetak hasil dari wacky_name sampai 100 kali secara berurutan.\n"
]
}
],
"source": [
"for i in range (100):\n",
" print(i) \n",
"for wacky_name in range (100):\n",
" print(wacky_name)\n",
"print('Dua Kode program diatas sama seperti contoh tadi,wacky_name range (100) lalu di cetak hasil dari wacky_name sampai 100 kali secara berurutan.')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.338467Z",
"iopub.status.busy": "2020-11-21T08:58:51.337619Z",
"iopub.status.idle": "2020-11-21T08:58:51.342086Z",
"shell.execute_reply": "2020-11-21T08:58:51.342896Z"
},
"papermill": {
"duration": 0.030615,
"end_time": "2020-11-21T08:58:51.343077",
"exception": false,
"start_time": "2020-11-21T08:58:51.312462",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54321Meluncurkan!! \n",
"Kode program diatas i rangenya (5,0,-1) ini untuk mendapatkan daftar nilai mundur lalu cetak hasil i lalu end. End adalah karakter yang dicetak diakhir baris. Defaultnya adalah tanda newline(baris baru), dan file adalah nama file kemana objek akan dicetak ,cetak meluncurkan, bisa dilihat pada program diatas.\n"
]
}
],
"source": [
"for i in range (5,0,-1):\n",
" print(i, end='' )\n",
"print('Meluncurkan!! ')\n",
"print('Kode program diatas i rangenya (5,0,-1) ini untuk mendapatkan daftar nilai mundur lalu cetak hasil i lalu end. End adalah karakter yang dicetak diakhir baris. Defaultnya adalah tanda newline(baris baru), dan file adalah nama file kemana objek akan dicetak ,cetak meluncurkan, bisa dilihat pada program diatas.')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.385229Z",
"iopub.status.busy": "2020-11-21T08:58:51.384171Z",
"iopub.status.idle": "2020-11-21T08:58:51.387780Z",
"shell.execute_reply": "2020-11-21T08:58:51.388405Z"
},
"papermill": {
"duration": 0.027527,
"end_time": "2020-11-21T08:58:51.388567",
"exception": false,
"start_time": "2020-11-21T08:58:51.361040",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"******\n",
"******\n",
"******\n",
"******\n",
"Kode program diatas menunjukkan perintah untuk mencetak persegi panjang dengan symbol (*) dengan 4 baris kebawah dan 6 baris kesamping kanan.\n"
]
}
],
"source": [
"for i in range (4):\n",
" print('*'*6)\n",
"print('Kode program diatas menunjukkan perintah untuk mencetak persegi panjang dengan symbol (*) dengan 4 baris kebawah dan 6 baris kesamping kanan.')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:58:51.432172Z",
"iopub.status.busy": "2020-11-21T08:58:51.430819Z",
"iopub.status.idle": "2020-11-21T08:58:51.434907Z",
"shell.execute_reply": "2020-11-21T08:58:51.435744Z"
},
"papermill": {
"duration": 0.02934,
"end_time": "2020-11-21T08:58:51.435956",
"exception": false,
"start_time": "2020-11-21T08:58:51.406616",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*\n",
"**\n",
"***\n",
"****\n",
"Kode program diatas dapat kita misalkan ingin membuat segitika sebagai gantinya. Kita dapat melakukannya dengan sedikit perubahan pada program persegi panjang, dapat kita lihat pada program perulangan for akan mengulangi pernyataan cetak 4 kali, membuat bentuk setinggi 4 baris, lalu 6 yang perlu diubah. Kuncinya adalah mengubah 6 menjadi i+1, setiap kali melalui program akan mencetak bintang i+1, bukan 6 bintang. Variabel penghitung perulangan dijalankan melalui nilai 0,1,2, dan 3. Menggunakannya memungkinkan kita untuk memvariasikan jumlah bintang. Perintah keluarannya dapat dilihat pada program diatas.\n"
]
}
],
"source": [
"for i in range (4):\n",
" print('*'*(i+1))\n",
"print('Kode program diatas dapat kita misalkan ingin membuat segitika sebagai gantinya. Kita dapat melakukannya dengan sedikit perubahan pada program persegi panjang, dapat kita lihat pada program perulangan for akan mengulangi pernyataan cetak 4 kali, membuat bentuk setinggi 4 baris, lalu 6 yang perlu diubah. Kuncinya adalah mengubah 6 menjadi i+1, setiap kali melalui program akan mencetak bintang i+1, bukan 6 bintang. Variabel penghitung perulangan dijalankan melalui nilai 0,1,2, dan 3. Menggunakannya memungkinkan kita untuk memvariasikan jumlah bintang. Perintah keluarannya dapat dilihat pada program diatas.')"
]
}
],
"metadata": {
"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.7.6"
},
"papermill": {
"duration": 6.170383,
"end_time": "2020-11-21T08:58:51.563083",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2020-11-21T08:58:45.392700",
"version": "2.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
| 0047/375/47375450.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"execution": {
"iopub.execute_input": "2020-11-21T08:59:54.517897Z",
"iopub.status.busy": "2020-11-21T08:59:54.517163Z",
"iopub.status.idle": "2020-11-21T08:59:54.522556Z",
"shell.execute_reply": "2020-11-21T08:59:54.521853Z"
},
"papermill": {
"duration": 0.021023,
"end_time": "2020-11-21T08:59:54.522686",
"exception": false,
"start_time": "2020-11-21T08:59:54.501663",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/kaggle/input/digit-recognizer/sample_submission.csv\n",
"/kaggle/input/digit-recognizer/test.csv\n",
"/kaggle/input/digit-recognizer/train.csv\n"
]
}
],
"source": [
"# This Python 3 environment comes with many helpful analytics libraries installed\n",
"# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
"# For example, here's several helpful packages to load\n",
"\n",
"import numpy as np # linear algebra\n",
"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
"\n",
"# Input data files are available in the read-only \"../input/\" directory\n",
"# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
"\n",
"import os\n",
"for dirname, _, filenames in os.walk('/kaggle/input'):\n",
" for filename in filenames:\n",
" print(os.path.join(dirname, filename))\n",
"\n",
"# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n",
"# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:59:54.549560Z",
"iopub.status.busy": "2020-11-21T08:59:54.548902Z",
"iopub.status.idle": "2020-11-21T08:59:55.299200Z",
"shell.execute_reply": "2020-11-21T08:59:55.298679Z"
},
"papermill": {
"duration": 0.765254,
"end_time": "2020-11-21T08:59:55.299318",
"exception": false,
"start_time": "2020-11-21T08:59:54.534064",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__notebook__.ipynb\r\n"
]
}
],
"source": [
"!dir"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:59:55.323317Z",
"iopub.status.busy": "2020-11-21T08:59:55.322712Z",
"iopub.status.idle": "2020-11-21T08:59:55.326076Z",
"shell.execute_reply": "2020-11-21T08:59:55.325454Z"
},
"papermill": {
"duration": 0.017129,
"end_time": "2020-11-21T08:59:55.326188",
"exception": false,
"start_time": "2020-11-21T08:59:55.309059",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:59:55.352036Z",
"iopub.status.busy": "2020-11-21T08:59:55.351009Z",
"iopub.status.idle": "2020-11-21T08:59:55.354297Z",
"shell.execute_reply": "2020-11-21T08:59:55.353684Z"
},
"papermill": {
"duration": 0.018647,
"end_time": "2020-11-21T08:59:55.354407",
"exception": false,
"start_time": "2020-11-21T08:59:55.335760",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"def load_data(path):\n",
" with np.load(path) as f:\n",
" X_train,y_train = f['x_train'], f['y_train']\n",
" X_test,y_test = f['x_test'],f['y_test']\n",
" return (X_train,y_test),(X_test,y_test)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T08:59:55.378949Z",
"iopub.status.busy": "2020-11-21T08:59:55.378190Z",
"iopub.status.idle": "2020-11-21T09:00:01.300766Z",
"shell.execute_reply": "2020-11-21T09:00:01.301243Z"
},
"papermill": {
"duration": 5.937568,
"end_time": "2020-11-21T09:00:01.301393",
"exception": false,
"start_time": "2020-11-21T08:59:55.363825",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"from tensorflow.keras.datasets import mnist\n",
"import tensorflow"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
"_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
"execution": {
"iopub.execute_input": "2020-11-21T09:00:01.338756Z",
"iopub.status.busy": "2020-11-21T09:00:01.324963Z",
"iopub.status.idle": "2020-11-21T09:00:01.815320Z",
"shell.execute_reply": "2020-11-21T09:00:01.814687Z"
},
"papermill": {
"duration": 0.504509,
"end_time": "2020-11-21T09:00:01.815463",
"exception": false,
"start_time": "2020-11-21T09:00:01.310954",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 0s 0us/step\n"
]
}
],
"source": [
"(X_train,y_train),(X_test,y_test) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:00:01.844485Z",
"iopub.status.busy": "2020-11-21T09:00:01.843514Z",
"iopub.status.idle": "2020-11-21T09:00:01.847156Z",
"shell.execute_reply": "2020-11-21T09:00:01.847659Z"
},
"papermill": {
"duration": 0.02134,
"end_time": "2020-11-21T09:00:01.847811",
"exception": false,
"start_time": "2020-11-21T09:00:01.826471",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(60000, 28, 28) (60000,) (10000, 28, 28) (10000,)\n"
]
}
],
"source": [
"print(f\"{X_train.shape} {y_train.shape} {X_test.shape} {y_test.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:00:01.877482Z",
"iopub.status.busy": "2020-11-21T09:00:01.876393Z",
"iopub.status.idle": "2020-11-21T09:00:02.176038Z",
"shell.execute_reply": "2020-11-21T09:00:02.175476Z"
},
"papermill": {
"duration": 0.316672,
"end_time": "2020-11-21T09:00:02.176166",
"exception": false,
"start_time": "2020-11-21T09:00:01.859494",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"X_train = X_train.reshape(X_train.shape[0],28,28,1) \n",
"X_test = X_test.reshape(X_test.shape[0],28,28,1)\n",
"X_train.astype('float32')\n",
"X_test.astype('float32')\n",
"X_train=X_train/255\n",
"X_test=X_test/255"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:00:02.207236Z",
"iopub.status.busy": "2020-11-21T09:00:02.206139Z",
"iopub.status.idle": "2020-11-21T09:00:02.212281Z",
"shell.execute_reply": "2020-11-21T09:00:02.211677Z"
},
"papermill": {
"duration": 0.023195,
"end_time": "2020-11-21T09:00:02.212388",
"exception": false,
"start_time": "2020-11-21T09:00:02.189193",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"y_train = tensorflow.keras.utils.to_categorical(y_train,10)\n",
"y_test = tensorflow.keras.utils.to_categorical(y_test,10)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:00:02.244759Z",
"iopub.status.busy": "2020-11-21T09:00:02.243756Z",
"iopub.status.idle": "2020-11-21T09:00:02.401433Z",
"shell.execute_reply": "2020-11-21T09:00:02.400704Z"
},
"papermill": {
"duration": 0.17784,
"end_time": "2020-11-21T09:00:02.401558",
"exception": false,
"start_time": "2020-11-21T09:00:02.223718",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"from tensorflow.keras import Sequential\n",
"from tensorflow.keras.layers import * \n",
"\n",
"model = Sequential()\n",
"model.add(Conv2D(24,kernel_size=(5,5),padding='same',activation='relu',input_shape=(28,28,1)))\n",
"model.add(MaxPool2D())\n",
"model.add(Flatten())\n",
"model.add(Dense(256,activation='relu'))\n",
"model.add(Dense(10,activation='softmax'))\n",
"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:00:02.430947Z",
"iopub.status.busy": "2020-11-21T09:00:02.429964Z",
"iopub.status.idle": "2020-11-21T09:03:08.901905Z",
"shell.execute_reply": "2020-11-21T09:03:08.902525Z"
},
"papermill": {
"duration": 186.489769,
"end_time": "2020-11-21T09:03:08.902705",
"exception": false,
"start_time": "2020-11-21T09:00:02.412936",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"469/469 [==============================] - 19s 40ms/step - loss: 0.1914 - accuracy: 0.9437 - val_loss: 0.0652 - val_accuracy: 0.9805\n",
"Epoch 2/10\n",
"469/469 [==============================] - 19s 41ms/step - loss: 0.0556 - accuracy: 0.9834 - val_loss: 0.0529 - val_accuracy: 0.9839\n",
"Epoch 3/10\n",
"469/469 [==============================] - 18s 38ms/step - loss: 0.0358 - accuracy: 0.9892 - val_loss: 0.0430 - val_accuracy: 0.9855\n",
"Epoch 4/10\n",
"469/469 [==============================] - 18s 39ms/step - loss: 0.0259 - accuracy: 0.9921 - val_loss: 0.0379 - val_accuracy: 0.9874\n",
"Epoch 5/10\n",
"469/469 [==============================] - 19s 40ms/step - loss: 0.0176 - accuracy: 0.9947 - val_loss: 0.0339 - val_accuracy: 0.9890\n",
"Epoch 6/10\n",
"469/469 [==============================] - 18s 39ms/step - loss: 0.0129 - accuracy: 0.9960 - val_loss: 0.0361 - val_accuracy: 0.9888\n",
"Epoch 7/10\n",
"469/469 [==============================] - 18s 39ms/step - loss: 0.0111 - accuracy: 0.9965 - val_loss: 0.0424 - val_accuracy: 0.9858\n",
"Epoch 8/10\n",
"469/469 [==============================] - 19s 40ms/step - loss: 0.0089 - accuracy: 0.9972 - val_loss: 0.0393 - val_accuracy: 0.9883\n",
"Epoch 9/10\n",
"469/469 [==============================] - 19s 40ms/step - loss: 0.0072 - accuracy: 0.9976 - val_loss: 0.0387 - val_accuracy: 0.9885\n",
"Epoch 10/10\n",
"469/469 [==============================] - 18s 39ms/step - loss: 0.0063 - accuracy: 0.9980 - val_loss: 0.0447 - val_accuracy: 0.9884\n"
]
}
],
"source": [
"model_log = model.fit(X_train,y_train,batch_size=128,epochs=10,verbose=1,validation_data=(X_test,y_test))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:10.284893Z",
"iopub.status.busy": "2020-11-21T09:03:10.284190Z",
"iopub.status.idle": "2020-11-21T09:03:11.563949Z",
"shell.execute_reply": "2020-11-21T09:03:11.563364Z"
},
"papermill": {
"duration": 1.997243,
"end_time": "2020-11-21T09:03:11.564066",
"exception": false,
"start_time": "2020-11-21T09:03:09.566823",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test loss : 0.04466145485639572\n",
"Test accuracy : 0.9883999824523926\n"
]
}
],
"source": [
"score = model.evaluate(X_test,y_test,verbose=0)\n",
"print(f\"Test loss : {score[0]}\")\n",
"print(f\"Test accuracy : {score[1]}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:12.886173Z",
"iopub.status.busy": "2020-11-21T09:03:12.885209Z",
"iopub.status.idle": "2020-11-21T09:03:12.888312Z",
"shell.execute_reply": "2020-11-21T09:03:12.887854Z"
},
"papermill": {
"duration": 0.663781,
"end_time": "2020-11-21T09:03:12.888450",
"exception": false,
"start_time": "2020-11-21T09:03:12.224669",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:14.223744Z",
"iopub.status.busy": "2020-11-21T09:03:14.223069Z",
"iopub.status.idle": "2020-11-21T09:03:15.828962Z",
"shell.execute_reply": "2020-11-21T09:03:15.828214Z"
},
"papermill": {
"duration": 2.275763,
"end_time": "2020-11-21T09:03:15.829082",
"exception": false,
"start_time": "2020-11-21T09:03:13.553319",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"results = model.predict(X_test)\n",
"results = np.argmax(results,axis=1)\n",
"results = pd.Series(results,name='Label')\n",
"submission = pd.concat([pd.Series(range(1,28001),name='ImageId'),results],axis=1)\n",
"submission.to_csv('submission.csv',index=False)"
]
}
],
"metadata": {
"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.7.6"
},
"papermill": {
"duration": 206.16683,
"end_time": "2020-11-21T09:03:16.605004",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2020-11-21T08:59:50.438174",
"version": "2.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
| 0047/375/47375533.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"execution": {
"iopub.execute_input": "2020-11-21T09:02:53.575644Z",
"iopub.status.busy": "2020-11-21T09:02:53.574755Z",
"iopub.status.idle": "2020-11-21T09:02:54.567183Z",
"shell.execute_reply": "2020-11-21T09:02:54.566589Z"
},
"papermill": {
"duration": 1.019915,
"end_time": "2020-11-21T09:02:54.567301",
"exception": false,
"start_time": "2020-11-21T09:02:53.547386",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/kaggle/input/breast-cancer-wisconsin-data/data.csv\n"
]
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"from sklearn.feature_selection import VarianceThreshold\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.metrics import roc_auc_score\n",
"from sklearn.model_selection import train_test_split, GridSearchCV\n",
"from sklearn.base import BaseEstimator\n",
"from sklearn.linear_model import SGDClassifier, LogisticRegression\n",
"from sklearn.pipeline import Pipeline\n",
"\n",
"import os\n",
"for dirname, _, filenames in os.walk('/kaggle/input'):\n",
" for filename in filenames:\n",
" print(os.path.join(dirname, filename))\n",
" \n",
"# to display all the columns of the dataframe in the notebook\n",
"pd.pandas.set_option('display.max_columns', None)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
"_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.605499Z",
"iopub.status.busy": "2020-11-21T09:02:54.604508Z",
"iopub.status.idle": "2020-11-21T09:02:54.662256Z",
"shell.execute_reply": "2020-11-21T09:02:54.662856Z"
},
"papermill": {
"duration": 0.079097,
"end_time": "2020-11-21T09:02:54.662997",
"exception": false,
"start_time": "2020-11-21T09:02:54.583900",
"status": "completed"
},
"tags": []
},
"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>symmetry_mean</th>\n",
" <th>fractal_dimension_mean</th>\n",
" <th>radius_se</th>\n",
" <th>texture_se</th>\n",
" <th>perimeter_se</th>\n",
" <th>area_se</th>\n",
" <th>smoothness_se</th>\n",
" <th>compactness_se</th>\n",
" <th>concavity_se</th>\n",
" <th>concave points_se</th>\n",
" <th>symmetry_se</th>\n",
" <th>fractal_dimension_se</th>\n",
" <th>radius_worst</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>0.2419</td>\n",
" <td>0.07871</td>\n",
" <td>1.0950</td>\n",
" <td>0.9053</td>\n",
" <td>8.589</td>\n",
" <td>153.40</td>\n",
" <td>0.006399</td>\n",
" <td>0.04904</td>\n",
" <td>0.05373</td>\n",
" <td>0.01587</td>\n",
" <td>0.03003</td>\n",
" <td>0.006193</td>\n",
" <td>25.38</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>0.1812</td>\n",
" <td>0.05667</td>\n",
" <td>0.5435</td>\n",
" <td>0.7339</td>\n",
" <td>3.398</td>\n",
" <td>74.08</td>\n",
" <td>0.005225</td>\n",
" <td>0.01308</td>\n",
" <td>0.01860</td>\n",
" <td>0.01340</td>\n",
" <td>0.01389</td>\n",
" <td>0.003532</td>\n",
" <td>24.99</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>0.2069</td>\n",
" <td>0.05999</td>\n",
" <td>0.7456</td>\n",
" <td>0.7869</td>\n",
" <td>4.585</td>\n",
" <td>94.03</td>\n",
" <td>0.006150</td>\n",
" <td>0.04006</td>\n",
" <td>0.03832</td>\n",
" <td>0.02058</td>\n",
" <td>0.02250</td>\n",
" <td>0.004571</td>\n",
" <td>23.57</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>0.2597</td>\n",
" <td>0.09744</td>\n",
" <td>0.4956</td>\n",
" <td>1.1560</td>\n",
" <td>3.445</td>\n",
" <td>27.23</td>\n",
" <td>0.009110</td>\n",
" <td>0.07458</td>\n",
" <td>0.05661</td>\n",
" <td>0.01867</td>\n",
" <td>0.05963</td>\n",
" <td>0.009208</td>\n",
" <td>14.91</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>0.1809</td>\n",
" <td>0.05883</td>\n",
" <td>0.7572</td>\n",
" <td>0.7813</td>\n",
" <td>5.438</td>\n",
" <td>94.44</td>\n",
" <td>0.011490</td>\n",
" <td>0.02461</td>\n",
" <td>0.05688</td>\n",
" <td>0.01885</td>\n",
" <td>0.01756</td>\n",
" <td>0.005115</td>\n",
" <td>22.54</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",
"</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",
" symmetry_mean fractal_dimension_mean radius_se texture_se perimeter_se \\\n",
"0 0.2419 0.07871 1.0950 0.9053 8.589 \n",
"1 0.1812 0.05667 0.5435 0.7339 3.398 \n",
"2 0.2069 0.05999 0.7456 0.7869 4.585 \n",
"3 0.2597 0.09744 0.4956 1.1560 3.445 \n",
"4 0.1809 0.05883 0.7572 0.7813 5.438 \n",
"\n",
" area_se smoothness_se compactness_se concavity_se concave points_se \\\n",
"0 153.40 0.006399 0.04904 0.05373 0.01587 \n",
"1 74.08 0.005225 0.01308 0.01860 0.01340 \n",
"2 94.03 0.006150 0.04006 0.03832 0.02058 \n",
"3 27.23 0.009110 0.07458 0.05661 0.01867 \n",
"4 94.44 0.011490 0.02461 0.05688 0.01885 \n",
"\n",
" symmetry_se fractal_dimension_se radius_worst texture_worst \\\n",
"0 0.03003 0.006193 25.38 17.33 \n",
"1 0.01389 0.003532 24.99 23.41 \n",
"2 0.02250 0.004571 23.57 25.53 \n",
"3 0.05963 0.009208 14.91 26.50 \n",
"4 0.01756 0.005115 22.54 16.67 \n",
"\n",
" perimeter_worst area_worst smoothness_worst compactness_worst \\\n",
"0 184.60 2019.0 0.1622 0.6656 \n",
"1 158.80 1956.0 0.1238 0.1866 \n",
"2 152.50 1709.0 0.1444 0.4245 \n",
"3 98.87 567.7 0.2098 0.8663 \n",
"4 152.20 1575.0 0.1374 0.2050 \n",
"\n",
" concavity_worst concave points_worst symmetry_worst \\\n",
"0 0.7119 0.2654 0.4601 \n",
"1 0.2416 0.1860 0.2750 \n",
"2 0.4504 0.2430 0.3613 \n",
"3 0.6869 0.2575 0.6638 \n",
"4 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 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filePath = \"/kaggle/input/breast-cancer-wisconsin-data/data.csv\"\n",
"df = pd.read_csv(filePath)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.707785Z",
"iopub.status.busy": "2020-11-21T09:02:54.706757Z",
"iopub.status.idle": "2020-11-21T09:02:54.711436Z",
"shell.execute_reply": "2020-11-21T09:02:54.710838Z"
},
"papermill": {
"duration": 0.031166,
"end_time": "2020-11-21T09:02:54.711560",
"exception": false,
"start_time": "2020-11-21T09:02:54.680394",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"id 0\n",
"diagnosis 0\n",
"radius_mean 0\n",
"texture_mean 0\n",
"perimeter_mean 0\n",
"area_mean 0\n",
"smoothness_mean 0\n",
"compactness_mean 0\n",
"concavity_mean 0\n",
"concave points_mean 0\n",
"symmetry_mean 0\n",
"fractal_dimension_mean 0\n",
"radius_se 0\n",
"texture_se 0\n",
"perimeter_se 0\n",
"area_se 0\n",
"smoothness_se 0\n",
"compactness_se 0\n",
"concavity_se 0\n",
"concave points_se 0\n",
"symmetry_se 0\n",
"fractal_dimension_se 0\n",
"radius_worst 0\n",
"texture_worst 0\n",
"perimeter_worst 0\n",
"area_worst 0\n",
"smoothness_worst 0\n",
"compactness_worst 0\n",
"concavity_worst 0\n",
"concave points_worst 0\n",
"symmetry_worst 0\n",
"fractal_dimension_worst 0\n",
"Unnamed: 32 569\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.759253Z",
"iopub.status.busy": "2020-11-21T09:02:54.758373Z",
"iopub.status.idle": "2020-11-21T09:02:54.762474Z",
"shell.execute_reply": "2020-11-21T09:02:54.761914Z"
},
"papermill": {
"duration": 0.032271,
"end_time": "2020-11-21T09:02:54.762574",
"exception": false,
"start_time": "2020-11-21T09:02:54.730303",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((569, 30), (569,))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop(['id', 'Unnamed: 32'], axis =1, inplace = True)\n",
"X = df.drop(['diagnosis'], axis = 1)\n",
"\n",
"lbl_encoder = LabelEncoder()\n",
"\n",
"y = pd.Series(\n",
" lbl_encoder.fit_transform(\n",
" df['diagnosis']))\n",
"X.shape, y.shape"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.805227Z",
"iopub.status.busy": "2020-11-21T09:02:54.804515Z",
"iopub.status.idle": "2020-11-21T09:02:54.821882Z",
"shell.execute_reply": "2020-11-21T09:02:54.821344Z"
},
"papermill": {
"duration": 0.042817,
"end_time": "2020-11-21T09:02:54.821999",
"exception": false,
"start_time": "2020-11-21T09:02:54.779182",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 569 entries, 0 to 568\n",
"Data columns (total 30 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 radius_mean 569 non-null float64\n",
" 1 texture_mean 569 non-null float64\n",
" 2 perimeter_mean 569 non-null float64\n",
" 3 area_mean 569 non-null float64\n",
" 4 smoothness_mean 569 non-null float64\n",
" 5 compactness_mean 569 non-null float64\n",
" 6 concavity_mean 569 non-null float64\n",
" 7 concave points_mean 569 non-null float64\n",
" 8 symmetry_mean 569 non-null float64\n",
" 9 fractal_dimension_mean 569 non-null float64\n",
" 10 radius_se 569 non-null float64\n",
" 11 texture_se 569 non-null float64\n",
" 12 perimeter_se 569 non-null float64\n",
" 13 area_se 569 non-null float64\n",
" 14 smoothness_se 569 non-null float64\n",
" 15 compactness_se 569 non-null float64\n",
" 16 concavity_se 569 non-null float64\n",
" 17 concave points_se 569 non-null float64\n",
" 18 symmetry_se 569 non-null float64\n",
" 19 fractal_dimension_se 569 non-null float64\n",
" 20 radius_worst 569 non-null float64\n",
" 21 texture_worst 569 non-null float64\n",
" 22 perimeter_worst 569 non-null float64\n",
" 23 area_worst 569 non-null float64\n",
" 24 smoothness_worst 569 non-null float64\n",
" 25 compactness_worst 569 non-null float64\n",
" 26 concavity_worst 569 non-null float64\n",
" 27 concave points_worst 569 non-null float64\n",
" 28 symmetry_worst 569 non-null float64\n",
" 29 fractal_dimension_worst 569 non-null float64\n",
"dtypes: float64(30)\n",
"memory usage: 133.5 KB\n"
]
}
],
"source": [
"X.info()"
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.017612,
"end_time": "2020-11-21T09:02:54.857061",
"exception": false,
"start_time": "2020-11-21T09:02:54.839449",
"status": "completed"
},
"tags": []
},
"source": [
"Feature Selection"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.901553Z",
"iopub.status.busy": "2020-11-21T09:02:54.900566Z",
"iopub.status.idle": "2020-11-21T09:02:54.906870Z",
"shell.execute_reply": "2020-11-21T09:02:54.906144Z"
},
"papermill": {
"duration": 0.031722,
"end_time": "2020-11-21T09:02:54.906973",
"exception": false,
"start_time": "2020-11-21T09:02:54.875251",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check for Constant Features\n",
"\n",
"const_features = VarianceThreshold(threshold = 0.001) # where 99.9% values are same\n",
"const_features.fit(X)\n",
"const_features.get_support().sum()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:54.948592Z",
"iopub.status.busy": "2020-11-21T09:02:54.947674Z",
"iopub.status.idle": "2020-11-21T09:02:54.950300Z",
"shell.execute_reply": "2020-11-21T09:02:54.950751Z"
},
"papermill": {
"duration": 0.025514,
"end_time": "2020-11-21T09:02:54.950898",
"exception": false,
"start_time": "2020-11-21T09:02:54.925384",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"non_const_features = X.columns[const_features.get_support()]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:55.106315Z",
"iopub.status.busy": "2020-11-21T09:02:55.101735Z",
"iopub.status.idle": "2020-11-21T09:02:55.112585Z",
"shell.execute_reply": "2020-11-21T09:02:55.111611Z"
},
"papermill": {
"duration": 0.143757,
"end_time": "2020-11-21T09:02:55.112707",
"exception": false,
"start_time": "2020-11-21T09:02:54.968950",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check for Duplicate Features\n",
"\n",
"x_transpose = X[non_const_features].T\n",
"x_transpose = pd.DataFrame(x_transpose)\n",
"x_transpose.shape\n",
"x_transpose.duplicated().sum()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:02:55.162300Z",
"iopub.status.busy": "2020-11-21T09:02:55.161359Z",
"iopub.status.idle": "2020-11-21T09:03:00.264966Z",
"shell.execute_reply": "2020-11-21T09:03:00.264233Z"
},
"papermill": {
"duration": 5.132209,
"end_time": "2020-11-21T09:03:00.265132",
"exception": false,
"start_time": "2020-11-21T09:02:55.132923",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"# feature selection based on Random Classifier\n",
"\n",
"roc_score = []\n",
"for feature in X[non_const_features].columns:\n",
" clf = RandomForestClassifier(n_estimators = 100, random_state = 0)\n",
" clf.fit(X[feature].to_frame(), y)\n",
" y_pred = clf.predict(X[feature].to_frame())\n",
" roc_score.append(roc_auc_score(y, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.310695Z",
"iopub.status.busy": "2020-11-21T09:03:00.309776Z",
"iopub.status.idle": "2020-11-21T09:03:00.325444Z",
"shell.execute_reply": "2020-11-21T09:03:00.326006Z"
},
"papermill": {
"duration": 0.041356,
"end_time": "2020-11-21T09:03:00.326169",
"exception": false,
"start_time": "2020-11-21T09:03:00.284813",
"status": "completed"
},
"tags": []
},
"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>ROC_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>concave points_mean</th>\n",
" <td>0.994840</td>\n",
" </tr>\n",
" <tr>\n",
" <th>radius_se</th>\n",
" <td>0.992039</td>\n",
" </tr>\n",
" <tr>\n",
" <th>area_worst</th>\n",
" <td>0.991081</td>\n",
" </tr>\n",
" <tr>\n",
" <th>concavity_worst</th>\n",
" <td>0.990639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>concavity_mean</th>\n",
" <td>0.990123</td>\n",
" </tr>\n",
" <tr>\n",
" <th>perimeter_se</th>\n",
" <td>0.989681</td>\n",
" </tr>\n",
" <tr>\n",
" <th>area_mean</th>\n",
" <td>0.987322</td>\n",
" </tr>\n",
" <tr>\n",
" <th>area_se</th>\n",
" <td>0.984448</td>\n",
" </tr>\n",
" <tr>\n",
" <th>compactness_worst</th>\n",
" <td>0.983563</td>\n",
" </tr>\n",
" <tr>\n",
" <th>perimeter_mean</th>\n",
" <td>0.983491</td>\n",
" </tr>\n",
" <tr>\n",
" <th>concave points_worst</th>\n",
" <td>0.982163</td>\n",
" </tr>\n",
" <tr>\n",
" <th>compactness_mean</th>\n",
" <td>0.976488</td>\n",
" </tr>\n",
" <tr>\n",
" <th>radius_worst</th>\n",
" <td>0.973171</td>\n",
" </tr>\n",
" <tr>\n",
" <th>perimeter_worst</th>\n",
" <td>0.971328</td>\n",
" </tr>\n",
" <tr>\n",
" <th>texture_se</th>\n",
" <td>0.953933</td>\n",
" </tr>\n",
" <tr>\n",
" <th>texture_worst</th>\n",
" <td>0.950617</td>\n",
" </tr>\n",
" <tr>\n",
" <th>radius_mean</th>\n",
" <td>0.950102</td>\n",
" </tr>\n",
" <tr>\n",
" <th>texture_mean</th>\n",
" <td>0.936466</td>\n",
" </tr>\n",
" <tr>\n",
" <th>symmetry_worst</th>\n",
" <td>0.936393</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ROC_score\n",
"concave points_mean 0.994840\n",
"radius_se 0.992039\n",
"area_worst 0.991081\n",
"concavity_worst 0.990639\n",
"concavity_mean 0.990123\n",
"perimeter_se 0.989681\n",
"area_mean 0.987322\n",
"area_se 0.984448\n",
"compactness_worst 0.983563\n",
"perimeter_mean 0.983491\n",
"concave points_worst 0.982163\n",
"compactness_mean 0.976488\n",
"radius_worst 0.973171\n",
"perimeter_worst 0.971328\n",
"texture_se 0.953933\n",
"texture_worst 0.950617\n",
"radius_mean 0.950102\n",
"texture_mean 0.936466\n",
"symmetry_worst 0.936393"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"roc_score = pd.DataFrame(roc_score)\n",
"roc_score.index = X[non_const_features].columns\n",
"roc_score.columns = ['ROC_score']\n",
"roc_score.sort_values(by = 'ROC_score', ascending = False, inplace = True)\n",
"roc_score"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.383682Z",
"iopub.status.busy": "2020-11-21T09:03:00.382345Z",
"iopub.status.idle": "2020-11-21T09:03:00.403638Z",
"shell.execute_reply": "2020-11-21T09:03:00.402507Z"
},
"papermill": {
"duration": 0.053352,
"end_time": "2020-11-21T09:03:00.403764",
"exception": false,
"start_time": "2020-11-21T09:03:00.350412",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"17"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"roc_feature = X[roc_score[roc_score.ROC_score > 0.95].index].columns\n",
"len(roc_feature)"
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.01982,
"end_time": "2020-11-21T09:03:00.444380",
"exception": false,
"start_time": "2020-11-21T09:03:00.424560",
"status": "completed"
},
"tags": []
},
"source": [
"Selected Features"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.496951Z",
"iopub.status.busy": "2020-11-21T09:03:00.494918Z",
"iopub.status.idle": "2020-11-21T09:03:00.499837Z",
"shell.execute_reply": "2020-11-21T09:03:00.499276Z"
},
"papermill": {
"duration": 0.034306,
"end_time": "2020-11-21T09:03:00.499944",
"exception": false,
"start_time": "2020-11-21T09:03:00.465638",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"Index(['concave points_mean', 'radius_se', 'area_worst', 'concavity_worst',\n",
" 'concavity_mean', 'perimeter_se', 'area_mean', 'area_se',\n",
" 'compactness_worst', 'perimeter_mean', 'concave points_worst',\n",
" 'compactness_mean', 'radius_worst', 'perimeter_worst', 'texture_se',\n",
" 'texture_worst', 'radius_mean'],\n",
" dtype='object')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"roc_feature"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.574975Z",
"iopub.status.busy": "2020-11-21T09:03:00.573373Z",
"iopub.status.idle": "2020-11-21T09:03:00.579651Z",
"shell.execute_reply": "2020-11-21T09:03:00.579008Z"
},
"papermill": {
"duration": 0.058878,
"end_time": "2020-11-21T09:03:00.579783",
"exception": false,
"start_time": "2020-11-21T09:03:00.520905",
"status": "completed"
},
"tags": []
},
"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>concave points_mean</th>\n",
" <th>radius_se</th>\n",
" <th>area_worst</th>\n",
" <th>concavity_worst</th>\n",
" <th>concavity_mean</th>\n",
" <th>perimeter_se</th>\n",
" <th>area_mean</th>\n",
" <th>area_se</th>\n",
" <th>compactness_worst</th>\n",
" <th>perimeter_mean</th>\n",
" <th>concave points_worst</th>\n",
" <th>compactness_mean</th>\n",
" <th>radius_worst</th>\n",
" <th>perimeter_worst</th>\n",
" <th>texture_se</th>\n",
" <th>texture_worst</th>\n",
" <th>radius_mean</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.14710</td>\n",
" <td>1.0950</td>\n",
" <td>2019.0</td>\n",
" <td>0.7119</td>\n",
" <td>0.30010</td>\n",
" <td>8.589</td>\n",
" <td>1001.0</td>\n",
" <td>153.40</td>\n",
" <td>0.66560</td>\n",
" <td>122.80</td>\n",
" <td>0.2654</td>\n",
" <td>0.27760</td>\n",
" <td>25.380</td>\n",
" <td>184.60</td>\n",
" <td>0.9053</td>\n",
" <td>17.33</td>\n",
" <td>17.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.07017</td>\n",
" <td>0.5435</td>\n",
" <td>1956.0</td>\n",
" <td>0.2416</td>\n",
" <td>0.08690</td>\n",
" <td>3.398</td>\n",
" <td>1326.0</td>\n",
" <td>74.08</td>\n",
" <td>0.18660</td>\n",
" <td>132.90</td>\n",
" <td>0.1860</td>\n",
" <td>0.07864</td>\n",
" <td>24.990</td>\n",
" <td>158.80</td>\n",
" <td>0.7339</td>\n",
" <td>23.41</td>\n",
" <td>20.57</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.12790</td>\n",
" <td>0.7456</td>\n",
" <td>1709.0</td>\n",
" <td>0.4504</td>\n",
" <td>0.19740</td>\n",
" <td>4.585</td>\n",
" <td>1203.0</td>\n",
" <td>94.03</td>\n",
" <td>0.42450</td>\n",
" <td>130.00</td>\n",
" <td>0.2430</td>\n",
" <td>0.15990</td>\n",
" <td>23.570</td>\n",
" <td>152.50</td>\n",
" <td>0.7869</td>\n",
" <td>25.53</td>\n",
" <td>19.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.10520</td>\n",
" <td>0.4956</td>\n",
" <td>567.7</td>\n",
" <td>0.6869</td>\n",
" <td>0.24140</td>\n",
" <td>3.445</td>\n",
" <td>386.1</td>\n",
" <td>27.23</td>\n",
" <td>0.86630</td>\n",
" <td>77.58</td>\n",
" <td>0.2575</td>\n",
" <td>0.28390</td>\n",
" <td>14.910</td>\n",
" <td>98.87</td>\n",
" <td>1.1560</td>\n",
" <td>26.50</td>\n",
" <td>11.42</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.10430</td>\n",
" <td>0.7572</td>\n",
" <td>1575.0</td>\n",
" <td>0.4000</td>\n",
" <td>0.19800</td>\n",
" <td>5.438</td>\n",
" <td>1297.0</td>\n",
" <td>94.44</td>\n",
" <td>0.20500</td>\n",
" <td>135.10</td>\n",
" <td>0.1625</td>\n",
" <td>0.13280</td>\n",
" <td>22.540</td>\n",
" <td>152.20</td>\n",
" <td>0.7813</td>\n",
" <td>16.67</td>\n",
" <td>20.29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>564</th>\n",
" <td>0.13890</td>\n",
" <td>1.1760</td>\n",
" <td>2027.0</td>\n",
" <td>0.4107</td>\n",
" <td>0.24390</td>\n",
" <td>7.673</td>\n",
" <td>1479.0</td>\n",
" <td>158.70</td>\n",
" <td>0.21130</td>\n",
" <td>142.00</td>\n",
" <td>0.2216</td>\n",
" <td>0.11590</td>\n",
" <td>25.450</td>\n",
" <td>166.10</td>\n",
" <td>1.2560</td>\n",
" <td>26.40</td>\n",
" <td>21.56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>565</th>\n",
" <td>0.09791</td>\n",
" <td>0.7655</td>\n",
" <td>1731.0</td>\n",
" <td>0.3215</td>\n",
" <td>0.14400</td>\n",
" <td>5.203</td>\n",
" <td>1261.0</td>\n",
" <td>99.04</td>\n",
" <td>0.19220</td>\n",
" <td>131.20</td>\n",
" <td>0.1628</td>\n",
" <td>0.10340</td>\n",
" <td>23.690</td>\n",
" <td>155.00</td>\n",
" <td>2.4630</td>\n",
" <td>38.25</td>\n",
" <td>20.13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>566</th>\n",
" <td>0.05302</td>\n",
" <td>0.4564</td>\n",
" <td>1124.0</td>\n",
" <td>0.3403</td>\n",
" <td>0.09251</td>\n",
" <td>3.425</td>\n",
" <td>858.1</td>\n",
" <td>48.55</td>\n",
" <td>0.30940</td>\n",
" <td>108.30</td>\n",
" <td>0.1418</td>\n",
" <td>0.10230</td>\n",
" <td>18.980</td>\n",
" <td>126.70</td>\n",
" <td>1.0750</td>\n",
" <td>34.12</td>\n",
" <td>16.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>567</th>\n",
" <td>0.15200</td>\n",
" <td>0.7260</td>\n",
" <td>1821.0</td>\n",
" <td>0.9387</td>\n",
" <td>0.35140</td>\n",
" <td>5.772</td>\n",
" <td>1265.0</td>\n",
" <td>86.22</td>\n",
" <td>0.86810</td>\n",
" <td>140.10</td>\n",
" <td>0.2650</td>\n",
" <td>0.27700</td>\n",
" <td>25.740</td>\n",
" <td>184.60</td>\n",
" <td>1.5950</td>\n",
" <td>39.42</td>\n",
" <td>20.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>568</th>\n",
" <td>0.00000</td>\n",
" <td>0.3857</td>\n",
" <td>268.6</td>\n",
" <td>0.0000</td>\n",
" <td>0.00000</td>\n",
" <td>2.548</td>\n",
" <td>181.0</td>\n",
" <td>19.15</td>\n",
" <td>0.06444</td>\n",
" <td>47.92</td>\n",
" <td>0.0000</td>\n",
" <td>0.04362</td>\n",
" <td>9.456</td>\n",
" <td>59.16</td>\n",
" <td>1.4280</td>\n",
" <td>30.37</td>\n",
" <td>7.76</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>569 rows × 17 columns</p>\n",
"</div>"
],
"text/plain": [
" concave points_mean radius_se area_worst concavity_worst \\\n",
"0 0.14710 1.0950 2019.0 0.7119 \n",
"1 0.07017 0.5435 1956.0 0.2416 \n",
"2 0.12790 0.7456 1709.0 0.4504 \n",
"3 0.10520 0.4956 567.7 0.6869 \n",
"4 0.10430 0.7572 1575.0 0.4000 \n",
".. ... ... ... ... \n",
"564 0.13890 1.1760 2027.0 0.4107 \n",
"565 0.09791 0.7655 1731.0 0.3215 \n",
"566 0.05302 0.4564 1124.0 0.3403 \n",
"567 0.15200 0.7260 1821.0 0.9387 \n",
"568 0.00000 0.3857 268.6 0.0000 \n",
"\n",
" concavity_mean perimeter_se area_mean area_se compactness_worst \\\n",
"0 0.30010 8.589 1001.0 153.40 0.66560 \n",
"1 0.08690 3.398 1326.0 74.08 0.18660 \n",
"2 0.19740 4.585 1203.0 94.03 0.42450 \n",
"3 0.24140 3.445 386.1 27.23 0.86630 \n",
"4 0.19800 5.438 1297.0 94.44 0.20500 \n",
".. ... ... ... ... ... \n",
"564 0.24390 7.673 1479.0 158.70 0.21130 \n",
"565 0.14400 5.203 1261.0 99.04 0.19220 \n",
"566 0.09251 3.425 858.1 48.55 0.30940 \n",
"567 0.35140 5.772 1265.0 86.22 0.86810 \n",
"568 0.00000 2.548 181.0 19.15 0.06444 \n",
"\n",
" perimeter_mean concave points_worst compactness_mean radius_worst \\\n",
"0 122.80 0.2654 0.27760 25.380 \n",
"1 132.90 0.1860 0.07864 24.990 \n",
"2 130.00 0.2430 0.15990 23.570 \n",
"3 77.58 0.2575 0.28390 14.910 \n",
"4 135.10 0.1625 0.13280 22.540 \n",
".. ... ... ... ... \n",
"564 142.00 0.2216 0.11590 25.450 \n",
"565 131.20 0.1628 0.10340 23.690 \n",
"566 108.30 0.1418 0.10230 18.980 \n",
"567 140.10 0.2650 0.27700 25.740 \n",
"568 47.92 0.0000 0.04362 9.456 \n",
"\n",
" perimeter_worst texture_se texture_worst radius_mean \n",
"0 184.60 0.9053 17.33 17.99 \n",
"1 158.80 0.7339 23.41 20.57 \n",
"2 152.50 0.7869 25.53 19.69 \n",
"3 98.87 1.1560 26.50 11.42 \n",
"4 152.20 0.7813 16.67 20.29 \n",
".. ... ... ... ... \n",
"564 166.10 1.2560 26.40 21.56 \n",
"565 155.00 2.4630 38.25 20.13 \n",
"566 126.70 1.0750 34.12 16.60 \n",
"567 184.60 1.5950 39.42 20.60 \n",
"568 59.16 1.4280 30.37 7.76 \n",
"\n",
"[569 rows x 17 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X[roc_feature]"
]
},
{
"cell_type": "markdown",
"metadata": {
"papermill": {
"duration": 0.024617,
"end_time": "2020-11-21T09:03:00.629433",
"exception": false,
"start_time": "2020-11-21T09:03:00.604816",
"status": "completed"
},
"tags": []
},
"source": [
"# Model Building"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.689579Z",
"iopub.status.busy": "2020-11-21T09:03:00.688063Z",
"iopub.status.idle": "2020-11-21T09:03:00.691995Z",
"shell.execute_reply": "2020-11-21T09:03:00.691382Z"
},
"papermill": {
"duration": 0.037862,
"end_time": "2020-11-21T09:03:00.692142",
"exception": false,
"start_time": "2020-11-21T09:03:00.654280",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.752412Z",
"iopub.status.busy": "2020-11-21T09:03:00.750709Z",
"iopub.status.idle": "2020-11-21T09:03:00.753333Z",
"shell.execute_reply": "2020-11-21T09:03:00.753940Z"
},
"papermill": {
"duration": 0.035607,
"end_time": "2020-11-21T09:03:00.754111",
"exception": false,
"start_time": "2020-11-21T09:03:00.718504",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"class DummyEstimator(BaseEstimator):\n",
" def fit(self): pass\n",
" def score(self): pass"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.825769Z",
"iopub.status.busy": "2020-11-21T09:03:00.823252Z",
"iopub.status.idle": "2020-11-21T09:03:00.826701Z",
"shell.execute_reply": "2020-11-21T09:03:00.827386Z"
},
"papermill": {
"duration": 0.04597,
"end_time": "2020-11-21T09:03:00.827544",
"exception": false,
"start_time": "2020-11-21T09:03:00.781574",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"pipeline = Pipeline([\n",
" ('estimator', DummyEstimator())\n",
"])\n",
"\n",
"lr = {\n",
" 'estimator' : [LogisticRegression()],\n",
" 'estimator__penalty': ['l2'],\n",
" 'estimator__C' : np.arange(1,100, 10),\n",
" 'estimator__random_state' : [0],\n",
" 'estimator__solver' : ['lbfgs', 'liblinear', 'sag', 'saga'],\n",
" 'estimator__max_iter' : np.arange(100,1000, 100),\n",
" 'estimator__n_jobs' : [-1]\n",
"}\n",
"\n",
"sgd ={\n",
" 'estimator' : [SGDClassifier()],\n",
" 'estimator__penalty': ['l1', 'l2', 'elasticnet'],\n",
" 'estimator__alpha' : [i*10**(-exp) for exp in range(0, 5) for i in [1]],\n",
" 'estimator__random_state' : [0],\n",
" 'estimator__learning_rate' : ['constant', 'optimal', 'invscaling', 'adaptive'],\n",
" 'estimator__eta0' : np.arange(1,100, 20),\n",
" 'estimator__max_iter' : np.arange(500,10000, 500),\n",
" 'estimator__n_jobs' : [-1]\n",
"}\n",
"\n",
"est = [lr, sgd]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:03:00.891397Z",
"iopub.status.busy": "2020-11-21T09:03:00.890359Z",
"iopub.status.idle": "2020-11-21T09:14:37.365820Z",
"shell.execute_reply": "2020-11-21T09:14:37.366533Z"
},
"papermill": {
"duration": 696.512769,
"end_time": "2020-11-21T09:14:37.366703",
"exception": false,
"start_time": "2020-11-21T09:03:00.853934",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------Logistic Regression----------------\n",
"Best score : 0.9623417721518986\n",
"Test score 0.9649122807017544\n",
"----------------SGD Classifier----------------\n",
"Best score : 0.9271518987341771\n",
"Test score 0.9298245614035088\n"
]
}
],
"source": [
"estimators = ['Logistic Regression', 'SGD Classifier']\n",
"index = 0\n",
"for estimator in est:\n",
" models = GridSearchCV(estimator= pipeline, param_grid= estimator, n_jobs= -1, cv = 5, verbose= 0)\n",
" models.fit(X_train, Y_train)\n",
" print(\"----------------\" + estimators[index] + \"----------------\")\n",
" print(\"Best score : \", models.best_score_)\n",
" score = models.score(X_test, Y_test)\n",
" print(\"Test score\", score)\n",
" index += 1"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:14:37.418006Z",
"iopub.status.busy": "2020-11-21T09:14:37.417438Z",
"iopub.status.idle": "2020-11-21T09:14:37.500592Z",
"shell.execute_reply": "2020-11-21T09:14:37.499641Z"
},
"papermill": {
"duration": 0.111854,
"end_time": "2020-11-21T09:14:37.500693",
"exception": false,
"start_time": "2020-11-21T09:14:37.388839",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-4b30515680d0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mestimator\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mest\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mestimators\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;34m+=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"\n",
"for estimator in est:\n",
" print(estimators[index])\n",
" index +=1"
]
}
],
"metadata": {
"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.7.6"
},
"papermill": {
"duration": 708.077126,
"end_time": "2020-11-21T09:14:37.642538",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2020-11-21T09:02:49.565412",
"version": "2.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
| 0047/375/47375706.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"execution": {
"iopub.execute_input": "2020-11-21T09:10:11.761851Z",
"iopub.status.busy": "2020-11-21T09:10:11.761004Z",
"iopub.status.idle": "2020-11-21T09:10:11.769382Z",
"shell.execute_reply": "2020-11-21T09:10:11.768749Z"
},
"papermill": {
"duration": 0.020942,
"end_time": "2020-11-21T09:10:11.769521",
"exception": false,
"start_time": "2020-11-21T09:10:11.748579",
"status": "completed"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/kaggle/input/investhack-data-generation/__results__.html\n",
"/kaggle/input/investhack-data-generation/__resultx__.html\n",
"/kaggle/input/investhack-data-generation/__notebook__.ipynb\n",
"/kaggle/input/investhack-data-generation/custom.css\n",
"/kaggle/input/investhack-data-generation/__output__.json\n",
"/kaggle/input/investhack-data-generation/stock_ownership.csv\n",
"/kaggle/input/investhack-data-generation/stock_lookup.csv\n"
]
}
],
"source": [
"# This Python 3 environment comes with many helpful analytics libraries installed\n",
"# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
"# For example, here's several helpful packages to load\n",
"\n",
"import numpy as np # linear algebra\n",
"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
"\n",
"# Input data files are available in the read-only \"../input/\" directory\n",
"# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
"\n",
"import os\n",
"for dirname, _, filenames in os.walk('/kaggle/input'):\n",
" for filename in filenames:\n",
" print(os.path.join(dirname, filename))\n",
"\n",
"# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n",
"# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:11.790141Z",
"iopub.status.busy": "2020-11-21T09:10:11.789209Z",
"iopub.status.idle": "2020-11-21T09:10:11.792278Z",
"shell.execute_reply": "2020-11-21T09:10:11.791690Z"
},
"papermill": {
"duration": 0.014441,
"end_time": "2020-11-21T09:10:11.792389",
"exception": false,
"start_time": "2020-11-21T09:10:11.777948",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"import random as ra\n",
"import math as ma\n",
"\n",
"def sigmoid(x, k=5.0):\n",
" x = x / k * 5\n",
" return 1.0 / (1.0 + ma.exp(-x))\n",
"\n",
"def score(x, k=5.0):\n",
" return sigmoid(x, k) - .5"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
"_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
"execution": {
"iopub.execute_input": "2020-11-21T09:10:11.809274Z",
"iopub.status.busy": "2020-11-21T09:10:11.808703Z",
"iopub.status.idle": "2020-11-21T09:10:11.837722Z",
"shell.execute_reply": "2020-11-21T09:10:11.837096Z"
},
"papermill": {
"duration": 0.039477,
"end_time": "2020-11-21T09:10:11.837832",
"exception": false,
"start_time": "2020-11-21T09:10:11.798355",
"status": "completed"
},
"tags": []
},
"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>Unnamed: 0</th>\n",
" <th>stock_id</th>\n",
" <th>share</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>84</td>\n",
" <td>8081</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>425</td>\n",
" <td>9842</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>212</td>\n",
" <td>8328</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>436</td>\n",
" <td>5791</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>327</td>\n",
" <td>12815</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9995</th>\n",
" <td>9995</td>\n",
" <td>416</td>\n",
" <td>2232</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9996</th>\n",
" <td>9996</td>\n",
" <td>183</td>\n",
" <td>7923</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9997</th>\n",
" <td>9997</td>\n",
" <td>471</td>\n",
" <td>5755</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9998</th>\n",
" <td>9998</td>\n",
" <td>315</td>\n",
" <td>6966</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9999</th>\n",
" <td>9999</td>\n",
" <td>353</td>\n",
" <td>7434</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>10000 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" Unnamed: 0 stock_id share\n",
"0 0 84 8081\n",
"1 1 425 9842\n",
"2 2 212 8328\n",
"3 3 436 5791\n",
"4 4 327 12815\n",
"... ... ... ...\n",
"9995 9995 416 2232\n",
"9996 9996 183 7923\n",
"9997 9997 471 5755\n",
"9998 9998 315 6966\n",
"9999 9999 353 7434\n",
"\n",
"[10000 rows x 3 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ownership = pd.read_csv('/kaggle/input/investhack-data-generation/stock_ownership.csv')\n",
"num_ownership = len(ownership)\n",
"ownership"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:11.856505Z",
"iopub.status.busy": "2020-11-21T09:10:11.855631Z",
"iopub.status.idle": "2020-11-21T09:10:11.875083Z",
"shell.execute_reply": "2020-11-21T09:10:11.874612Z"
},
"papermill": {
"duration": 0.030469,
"end_time": "2020-11-21T09:10:11.875192",
"exception": false,
"start_time": "2020-11-21T09:10:11.844723",
"status": "completed"
},
"tags": []
},
"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>Unnamed: 0</th>\n",
" <th>symbol</th>\n",
" <th>cost</th>\n",
" <th>var_1</th>\n",
" <th>var_2</th>\n",
" <th>sector</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>AAA</td>\n",
" <td>53</td>\n",
" <td>4.827717</td>\n",
" <td>7.034612</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>AAB</td>\n",
" <td>67</td>\n",
" <td>5.943506</td>\n",
" <td>2.930844</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>AAC</td>\n",
" <td>86</td>\n",
" <td>4.198631</td>\n",
" <td>5.383598</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>AAD</td>\n",
" <td>40</td>\n",
" <td>4.009809</td>\n",
" <td>5.978331</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>AAE</td>\n",
" <td>52</td>\n",
" <td>4.260545</td>\n",
" <td>6.710411</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>495</th>\n",
" <td>495</td>\n",
" <td>ATU</td>\n",
" <td>83</td>\n",
" <td>4.479986</td>\n",
" <td>6.867301</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>496</th>\n",
" <td>496</td>\n",
" <td>ATV</td>\n",
" <td>72</td>\n",
" <td>4.336352</td>\n",
" <td>5.786692</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>497</th>\n",
" <td>497</td>\n",
" <td>ATW</td>\n",
" <td>81</td>\n",
" <td>4.962295</td>\n",
" <td>4.084476</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>498</th>\n",
" <td>498</td>\n",
" <td>ATX</td>\n",
" <td>61</td>\n",
" <td>4.505812</td>\n",
" <td>4.784263</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>499</th>\n",
" <td>499</td>\n",
" <td>ATY</td>\n",
" <td>44</td>\n",
" <td>4.887142</td>\n",
" <td>2.186926</td>\n",
" <td>7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>500 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" Unnamed: 0 symbol cost var_1 var_2 sector\n",
"0 0 AAA 53 4.827717 7.034612 0\n",
"1 1 AAB 67 5.943506 2.930844 5\n",
"2 2 AAC 86 4.198631 5.383598 10\n",
"3 3 AAD 40 4.009809 5.978331 7\n",
"4 4 AAE 52 4.260545 6.710411 6\n",
".. ... ... ... ... ... ...\n",
"495 495 ATU 83 4.479986 6.867301 2\n",
"496 496 ATV 72 4.336352 5.786692 9\n",
"497 497 ATW 81 4.962295 4.084476 9\n",
"498 498 ATX 61 4.505812 4.784263 3\n",
"499 499 ATY 44 4.887142 2.186926 7\n",
"\n",
"[500 rows x 6 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lookup = pd.read_csv('/kaggle/input/investhack-data-generation/stock_lookup.csv')\n",
"lookup"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:11.923300Z",
"iopub.status.busy": "2020-11-21T09:10:11.918210Z",
"iopub.status.idle": "2020-11-21T09:10:28.459507Z",
"shell.execute_reply": "2020-11-21T09:10:28.458942Z"
},
"papermill": {
"duration": 16.577262,
"end_time": "2020-11-21T09:10:28.459627",
"exception": false,
"start_time": "2020-11-21T09:10:11.882365",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"# we choose 2 random ownerships and give them a suitability rating\n",
"# in real scenarios, this is learnt from user statistics and historical data\n",
"\n",
"# we will generate 10000 such pairs from 50 billion (50000000) of unique pairings\n",
"\n",
"rows = []\n",
"\n",
"num_pairing = 10000\n",
"\n",
"for i in range(num_pairing):\n",
" A_id = ra.randint(0, len(ownership)-1)\n",
" B_id = A_id\n",
" while B_id == A_id:\n",
" B_id = ra.randint(0, len(ownership)-1)\n",
" \n",
" A_stock = ownership.iloc[A_id]['stock_id']\n",
" A_share = ownership.iloc[A_id]['share']\n",
" A_cap = A_share * lookup.iloc[A_stock]['cost']\n",
" A_var_1 = lookup.iloc[A_stock]['var_1']\n",
" A_var_2 = lookup.iloc[A_stock]['var_2']\n",
" A_sec = lookup.iloc[A_stock]['sector']\n",
" \n",
" B_stock = ownership.iloc[B_id]['stock_id']\n",
" B_share = ownership.iloc[B_id]['share']\n",
" B_cap = B_share * lookup.iloc[B_stock]['cost']\n",
" B_var_1 = lookup.iloc[B_stock]['var_1']\n",
" B_var_2 = lookup.iloc[B_stock]['var_2']\n",
" B_sec = lookup.iloc[B_stock]['sector']\n",
" \n",
" suitability = 0\n",
" if A_sec != B_sec:\n",
" suitability += 1.0\n",
" cap_diff = abs(A_cap - B_cap) / min(A_cap, B_cap)\n",
" d1 = score(1 / sigmoid(cap_diff, 1.2) - 1)\n",
" d2 = score(abs(A_var_1 - B_var_1) / max(A_var_1, B_var_1), 1)\n",
" d3 = score(abs(A_var_2 - B_var_2) / max(A_var_2, B_var_2), 1)\n",
" # max total score is 2.500\n",
" suitability += d1 + d2 + d3\n",
" rows.append([A_cap, A_var_1, A_var_2, A_sec, B_cap, B_var_1, B_var_2, B_sec, d1, d2, d3, suitability])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:28.479040Z",
"iopub.status.busy": "2020-11-21T09:10:28.478431Z",
"iopub.status.idle": "2020-11-21T09:10:28.533878Z",
"shell.execute_reply": "2020-11-21T09:10:28.534391Z"
},
"papermill": {
"duration": 0.067589,
"end_time": "2020-11-21T09:10:28.534551",
"exception": false,
"start_time": "2020-11-21T09:10:28.466962",
"status": "completed"
},
"tags": []
},
"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>A_cap</th>\n",
" <th>A_var_1</th>\n",
" <th>A_var_2</th>\n",
" <th>A_sec</th>\n",
" <th>B_cap</th>\n",
" <th>B_var_1</th>\n",
" <th>B_var_2</th>\n",
" <th>B_sec</th>\n",
" <th>d1</th>\n",
" <th>d2</th>\n",
" <th>d3</th>\n",
" <th>suitability</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>538959</td>\n",
" <td>6.806461</td>\n",
" <td>3.322786</td>\n",
" <td>1</td>\n",
" <td>579348</td>\n",
" <td>3.512311</td>\n",
" <td>3.700824</td>\n",
" <td>7</td>\n",
" <td>0.175201</td>\n",
" <td>0.418330</td>\n",
" <td>0.124982</td>\n",
" <td>1.718513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>612074</td>\n",
" <td>3.366237</td>\n",
" <td>6.245446</td>\n",
" <td>2</td>\n",
" <td>556425</td>\n",
" <td>3.690644</td>\n",
" <td>4.351694</td>\n",
" <td>7</td>\n",
" <td>0.159083</td>\n",
" <td>0.108140</td>\n",
" <td>0.319964</td>\n",
" <td>1.587187</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>993048</td>\n",
" <td>7.964037</td>\n",
" <td>7.087298</td>\n",
" <td>0</td>\n",
" <td>1076580</td>\n",
" <td>4.761106</td>\n",
" <td>7.021851</td>\n",
" <td>8</td>\n",
" <td>0.169150</td>\n",
" <td>0.381934</td>\n",
" <td>0.011541</td>\n",
" <td>1.562625</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>220428</td>\n",
" <td>6.406345</td>\n",
" <td>2.737331</td>\n",
" <td>4</td>\n",
" <td>666997</td>\n",
" <td>6.940455</td>\n",
" <td>3.809786</td>\n",
" <td>5</td>\n",
" <td>0.000054</td>\n",
" <td>0.095025</td>\n",
" <td>0.303371</td>\n",
" <td>1.398451</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>428464</td>\n",
" <td>5.489062</td>\n",
" <td>4.976541</td>\n",
" <td>1</td>\n",
" <td>356048</td>\n",
" <td>5.914751</td>\n",
" <td>5.344931</td>\n",
" <td>5</td>\n",
" <td>0.105517</td>\n",
" <td>0.089005</td>\n",
" <td>0.085311</td>\n",
" <td>1.279833</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9995</th>\n",
" <td>544272</td>\n",
" <td>6.219524</td>\n",
" <td>6.139394</td>\n",
" <td>8</td>\n",
" <td>208964</td>\n",
" <td>4.667888</td>\n",
" <td>4.763949</td>\n",
" <td>3</td>\n",
" <td>0.000312</td>\n",
" <td>0.276848</td>\n",
" <td>0.254022</td>\n",
" <td>1.531182</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9996</th>\n",
" <td>406973</td>\n",
" <td>3.961823</td>\n",
" <td>4.123096</td>\n",
" <td>10</td>\n",
" <td>114750</td>\n",
" <td>4.008947</td>\n",
" <td>6.492599</td>\n",
" <td>0</td>\n",
" <td>0.000006</td>\n",
" <td>0.014689</td>\n",
" <td>0.361138</td>\n",
" <td>1.375833</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9997</th>\n",
" <td>404754</td>\n",
" <td>5.918034</td>\n",
" <td>5.737960</td>\n",
" <td>6</td>\n",
" <td>657094</td>\n",
" <td>4.826608</td>\n",
" <td>4.732532</td>\n",
" <td>8</td>\n",
" <td>0.018603</td>\n",
" <td>0.215474</td>\n",
" <td>0.206017</td>\n",
" <td>1.440094</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9998</th>\n",
" <td>757702</td>\n",
" <td>5.619711</td>\n",
" <td>2.134408</td>\n",
" <td>10</td>\n",
" <td>244412</td>\n",
" <td>4.927811</td>\n",
" <td>4.842467</td>\n",
" <td>3</td>\n",
" <td>0.000040</td>\n",
" <td>0.149217</td>\n",
" <td>0.442468</td>\n",
" <td>1.591725</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9999</th>\n",
" <td>802940</td>\n",
" <td>5.481961</td>\n",
" <td>4.699225</td>\n",
" <td>3</td>\n",
" <td>79815</td>\n",
" <td>4.192213</td>\n",
" <td>3.751413</td>\n",
" <td>7</td>\n",
" <td>0.000000</td>\n",
" <td>0.264292</td>\n",
" <td>0.232722</td>\n",
" <td>1.497014</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>10000 rows × 12 columns</p>\n",
"</div>"
],
"text/plain": [
" A_cap A_var_1 A_var_2 A_sec B_cap B_var_1 B_var_2 B_sec \\\n",
"0 538959 6.806461 3.322786 1 579348 3.512311 3.700824 7 \n",
"1 612074 3.366237 6.245446 2 556425 3.690644 4.351694 7 \n",
"2 993048 7.964037 7.087298 0 1076580 4.761106 7.021851 8 \n",
"3 220428 6.406345 2.737331 4 666997 6.940455 3.809786 5 \n",
"4 428464 5.489062 4.976541 1 356048 5.914751 5.344931 5 \n",
"... ... ... ... ... ... ... ... ... \n",
"9995 544272 6.219524 6.139394 8 208964 4.667888 4.763949 3 \n",
"9996 406973 3.961823 4.123096 10 114750 4.008947 6.492599 0 \n",
"9997 404754 5.918034 5.737960 6 657094 4.826608 4.732532 8 \n",
"9998 757702 5.619711 2.134408 10 244412 4.927811 4.842467 3 \n",
"9999 802940 5.481961 4.699225 3 79815 4.192213 3.751413 7 \n",
"\n",
" d1 d2 d3 suitability \n",
"0 0.175201 0.418330 0.124982 1.718513 \n",
"1 0.159083 0.108140 0.319964 1.587187 \n",
"2 0.169150 0.381934 0.011541 1.562625 \n",
"3 0.000054 0.095025 0.303371 1.398451 \n",
"4 0.105517 0.089005 0.085311 1.279833 \n",
"... ... ... ... ... \n",
"9995 0.000312 0.276848 0.254022 1.531182 \n",
"9996 0.000006 0.014689 0.361138 1.375833 \n",
"9997 0.018603 0.215474 0.206017 1.440094 \n",
"9998 0.000040 0.149217 0.442468 1.591725 \n",
"9999 0.000000 0.264292 0.232722 1.497014 \n",
"\n",
"[10000 rows x 12 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"label = pd.DataFrame(data=rows, columns=['A_cap','A_var_1','A_var_2','A_sec','B_cap','B_var_1','B_var_2','B_sec','d1','d2','d3','suitability'])\n",
"label"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:28.558228Z",
"iopub.status.busy": "2020-11-21T09:10:28.557616Z",
"iopub.status.idle": "2020-11-21T09:10:28.601159Z",
"shell.execute_reply": "2020-11-21T09:10:28.601697Z"
},
"papermill": {
"duration": 0.059327,
"end_time": "2020-11-21T09:10:28.601855",
"exception": false,
"start_time": "2020-11-21T09:10:28.542528",
"status": "completed"
},
"tags": []
},
"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>A_cap</th>\n",
" <th>A_var_1</th>\n",
" <th>A_var_2</th>\n",
" <th>A_sec</th>\n",
" <th>B_cap</th>\n",
" <th>B_var_1</th>\n",
" <th>B_var_2</th>\n",
" <th>B_sec</th>\n",
" <th>d1</th>\n",
" <th>d2</th>\n",
" <th>d3</th>\n",
" <th>suitability</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>1.000000e+04</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>1.000000e+04</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" <td>10000.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>5.121374e+05</td>\n",
" <td>5.010020</td>\n",
" <td>5.091467</td>\n",
" <td>4.968800</td>\n",
" <td>5.099291e+05</td>\n",
" <td>5.023853</td>\n",
" <td>5.054740</td>\n",
" <td>4.952000</td>\n",
" <td>0.049959</td>\n",
" <td>0.199814</td>\n",
" <td>0.293057</td>\n",
" <td>1.452731</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>2.483671e+05</td>\n",
" <td>0.927334</td>\n",
" <td>1.846819</td>\n",
" <td>3.149003</td>\n",
" <td>2.462469e+05</td>\n",
" <td>0.959318</td>\n",
" <td>1.875682</td>\n",
" <td>3.152981</td>\n",
" <td>0.064193</td>\n",
" <td>0.115225</td>\n",
" <td>0.140798</td>\n",
" <td>0.349280</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>1.946000e+03</td>\n",
" <td>2.534369</td>\n",
" <td>0.131894</td>\n",
" <td>0.000000</td>\n",
" <td>1.946000e+03</td>\n",
" <td>2.534369</td>\n",
" <td>0.131894</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>3.307950e+05</td>\n",
" <td>4.381435</td>\n",
" <td>3.790489</td>\n",
" <td>2.000000</td>\n",
" <td>3.316795e+05</td>\n",
" <td>4.362724</td>\n",
" <td>3.708197</td>\n",
" <td>2.000000</td>\n",
" <td>0.000701</td>\n",
" <td>0.102192</td>\n",
" <td>0.181306</td>\n",
" <td>1.355158</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>4.835380e+05</td>\n",
" <td>4.923997</td>\n",
" <td>5.072553</td>\n",
" <td>5.000000</td>\n",
" <td>4.779690e+05</td>\n",
" <td>4.927811</td>\n",
" <td>5.063753</td>\n",
" <td>5.000000</td>\n",
" <td>0.017348</td>\n",
" <td>0.200053</td>\n",
" <td>0.319679</td>\n",
" <td>1.522184</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>6.676670e+05</td>\n",
" <td>5.669419</td>\n",
" <td>6.247837</td>\n",
" <td>8.000000</td>\n",
" <td>6.610612e+05</td>\n",
" <td>5.709178</td>\n",
" <td>6.237358</td>\n",
" <td>8.000000</td>\n",
" <td>0.081866</td>\n",
" <td>0.293117</td>\n",
" <td>0.416685</td>\n",
" <td>1.668729</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>1.627320e+06</td>\n",
" <td>8.073182</td>\n",
" <td>10.656804</td>\n",
" <td>10.000000</td>\n",
" <td>2.175864e+06</td>\n",
" <td>8.073182</td>\n",
" <td>10.656804</td>\n",
" <td>10.000000</td>\n",
" <td>0.231048</td>\n",
" <td>0.457956</td>\n",
" <td>0.492648</td>\n",
" <td>2.123746</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A_cap A_var_1 A_var_2 A_sec B_cap \\\n",
"count 1.000000e+04 10000.000000 10000.000000 10000.000000 1.000000e+04 \n",
"mean 5.121374e+05 5.010020 5.091467 4.968800 5.099291e+05 \n",
"std 2.483671e+05 0.927334 1.846819 3.149003 2.462469e+05 \n",
"min 1.946000e+03 2.534369 0.131894 0.000000 1.946000e+03 \n",
"25% 3.307950e+05 4.381435 3.790489 2.000000 3.316795e+05 \n",
"50% 4.835380e+05 4.923997 5.072553 5.000000 4.779690e+05 \n",
"75% 6.676670e+05 5.669419 6.247837 8.000000 6.610612e+05 \n",
"max 1.627320e+06 8.073182 10.656804 10.000000 2.175864e+06 \n",
"\n",
" B_var_1 B_var_2 B_sec d1 d2 \\\n",
"count 10000.000000 10000.000000 10000.000000 10000.000000 10000.000000 \n",
"mean 5.023853 5.054740 4.952000 0.049959 0.199814 \n",
"std 0.959318 1.875682 3.152981 0.064193 0.115225 \n",
"min 2.534369 0.131894 0.000000 0.000000 0.000000 \n",
"25% 4.362724 3.708197 2.000000 0.000701 0.102192 \n",
"50% 4.927811 5.063753 5.000000 0.017348 0.200053 \n",
"75% 5.709178 6.237358 8.000000 0.081866 0.293117 \n",
"max 8.073182 10.656804 10.000000 0.231048 0.457956 \n",
"\n",
" d3 suitability \n",
"count 10000.000000 10000.000000 \n",
"mean 0.293057 1.452731 \n",
"std 0.140798 0.349280 \n",
"min 0.000000 0.000002 \n",
"25% 0.181306 1.355158 \n",
"50% 0.319679 1.522184 \n",
"75% 0.416685 1.668729 \n",
"max 0.492648 2.123746 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"label.describe()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2020-11-21T09:10:28.624203Z",
"iopub.status.busy": "2020-11-21T09:10:28.623581Z",
"iopub.status.idle": "2020-11-21T09:10:29.008992Z",
"shell.execute_reply": "2020-11-21T09:10:29.008271Z"
},
"papermill": {
"duration": 0.398446,
"end_time": "2020-11-21T09:10:29.009110",
"exception": false,
"start_time": "2020-11-21T09:10:28.610664",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"label.to_csv('labelled_pair.csv',index=False)"
]
}
],
"metadata": {
"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.7.6"
},
"papermill": {
"duration": 21.108568,
"end_time": "2020-11-21T09:10:29.124892",
"environment_variables": {},
"exception": null,
"input_path": "__notebook__.ipynb",
"output_path": "__notebook__.ipynb",
"parameters": {},
"start_time": "2020-11-21T09:10:08.016324",
"version": "2.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
| 0047/376/47376103.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n for filename in filenames:\n print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder, MinMaxScaler\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout\nfrom keras.optimizers import RMSprop,Adam\n\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.linear_model import LogisticRegression\n\nfrom sklearn.metrics import accuracy_score, roc_curve, auc, f1_score\nfrom sklearn.svm import SVC, LinearSVC\n\nimport matplotlib.pyplot as plt\nplt.style.use('bmh')\n\nnames = [\"duration\",\"protocol\",\"service\",\"flag\",\"src_bytes\",\n \"dst_bytes\",\"land\",\"wrong_fragment\",\"urgent\",\"hot\",\n \"num_failed_logins\",\"logged_in\",\"num_compromised\",\n \"root_shell\",\"su_attempted\",\"num_root\",\"num_file_creations\",\n \"num_shells\",\"num_access_files\",\"num_outbound_cmds\",\n \"is_host_login\",\"is_guest_login\",\"count\",\"srv_count\",\n \"serror_rate\",\"srv_serror_rate\",\"rerror_rate\",\"srv_rerror_rate\",\n \"same_srv_rate\",\"diff_srv_rate\",\"srv_diff_host_rate\",\n \"dst_host_count\",\"dst_host_srv_count\",\"dst_host_same_srv_rate\",\n \"dst_host_diff_srv_rate\",\"dst_host_same_src_port_rate\",\"dst_host_srv_diff_host_rate\",\n \"dst_host_serror_rate\",\"dst_host_srv_serr_rate\",\"dst_host_rerror_rate\",\"dst_host_srv_rerror_rate\",\n \"attack_type\",\"other\"]\n\ntrain_path = \"/kaggle/input/nslkdd/NSL-KDD/KDDTrain+.txt\"\ntest_path = \"/kaggle/input/nslkdd/NSL-KDD/KDDTest+.txt\"\n\ndf_train = pd.read_csv(train_path,names=names,header=None)\ndf_test = pd.read_csv(test_path,names=names,header=None)\n\nprint(\"Shapes of training and testing are:\",df_train.shape,df_test.shape)\n\nfull_dataset = pd.concat([df_train,df_test])\nfull_dataset['label'] = full_dataset['attack_type']\n\nfull_dataset.loc[full_dataset.label == 'neptune','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'back','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'land','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'pod','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'smurf','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'teardrop','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'mailbomb','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'processtable','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'udpstorm','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'apache2','label'] = 'DOS'\nfull_dataset.loc[full_dataset.label == 'worm','label'] = 'DOS'\n\nfull_dataset.loc[full_dataset.label == 'buffer_overflow','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'loadmodule','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'perl','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'rootkit','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'sqlattack','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'xterm','label'] = 'U2R'\nfull_dataset.loc[full_dataset.label == 'ps','label'] = 'U2R'\n\nfull_dataset.loc[full_dataset.label == 'ftp_write','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'guess_passwd','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'imap','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'multihop','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'phf','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'spy','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'warezclient','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'warezmaster','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'xlock','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'xsnoop','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'snmpgetattack','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'httptunnel','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'snmpguess','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'sendmail','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'named','label'] = 'R2L'\nfull_dataset.loc[full_dataset.label == 'satan','label'] = 'Probe'\nfull_dataset.loc[full_dataset.label == 'ipsweep','label'] = 'Probe'\nfull_dataset.loc[full_dataset.label == 'nmap','label'] = 'Probe'\nfull_dataset.loc[full_dataset.label == 'portsweep','label'] = 'Probe'\nfull_dataset.loc[full_dataset.label == 'saint','label'] = 'Probe'\nfull_dataset.loc[full_dataset.label == 'mscan','label'] = 'Probe'\n\nfull_dataset = full_dataset.drop(['other','attack_type'],axis=1)\nprint(\"Unique Labels\",full_dataset.label.unique())\nfull_dataset_svm = full_dataset.copy()\n\n#One Hot Encoding\nfull_dataset = pd.get_dummies(full_dataset,drop_first=False)\n#Train test split\nfeatures = list(full_dataset.columns[:-5])\ny_train = np.array(full_dataset[:df_train.shape[0]][['label_normal','label_DOS','label_Probe','label_R2L','label_U2R']])\nX_train = full_dataset[:df_train.shape[0]][features]\n\ny_test = np.array(full_dataset[:df_test.shape[0]][['label_normal','label_DOS','label_Probe','label_R2L','label_U2R']])\nX_test = full_dataset[:df_test.shape[0]][features]\n\n#Scaling data\nscaler = MinMaxScaler().fit(X_train)\nX_train_scaled = np.array(scaler.transform(X_train))\nX_test_scaled = np.array(scaler.transform(X_test))\n\nprint()\n\n","execution_count":1,"outputs":[{"output_type":"stream","text":"Shapes of training and testing are: (125973, 43) (22544, 43)\nUnique Labels ['normal' 'DOS' 'R2L' 'Probe' 'U2R']\n\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install mlrose\n!pip install six\n","execution_count":8,"outputs":[{"output_type":"stream","text":"Requirement already satisfied: mlrose in /opt/conda/lib/python3.7/site-packages (1.3.0)\nRequirement already satisfied: numpy in /opt/conda/lib/python3.7/site-packages (from mlrose) (1.18.5)\nRequirement already satisfied: sklearn in /opt/conda/lib/python3.7/site-packages (from mlrose) (0.0)\nRequirement already satisfied: scipy in /opt/conda/lib/python3.7/site-packages (from mlrose) (1.4.1)\nRequirement already satisfied: scikit-learn in /opt/conda/lib/python3.7/site-packages (from sklearn->mlrose) (0.23.2)\nRequirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn->sklearn->mlrose) (2.1.0)\nRequirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.7/site-packages (from scikit-learn->sklearn->mlrose) (0.14.1)\nRequirement already satisfied: six in /opt/conda/lib/python3.7/site-packages (1.14.0)\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sklearn\nimport six\nimport sys\nsys.modules['sklearn.externals.six'] = six\nimport mlrose\nnn_model1 = mlrose.NeuralNetwork(hidden_nodes = [2], activation = 'relu', \\\n algorithm = 'random_hill_climb', max_iters = 1000, \\\n bias = True, is_classifier = True, learning_rate = 0.0001, \\\n early_stopping = True, clip_max = 5, max_attempts = 100, \\\n random_state = 3)\n\nnn_model1.fit(X_train_scaled, y_train)","execution_count":11,"outputs":[{"output_type":"execute_result","execution_count":11,"data":{"text/plain":"NeuralNetwork(clip_max=5, early_stopping=True, hidden_nodes=[2],\n learning_rate=0.0001, max_iters=1000)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\n# Predict labels for train set and assess accuracy\ny_train_pred = nn_model1.predict(X_train_scaled)\n\ny_train_accuracy = accuracy_score(y_train, y_train_pred)\n\nprint(y_train_accuracy)\n\n# Predict labels for test set and assess accuracy\ny_test_pred = nn_model1.predict(X_test_scaled)\n\ny_test_accuracy = accuracy_score(y_test, y_test_pred)\n\nprint(y_test_accuracy)","execution_count":12,"outputs":[{"output_type":"stream","text":"0.22703277686488374\n0.22711142654364797\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"nn_model1 = mlrose.NeuralNetwork(hidden_nodes = [2], activation = 'relu', \\\n algorithm = 'simulated_annealing', max_iters = 1000, \\\n bias = True, is_classifier = True, learning_rate = 0.0001, \\\n early_stopping = True, clip_max = 5, max_attempts = 100, \\\n random_state = 3)\n\nnn_model1.fit(X_train_scaled, y_train)\nfrom sklearn.metrics import accuracy_score\n\n# Predict labels for train set and assess accuracy\ny_train_pred = nn_model1.predict(X_train_scaled)\n\ny_train_accuracy = accuracy_score(y_train, y_train_pred)\n\nprint(y_train_accuracy)\n\n# Predict labels for test set and assess accuracy\ny_test_pred = nn_model1.predict(X_test_scaled)\n\ny_test_accuracy = accuracy_score(y_test, y_test_pred)\n\nprint(y_test_accuracy)","execution_count":13,"outputs":[{"output_type":"stream","text":"0.226683495669707\n0.2266678495386799\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"nn_model1 = mlrose.NeuralNetwork(hidden_nodes = [2], activation = 'relu', \\\n algorithm = 'genetic_alg', max_iters = 2, \\\n bias = True, is_classifier = True, learning_rate = 0.0001, \\\n early_stopping = True, clip_max = 5, max_attempts = 100, \\\n random_state = 3)\n\nnn_model1.fit(X_train_scaled, y_train)\nfrom sklearn.metrics import accuracy_score\n\n# Predict labels for train set and assess accuracy\ny_train_pred = nn_model1.predict(X_train_scaled)\n\ny_train_accuracy = accuracy_score(y_train, y_train_pred)\n\nprint(y_train_accuracy)\n\n# Predict labels for test set and assess accuracy\ny_test_pred = nn_model1.predict(X_test_scaled)\n\ny_test_accuracy = accuracy_score(y_test, y_test_pred)\n\nprint(y_test_accuracy)","execution_count":17,"outputs":[{"output_type":"stream","text":"0.5090217745072356\n0.5087384669978708\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":4} | 0047/376/47376138.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
"{\"cells\":[{\"metadata\":{\"_cell_guid\":\"b1076dfc-b9ad-4769-8c92-a6c4dae69d19\",\"_uuid\":\"8f28(...TRUNCATED) | 0047/376/47376230.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
"{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"metadata\": {\(...TRUNCATED) | 0047/376/47376545.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
"{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"metadata\": {\(...TRUNCATED) | 0047/376/47376623.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
"{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"metadata\": {\(...TRUNCATED) | 0047/376/47376691.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
"{\n \"cells\": [\n {\n \"cell_type\": \"markdown\",\n \"metadata\": {\n \"papermill\": {\n (...TRUNCATED) | 0047/376/47376872.ipynb | s3://data-agents/kaggle-outputs/sharded/016_00047.jsonl.gz |
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 26