{"id":112,"date":"2021-06-13T14:24:15","date_gmt":"2021-06-13T06:24:15","guid":{"rendered":"http:\/\/106.52.213.145:21080\/?p=112"},"modified":"2023-03-22T02:16:50","modified_gmt":"2023-03-21T18:16:50","slug":"zhouqizongjie-210614-and-ex7andttnicpre","status":"publish","type":"post","link":"https:\/\/apifj.com\/index.php\/2021\/06\/13\/zhouqizongjie-210614-and-ex7andttnicpre\/","title":{"rendered":"[\u5468\u671f\u603b\u7ed3] 210614 \u5434\u6069\u8fbeex7\u3001\u6cf0\u5766\u5c3c\u514b\u53f7\u4eba\u6570\u9884\u6d4b"},"content":{"rendered":"<h1>Programming Exercise 7:<\/h1>\n<h3>K-means Clustering and Principal Component Analysis<\/h3>\n<p>In this exercise, you will implement the K-means clustering algorithm and apply it to compress an image. In the second part, you will use principal component analysis to find a low-dimensional representation of face images. Before starting on the programming exercise, we strongly recommend watch- ing the video lectures and completing the review questions for the associated topics.<br \/>\nTo get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave to change to this directory before starting this exercise.<\/p>\n<h3>Files included in this exercise<\/h3>\n<p>Throughout the first part of the exercise, you will be using the script ex7.m, for the second part you will use ex7 pca.m. These scripts set up the dataset for the problems and make calls to functions that you will write. You are only required to modify functions in other files, by following the instructions in this assignment.<\/p>\n<h2>K-means Clustering<\/h2>\n<p>In this this exercise, you will implement the K-means algorithm and use it for image compression. You will first start on an example 2D dataset that will help you gain an intuition of how the K-means algorithm works. After that, you wil use the K-means algorithm for image compression by reducing the number of colors that occur in an image to only those that are most common in that image. You will be using ex7.m for this part of the exercise.<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from scipy.io import loadmat\nimport numpy as np\nfrom matplotlib import pyplot as plt\ndata = loadmat(&quot;ex7data2.mat&quot;)[&#039;X&#039;]\nx = data.T[0]\ny = data.T[1]\n\ninix = np.random.rand(1,3)\niniy = np.random.rand(1,3)\nmark=[]\nplt.scatter(inix,iniy,marker=&#039;x&#039;)\ndef find_near():\n    l=[]\n    for i in range(x.size):\n        xp = x[i]\n        yp = y[i]\n        r=(((inix-xp)**2)+((iniy-yp)**2))[0]\n        l.append(np.where(r==np.min(r))[0][0])\n    return np.asarray(l)\n\ndef adjust_point(mark):\n    for i in range(iniy.size):\n        nx = np.mean(x[np.where(mark==i)])\n        ny = np.mean(y[np.where(mark==i)])\n        if np.isnan(nx):\n            continue\n        inix[0][i]=nx\n        iniy[0][i]=ny\ndef cost(x,y,inix,iniy):\n    tlt=0\n    for i in range(len(inix[0])):\n        tlt=tlt+np.mean(np.sqrt((x-inix[0][i])**2+(y-iniy[0][i])**2))\n    return tlt\/len(inix[0])\n\nct=[]\nitcnt=10\nfor i in range(itcnt):\n    mark = find_near()\n    adjust_point(mark)\n    ct.append(cost(x,y,inix,iniy))\nplt.scatter(x, y,alpha=0.5)\nc = [&#039;r&#039;,&#039;g&#039;,&#039;b&#039;]\nfor i in range(iniy.size):\n    nx = x[np.where(mark == i)]\n    ny = y[np.where(mark == i)]\n    plt.scatter(nx,ny,c=c[i])\nplt.scatter(inix,iniy,marker=&#039;x&#039;,c=&#039;r&#039;)\nplt.show()\ncx=np.arange(0,itcnt)\nplt.plot(cx,ct)\nplt.show()<\/code><\/pre>\n<p>\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/\u805a\u7c7b\u6548\u679c\u56fe.png\" alt=\"\" \/><br \/>\n\u4ee3\u4ef7\u51fd\u6570\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/\u4ee3\u4ef7\u51fd\u6570\u66f2\u7ebf.png\" alt=\"\" \/><\/p>\n<h1>\u6cf0\u5766\u5c3c\u514b\u53f7\u751f\u5b58\u9884\u6d4b<\/h1>\n<p>\u4e3b\u8981\u95ee\u9898\uff1a\u4ec0\u4e48\u6837\u7684\u4eba\u5728\u6cf0\u5766\u5c3c\u514b\u53f7\u4e2d\u66f4\u5bb9\u6613\u5b58\u6d3b\uff1f<\/p>\n<h2>\u5355\u5355ROOM\u7684\u56e0\u7d20<\/h2>\n<pre><code class=\"language-python\">import numpy as np\nfrom matplotlib import pyplot as plt\nimport pandas as pd\nimport scipy.optimize as opt\nfrom sklearn.preprocessing import MinMaxScaler\n#\u8f7d\u5165\u6587\u4ef6\nfrom sklearn.preprocessing import PolynomialFeatures\n\ndata=pd.read_csv(&quot;python_dataset.csv&quot;)\n#\u6784\u5efanumpy\u77e9\u9635\nX = np.asarray(data.iloc[0:,0:1])\nY = np.asarray(data[&#039;Price&#039;])\nY=(Y-Y.min())\/(Y.max()-Y.min())\nplt.scatter(X,Y)\npoly_reg = PolynomialFeatures(degree=3)\nX = poly_reg.fit_transform(X).T\ndef gradient(theta, X, y,lamda=1):\n    theta_n = theta[1:]\n    reg_theta = (lamda\/len(X))*theta_n\n    reg_theta = np.concatenate((np.asarray([0]),reg_theta))\n    return (1 \/ len(X)) * np.dot(X,((h(X,theta)) - y).T) + reg_theta\n\n#\u5b9a\u4e49\u5047\u8bbe\u51fd\u6570\ndef h(X,theta):\n    return np.sin(np.matmul(theta,X))\/2\n#\u5b9a\u4e49\u4ee3\u4ef7\u51fd\u6570\ndef costFunction(theta,X,Y):\n    inner = np.power(h(X,theta)-Y,2)\n    return np.sum(inner)\/(2 * len(X[0]))\n#\u521d\u59cb\u5316theta\u77e9\u9635\ntheta = np.zeros([1,X.shape[0]])\n\n#\u8f93\u51fatheta\u521d\u59cb\u503c\u7684\u77e9\u9635\nprint(&quot;Theta \u4e3a0\u7684\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(theta,X,Y))\n\n#cost\u6570\u5217\u7528\u6765\u5b58\u50a8\u6bcf\u6b21\u8bad\u7ec3\u7684\u4ee3\u4ef7\ncost=[]\n#\u8fd0\u7528\u68af\u5ea6\u4e0b\u964d\u6765\u5bf9\u51fd\u6570\u62df\u5408\ndef gradientDescent(X, y, theta, alpha, iters):\n    #\u5f00\u59cb\u8fed\u4ee3iters\u6b21\n    for i in range(iters):\n        #\u6784\u5efa h_theta(x^(i))-y \u7684\u503c\n        error = (h(X,theta)-y)\n        #theta_tempy\u7528\u6765\u5b58\u50a8\u4e0b\u4e00\u6b21\u7684theta\u5217\u8868\u6765\u5b9e\u73b0\u540c\u65f6\u66f4\u65b0\n        theta_temp=[[]]\n        #\u5206\u522b\u8ba1\u7b97\u6bcf\u4e00\u4e2atheta\u7684\u503c\n        for j in range(len(theta[0])):\n            #\u8ba1\u7b97 \uff08 h_theta(x^(i))-y \uff09\u00b7 x_j \u9879\n            term = np.multiply(error,X[j])\n            #\u6c42\u51fatheta\uff0csum\u5373\u516c\u5f0f\u4e2d\u7684\u6c42\u548c\u7b26\u53f7\n            n = theta[0][j]-(( alpha \/ len(X[0]))*np.sum(term))\n            #\u52a0\u5165\u96f6\u4e34\u65f6\u5e8f\u5217\u4e2d\n            theta_temp[0].append(n)\n        #\u5c06\u6570\u5217\u8f6c\u5316\u4e3anumpy\u5e76\u66f4\u65b0\u5230theta\n        theta_temp = np.asarray(theta_temp)\n        theta=theta_temp\n        #\u52a0\u5165\u4ee3\u4ef7\u5e8f\u5217\n        cost.append(costFunction(theta,X,Y))\n    return theta\n#alpha\u662f\u5b66\u4e60\u7387\uff0citers\u662f\u8fed\u4ee3\u6b21\u6570\nalpha=0.001\niters=1500\n\n#\u5f00\u59cb\u68af\u5ea6\u4e0b\u964d\u62df\u5408\u51fd\u6570\ng = gradientDescent(X, Y, theta, alpha, iters)\nprint(&quot;Theta \u62df\u5408\u540e\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(g,X,Y))\n# res = opt.minimize(fun=costFunction, x0=theta, args=(X, Y), jac=gradient,method=&#039;TNC&#039;,)\n# print(res)\n# g=np.asarray([res.x])\n#\u753b\u51fa\u56fe\u50cf\n#\u6253\u5370\u51fa\u6563\u70b9\u56fe\u548c\u62df\u5408\u540e\u7684\u76f4\u7ebf\nx=np.linspace(X[1].min(),X[1].max(),10).reshape((10,1))\nx=poly_reg.fit_transform(x).T\ny = np.sin(g @ x)\/2\nx=np.linspace(X[1].min(),X[1].max(),10).reshape((10,1))\nplt.plot(x.T[0],y[0],&#039;r&#039;)\nplt.show()\n#\u6253\u5370\u51fa\u4ee3\u4ef7\u51fd\u6570\u7684\u53d8\u5316\u66f2\u7ebf\nt = np.arange(iters)\nplt.plot(t,cost,&#039;r&#039;)\nplt.show()<\/code><\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/ROOM\u62df\u5408\u66f2\u7ebf.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/ROOM\u4ee3\u4ef7\u51fd\u6570.png\" alt=\"\" \/><\/p>\n<h2>ROOM with Distance\u7684\u56e0\u7d20<\/h2>\n<pre><code class=\"language-python\">import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n#import scipy.optimize as opt\n#\u8f7d\u5165\u6587\u4ef6\n\ndata=pd.read_csv(&quot;python_dataset.csv&quot;)\n#\u6784\u5efanumpy\u77e9\u9635\ndata.insert(0,&#039;one&#039;,1)\nX = np.asarray(data.iloc[0:,0:3]).T\nfor i in  range(1,len(X)):\n    X[i]=(X[i]-X[i].min())\/(X[i].max()-X[i].min())\nX = np.concatenate((X,[X[1]*X[2]],[(X[1]**2)*X[2]],[(X[2]**2)*X[1]]))\nY = np.asarray(data[&#039;Price&#039;])\nY=(Y-Y.min())\/(Y.max()-Y.min())\n\nfig = plt.figure()\nax = Axes3D(fig)\nax.scatter(X[1],X[2],Y,zdir=&#039;z&#039;)\n\ndef gradient(theta, X, y,lamda=1):\n    theta_n = theta[1:]\n    reg_theta = (lamda\/len(X))*theta_n\n    reg_theta = np.concatenate((np.asarray([0]),reg_theta))\n    return (1 \/ len(X)) * np.dot(X,((h(theta,X)) - y).T) + reg_theta\n\n#\u5b9a\u4e49\u5047\u8bbe\u51fd\u6570\ndef h(theta,X):\n    return np.matmul(theta,X)\n# #\u5b9a\u4e49\u4ee3\u4ef7\u51fd\u6570\ndef costFunction(theta,X,Y):\n    inner = np.power(h(theta,X)-Y,2)\n    return np.sum(inner)\/(2 * len(X[0]))\n# #\u521d\u59cb\u5316theta\u77e9\u9635\ntheta = np.zeros([1,X.shape[0]])\n\n# #\u8f93\u51fatheta\u521d\u59cb\u503c\u7684\u77e9\u9635\nprint(&quot;Theta \u4e3a0\u7684\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(theta,X,Y))\n#\n#\n# #cost\u6570\u5217\u7528\u6765\u5b58\u50a8\u6bcf\u6b21\u8bad\u7ec3\u7684\u4ee3\u4ef7\ncost=[]\n# #\u8fd0\u7528\u68af\u5ea6\u4e0b\u964d\u6765\u5bf9\u51fd\u6570\u62df\u5408\ndef gradientDescent(X, y, theta, alpha, iters):\n    #\u5f00\u59cb\u8fed\u4ee3iters\u6b21\n    for i in range(iters):\n        #\u6784\u5efa h_theta(x^(i))-y \u7684\u503c\n        error = (h(theta,X)-y)\n        #theta_tempy\u7528\u6765\u5b58\u50a8\u4e0b\u4e00\u6b21\u7684theta\u5217\u8868\u6765\u5b9e\u73b0\u540c\u65f6\u66f4\u65b0\n        theta_temp=[[]]\n        #\u5206\u522b\u8ba1\u7b97\u6bcf\u4e00\u4e2atheta\u7684\u503c\n        for j in range(len(theta[0])):\n            #\u8ba1\u7b97 \uff08 h_theta(x^(i))-y \uff09\u00b7 x_j \u9879\n            term = np.multiply(error,X[j])\n            #\u6c42\u51fatheta\uff0csum\u5373\u516c\u5f0f\u4e2d\u7684\u6c42\u548c\u7b26\u53f7\n            n = theta[0][j]-(( alpha \/ len(X[0]))*np.sum(term))\n            #\u52a0\u5165\u96f6\u4e34\u65f6\u5e8f\u5217\u4e2d\n            theta_temp[0].append(n)\n        #\u5c06\u6570\u5217\u8f6c\u5316\u4e3anumpy\u5e76\u66f4\u65b0\u5230theta\n        theta_temp = np.asarray(theta_temp)\n        theta=theta_temp\n        #\u52a0\u5165\u4ee3\u4ef7\u5e8f\u5217\n        cost.append(costFunction(theta,X,Y))\n    return theta\n#alpha\u662f\u5b66\u4e60\u7387\uff0citers\u662f\u8fed\u4ee3\u6b21\u6570\nalpha=0.1\niters=30000\n#\n# #\u5f00\u59cb\u68af\u5ea6\u4e0b\u964d\u62df\u5408\u51fd\u6570\ng = gradientDescent(X, Y, theta, alpha, iters)\nprint(&quot;Theta \u62df\u5408\u540e\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(g,X,Y))\n\n# res = opt.minimize(fun=costFunction, x0=theta, args=(X, Y), jac=gradient,method=&#039;TNC&#039;)\n# g=res.x\n# print(res)\n#x: array([ 0.0341108 ,  0.48157589, -0.17348541, -0.27254023, -0.71858818,\n#        0.47850217])\n# g=np.asarray([res.x])\n# #\u753b\u51fa\u56fe\u50cf\ntheta = np.asarray([g])\ntheta0 = np.arange(0, 1, 0.1)\ntheta1 = np.arange(0, 1, 0.1)\ntheta0,theta1 = np.meshgrid(theta0, theta1)\ntheta0 = np.ravel(theta0)\ntheta1 = np.ravel(theta1)\nxt = np.asarray([[1]*len(theta0),theta0,theta1,theta1*theta0,(theta1**2)*theta0,(theta0**2)*theta1])\nz = h(theta,xt).reshape(10,10)\ntheta0 = np.arange(0, 1, 0.1)\ntheta1 = np.arange(0, 1, 0.1)\ntheta0,theta1 = np.meshgrid(theta0, theta1)\n\nax.plot_surface(theta0,theta1,z,cmap=&#039;rainbow&#039;)\nplt.show()\n#\u6253\u5370\u51fa\u4ee3\u4ef7\u51fd\u6570\u7684\u53d8\u5316\u66f2\u7ebf\nt = np.arange(iters)\nplt.plot(t,cost,&#039;r&#039;)\nplt.show()<\/code><\/pre>\n<p>\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/RD\u62df\u5408\u6548\u679c.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/RD\u4ee3\u4ef7\u51fd\u6570.png\" alt=\"\" \/><\/p>\n<h2>Room and Distence and bedroom<\/h2>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport scipy.optimize as opt\n#\u8f7d\u5165\u6587\u4ef6\nfrom sklearn.preprocessing import PolynomialFeatures\n\ndata=pd.read_csv(&quot;python_dataset.csv&quot;)\n#\u6784\u5efanumpy\u77e9\u9635\nX = np.asarray(data.iloc[0:,0:2]).T\nfor i in  range(0,len(X)):\n    X[i]=(X[i]-X[i].min())\/(X[i].max()-X[i].min())\nn = np.asarray(data[&#039;Bedroom2&#039;])\nn=(n-n.min())\/(n.max()-n.min())\nX = np.concatenate((X,[n]))\n\npoly_reg = PolynomialFeatures(degree=3)\nP = poly_reg.fit_transform(X.T).T\n\nY = np.asarray(data[&#039;Price&#039;])\nY=(Y-Y.min())\/(Y.max()-Y.min())\n\nfig = plt.figure()\nax = Axes3D(fig)\nax.scatter(X[0],X[1],X[2],c=Y,cmap=&quot;viridis&quot;,zdir=&#039;z&#039;)\nplt.show()\ndef gradient(theta, X, y,lamda=1):\n    theta_n = theta[1:]\n    reg_theta = (lamda\/len(X))*theta_n\n    reg_theta = np.concatenate((np.asarray([0]),reg_theta))\n    return (1 \/ len(X)) * np.dot(X,((h(theta,X)) - y).T) + reg_theta\n\n#\u5b9a\u4e49\u5047\u8bbe\u51fd\u6570\ndef h(theta,X):\n    return np.matmul(theta,X)\n# #\u5b9a\u4e49\u4ee3\u4ef7\u51fd\u6570\ndef costFunction(theta,X,Y):\n    inner = np.power(h(theta,X)-Y,2)\n    return np.sum(inner)\/(2 * len(X[0]))\n# #\u521d\u59cb\u5316theta\u77e9\u9635\ntheta = np.zeros([1,P.shape[0]])\n#\n#\u8f93\u51fatheta\u521d\u59cb\u503c\u7684\u77e9\u9635\nprint(&quot;Theta \u4e3a0\u7684\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(theta,P,Y))\n# #\n# #\n# # #cost\u6570\u5217\u7528\u6765\u5b58\u50a8\u6bcf\u6b21\u8bad\u7ec3\u7684\u4ee3\u4ef7\ncost=[]\n# # #\u8fd0\u7528\u68af\u5ea6\u4e0b\u964d\u6765\u5bf9\u51fd\u6570\u62df\u5408\ndef gradientDescent(X, y, theta, alpha, iters):\n    #\u5f00\u59cb\u8fed\u4ee3iters\u6b21\n    for i in range(iters):\n        #\u6784\u5efa h_theta(x^(i))-y \u7684\u503c\n        error = (h(theta,X)-y)\n        #theta_tempy\u7528\u6765\u5b58\u50a8\u4e0b\u4e00\u6b21\u7684theta\u5217\u8868\u6765\u5b9e\u73b0\u540c\u65f6\u66f4\u65b0\n        theta_temp=[[]]\n        #\u5206\u522b\u8ba1\u7b97\u6bcf\u4e00\u4e2atheta\u7684\u503c\n        for j in range(len(theta[0])):\n            #\u8ba1\u7b97 \uff08 h_theta(x^(i))-y \uff09\u00b7 x_j \u9879\n            term = np.multiply(error,X[j])\n            #\u6c42\u51fatheta\uff0csum\u5373\u516c\u5f0f\u4e2d\u7684\u6c42\u548c\u7b26\u53f7\n            n = theta[0][j]-(( alpha \/ len(X[0]))*np.sum(term))\n            #\u52a0\u5165\u96f6\u4e34\u65f6\u5e8f\u5217\u4e2d\n            theta_temp[0].append(n)\n        #\u5c06\u6570\u5217\u8f6c\u5316\u4e3anumpy\u5e76\u66f4\u65b0\u5230theta\n        theta_temp = np.asarray(theta_temp)\n        theta=theta_temp\n        #\u52a0\u5165\u4ee3\u4ef7\u5e8f\u5217\n        cost.append(costFunction(theta,X,Y))\n    return theta\n# #alpha\u662f\u5b66\u4e60\u7387\uff0citers\u662f\u8fed\u4ee3\u6b21\u6570\nalpha=0.1\niters=3000\n# #\n# #\u5f00\u59cb\u68af\u5ea6\u4e0b\u964d\u62df\u5408\u51fd\u6570\ng = gradientDescent(P, Y, theta, alpha, iters)\nprint(&quot;Theta \u62df\u5408\u540e\u65f6\u5019\u4ee3\u4ef7\u51fd\u6570\u7684\u503c\uff1a&quot;,end=&#039;&#039;)\nprint(costFunction(g,P,Y))\n#\n\n# res = opt.minimize(fun=costFunction, x0=theta, args=(P, Y), jac=gradient,method=&#039;TNC&#039;)\n# g=res.x\n# print(res)\n\nx1 = np.linspace(0,1,10)\nx2 = np.linspace(0,1,10)\nx3 = np.linspace(0,1,10)\nx1,x2,x3=np.meshgrid(x1,x2,x3)\ny=[]\ni=np.ravel(x1)\nj=np.ravel(x2)\nk=np.ravel(x3)\nfor m in range(len(i)):\n    l=np.asarray([[i[m],j[m],k[m]]])\n    P = poly_reg.fit_transform(l).T\n    y.append(h(g,P))\nax.scatter(x1,x2,x3,c=y,cmap=&quot;viridis&quot;,zdir=&#039;z&#039;)\nplt.show()\n\n# # g=np.asarray([res.x])\n# # #\u753b\u51fa\u56fe\u50cf\n# theta = np.asarray([g])\n# theta0 = np.arange(0, 1, 0.1)\n# theta1 = np.arange(0, 1, 0.1)\n# theta0,theta1 = np.meshgrid(theta0, theta1)\n# theta0 = np.ravel(theta0)\n# theta1 = np.ravel(theta1)\n# xt = np.asarray([[1]*len(theta0),theta0,theta1,theta1*theta0,(theta1**2)*theta0,(theta0**2)*theta1])\n# z = h(theta,xt).reshape(10,10)\n# theta0 = np.arange(0, 1, 0.1)\n# theta1 = np.arange(0, 1, 0.1)\n# theta0,theta1 = np.meshgrid(theta0, theta1)\n#\n# ax.plot_surface(theta0,theta1,z,cmap=&#039;rainbow&#039;)\n# plt.show()\n#\u6253\u5370\u51fa\u4ee3\u4ef7\u51fd\u6570\u7684\u53d8\u5316\u66f2\u7ebf\nt = np.arange(iters)\nplt.plot(t,cost,&#039;r&#039;)\nplt.show()<\/code><\/pre>\n<p>\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/RDB\u62df\u5408\u6548\u679c.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/106.52.213.145:21080\/wp-content\/uploads\/2023\/01\/RDB\u4ee3\u4ef7\u51fd\u6570.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programming Exercise 7: K-means Clustering and Principa&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/apifj.com\/index.php\/2021\/06\/13\/zhouqizongjie-210614-and-ex7andttnicpre\/\">\u9605\u8bfb\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-6"],"_links":{"self":[{"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":5,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/apifj.com\/index.php\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}