日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作...

發布時間:2025/4/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
%測試數據 'ex1data1.txt', 第一列為 population of City in 10,000s, 第二列為 Profit in $10,000s
1
6.1101,17.592 2 5.5277,9.1302 3 8.5186,13.662 4 7.0032,11.854 5 5.8598,6.8233 6 8.3829,11.886 7 7.4764,4.3483 8 8.5781,12 9 6.4862,6.5987 10 5.0546,3.8166 11 5.7107,3.2522 12 14.164,15.505 13 5.734,3.1551 14 8.4084,7.2258 15 5.6407,0.71618 16 5.3794,3.5129 17 6.3654,5.3048 18 5.1301,0.56077 19 6.4296,3.6518 20 7.0708,5.3893 21 6.1891,3.1386 22 20.27,21.767 23 5.4901,4.263 24 6.3261,5.1875 25 5.5649,3.0825 26 18.945,22.638 27 12.828,13.501 28 10.957,7.0467 29 13.176,14.692 30 22.203,24.147 31 5.2524,-1.22 32 6.5894,5.9966 33 9.2482,12.134 34 5.8918,1.8495 35 8.2111,6.5426 36 7.9334,4.5623 37 8.0959,4.1164 38 5.6063,3.3928 39 12.836,10.117 40 6.3534,5.4974 41 5.4069,0.55657 42 6.8825,3.9115 43 11.708,5.3854 44 5.7737,2.4406 45 7.8247,6.7318 46 7.0931,1.0463 47 5.0702,5.1337 48 5.8014,1.844 49 11.7,8.0043 50 5.5416,1.0179 51 7.5402,6.7504 52 5.3077,1.8396 53 7.4239,4.2885 54 7.6031,4.9981 55 6.3328,1.4233 56 6.3589,-1.4211 57 6.2742,2.4756 58 5.6397,4.6042 59 9.3102,3.9624 60 9.4536,5.4141 61 8.8254,5.1694 62 5.1793,-0.74279 63 21.279,17.929 64 14.908,12.054 65 18.959,17.054 66 7.2182,4.8852 67 8.2951,5.7442 68 10.236,7.7754 69 5.4994,1.0173 70 20.341,20.992 71 10.136,6.6799 72 7.3345,4.0259 73 6.0062,1.2784 74 7.2259,3.3411 75 5.0269,-2.6807 76 6.5479,0.29678 77 7.5386,3.8845 78 5.0365,5.7014 79 10.274,6.7526 80 5.1077,2.0576 81 5.7292,0.47953 82 5.1884,0.20421 83 6.3557,0.67861 84 9.7687,7.5435 85 6.5159,5.3436 86 8.5172,4.2415 87 9.1802,6.7981 88 6.002,0.92695 89 5.5204,0.152 90 5.0594,2.8214 91 5.7077,1.8451 92 7.6366,4.2959 93 5.8707,7.2029 94 5.3054,1.9869 95 8.2934,0.14454 96 13.394,9.0551 97 5.4369,0.61705 %繪制實際數據圖像——人口和利潤的關系圖 fprintf('Plotting Data ...\n') data = load('ex1data1.txt'); X = data(:, 1); y = data(:, 2); m = length(y); % number of training examples% Plot Data % Note: You have to complete the code in plotData.m plotData(X, y);fprintf('Program paused. Press enter to continue.\n'); pause; 1 %plotData()函數實現 2 3 function plotData(x, y) 4 5 figure; % open a new figure window 6 plot(x, y, 'rx', 'MarkerSize', 10); %Set the size of Points('MarkerSize', 10) 7 ylabel('profit in $10,1000s'); 8 xlabel('population of City in 10,000s'); 9 10 end 1 %% =================== Part 3: Gradient descent 2 3 fprintf('Running Gradient Descent ...\n') 4 5 X = [ones(m, 1), data(:,1)]; % Add a column of ones to x 6 7 theta = zeros(2, 1); % initialize fitting parameters 8 9 % Some gradient descent settings 10 iterations = 1500; %迭代次數 11 alpha = 0.01; %learning rate 12 13 % compute and display initial cost 14 computeCost(X, y, theta) %y是真實的值 1 % Compute Cost for linear regression 2 % cost Function函數實現___利用矩陣操作進行!! 3 function J = computeCost(X, y, theta) 4 5 % Initialize some useful values 6 m = length(y); % number of training examples 7 J = 0; 8 9 % Instructions: Compute the cost of a particular choice of theta 10 % You should set J to the cost. 11 12 % X = [ ones(m, 1), data(:, 1) ], theta = [ th1; th2] 13 predictions = X * theta; %矩陣操作--預測函數 14 sqrError = (predictions - y).^2; 15 J = sum(sqrError) / (2*m); 16 17 end

