{"id":205252,"date":"2025-05-29T13:12:28","date_gmt":"2025-05-29T05:12:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205252\/"},"modified":"2025-05-29T13:12:28","modified_gmt":"2025-05-29T05:12:28","slug":"%e7%94%a8-python-%e4%bb%8e%e5%a4%b4%e5%bc%80%e5%a7%8b%e5%ae%9e%e7%8e%b0%e6%84%9f%e7%9f%a5%e5%99%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205252\/","title":{"rendered":"\u7528 Python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u5b9e\u73b0\u611f\u77e5\u5668"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7528 Python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u5b9e\u73b0\u611f\u77e5\u5668<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u7528 Python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u5b9e\u73b0\u611f\u77e5\u5668\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17326861486746b1440c638.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5f00\u53d1\u8005\u4eec\u5927\u5bb6\u597d\uff0c<\/p>\n<p>\u611f\u77e5\u5668\u662f\u673a\u5668\u5b66\u4e60\u4e2d\u6700\u7b80\u5355\u3001\u6700\u57fa\u672c\u7684\u6982\u5ff5\u4e4b\u4e00\u3002\u5b83\u662f\u6784\u6210\u795e\u7ecf\u7f51\u7edc\u57fa\u7840\u7684\u4e8c\u5143\u7ebf\u6027\u5206\u7c7b\u5668\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u9010\u6b65\u4ecb\u7ecd\u4f7f\u7528 python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u7406\u89e3\u548c\u5b9e\u73b0\u611f\u77e5\u5668\u7684\u6b65\u9aa4\u3002 <\/p>\n<p>\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n<hr>\n<p>a <strong>\u611f\u77e5\u5668<\/strong> \u662f\u4e8c\u5143\u5206\u7c7b\u5668\u76d1\u7763\u5b66\u4e60\u7684\u57fa\u672c\u7b97\u6cd5\u3002\u7ed9\u5b9a\u8f93\u5165\u7279\u5f81\uff0c\u611f\u77e5\u5668\u5b66\u4e60\u6743\u91cd\uff0c\u5e2e\u52a9\u57fa\u4e8e\u7b80\u5355\u7684\u9608\u503c\u51fd\u6570\u5206\u79bb\u7c7b\u522b\u3002\u7b80\u5355\u6765\u8bf4\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li> <strong>\u8f93\u5165<\/strong>\uff1a\u7279\u5f81\u5411\u91cf\uff08\u4f8b\u5982\uff0c[x1, x2]\uff09\u3002<\/li>\n<li> <strong>\u6743\u91cd<\/strong>\uff1a\u6bcf\u4e2a\u8f93\u5165\u7279\u5f81\u90fd\u6709\u4e00\u4e2a\u6743\u91cd\uff0c\u6a21\u578b\u6839\u636e\u6a21\u578b\u7684\u8868\u73b0\u6765\u8c03\u6574\u6743\u91cd\u3002<\/li>\n<li> <strong>\u6fc0\u6d3b\u51fd\u6570<\/strong>\uff1a\u8ba1\u7b97\u8f93\u5165\u7279\u5f81\u7684\u52a0\u6743\u548c\u5e76\u5e94\u7528\u9608\u503c\u6765\u51b3\u5b9a\u7ed3\u679c\u662f\u5426\u5c5e\u4e8e\u4e00\u4e2a\u7c7b\u6216\u53e6\u4e00\u7c7b\u3002<\/li>\n<\/ol>\n<p>\u4ece\u6570\u5b66\u4e0a\u6765\u8bf4\uff0c\u5b83\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<p><strong>f(x) = w1*x1 + w2*x2 + &#8230; + wn*xn + b<\/strong><\/p>\n<p>\u5730\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>f(x)<\/strong> \u662f\u8f93\u51fa\uff0c<\/li>\n<li> <strong>w<\/strong>\u4ee3\u8868\u6743\u91cd\uff0c<\/li>\n<li> <strong>x<\/strong> \u4ee3\u8868\u8f93\u5165\u7279\u5f81\uff0c<\/li>\n<li> <strong>b<\/strong> \u662f\u504f\u5dee\u9879\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c f(x) \u5927\u4e8e\u6216\u7b49\u4e8e\u9608\u503c\uff0c\u5219\u8f93\u51fa\u4e3a\u7c7b\u522b 1\uff1b\u5426\u5219\uff0c\u5b83\u662f 0 \u7c7b\u3002<\/p>\n<hr>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c06\u4ec5\u4f7f\u7528 numpy \u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff0c\u4ee5\u4fdd\u6301\u8f7b\u91cf\u7ea7\u3002<\/p>\n<pre>import numpy as np\n<\/pre>\n<hr>\n<p>\u6211\u4eec\u5c06\u628a\u611f\u77e5\u5668\u6784\u5efa\u4e3a\u4e00\u4e2a\u7c7b\uff0c\u4ee5\u4fdd\u6301\u4e00\u5207\u4e95\u4e95\u6709\u6761\u3002\u8be5\u8bfe\u7a0b\u5c06\u5305\u62ec\u8bad\u7ec3\u548c\u9884\u6d4b\u65b9\u6cd5\u3002<\/p>\n<pre>class perceptron:\n    def __init__(self, learning_rate=0.01, epochs=1000):\n        self.learning_rate = learning_rate\n        self.epochs = epochs\n        self.weights = none\n        self.bias = none\n\n    def fit(self, x, y):\n        # number of samples and features\n        n_samples, n_features = x.shape\n\n        # initialize weights and bias\n        self.weights = np.zeros(n_features)\n        self.bias = 0\n\n        # training\n        for _ in range(self.epochs):\n            for idx, x_i in enumerate(x):\n                # calculate linear output\n                linear_output = np.dot(x_i, self.weights) + self.bias\n                # apply step function\n                y_predicted = self._step_function(linear_output)\n\n                # update weights and bias if there is a misclassification\n                if y[idx] != y_predicted:\n                    update = self.learning_rate * (y[idx] - y_predicted)\n                    self.weights += update * x_i\n                    self.bias += update\n\n    def predict(self, x):\n        # calculate linear output and apply step function\n        linear_output = np.dot(x, self.weights) + self.bias\n        y_predicted = self._step_function(linear_output)\n        return y_predicted\n\n    def _step_function(self, x):\n        return np.where(x &gt;= 0, 1, 0)\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff1a<\/p>\n<ul>\n<li> fit\uff1a\u6b64\u65b9\u6cd5\u901a\u8fc7\u5728\u9519\u8bef\u5206\u7c7b\u70b9\u65f6\u8c03\u6574\u6743\u91cd\u548c\u504f\u5dee\u6765\u8bad\u7ec3\u6a21\u578b\u3002<\/li>\n<li> \u9884\u6d4b\uff1a\u6b64\u65b9\u6cd5\u8ba1\u7b97\u65b0\u6570\u636e\u7684\u9884\u6d4b\u3002<\/li>\n<li> _step_function\uff1a\u6b64\u51fd\u6570\u5e94\u7528\u9608\u503c\u6765\u786e\u5b9a\u8f93\u51fa\u7c7b\u522b\u3002<\/li>\n<\/ul>\n<hr>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6570\u636e\u96c6\u6765\u8f7b\u677e\u53ef\u89c6\u5316\u8f93\u51fa\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e0e\u95e8\u6570\u636e\u96c6\uff1a<\/p>\n<pre># and gate dataset\nx = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])\ny = np.array([0, 0, 0, 1])  # labels for and gate\n<\/pre>\n<hr>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u8bad\u7ec3\u611f\u77e5\u5668\u5e76\u6d4b\u8bd5\u5b83\u7684\u9884\u6d4b\u3002<\/p>\n<pre># initialize perceptron\np = perceptron(learning_rate=0.1, epochs=10)\n\n# train the model\np.fit(x, y)\n\n# test the model\nprint(\"predictions:\", p.predict(x))\n<\/pre>\n<p>\u4e0e\u95e8\u7684\u9884\u671f\u8f93\u51fa\uff1a<\/p>\n<pre>Predictions: [0 0 0 1]\n<\/pre>\n<hr>\n<ol>\n<li> <strong>\u521d\u59cb\u5316\u6743\u91cd\u548c\u504f\u5dee<\/strong>\uff1a\u5f00\u59cb\u65f6\uff0c\u6743\u91cd\u8bbe\u7f6e\u4e3a\u96f6\uff0c\u8fd9\u5141\u8bb8\u6a21\u578b\u4ece\u5934\u5f00\u59cb\u5b66\u4e60\u3002<\/li>\n<li> <strong>\u8ba1\u7b97\u7ebf\u6027\u8f93\u51fa<\/strong>\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u6570\u636e\u70b9\uff0c\u611f\u77e5\u5668\u8ba1\u7b97\u8f93\u5165\u7684\u52a0\u6743\u548c\u52a0\u4e0a\u504f\u5dee\u3002<\/li>\n<li> <strong>\u6fc0\u6d3b\uff08step function\uff09<\/strong>\uff1a\u5982\u679c\u7ebf\u6027\u8f93\u51fa\u5927\u4e8e\u6216\u7b49\u4e8e0\uff0c\u5219\u5206\u914d\u7c7b\u522b1\uff1b\u5426\u5219\uff0c\u5b83\u5206\u914d\u7c7b 0\u3002<\/li>\n<li> <strong>\u66f4\u65b0\u89c4\u5219<\/strong>\uff1a\u5982\u679c\u9884\u6d4b\u4e0d\u6b63\u786e\uff0c\u6a21\u578b\u4f1a\u671d\u51cf\u5c11\u8bef\u5dee\u7684\u65b9\u5411\u8c03\u6574\u6743\u91cd\u548c\u504f\u5dee\u3002\u66f4\u65b0\u89c4\u5219\u7531\u4e0b\u5f0f\u7ed9\u51fa\uff1a <strong>\u6743\u91cd += \u5b66\u4e60\u7387 * (y_true &#8211; y_pred) * x<\/strong> <\/li>\n<\/ol>\n<p>\u8fd9\u4f7f\u5f97\u611f\u77e5\u5668\u4ec5\u66f4\u65b0\u9519\u8bef\u5206\u7c7b\u7684\u70b9\uff0c\u9010\u6e10\u63a8\u52a8\u6a21\u578b\u66f4\u63a5\u8fd1\u6b63\u786e\u7684\u51b3\u7b56\u8fb9\u754c\u3002<\/p>\n<hr>\n<p>\u8bad\u7ec3\u540e\u53ef\u89c6\u5316\u51b3\u7b56\u8fb9\u754c\u3002\u5982\u679c\u60a8\u6b63\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u96c6\uff0c\u8fd9\u5c24\u5176\u6709\u7528\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 and \u95e8\u8ba9\u4e8b\u60c5\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<hr>\n<p>\u867d\u7136\u611f\u77e5\u5668\u4ec5\u9650\u4e8e\u7ebf\u6027\u53ef\u5206\u79bb\u95ee\u9898\uff0c\u4f46\u5b83\u662f\u591a\u5c42\u611f\u77e5\u5668 (mlp) \u7b49\u66f4\u590d\u6742\u795e\u7ecf\u7f51\u7edc\u7684\u57fa\u7840\u3002\u901a\u8fc7 mlp\uff0c\u6211\u4eec\u6dfb\u52a0\u9690\u85cf\u5c42\u548c\u6fc0\u6d3b\u51fd\u6570\uff08\u5982 relu \u6216 sigmoid\uff09\u6765\u89e3\u51b3\u975e\u7ebf\u6027\u95ee\u9898\u3002<\/p>\n<hr>\n<p>\u611f\u77e5\u5668\u662f\u4e00\u79cd\u7b80\u5355\u4f46\u57fa\u7840\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u3002\u901a\u8fc7\u4e86\u89e3\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u5e76\u4ece\u5934\u5f00\u59cb\u5b9e\u65bd\u5b83\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u673a\u5668\u5b66\u4e60\u548c\u795e\u7ecf\u7f51\u7edc\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u611f\u77e5\u5668\u7684\u7f8e\u5999\u4e4b\u5904\u5728\u4e8e\u5b83\u7684\u7b80\u5355\u6027\uff0c\u4f7f\u5176\u6210\u4e3a\u4efb\u4f55\u5bf9\u4eba\u5de5\u667a\u80fd\u611f\u5174\u8da3\u7684\u4eba\u7684\u5b8c\u7f8e\u8d77\u70b9\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u7528 Python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u5b9e\u73b0\u611f\u77e5\u5668\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Python \u4ece\u5934\u5f00\u59cb\u200b\u200b\u5b9e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205252","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205252"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205252\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}