Created
June 16, 2019 02:33
-
-
Save tungaqhd/a7a2126d923b92d567959424f635bb1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from sklearn.model_selection import train_test_split\n", | |
"from sklearn.metrics import accuracy_score\n", | |
"from sklearn.model_selection import GridSearchCV" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = pd.read_csv('data.csv', header=None)\n", | |
"X = np.array(data.iloc[:, [0, 1]])\n", | |
"y = np.array(data.iloc[:, 2])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def rs(result, y):\n", | |
" print(result)\n", | |
" print(y)\n", | |
" print(accuracy_score(result, y))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# SVM" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.svm import SVC" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## GridSearch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model = SVC(gamma='auto')\n", | |
"parameters = {\n", | |
" 'C' : [1, 2, 3, 4, 5],\n", | |
" 'kernel' : ['linear', 'poly', 'rbf', 'sigmoid'],\n", | |
" 'decision_function_shape' : ['ovo', 'ovr'],\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'C': 1, 'decision_function_shape': 'ovo', 'kernel': 'linear'}" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"gr = GridSearchCV(model, parameters, cv=4)\n", | |
"gr.fit(X_train, y_train)\n", | |
"gr.best_params_" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Predict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]\n", | |
"[1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0]\n", | |
"0.85\n" | |
] | |
} | |
], | |
"source": [ | |
"model = SVC(C=1, decision_function_shape='ovo', kernel='linear')\n", | |
"model.fit(X_train, y_train)\n", | |
"svm_result = model.predict(X_test)\n", | |
"rs(svm_result, y_test)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Decision Tree" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.tree import DecisionTreeClassifier" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## GridSearch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'presort': True, 'splitter': 'best'}" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model = DecisionTreeClassifier()\n", | |
"parameters = {\n", | |
" 'splitter' : ['random', 'best'],\n", | |
" 'presort' : [True, False]\n", | |
"}\n", | |
"gr = GridSearchCV(model, parameters, cv=4, iid=True)\n", | |
"gr.fit(X_train, y_train)\n", | |
"gr.best_params_" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Predict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0]\n", | |
"[1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0]\n", | |
"1.0\n" | |
] | |
} | |
], | |
"source": [ | |
"model = DecisionTreeClassifier(presort=True, splitter='best')\n", | |
"model.fit(X_train, y_train)\n", | |
"tree_result = model.predict(X_test)\n", | |
"rs(tree_result, y_test)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# MLPClassifier" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sklearn.neural_network import MLPClassifier" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## GridSearch" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"model = MLPClassifier(hidden_layer_sizes=76)\n", | |
"parameters = {\n", | |
" 'activation' : ['identity', 'logistic', 'tanh', 'relu'],\n", | |
" 'solver' : ['lbfgs', 'sgd', 'adam'],\n", | |
" 'learning_rate' : ['constant', 'invscaling', 'adaptive'],\n", | |
" 'shuffle' : [True, False],\n", | |
" 'momentum' : [0.5, 0.6, 0.7, 0.8, 0.9, 1]\n", | |
"}\n", | |
"gr = GridSearchCV(model, parameters, cv=4)\n", | |
"gr.fit(X_train, y_train)\n", | |
"gr.best_params_" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Google Colab:\n", | |
"{'activation': 'tanh',\n", | |
" 'learning_rate': 'adaptive',\n", | |
" 'momentum': 1,\n", | |
" 'shuffle': True,\n", | |
" 'solver': 'lbfgs'}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Predict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 0]\n", | |
"[1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0]\n", | |
"0.9\n" | |
] | |
} | |
], | |
"source": [ | |
"model = MLPClassifier(hidden_layer_sizes=76, activation='tanh', learning_rate='adaptive', momentum=1, shuffle=True, solver='lbfgs')\n", | |
"model.fit(X_train, y_train)\n", | |
"pla_result = model.predict(X_test)\n", | |
"rs(pla_result, y_test)" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment