| @@ -80,7 +80,7 @@ | |||||
| "import numpy as np\n", | "import numpy as np\n", | ||||
| "import matplotlib.pyplot as plt\n", | "import matplotlib.pyplot as plt\n", | ||||
| "\n", | "\n", | ||||
| "# data generation\n", | |||||
| "# data generation (FIXME: feature name not good)\n", | |||||
| "np.random.seed(314)\n", | "np.random.seed(314)\n", | ||||
| "data_size_1 = 300\n", | "data_size_1 = 300\n", | ||||
| "x1_1 = np.random.normal(loc=5.0, scale=1.0, size=data_size_1)\n", | "x1_1 = np.random.normal(loc=5.0, scale=1.0, size=data_size_1)\n", | ||||
| @@ -127,7 +127,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 4, | |||||
| "execution_count": 5, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -147,6 +147,7 @@ | |||||
| "\n", | "\n", | ||||
| "def knn_vote(ys):\n", | "def knn_vote(ys):\n", | ||||
| " vote_dict = {}\n", | " vote_dict = {}\n", | ||||
| " \n", | |||||
| " for y in ys:\n", | " for y in ys:\n", | ||||
| " if y not in vote_dict.keys():\n", | " if y not in vote_dict.keys():\n", | ||||
| " vote_dict[y] = 1\n", | " vote_dict[y] = 1\n", | ||||
| @@ -166,13 +167,14 @@ | |||||
| " \n", | " \n", | ||||
| "\n", | "\n", | ||||
| "#a = knn_predict(x_train[0], x_train, y_train)\n", | "#a = knn_predict(x_train[0], x_train, y_train)\n", | ||||
| "\n", | |||||
| "y_train_est = [knn_predict(x_train[i], x_train, y_train) for i in range(len(x_train))]\n", | "y_train_est = [knn_predict(x_train[i], x_train, y_train) for i in range(len(x_train))]\n", | ||||
| "print(y_train_est)" | "print(y_train_est)" | ||||
| ] | ] | ||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 5, | |||||
| "execution_count": 6, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -194,7 +196,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 6, | |||||
| "execution_count": 7, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -224,7 +226,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 7, | |||||
| "execution_count": 9, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [], | "outputs": [], | ||||
| "source": [ | "source": [ | ||||
| @@ -278,7 +280,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 17, | |||||
| "execution_count": 10, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -298,6 +300,8 @@ | |||||
| "# knn classifier\n", | "# knn classifier\n", | ||||
| "clf = KNN(k=3)\n", | "clf = KNN(k=3)\n", | ||||
| "train_acc = clf.fit(x_train, y_train).score() * 100.0\n", | "train_acc = clf.fit(x_train, y_train).score() * 100.0\n", | ||||
| "\n", | |||||
| "y_test_pred = clf.predict(x_test)\n", | |||||
| "test_acc = clf.score(y_test, y_test_pred) * 100.0\n", | "test_acc = clf.score(y_test, y_test_pred) * 100.0\n", | ||||
| "\n", | "\n", | ||||
| "print('train accuracy: %f %%' % train_acc)\n", | "print('train accuracy: %f %%' % train_acc)\n", | ||||
| @@ -313,7 +317,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 33, | |||||
| "execution_count": 11, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -342,7 +346,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 34, | |||||
| "execution_count": 12, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -371,7 +375,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 36, | |||||
| "execution_count": 13, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [], | "outputs": [], | ||||
| "source": [ | "source": [ | ||||
| @@ -387,7 +391,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 37, | |||||
| "execution_count": 14, | |||||
| "metadata": {}, | "metadata": {}, | ||||
| "outputs": [ | "outputs": [ | ||||
| { | { | ||||
| @@ -1,3 +1,7 @@ | |||||
| ## 内容 | |||||
| 增加一个Bag of Words的说明和例子程序 (https://blog.csdn.net/wsj998689aa/article/details/47089153) | |||||
| ## References | ## References | ||||
| @@ -24,7 +24,7 @@ | |||||
| "name": "python", | "name": "python", | ||||
| "nbconvert_exporter": "python", | "nbconvert_exporter": "python", | ||||
| "pygments_lexer": "ipython3", | "pygments_lexer": "ipython3", | ||||
| "version": "3.6.8" | |||||
| "version": "3.6.9" | |||||
| } | } | ||||
| }, | }, | ||||
| "nbformat": 4, | "nbformat": 4, | ||||
| @@ -119,17 +119,22 @@ | |||||
| "\n", | "\n", | ||||
| "\n", | "\n", | ||||
| "算法\n", | "算法\n", | ||||
| "```\n", | |||||
| "\n", | |||||
| "\n", | |||||
| "输入:T={(x1,y1),(x2,y2)...(xN,yN)}(其中xi∈X=Rn,yi∈Y={-1, +1},i=1,2...N,学习速率为η)\n", | "输入:T={(x1,y1),(x2,y2)...(xN,yN)}(其中xi∈X=Rn,yi∈Y={-1, +1},i=1,2...N,学习速率为η)\n", | ||||
| "\n", | |||||
| "输出:w, b;感知机模型f(x)=sign(w·x+b)\n", | "输出:w, b;感知机模型f(x)=sign(w·x+b)\n", | ||||
| "(1) 初始化w0,b0\n", | |||||
| "(2) 在训练数据集中选取(xi, yi)\n", | |||||
| "(3) 如果yi(w * xi+b)≤0\n", | |||||
| " w = w + ηyixi\n", | |||||
| " b = b + ηyi\n", | |||||
| "(4) 如果所有的样本都正确分类,或者迭代次数超过设定值,则终止\n", | |||||
| "(5) 否则,跳转至(2)\n", | |||||
| "```\n", | |||||
| "\n", | |||||
| "1. 初始化$w_0$,$b_0$\n", | |||||
| "2. 在训练数据集中选取$(x_i, y_i)$\n", | |||||
| "3. 如果$y_i(w * x_i+b)≤0$\n", | |||||
| " \n", | |||||
| " $w = w + η y_i x_i$\n", | |||||
| " \n", | |||||
| " $b = b + η y_i$\n", | |||||
| "\n", | |||||
| "4. 如果所有的样本都正确分类,或者迭代次数超过设定值,则终止\n", | |||||
| "5. 否则,跳转至(2)\n", | |||||
| "\n" | "\n" | ||||
| ] | ] | ||||
| }, | }, | ||||
| @@ -142,7 +147,7 @@ | |||||
| }, | }, | ||||
| { | { | ||||
| "cell_type": "code", | "cell_type": "code", | ||||
| "execution_count": 3, | |||||
| "execution_count": 1, | |||||
| "metadata": { | "metadata": { | ||||
| "lines_to_end_of_cell_marker": 2 | "lines_to_end_of_cell_marker": 2 | ||||
| }, | }, | ||||
| @@ -151,13 +156,10 @@ | |||||
| "name": "stdout", | "name": "stdout", | ||||
| "output_type": "stream", | "output_type": "stream", | ||||
| "text": [ | "text": [ | ||||
| "update weight and bias: 1.0 2.5 0.5\n", | |||||
| "update weight and bias: -2.5 1.0 0.0\n", | |||||
| "update weight and bias: -1.5 3.5 0.5\n", | |||||
| "update weight and bias: -5.0 2.0 0.0\n", | |||||
| "update weight and bias: -4.0 4.5 0.5\n", | |||||
| "w = [-4.0, 4.5]\n", | |||||
| "b = 0.5\n", | |||||
| "update weight and bias: 1.0 3.0 0.5\n", | |||||
| "update weight and bias: -2.5 1.5 0.0\n", | |||||
| "w = [-2.5, 1.5]\n", | |||||
| "b = 0.0\n", | |||||
| "ground_truth: [1, 1, 1, 1, -1, -1, -1, -1]\n", | "ground_truth: [1, 1, 1, 1, -1, -1, -1, -1]\n", | ||||
| "predicted: [1, 1, 1, 1, -1, -1, -1, -1]\n" | "predicted: [1, 1, 1, 1, -1, -1, -1, -1]\n" | ||||
| ] | ] | ||||
| @@ -248,7 +250,7 @@ | |||||
| "name": "python", | "name": "python", | ||||
| "nbconvert_exporter": "python", | "nbconvert_exporter": "python", | ||||
| "pygments_lexer": "ipython3", | "pygments_lexer": "ipython3", | ||||
| "version": "3.6.8" | |||||
| "version": "3.6.9" | |||||
| } | } | ||||
| }, | }, | ||||
| "nbformat": 4, | "nbformat": 4, | ||||