?

1 %運行梯度下降算法 2 % run gradient descent 3 4 theta = gradientDescent(X, y, theta, alpha, iterations); 5 6 % print theta to screen 7 fprintf('Theta found by gradient descent: '); 8 fprintf('%f %f \n', theta(1), theta(2)); 1 %梯度下降算法實現 gradientDescent(X, y, theta, alpha, iterations)
%X-training example,y-實際數值,alpha-learning rate
2 function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) 3 4 % theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by 5 % taking num_iters gradient steps with learning rate alpha 6 7 % Initialize some useful values 8 m = length(y); % number of training examples 9 J_history = zeros(num_iters, 1); 10 11 for iter = 1:num_iters 12 predictions = X * theta; %預測值h(xi)--利用了矩陣運算 13 sqrError = (predictions - y); %預測值 - 實際值 14 15 % Simultaneously update(同時更新thetaj) thetaj for all j. 16 % alpha - learning rate, '.*'---是內積(矩陣對應元素相乘) 17 theta1 = theta(1) - alpha * (1/m) * sum(sqrError .* X(:,1)); 18 theta2 = theta(2) - alpha * (1/m) * sum(sqrError .* X(:,2)); 19 theta(1) = theta1; 20 theta(2) = theta2; 21 22 23 % Save the cost J in every iteration 24 J_history(iter) = computeCost(X, y, theta); 25 26 %disp(J_history); %增加輸出語句,方便調試 27 28 end 29 30 end 1 %繪制擬合曲線 2 % Plot the linear fit 3 hold on; % keep previous plot visible 4 plot(X(:,2), X*theta, '-') 5 legend('Training data', 'Linear regression') %添加圖例 6 hold off % don't overlay any more plots on this figure 1 % Predict values for population sizes of 35,000 and 70,0002 %利用求出的擬合參數--預測新值,利用矩陣運算 3 predict1 = [1, 3.5] *theta; 4 fprintf('For population = 35,000, we predict a profit of %f\n',... 5 predict1*10000); 6 7 predict2 = [1, 7] * theta; 8 fprintf('For population = 70,000, we predict a profit of %f\n',... 9 predict2*10000); 10 11 fprintf('Program paused. Press enter to continue.\n'); 12 pause; 1 %計算不同 theta參數下, J(θ)值的變化, 繪制圖像2 %% ============= Part 4: Visualizing J(theta_0, theta_1) ============= 3 4 fprintf('Visualizing J(theta_0, theta_1) ...\n') 5 6 % Grid over which we will calculate J 7 %linspace(x, y, n)--在(x,y)區間內均勻生成n個數 8 theta0_vals = linspace(-10, 10, 100); 9 theta1_vals = linspace(-1, 4, 100); 10 11 % initialize J_vals to a matrix of 0's 12 J_vals = zeros(length(theta0_vals), length(theta1_vals)); 13 14 % Fill out J_vals 15 for i = 1:length(theta0_vals) 16 for j = 1:length(theta1_vals) 17 t = [theta0_vals(i); theta1_vals(j)]; 18 J_vals(i,j) = computeCost(X, y, t); 19 end 20 end 21 22 23 % Because of the way meshgrids work in the surf command, we need to 24 % transpose J_vals before calling surf, or else the axes will be flipped 25 26 J_vals = J_vals'; 27 % Surface plot 28 figure; 29 30 %surf(X,Y,Z)--creates the surface plot from corresponding(對應值) value in X, Y,Z (default: color is proportional(成正比) to surface height.) 31 32 surf(theta0_vals, theta1_vals, J_vals) 33 xlabel('\theta_0'); ylabel('\theta_1'); 1 % Contour plot----輪廓圖的繪制 2 figure; 3 4 % Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100 5 6 contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20)) 7 xlabel('\theta_0'); ylabel('\theta_1'); 8 hold on; 9 plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

