{"id":2112,"date":"2019-10-30T20:18:15","date_gmt":"2019-10-30T12:18:15","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2112"},"modified":"2019-10-30T20:18:15","modified_gmt":"2019-10-30T12:18:15","slug":"%e3%80%90%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e3%80%91%e5%bc%82%e5%b8%b8%e6%a3%80%e6%b5%8b%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/machine-learning-in-action\/2112\/","title":{"rendered":"\u3010\u5434\u6069\u8fbe\u673a\u5668\u5b66\u4e60\u3011\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5"},"content":{"rendered":"\n<p> \u5434\u6069\u8fbeMachine-Learning \u7b2c\u4e5d\u5468\uff1a\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\uff08anomaly detection\uff09  <\/p>\n\n\n<p>\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\u4e3b\u8981\u7528\u6765\u76d1\u6d4b\u6570\u503c\u4e2d\u7684\u5f02\u5e38\u503c\uff0c\u901a\u8fc7\u9ad8\u65af\u5206\u5e03\uff0c\u5c06\u6570\u636e\u6620\u5c04\u5728\u5982\u4e0b\u4e09\u7ef4\u6216\u8005\u4e8c\u7ef4\u4e0a\uff0c\u7136\u540e\u9009\u62e9\u51fa\u5f02\u5e38\u503c\u3002<\/p>\n\n\n<div class=\"wp-block-columns alignwide has-2-columns is-layout-flex wp-container-core-columns-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"254\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-137.png\" alt=\"\" class=\"wp-image-2114\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-137.png 402w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-137-300x190.png 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n<p><\/p>\n<\/div>\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"156\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-138.png\" alt=\"\" class=\"wp-image-2115\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n<p>\u5bf9\u4e8e\u666e\u901a\u7684\u9ad8\u65af\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u9ad8\u65af\u51fd\u6570\u7684\u53c2\u6570\u6765\u4f7f\u9ad8\u65af\u51fd\u6570\u53ef\u4ee5\u66f4\u597d\u7684\u62df\u5408\u6570\u636e\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"382\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-139.png\" alt=\"\" class=\"wp-image-2117\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-139.png 685w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-139-300x167.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n\n\n<p>\u800c\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u5f0f\u89e3\u51fa\uff1a<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"180\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-145.png\" alt=\"\" class=\"wp-image-2123\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-145.png 363w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-145-300x149.png 300w\" sizes=\"(max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n<p>\u7136\u540e\u901a\u8fc7\u4e0b\u5f0f\uff0c\u5c31\u53ef\u4ee5\u5c06\u6570\u636e\u5728\u9ad8\u65af\u51fd\u6570\u4e2d\u7684\u6620\u5c04\u83b7\u5f97\uff1a<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"84\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-146.png\" alt=\"\" class=\"wp-image-2124\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-146.png 524w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-146-300x48.png 300w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/figure>\n\n\n<p>\u90a3\u4e48\u5bf9\u4e8e\u4e00\u4e9b\u6570\u636e\u6ca1\u6709\u660e\u663e\u7684\u6ee1\u8db3\u9ad8\u65af\u5206\u5e03\uff0c\u4f8b\u5982\u4e0b\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6570\u5b66\u53d8\u5316\u8ba9\u5b83\u6ee1\u8db3\u9ad8\u65af\u5206\u5e03<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"208\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-141.png\" alt=\"\" class=\"wp-image-2119\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-141.png 584w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-141-300x107.png 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/figure><\/div>\n\n\n<p>\u540c\u65f6\uff0c\u6211\u4eec\u53d1\u73b0 \u6709\u4e00\u4e9b\u6570\u636e\u5e76\u4e0d\u662f\u670d\u4ece\u201c\u6c34\u5e73\u201d\u6216\u8005\u201c\u7ad6\u76f4\u201d\u7684\u9ad8\u65af\u5206\u5e03\uff0c\u5982\u4e0b\u5982\u84dd\u7ebf\uff0c\u5f88\u660e\u663e\u80fd\u5bf9\u6570\u636e\u66f4\u597d\u7684\u62df\u5408\uff0c<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"345\" height=\"276\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-143.png\" alt=\"\" class=\"wp-image-2121\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-143.png 345w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-143-300x240.png 300w\" sizes=\"(max-width: 345px) 100vw, 345px\" \/><\/figure><\/div>\n\n\n<p> \u56e0\u6b64\u6211\u4eec\u8fd9\u91cc\u591a\u4e86\u4e00\u7ec4\u53c2\u6570 \uff0c\u53ef\u4ee5\u5bf9\u9ad8\u65af\u51fd\u6570\u7684\u5206\u5e03\u505a\u51fa\u5f71\u54cd\uff0c\u4e5f\u5c31\u662f<strong>\u591a\u5143\u9ad8\u65af\u51fd\u6570<\/strong>\u3002<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"281\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-144.png\" alt=\"\" class=\"wp-image-2122\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-144.png 763w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-144-300x110.png 300w\" sizes=\"(max-width: 763px) 100vw, 763px\" \/><\/figure>\n\n\n<p>\u800c\u5bf9\u4e8e\u6620\u5c04\u4e4b\u540e\u7684\u503c\u8981\u591a\u5c0f\u624d\u88ab\u8ba4\u4e3a\u662f\u5f02\u5e38\uff0c\u6211\u4eec\u901a\u8fc7\u4e0b\u5f0f\u8ba1\u7b97\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"280\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-147.png\" alt=\"\" class=\"wp-image-2125\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-147.png 816w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-147-300x103.png 300w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-147-768x264.png 768w\" sizes=\"(max-width: 816px) 100vw, 816px\" \/><\/figure><\/div>\n\n\n<p>\u5176\u4e2d\uff1a<\/p>\n\n\n<ul><li>tp means true positives\uff1a\u662f\u5f02\u5e38\u503c\uff0c\u5e76\u4e14\u6211\u4eec\u7684\u6a21\u578b\u9884\u6d4b\u6210\u5f02\u5e38\u503c\u4e86\uff0c\u5373\u771f\u7684\u5f02\u5e38\u503c\u3002<\/li><li>fp means false positives\uff1a\u662f\u6b63\u5e38\u503c\uff0c\u4f46\u6a21\u578b\u628a\u5b83\u9884\u6d4b\u6210\u5f02\u5e38\u503c\uff0c\u5373\u5047\u7684\u5f02\u5e38\u503c\u3002<\/li><li>fn means false negatives\uff1a\u662f\u5f02\u5e38\u503c\uff0c\u4f46\u662f\u6a21\u578b\u628a\u5b83\u9884\u6d4b\u6210\u6b63\u5e38\u503c\uff0c\u5373\u5047\u7684\u6b63\u5e38\u503c\u3002<\/li><\/ul>\n\n\n<p>\u5230\u8fd9\u91cc\u6211\u4eec\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\u7684\u539f\u7406\u5c31\u57fa\u672c\u8bb2\u5b8c\u4e86\uff0c\u90a3\u4e48\u540c\u6837\u662f\u201c\u5206\u7c7b\u201d\u95ee\u9898\uff0c\u6211\u4eec\u600e\u4e48\u9009\u62e9\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\u4e0e\u5176\u4ed6\u76d1\u7763\u5b66\u4e60\u7b97\u6cd5\u5462\uff1f<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"494\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-140.png\" alt=\"\" class=\"wp-image-2118\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-140.png 719w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-140-300x206.png 300w\" sizes=\"(max-width: 719px) 100vw, 719px\" \/><\/figure><\/div>\n\n\n<p>\u90a3\u4e48\u5bf9\u4e8e\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\uff0c\u6211\u4eec\u53c8\u4ec0\u4e48\u65f6\u5019\u9009\u62e9\u9ad8\u65af\u5206\u5e03\uff0c\u4ec0\u4e48\u65f6\u5019\u9009\u62e9\u591a\u5143\u7684\u9ad8\u65af\u5206\u5e03\u5462\uff1f<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"593\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-142.png\" alt=\"\" class=\"wp-image-2120\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-142.png 713w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-142-300x250.png 300w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure><\/div>\n\n\n<p>\u4e0b\u9762\u662f\u4ee3\u7801\u8bb2\u89e3\uff1a<\/p>\n\n\n<p>\u9996\u5148\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>mat = loadmat('data\/ex8data1.mat')\n# dict_keys(['__header__', '__version__', '__globals__', 'X', 'Xval', 'yval'])\nprint(mat.keys())\nX = mat['X']\nXval, yval = mat['Xval'], mat['yval']\n# ((307, 2), (307, 2), (307, 1))\nprint(X.shape, Xval.shape, yval.shape)<\/code><\/pre>\n\n\n<p>\u5c06\u70b9\u6dfb\u52a0\u8fdb\u7ed8\u56fe\u4e2d\u65b9\u6cd5\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def plot_data():\n    \"\"\"\n    \u7ed8\u5236\u6570\u636e\u6837\u5f0f\uff0c\u7b2c\u4e00\u6b21\u8f93\u51fa\n    :return:\n    \"\"\"\n    plt.figure(figsize=(8, 5))\n    # \u8bad\u7ec3\u96c6\n    plt.plot(X[:, 0], X[:, 1], 'bx')\n    # \u9a8c\u8bc1\u96c6\n    # plt.scatter(Xval[:,0], Xval[:,1], c=yval.flatten(), marker='x', cmap='rainbow')<\/code><\/pre>\n\n\n<p>\u83b7\u53d6\u5f53\u524d\u6570\u636e\u7684\u9ad8\u65af\u51fd\u6570\u6a21\u578b\u53c2\u6570\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def getGaussianParams(X, useMultivariate):\n    \"\"\"\n    \u83b7\u53d6\u9ad8\u65af\u53c2\u6570\n    :param X: data\n    :param useMultivariate: \u662f\u5426\u4f7f\u7528\u591a\u7ef4\u9ad8\u65af\u51fd\u6570\n    :return: mu\u548c\u53c2\u6570\n    \"\"\"\n    # \u6c42\u5e73\u5747\u503c\uff0cmu\n    mu = X.mean(axis=0)\n    if useMultivariate:\n        # \u591a\u7ef4\u9ad8\u65af\u53c2\u6570\u8ba1\u7b97\u65b9\u6cd5  \u6709\u56db\u4e2a\u53c2\u6570 2*2\n        sigma2 = ((X - mu).T @ (X - mu)) \/ len(X)\n    else:\n        # \u6837\u672c\u65b9\u5dee \u53ea\u6709\u4e24\u4e2a\u53c2\u6570\n        sigma2 = X.var(axis=0, ddof=0)\n    return mu, sigma2<\/code><\/pre>\n\n\n<p>\u5c06\u6570\u636e\u5728\u9ad8\u65af\u6a21\u578b\u4e0a\u7684\u6620\u5c04\u8fd4\u56de\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def gaussian(X, mu, sigma2):\n    '''\n    mu, sigma2\u53c2\u6570\u5df2\u7ecf\u51b3\u5b9a\u4e86\u4e00\u4e2a\u9ad8\u65af\u5206\u5e03\u6a21\u578b\n    \u56e0\u4e3a\u539f\u59cb\u6a21\u578b\u5c31\u662f\u591a\u5143\u9ad8\u65af\u6a21\u578b\u5728sigma2\u4e0a\u662f\u5bf9\u89d2\u77e9\u9635\u800c\u5df2\uff0c\u6240\u4ee5\u5982\u4e0b\uff1a\n    If Sigma2 is a matrix, it is treated as the covariance matrix.\n    If Sigma2 is a vector, it is treated as the sigma^2 values of the variances\n    in each dimension (a diagonal covariance matrix)\n    output:\n        \u4e00\u4e2a(m, )\u7ef4\u5411\u91cf\uff0c\u5305\u542b\u6bcf\u4e2a\u6837\u672c\u7684\u6982\u7387\u503c\u3002\n    '''\n    m, n = X.shape\n    # ndim\u8fd4\u56de\u7684\u662f\u6570\u7ec4\u7684\u7ef4\u5ea6\n    if np.ndim(sigma2) == 1:\n        # diag\uff1aarray\u662f\u4e00\u4e2a1\u7ef4\u6570\u7ec4\u65f6\uff0c\u7ed3\u679c\u5f62\u6210\u4e00\u4e2a\u4ee5\u4e00\u7ef4\u6570\u7ec4\u4e3a\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u77e9\u9635\n        #       array\u662f\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\u65f6\uff0c\u7ed3\u679c\u8f93\u51fa\u77e9\u9635\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\n        # \u8fd9\u91cc\u8f93\u51fa\u4e86\u5bf9\u89d2\u7ebf\u77e9\u9635\n        sigma2 = np.diag(sigma2)\n    # \u8ba1\u7b97\u53c2\u6570\n    norm = 1. \/ (np.power((2 * np.pi), n \/ 2) * np.sqrt(np.linalg.det(sigma2)))\n    exp = np.zeros((m, 1))\n    for row in range(m):\n        xrow = X[row]\n        exp[row] = np.exp(-0.5 * ((xrow - mu).T).dot(np.linalg.inv(sigma2)).dot(xrow - mu))\n    return norm * exp<\/code><\/pre>\n\n\n<p>\u7ed8\u5236\u9ad8\u65af\u51fd\u6570\u56fe\u50cf\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def plotContours(mu, sigma2):\n    \"\"\"\n    \u753b\u51fa\u9ad8\u65af\u6982\u7387\u5206\u5e03\u7684\u56fe\uff0c\u5728\u4e09\u7ef4\u4e2d\u662f\u4e00\u4e2a\u4e0a\u51f8\u7684\u66f2\u9762\u3002\u6295\u5f71\u5230\u5e73\u9762\u4e0a\u5219\u662f\u4e00\u5708\u5708\u7684\u7b49\u9ad8\u7ebf\u3002\n    \"\"\"\n    # \u8ba1\u7b97\u70b9\u7684\u95f4\u9694\n    delta = .3  # \u6ce8\u610fdelta\u4e0d\u80fd\u592a\u5c0f\uff01\uff01\uff01\u5426\u5219\u4f1a\u751f\u6210\u592a\u591a\u7684\u6570\u636e\uff0c\u5bfc\u81f4\u77e9\u9635\u76f8\u4e58\u4f1a\u51fa\u73b0\u5185\u5b58\u9519\u8bef\u3002\n    x = np.arange(0, 30, delta)\n    y = np.arange(0, 30, delta)\n    # \u8fd9\u90e8\u5206\u8981\u8f6c\u5316\u4e3aX\u5f62\u5f0f\u7684\u5750\u6807\u77e9\u9635\uff0c\u4e5f\u5c31\u662f\u4e00\u5217\u662f\u6a2a\u5750\u6807\uff0c\u4e00\u5217\u662f\u7eb5\u5750\u6807\uff0c\n    # \u7136\u540e\u624d\u80fd\u4f20\u5165gaussian\u4e2d\u6c42\u89e3\u5f97\u5230\u6bcf\u4e2a\u70b9\u7684\u6982\u7387\u503c\n    # meshgrid \u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5750\u6807\u5f62\u5f0f\n    xx, yy = np.meshgrid(x, y)\n    # \u662f\u6309\u884c\u8fde\u63a5\u4e24\u4e2a\u77e9\u9635\uff0c\u5c31\u662f\u628a\u4e24\u77e9\u9635\u5de6\u53f3\u76f8\u52a0\uff0c\u8981\u6c42\u884c\u6570\u76f8\u7b49\n    points = np.c_[xx.ravel(), yy.ravel()]  # \u6309\u5217\u5408\u5e76\uff0c\u4e00\u5217\u6a2a\u5750\u6807\uff0c\u4e00\u5217\u7eb5\u5750\u6807\n    z = gaussian(points, mu, sigma2)\n    z = z.reshape(xx.shape)  # \u8fd9\u6b65\u9aa4\u4e0d\u80fd\u5fd8\n    # \u8fd9\u4e2alevels\u662f\u4f5c\u4e1a\u91cc\u9762\u7ed9\u7684\u53c2\u8003,\u6216\u8005\u901a\u8fc7\u6c42\u89e3\u7684\u6982\u7387\u63a8\u51fa\u6765\u3002\n    cont_levels = [10 ** h for h in range(-20, 0, 3)]\n    plt.contour(xx, yy, z, cont_levels)\n    plt.title('Gaussian Contours', fontsize=16)<\/code><\/pre>\n\n\n<p>\u6211\u4eec\u9996\u5148\u770b\u4e00\u4e0b\u6b63\u5e38\u7684\u9ad8\u65af\u51fd\u6570\u5206\u5e03\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">plot_data()\nuseMV = False\nplotContours(*getGaussianParams(X, useMV))\nplt.show()<\/pre>\n\n\n<p>\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"467\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-148.png\" alt=\"\" class=\"wp-image-2127\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-148.png 702w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-148-300x200.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/figure><\/div>\n\n\n<p>\u7ed8\u5236\u591a\u5143\u9ad8\u65af\u51fd\u6570\u5206\u5e03\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">plot_data()<br \/>useMV = True<br \/>plotContours(*getGaussianParams(X, useMV))<br \/>plt.show()<\/pre>\n\n\n<p>\u5982\u4e0b\u56fe\uff0c\u53ef\u4ee5\u770b\u5230\u6574\u4e2a\u9ad8\u65af\u51fd\u6570\u8fdb\u884c\u4e86\u504f\u8f6c<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"466\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-149.png\" alt=\"\" class=\"wp-image-2128\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-149.png 739w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-149-300x189.png 300w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n<p>\u9009\u62e9\u6700\u4f18\u9608\u503c\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def selectThreshold(yval, pval):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u9009\u62e9\u5408\u9002\u7684\u9608\u503c\uff0c\u5373\u591a\u5c0f\u7684\u503c\u5e94\u8be5\u88ab\u5224\u65ad\u4e3a\u5f02\u5e38<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> yval:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> pval:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em><br \/><\/em><em>    <\/em>def computeF1(yval, pval):<br \/>        m = len(yval)<br \/>        tp = float(len([i for i in range(m) if pval[i] and yval[i]]))<br \/>        fp = float(len([i for i in range(m) if pval[i] and not yval[i]]))<br \/>        fn = float(len([i for i in range(m) if not pval[i] and yval[i]]))<br \/>        prec = tp \/ (tp + fp) if (tp + fp) else 0<br \/>        rec = tp \/ (tp + fn) if (tp + fn) else 0<br \/>        F1 = 2 * prec * rec \/ (prec + rec) if (prec + rec) else 0<br \/>        return F1<br \/><br \/>    # \u6570\u636e\u5206\u5272 1000\u4efd<br \/>    epsilons = np.linspace(min(pval), max(pval), 1000)<br \/>    bestF1, bestEpsilon = 0, 0<br \/>    for e in epsilons:<br \/>        pval_ = pval &lt; e<br \/>        thisF1 = computeF1(yval, pval_)<br \/>        if thisF1 &gt; bestF1:<br \/>            bestF1 = thisF1<br \/>            bestEpsilon = e<br \/><br \/>    return bestF1, bestEpsilon<\/pre>\n\n\n<p>\u7136\u540e\u6211\u4eec\u4f7f\u7528\u9a8c\u8bc1\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># \u83b7\u53d6mu \u548csigma<br \/>mu, sigma2 = getGaussianParams(X, useMultivariate=True)<br \/># \u5229\u7528mu\u548csigma\u786e\u5b9a\u9ad8\u65af\u51fd\u6570\uff08\u4f7f\u7528\u9a8c\u8bc1\u96c6\uff09<br \/>pval = gaussian(Xval, mu, sigma2)<br \/># \u786e\u5b9a\u6700\u4f73\u53c2\u6570<br \/>bestF1, bestEpsilon = selectThreshold(yval, pval)<\/pre>\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f73\u53c2\u6570\u4e4b\u540e\uff0c\u548c\u8bad\u7ec3\u6570\u636e\u7b97\u51fa\u7684\u9ad8\u65af\u51fd\u6570\u8fdb\u884c\u5bf9\u6bd4\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">y = gaussian(X, mu, sigma2)  # X\u7684\u6982\u7387<\/pre>\n\n\n<p>\u627e\u5230\u79bb\u7fa4\u70b9\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">xx = np.array([X[i] for i in range(len(y)) if y[i] &lt; bestEpsilon])<br \/>print(xx)  # \u79bb\u7fa4\u70b9<\/pre>\n\n\n<p>\u753b\u56fe\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">plot_data()<br \/>plotContours(mu, sigma2)<br \/>plt.scatter(xx[:, 0], xx[:, 1], s=80, facecolors='none', edgecolors='r')<br \/>plt.show()<\/pre>\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u79bb\u7fa4\u70b9\uff08\u5f02\u5e38\u70b9\uff09\u5df2\u7ecf\u88ab\u6807\u51fa\uff1a<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"477\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-150.png\" alt=\"\" class=\"wp-image-2129\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-150.png 748w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/10\/\u56fe\u7247-150-300x191.png 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n<p>\u6574\u7406\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-<br \/><br \/>import numpy as np<br \/>from scipy.io import loadmat<br \/>import matplotlib.pyplot as plt<br \/><br \/><br \/>def load_data():<br \/>    <em>\"\"\"<br \/><\/em><em>    \u8bfb\u53d6\u6570\u636e<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>mat = loadmat('data\/ex8data1.mat')<br \/>    X = mat['X']<br \/>    Xval, yval = mat['Xval'], mat['yval']<br \/>    return X, Xval, yval<br \/><br \/><br \/>def get_gaussian_garams(X, useMultivariate=False):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u786e\u5b9a\u9ad8\u65af\u6a21\u578b\u53c2\u6570<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X: \u6570\u636e<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> useMultivariate: \u662f\u5426\u4f7f\u7528\u591a\u5143\u9ad8\u65af\u6a21\u578b<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>: \u9ad8\u65af\u53c2\u6570\u7684mu\u548csigma^2<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>mu = X.mean(axis=0)<br \/>    if useMultivariate:<br \/>        sigma2 = ((X - mu).T @ (X - mu)) \/ len(X)<br \/>    else:<br \/>        sigma2 = X.var(axis=0, ddof=0)<br \/>    return mu, sigma2<br \/><br \/><br \/>def gaussian(X, mu, sigma2):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u83b7\u53d6\u6570\u636e\u901a\u8fc7\u9ad8\u65af\u6a21\u578b\u4e4b\u540e\u7684\u6620\u5c04<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> mu:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> sigma2:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>m, n = X.shape<br \/>    # \u5982\u679csigma2\u662f\u4e00\u7ef4\u7684\uff0c\u8f6c\u6362\u591a\u7ef4\u65b9\u4fbf\u8ba1\u7b97<br \/>    if np.ndim(sigma2) == 1:<br \/>        sigma2 = np.diag(sigma2)<br \/>    # \u8ba1\u7b97\u516c\u5f0f<br \/>    norm = 1. \/ (np.power((2 * np.pi), n \/ 2) * np.sqrt(np.linalg.det(sigma2)))<br \/>    exp = np.zeros((m, 1))<br \/>    for row in range(m):<br \/>        xrow = X[row]<br \/>        exp[row] = np.exp(-0.5 * ((xrow - mu).T).dot(np.linalg.inv(sigma2)).dot(xrow - mu))<br \/>    return norm * exp<br \/><br \/><br \/>def select_threshold(yval, pval):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u9608\u503c\u4f5c\u4e3a\u5f02\u5e38\u6570\u636e\u7684\u68c0\u6d4b\u8fb9\u754c<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> yval: \u6570\u636e\u7ed3\u679c\uff08key\uff09<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> pval: \u901a\u8fc7\u9ad8\u65af\u6620\u5c04\u540e\u7684\u503c<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em><br \/><\/em><em>    <\/em>def computeF1(yval, pval):<br \/>        <em>\"\"\"<br \/><\/em><em>        \u8ba1\u7b97F1<br \/><\/em><em>        <\/em><strong><em>:param<\/em><\/strong><em> yval:<br \/><\/em><em>        <\/em><strong><em>:param<\/em><\/strong><em> pval:<br \/><\/em><em>        <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>        \"\"\"<br \/><\/em><em>        <\/em>m = len(yval)<br \/>        tp = float(len([i for i in range(m) if pval[i] and yval[i]]))<br \/>        fp = float(len([i for i in range(m) if pval[i] and not yval[i]]))<br \/>        fn = float(len([i for i in range(m) if not pval[i] and yval[i]]))<br \/>        prec = tp \/ (tp + fp) if (tp + fp) else 0<br \/>        rec = tp \/ (tp + fn) if (tp + fn) else 0<br \/>        F1 = 2 * prec * rec \/ (prec + rec) if (prec + rec) else 0<br \/>        return F1<br \/><br \/>    # \u5206\u5272\u6570\u636e<br \/>    epslions = np.linspace(min(pval), max(pval), 1000)<br \/>    bestF1, bestEpslion = 0, 0<br \/>    # \u5bfb\u627e\u4e00\u4e2a\u6700\u5408\u9002\u7684\u70b9\uff0c\u6765\u4f5c\u4e3a\u6700\u4f73\u7684\u9608\u503c<br \/>    for e in epslions:<br \/>        _pvel = pval &lt; e<br \/>        thisF1 = computeF1(yval, _pvel)<br \/>        if thisF1 &gt; bestF1:<br \/>            bestF1 = thisF1<br \/>            bestEpslion = e<br \/>    return bestF1, bestEpslion<br \/><br \/><br \/>def plot_data(X):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u7ed8\u5236\u6570\u636e\u70b9\uff0c\u7531\u4e8e\u9700\u8981\u591a\u6b21\u7ed8\u5236\uff0c\u4f7f\u7528\u65b9\u6cd5\u7b80\u5316\u6d41\u7a0b<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>plt.figure(figsize=(8, 5))<br \/>    plt.plot(X[:, 0], X[:, 1], 'bx')<br \/><br \/><br \/>def plot_contours(mu, sigma2):<br \/>    <em>\"\"\"<br \/><\/em><em>    \u753b\u51fa\u9ad8\u65af\u5206\u5e03\u56fe<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> mu:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> sigma2:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>delta = 0.3<br \/>    # \u5206\u522b\u57280\uff0c30\u4e2d\u95f4\u5747\u5206\u51fax y<br \/>    x = np.arange(0, 30, delta)<br \/>    y = np.arange(0, 30, delta)<br \/>    # \u5c06x y\u8f6c\u6362\u4e3a\u5750\u6807<br \/>    xx, yy = np.meshgrid(x, y)<br \/>    points = np.c_[xx.ravel(), yy.ravel()]<br \/>    z = gaussian(points, mu, sigma2)<br \/>    z = z.reshape(xx.shape)<br \/>    cont_levels = [10 ** h for h in range(-20, 0, 3)]<br \/>    plt.contour(xx, yy, z, cont_levels)<br \/>    plt.title('Gaussian Contours', fontsize=16)<br \/><br \/><br \/>X, Xval, yval = load_data()<br \/>mu, sigma2 = get_gaussian_garams(X, useMultivariate=True)<br \/>pval = gaussian(Xval, mu, sigma2)<br \/>bestF1, bestEpsilon = select_threshold(yval, pval)<br \/><br \/>y = gaussian(X, mu, sigma2)  # X\u7684\u6982\u7387<br \/># \u8ba1\u7b97\u79bb\u7fa4\u70b9<br \/>xx = np.array([X[i] for i in range(len(y)) if y[i] &lt; bestEpsilon])<br \/>plot_data(X)<br \/>plot_contours(mu, sigma2)<br \/># \u7ed8\u5236\u79bb\u7fa4\u70b9<br \/>plt.scatter(xx[:, 0], xx[:, 1], s=80, facecolors='none', edgecolors='r')<br \/>plt.show()<br \/>print()<br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5434\u6069\u8fbeMachine-Learning \u7b2c\u4e5d\u5468\uff1a\u5f02\u5e38\u68c0\u6d4b\u7b97\u6cd5\uff08anomaly detection\uff09  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[],"views":5161,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2112"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2112"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2112\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2112"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}