程序代写案例-P1

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
P1
Square loss: 3.3348
Accuracy: 0.9385 {1pts)
Example Code: (3pts)
XTrain = load("d79train.mat").d79train;
XTest = load("d79test.mat")
.d79test;
% labels, same for train and test
y = cat(1, ones(1000,1), ones(1000,-1));
% Linear regression
% Add bias feature
X = [XTrain, ones(n,1)];
% Calculate using formula w = (X^T X)^{-1} X^T y
w = pinv(X’ * X) * X’ * y;
% Compute square loss and accuracy on the test set
xTest = cat(2, XTest,ones(2000,1));
yPred = xTest*w;
squareLoss = mean((yPred - yTest).^2)
preds = (yHat > 0).*2 - 1;
accuracy = mean(preds == yTest)
P2
Eigenvalue Estimated = 5.5527e+09 (1pts)
Eigenvalue by eig = 5.5527e+09
Example Code: (3pts)
% Power method
% Covariance matrix including the bias term
X = [XTrain, ones(2000,1)];
C = X’*X;
% Iterating until convergence
tol = 1e-10;
maxIter = 100;
v = rand(size(X,2), 1);
for i=1:maxIter;
v = C*v / norm(C*v);
newlambda = rmmissing(uniquetol(C*v ./ v, tol));
if length(newlambda) == 1
lambda = newlambda;
break;
end
end
lambda
% Check built-in eigenvalue function
eigLambda = max(eig(C));
eigLambda
P3
Example code 1#:


Example code 2#:

P4
Example code 1#:


Example code 2#:


P5

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468