?繪圖效果如上。

轉載于:https://www.cnblogs.com/douzujun/p/5814219.html

總結

以上是生活随笔為你收集整理的Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美用舌头去添高潮 | 无遮挡的裸体按摩的视频 | 亚洲麻豆一区 | 国产精品福利一区二区 | 在线视频自拍 | jizjiz中国少妇高潮水多 | 国产精品入口夜色视频大尺度 | 国产观看 | 亚洲精品亚洲人成人网 | 日韩综合另类 | 中文精品无码中文字幕无码专区 | 欧美丰满一区二区免费视频 | 俄罗斯美女一级爱片 | a级片免费网站 | 青青草官网| 91动态图| 影音先锋在线中文字幕 | 国产人成精品 | 国产精品三区四区 | 操比视频网站 | 3d动漫啪啪精品一区二区中文字幕 | 在线视频 中文字幕 | 国产精品yy| 欧美一级黄 | 亚洲黄色av网站 | 男女一级特黄 | 99久99| 涩涩av | 台湾150部性三级 | 男人的天堂97 | 国产精品v欧美精品v日韩 | 欧美成人日韩 | 精品少妇人妻av一区二区 | 波多野结衣 一区 | 手机版av | 国产电影一区在线观看 | 成人免费看高清电影在线观看 | 欧美xxxx18| 一个人在线观看www软件 | 亚洲视频久久 | 亚洲精品免费在线 | 中文字幕免费高 | 欧美成人91 | 三级av网 | 福利视频黄色 | 在线视频亚洲 | 老妇裸体性激交老太视频 | 91吃瓜今日吃瓜入口 | 91视频最新入口 | 阿v视频免费在线观看 | 欧美亚洲国产视频 | 黄色的毛片 | 日本性生活一级片 | 青青精品视频 | 国产一区二区三区免费播放 | 婷婷精品进入 | 亚洲国产精品一 | 一二三不卡视频 | 97超碰总站| 国产一区二区a | 亚洲一区二区三区视频在线 | 极品人妻一区二区三区 | 亚洲一区二区三区四区五区午夜 | 亚洲av无码成人精品区 | 最新国产拍偷乱偷精品 | 天天色天天搞 | 亚洲深夜av | 成人毛片一区二区三区 | 久久性生活片 | 精品www | 任你躁av一区二区三区 | 日本a级片视频 | 色哟哟免费观看 | 国产精品怡红院 | 天堂在线 | 丝袜一区二区三区 | 麻豆视频网站在线观看 | 国产一级黄色电影 | 欧美一级片在线免费观看 | 国产精品嫩草av | 天天鲁一鲁摸一摸爽一爽 | 日本少妇高潮喷水xxxxxxx | 国产乱来| 欧美女人交配视频 | 丁香婷婷在线观看 | 色悠悠在线视频 | 校园春色欧美 | 亚洲性xxxx | 剧情av在线 | 色一区二区三区四区 | 久草aⅴ | 久久国精品 | 精品人妻一区二区三区浪潮在线 | 美女又大又黄 | 成人免费入口 | www视频在线观看免费 | av黄色成人 | 快播色图| 中文字幕人妻丝袜乱一区三区 |