{ "metadata": { "kernelspec": { "name": "python", "display_name": "Pyolite", "language": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" }, "toc-showcode": false }, "nbformat_minor": 4, "nbformat": 4, "cells": [ { "cell_type": "markdown", "source": "
\n \n
\n \n
sklearn does this automatically, but we can verify the prediction step, we fit the model:\n",
"metadata": {}
},
{
"cell_type": "code",
"source": "lr = LogisticRegression(random_state=0).fit(X, y)",
"metadata": {
"trusted": true
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "markdown",
"source": "We generate the probability using the method predict .\n",
"metadata": {}
},
{
"cell_type": "code",
"source": "yhat =lr.predict(X)\naccuracy_score(yhat,softmax_prediction)",
"metadata": {
"trusted": true
},
"execution_count": 15,
"outputs": [
{
"execution_count": 15,
"output_type": "execute_result",
"data": {
"text/plain": "1.0"
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": "We can't use Softmax regression for SVMs let explore two methods of Multi-class Classification. that we can apply to SVM.\n",
"metadata": {}
},
{
"cell_type": "markdown",
"source": "## SVM\n",
"metadata": {}
},
{
"cell_type": "markdown",
"source": "Sklean performs Multi-class Classification automatically, we can apply the method and calculate the accuracy. Train a SVM classifier with the `kernel` set to `linear`, `gamma` set to `0.5`, and the `probability` paramter set to `True`, then train the model using the `X` and `y` data.\n",
"metadata": {}
},
{
"cell_type": "code",
"source": "model = SVC(kernel='linear', gamma=.5, probability=True)\n\nmodel.fit(X,y)",
"metadata": {
"trusted": true
},
"execution_count": 17,
"outputs": [
{
"execution_count": 17,
"output_type": "execute_result",
"data": {
"text/plain": "SVC(gamma=0.5, kernel='linear', probability=True)"
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": "my_models. For each class we take the class samples we would like to classify, and the rest will be labelled as a dummy class. We repeat the process for each class. For each classifier, we plot the decision regions. The class we are interested in is in red, and the dummy class is in blue. Similarly, the class samples are marked in blue, and the dummy samples are marked with a black x.\n",
"metadata": {}
},
{
"cell_type": "code",
"source": "#dummy class\ndummy_class=y.max()+1\n#list used for classifiers \nmy_models=[]\n#iterate through each class\nfor class_ in np.unique(y):\n #select the index of our class\n select=(y==class_)\n temp_y=np.zeros(y.shape)\n #class, we are trying to classify \n temp_y[y==class_]=class_\n #set other samples to a dummy class \n temp_y[y!=class_]=dummy_class\n #Train model and add to list \n model=SVC(kernel='linear', gamma=.5, probability=True) \n my_models.append(model.fit(X,temp_y))\n #plot decision boundary \n decision_boundary (X,temp_y,model,iris)\n",
"metadata": {
"trusted": true
},
"execution_count": 20,
"outputs": [
{
"name": "stdout",
"text": "{0, 1, 2}\n",
"output_type": "stream"
},
{
"output_type": "display_data",
"data": {
"text/plain": "| \n | 0 and 1 | \n0 and 2 | \n1 and 2 | \n
|---|---|---|---|
| 0 | \n0 | \n0 | \n1 | \n
| 1 | \n0 | \n0 | \n1 | \n
| 2 | \n0 | \n0 | \n1 | \n
| 3 | \n0 | \n0 | \n1 | \n
| 4 | \n0 | \n0 | \n1 | \n
| 5 | \n0 | \n0 | \n1 | \n
| 6 | \n0 | \n0 | \n1 | \n
| 7 | \n0 | \n0 | \n1 | \n
| 8 | \n0 | \n0 | \n1 | \n
| 9 | \n0 | \n0 | \n1 | \n