{ "cells": [ { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2026-10-01T14:23:10.794715300Z", "start_time": "2026-10-01T14:23:10.677571100Z" } }, "source": [ "from sklearn.svm import LinearSVC\n", "import numpy as np" ], "outputs": [], "execution_count": 27 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:23:10.901047600Z", "start_time": "2026-10-01T14:23:10.847255400Z" } }, "cell_type": "code", "source": [ "flower_data = np.loadtxt('data/iris.csv', delimiter=',')\n", "inverse = flower_data.T\n", "X = inverse[:-1]\n", "T = inverse[-1]" ], "id": "b4e863de5bc956", "outputs": [], "execution_count": 28 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:23:11.054318400Z", "start_time": "2026-10-01T14:23:10.907043600Z" } }, "cell_type": "code", "source": [ "two_features = X[:2].T\n", "print(two_features.shape)" ], "id": "bcd23c414c49a88e", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 2)\n" ] } ], "execution_count": 29 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:23:11.153029400Z", "start_time": "2026-10-01T14:23:11.085339100Z" } }, "cell_type": "code", "source": [ "clf = LinearSVC()\n", "\n", "clf.fit(two_features,T.T)" ], "id": "dcb75df28cbf865f", "outputs": [ { "data": { "text/plain": [ "LinearSVC()" ], "text/html": [ "
LinearSVC()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 30 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:23:11.380597900Z", "start_time": "2026-10-01T14:23:11.187662800Z" } }, "cell_type": "code", "source": [ "y = clf.predict(two_features)\n", "print(y)" ], "id": "2e0e66f9f3a60d87", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.\n", " 0. 0. 2. 2. 2. 1. 2. 1. 2. 1. 2. 1. 1. 2. 1. 2. 1. 2. 1. 1. 1. 1. 2. 1.\n", " 1. 1. 2. 2. 2. 2. 2. 1. 1. 1. 1. 1. 0. 0. 2. 1. 1. 1. 1. 2. 1. 1. 1. 1.\n", " 1. 2. 1. 1. 2. 1. 2. 2. 2. 2. 1. 2. 2. 2. 2. 2. 2. 1. 1. 2. 2. 2. 2. 1.\n", " 2. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 1. 2. 2. 2. 2. 2. 2. 2. 1. 2.\n", " 2. 2. 1. 2. 2. 2.]\n" ] } ], "execution_count": 31 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:26:59.459276400Z", "start_time": "2026-10-01T14:26:59.386191100Z" } }, "cell_type": "code", "source": "index = y==T", "id": "ea068a1b9d64787d", "outputs": [], "execution_count": 39 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:38:15.112322Z", "start_time": "2026-10-01T14:38:14.936347100Z" } }, "cell_type": "code", "source": "two_features.shape", "id": "80e4213e33d6c2", "outputs": [ { "data": { "text/plain": [ "(150, 2)" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 58 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:38:20.409051600Z", "start_time": "2026-10-01T14:38:20.245872400Z" } }, "cell_type": "code", "source": "T.T.shape", "id": "8f18658e9536dcf0", "outputs": [ { "data": { "text/plain": [ "(150,)" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 59 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:38:42.682356Z", "start_time": "2026-10-01T14:38:42.348831900Z" } }, "cell_type": "code", "source": "np.concatenate((two_features, T), axis=1)", "id": "68d556d64d2ad82f", "outputs": [ { "ename": "ValueError", "evalue": "all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)", "output_type": "error", "traceback": [ "\u001B[31m---------------------------------------------------------------------------\u001B[39m", "\u001B[31mValueError\u001B[39m Traceback (most recent call last)", "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[62]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m np.concatenate((two_features, T), axis=\u001B[32m1\u001B[39m)\n", "\u001B[31mValueError\u001B[39m: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)" ] } ], "execution_count": 62 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:28:17.348307800Z", "start_time": "2026-10-01T14:28:17.266740100Z" } }, "cell_type": "code", "source": "two_features[:,index==False]", "id": "3906d80d8bb319ee", "outputs": [ { "data": { "text/plain": [ "array([[4.5, 7. , 6.4, 6.9, 6.5, 6.3, 6.6, 5.9, 6.1, 6.7, 5.9, 6.4, 6.6,\n", " 6.8, 6.7, 6. , 5.4, 6. , 6.7, 6.1, 6.2, 5.8, 4.9, 5.7, 5.8, 6. ,\n", " 5.6, 6.1, 5.8, 6.3],\n", " [2.3, 3.2, 3.2, 3.1, 2.8, 3.3, 2.9, 3. , 2.9, 3.1, 3.2, 2.9, 3. ,\n", " 2.8, 3. , 2.9, 3. , 3.4, 3.1, 3. , 2.9, 2.7, 2.5, 2.5, 2.8, 2.2,\n", " 2.8, 2.6, 2.7, 2.5],\n", " [1.3, 4.7, 4.5, 4.9, 4.6, 4.7, 4.6, 4.2, 4.7, 4.4, 4.8, 4.3, 4.4,\n", " 4.8, 5. , 4.5, 4.5, 4.5, 4.7, 4.6, 4.3, 5.1, 4.5, 5. , 5.1, 5. ,\n", " 4.9, 5.6, 5.1, 5. ],\n", " [0.3, 1.4, 1.5, 1.5, 1.5, 1.6, 1.3, 1.5, 1.4, 1.4, 1.8, 1.3, 1.4,\n", " 1.4, 1.7, 1.5, 1.5, 1.6, 1.5, 1.4, 1.3, 1.9, 1.7, 2. , 2.4, 1.5,\n", " 2. , 1.4, 1.9, 1.9]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 43 }, { "metadata": { "ExecuteTime": { "end_time": "2026-10-01T14:26:05.035597400Z", "start_time": "2026-10-01T14:26:04.961584600Z" } }, "cell_type": "code", "source": "", "id": "93e92a26c6f8690b", "outputs": [ { "data": { "text/plain": [ "array([[4.9, 4.6],\n", " [3. , 3.1],\n", " [1.4, 1.5],\n", " [0.2, 0.2]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 38 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "f8fd7c58418b5620" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }