{"id":1394,"date":"2019-08-30T10:40:58","date_gmt":"2019-08-30T02:40:58","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=1394"},"modified":"2019-08-30T10:40:58","modified_gmt":"2019-08-30T02:40:58","slug":"%e3%80%90%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e3%80%91%e5%89%8d%e9%a6%88%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/machine-learning-in-action\/1394\/","title":{"rendered":"\u3010\u5434\u6069\u8fbe\u673a\u5668\u5b66\u4e60\u3011\u524d\u9988\u795e\u7ecf\u7f51\u7edc"},"content":{"rendered":"\n<p> \u5434\u6069\u8fbeMachine-Learning \u7b2c\u56db\u5468\uff1a\u524d\u9988\u795e\u7ecf\u7f51\u7edc\uff08Neural Network\uff09 <\/p>\n\n\n<p>\u5176\u5b9e\u795e\u7ecf\u7f51\u7edc\u7684\u6027\u80fd\u5e76\u4e0d\u662f\u4e00\u5b9a\u6bd4\u903b\u8f91\u56de\u5f52\u8981\u9ad8\u3002\u8981\u6839\u636e\u6570\u636e\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u7b97\u6cd5\u3002<\/p>\n\n\n<p>\u672c\u8282\u5c06\u9488\u5bf9\u903b\u8f91\u56de\u5f52\u548c\u524d\u9988\u795e\u7ecf\u7f51\u7edc\u5bf9\u624b\u5199\u5b57\u7684\u8bc6\u522b\u5206\u522b\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n\n\n<p>\u9996\u5148\u770b\u4e00\u4e0b \u624b\u5199\u6570\u5b57\u7684\u56fe\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def plot_100_image(X):\n    \"\"\"\n    #\u7ed8\u56fe\u51fd\u6570\uff0c\u753b100\u5f20\u56fe\u7247\n    X :\n    \"\"\"\n    # \u83b7\u5f97\u56fe\u7247\u5927\u5c0f\uff08\u6839\u53f7\u4e0b400\uff09\n    size = int(numpy.sqrt(X.shape[1]))\n    # \u968f\u673a\u4eceX\u4e2d\u9009\u62e9100\u7ec4\u6570\u636e\n    sample_idx = numpy.random.choice(numpy.arange(X.shape[0]), 100)\n    # \u53d6\u6570\u968f\u673a\u6570\u636e\n    sample_images = X[sample_idx, :]\n    fig, ax_array = plt.subplots(nrows=10, ncols=10, sharey=True, sharex=True, figsize=(8, 8))\n    for r in range(10):\n        for c in range(10):\n            ax_array[r, c].matshow(sample_images[10 * r + c].reshape((size, size)),\n                                   cmap=matplotlib.cm.binary)\n            plt.xticks(numpy.array([]))\n            plt.yticks(numpy.array([]))\n# \u968f\u673a\u7ed8\u5236100\u5f20\u56fe\u7247\nplot_100_image(X)\nplt.show()<\/code><\/pre>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"447\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-58.png\" alt=\"\" class=\"wp-image-1523\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-58.png 444w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-58-298x300.png 298w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-58-150x150.png 150w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/figure><\/div>\n\n\n<p>\u9996\u5148\u662f\u5bfc\u5305\u548c\u57fa\u7840\u65b9\u6cd5\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\nimport numpy\nimport scipy.optimize as opt\nimport scipy.io as sio\nfrom sklearn.metrics import classification_report\n\"\"\"\n\u76f8\u5173\u51fd\u6570\n\"\"\"\ndef read_data(filename, transpose=True):\n    \"\"\"\n    \u6587\u4ef6\u8bfb\u53d6\n    :param filaname:\n    :return:\n    \"\"\"\n    data = sio.loadmat(filename)\n    y = data.get('y')\n    y = y.reshape(y.shape[0])\n    X = data.get('X')\n    if transpose:\n        # \u8f6c\u7f6e\u5f53\u524d\u6570\u636e\u4e2d,\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c06\u56fe\u7247\u65cb\u8f6c\n        X = numpy.array([im.reshape((20, 20)).T for im in X])\n        # \u5c06\u65cb\u8f6c\u540e\u7684\u56fe\u7247\u5c55\u5f00\u6210\u4e00\u884c\n        X = numpy.array([im.reshape(400) for im in X])\n    return X, y\ndef read_weight(filename):\n    \"\"\"\n    \u8bfb\u53d6\u795e\u7ecf\u7f51\u7edc\u7684\u6743\u91cd\n    :param filename:\n    :return:\n    \"\"\"\n    data = sio.loadmat(filename)\n    return data['Theta1'], data['Theta2']\ndef sigmoid(x):\n    \"\"\"\n    sigmoid\u51fd\u6570\n    :param x:\n    :return:\n    \"\"\"\n    return 1.0 \/ (1.0 + numpy.exp(-x))\ndef cost(theta, X, y):\n    \"\"\"\n    \u4ee3\u4ef7\u51fd\u6570\n    :param theta:\n    :param X:\n    :param y:\n    :return:\n    \"\"\"\n    return numpy.mean(-y * numpy.log(sigmoid(X @ theta)) - (1 - y) * numpy.log(1 - sigmoid(X @ theta)))\ndef gradient(theta, X, y):\n    \"\"\"\n    \u4e0b\u964d\u51fd\u6570\n    :param theta:\n    :param X:\n    :param y:\n    :return:\n    \"\"\"\n    return (1 \/ len(X)) * (sigmoid(X @ theta) - y) @ X\ndef regularized_cost(theta, X, y, l=1):\n    \"\"\"\n    \u6b63\u5219\u5316\u7684\u4ee3\u4ef7\u51fd\u6570\n    :param theta:\n    :param X:\n    :param y:\n    :param l:\n    :return:\n    \"\"\"\n    _theta = theta[1:]\n    reg = (1 \/ 2 * len(X)) * (_theta @ _theta)\n    return cost(theta, X, y) + reg\ndef regularized_gradient(theta, X, y, l=1):\n    \"\"\"\n    \u6b63\u5219\u5316\u7684\u4e0b\u964d\u51fd\u6570\n    :param theta:\n    :param X:\n    :param y:\n    :param l:\n    :return:\n    \"\"\"\n    reg = (1 \/ len(X)) * theta\n    reg[0] = 0\n    return gradient(theta, X, y) + reg\ndef predict(X, theta):\n    \"\"\"\n    \u9884\u6d4b\u51fd\u6570\n    :param X:\n    :param theta:\n    :return:\n    \"\"\"\n    prob = sigmoid(X @ theta)\n    return (prob >= 0.5).astype(int)\ndef logistic_regression(X, y, l=1):\n    \"\"\"\n    \u903b\u8f91\u56de\u5f52\u51fd\u6570\n    :param X:\n    :param y:\n    :param l:\n    :return:\n    \"\"\"\n    theta = numpy.zeros(X.shape[1])\n    result = opt.minimize(fun=regularized_cost,\n                          x0=theta,\n                          args=(X, y, l),\n                          method='TNC',\n                          jac=regularized_gradient,\n                          options={'disp': True})\n    return result.x\n<\/code><\/pre>\n\n\n<p>\u903b\u8f91\u56de\u5f52\u7684\u6f14\u793a\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\n\u903b\u8f91\u56de\u5f52\u6f14\u793a\n\"\"\"\nraw_X, raw_y = read_data('data\/ex3data1.mat')\n# \u5904\u7406\u5411\u91cf\nX = numpy.insert(raw_X, 0, values=numpy.ones(raw_X.shape[0]), axis=1)\ny_matrix = []\nfor k in range(1, 11):\n    y_matrix.append((raw_y == k).astype(int))\n# \u6700\u540e\u4e00\u63920\u79fb\u5230\u6700\u524d\u9762\ny_matrix = [y_matrix[-1]] + y_matrix[:-1]\ny = numpy.array(y_matrix)\nk_theta = numpy.array([logistic_regression(X, y[k]) for k in range(10)])\nprob_matrix = sigmoid(X @ k_theta.T)\n# \u8fd4\u56de\u6cbf\u8f74axis\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0caxis=1\u4ee3\u8868\u884c\ny_pred = numpy.argmax(prob_matrix, axis=1)\ny_answer = raw_y.copy()\ny_answer[y_answer == 10] = 0\nprint(classification_report(y_answer, y_pred))<\/code><\/pre>\n\n\n<p>\u903b\u8f91\u56de\u5f52\u7684\u6210\u529f\u7387\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"353\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-59.png\" alt=\"\" class=\"wp-image-1524\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-59.png 448w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-59-300x236.png 300w\" sizes=\"(max-width: 448px) 100vw, 448px\" \/><\/figure><\/div>\n\n\n<p>\u7136\u540e\u662f\u795e\u7ecf\u7f51\u7edc\uff0c\u8fd9\u91cc\u7684\u795e\u7ecf\u7f51\u7edc\u4f7f\u7528\u4e86\u4e00\u4e2a\u9690\u85cf\u5c42\u4e3a26\u4e2a\u8282\u70b9\u7684\u9690\u85cf\u5c42\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\n\u795e\u7ecf\u7f51\u7edc\u6f14\u793a\n\"\"\"\ntheta1, theta2 = read_weight('.\/data\/ex3weights.mat')\nX, y = read_data('.\/data\/ex3data1.mat', transpose=False)\n# \u7b2c\u4e00\u5c42\nl1 = X\n# \u5bf9\u7b2c\u4e00\u5c42\u6dfb\u52a0\u70b9\nl1 = numpy.insert(l1, 0, values=numpy.ones(X.shape[0]), axis=1)\n# \u8ba1\u7b97\u7b2c\u4e8c\u5c42\nl2 = sigmoid(l1 @ theta1.T)\n# \u5bf9\u7b2c\u4e8c\u5c42\u6dfb\u52a0\u70b9\nl2 = numpy.insert(l2, 0, numpy.ones(l2.shape[0]), axis=1)\n# \u7b2c\u4e09\u5c42\nl3 = sigmoid(l2 @ theta2.T)\ny_pred = numpy.argmax(l3, axis=1) + 1\nprint(classification_report(y, y_pred))<\/code><\/pre>\n\n\n<p>\u795e\u7ecf\u7f51\u7edc\u7684\u6210\u529f\u7387\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"362\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-60.png\" alt=\"\" class=\"wp-image-1525\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-60.png 435w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-60-300x250.png 300w\" sizes=\"(max-width: 435px) 100vw, 435px\" \/><\/figure><\/div>\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u795e\u7ecf\u7f51\u7edc\u8fbe\u5230\u4e8698\u7684\u51c6\u786e\u7387\uff0c\u800c\u903b\u8f91\u56de\u5f52\u53ea\u670994\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u7edd\u5bf9\uff0c\u8fd9\u53ea\u80fd\u8bf4\u660e\u8fd9\u7ec4\u6570\u636e\u66f4\u9002\u5408\u795e\u7ecf\u7f51\u7edc<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5434\u6069\u8fbeMachine-Learning \u7b2c\u56db\u5468\uff1a\u524d\u9988\u795e\u7ecf\u7f51\u7edc\uff08Neural Network\uff09 \u5176\u5b9e\u795e [&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,10],"tags":[],"views":4071,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1394"}],"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=1394"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}