190 lines
4.8 KiB
Plaintext
190 lines
4.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "OqblfyiOsa03"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "RCtgADNqtaVd"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"from sklearn.linear_model import *\n",
|
|
"from sklearn.tree import *\n",
|
|
"from sklearn.naive_bayes import *\n",
|
|
"from sklearn.neighbors import *\n",
|
|
"from sklearn.metrics import accuracy_score\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"%matplotlib inline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"id": "qg6BvpkHt2if"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"dataset= pd.read_csv('datasets2/network-logs.csv')\n",
|
|
"samples = dataset.iloc[:, [1,2]].values\n",
|
|
"targets = dataset['ANOMALY'].values\n",
|
|
"training_samples, testing_samples, training_targets, testing_targets = train_test_split(samples, targets, test_size = 0.3, random_state = 0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "qOBEmoQuuohe",
|
|
"outputId": "4dc3954f-2346-4b38-f2e2-14ccd7ab6c6a"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#K-Nearest neighbors model\n",
|
|
"knc = KNeighborsClassifier(n_neighbors= 2)\n",
|
|
"knc.fit(training_samples, training_targets)\n",
|
|
"knc_prediction = knc.predict(testing_samples)\n",
|
|
"knc_accuracy = 100.0 * accuracy_score(testing_targets, knc_prediction)\n",
|
|
"print(\"K-Nearest Neighbours accuracy: \" + str(knc_accuracy))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.metrics import classification_report , confusion_matrix\n",
|
|
"\n",
|
|
"print(\"Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(testing_targets, knc_prediction))\n",
|
|
"\n",
|
|
"print(\"\\nClassification Report:\")\n",
|
|
"print(classification_report(testing_targets, knc_prediction))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "40I7cKhEvJ5i",
|
|
"outputId": "bf6f93c6-b3ee-467f-c6f0-41fc47dc9cbd"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Decision tree model\n",
|
|
"dtc = DecisionTreeClassifier(random_state=0)\n",
|
|
"dtc.fit(training_samples, training_targets)\n",
|
|
"dtc_prediction = dtc.predict(testing_samples)\n",
|
|
"dtc_accuracy = 100.0 * accuracy_score(testing_targets, dtc_prediction)\n",
|
|
"print(\"Decision Tree Accuracy: \" + str(dtc_accuracy))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(testing_targets, dtc_prediction))\n",
|
|
"\n",
|
|
"print(\"\\nClassification Report:\")\n",
|
|
"print(classification_report(testing_targets, dtc_prediction))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "_VSTLGPCvrla",
|
|
"outputId": "fd0bb5c4-e8e4-42f2-b5c3-5541e64a31f4"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Gaussian Naive Bayes Model\n",
|
|
"gnb = GaussianNB()\n",
|
|
"gnb.fit(training_samples, training_targets)\n",
|
|
"gnb_prediction = gnb.predict(testing_samples)\n",
|
|
"gnb_accuracy = 100 * accuracy_score(testing_targets, gnb_prediction)\n",
|
|
"print(\"Gaussian Naive Bayes accuracy: \" + str(gnb_accuracy))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(testing_targets, gnb_prediction))\n",
|
|
"\n",
|
|
"print(\"\\nClassification Report:\")\n",
|
|
"print(classification_report(testing_targets, gnb_prediction))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "botnet detection with ML Udemy.ipynb",
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.8"
|
|
},
|
|
"widgets": {
|
|
"application/vnd.jupyter.widget-state+json": {
|
|
"state": {},
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|