|
Classes /
NSCcd regression Dtrain = load('mcycleTrain.txt'); Xtr=Dtrain(:,2); Ytr=Dtrain(:,1); figure; plot(Xtr,Ytr,'r.'); Xtrain = [Xtr.^0 Xtr.^1]; Ytrain = Ytr; th = regress( Ytrain, Xtrain ) Xp = [0:.01:2]'; Xp1= [Xp.^0 Xp.^1]; rfun = Xp1 * th; hold on; plot(Xp,rfun,'k-'); % degree 5 polynomial fit Xtrain = [Xtr.^0 Xtr.^1 Xtr.^2 Xtr.^3 Xtr.^4 Xtr.^5]; th = regress( Ytrain, Xtrain ) Xp = [0:.01:2]'; %Xp1= [Xp.^0 Xp.^1 Xp.^2 Xp.^3 Xp.^4 Xp.^5]; Xp1= polyx( Xp , 5); rfun = Xp1 * th; hold on; plot(Xp,rfun,'b-'); % X = Xtr(1:15,:); Y=Ytr(1:15); for p=0:10, th = regress( Y, polyx(X,p) ); Yhat = polyx(X,p)*th; mseTrain(p+1) = mean( (Y-Yhat).^2 ); end ; figure; semilogy(0:10,mseTrain,'r-'); Dtest = load('mcycleTest.txt'); Xtest=Dtest(:,2); Ytest=Dtest(:,1); for p=0:10, th = regress( Y, polyx(X,p) ); Yhat = polyx(Xtest,p)*th; mseTest(p+1) = mean( (Ytest-Yhat).^2 ); end; hold on; semilogy(0:10,mseTest,'g-'); Dtrain = load('mcycleTrain.txt');
Xtr=Dtrain(1:15,2); Ytr=Dtrain(1:15,1);
Dtest = load('mcycleTest.txt');
Xtest=Dtest(:,2); Ytest=Dtest(:,1);
mseTrain=[]; mseTest=[];
p=10; alpha=10.^[-5 -4 -3 -2 -1 0 1]
for j=1:length(alpha),
X1 = polyx(Xtr,p);
th = Ytr'*X1*inv(X1'*X1 + alpha(j)*eye(p+1)),
Yhat = polyx(Xtr,p)*th';
mseTrain(j) = mean( (Ytr-Yhat).^2 );
Yhat = polyx(Xtest,p)*th';
mseTest(j) = mean( (Ytest-Yhat).^2 );
end ;
figure; loglog(alpha,mseTrain,'r-',alpha,mseTest,'g-');
% L1-regularized models
addpath('../downloads/glmnet_matlab');
which glmnet
Xtr = polyx(Xtrain(1:15,:),10); Ytr=Ytrain(1:15,:); Xtr = polyx(Xtrain,10); Ytr=Ytrain; fit = glmnet(Xtr,Ytr); % default: gaussian lasso glmnetPrint(fit); % show penalties corresponding to changes in feature set theta = glmnetCoef(fit,lambda); % get actual coefficients at some value of lambda lambda=1.5; Yhat = glmnetPredict(fit, 'response', Xtest, lambda) % make predictions Xp = [0:.01:2]'; Xp1= polyx( Xp , 10); rfun=glmnetPredict(fit,'response',Xp1,1.5); figure; plot(Xp,rfun,'k-',Xtr,Ytr,'r.') =================================== D=load('iris.txt'); X=D(:,1:2); Y=D(:,end); I0=find(Y==0); I1=find(Y==1); I2=find(Y==2); figure; plot(X(I0,1),X(I0,2),'bo'); hold on; plot(X(I1,1),X(I1,2),'gx'); hold on; plot(X(I2,1),X(I2,2),'rs'); knn = knnClassifyC(1,X,Y); knn = knn.setK(1); class2DPlot(knn,X,Y); % Logistic regression %Y=D(:,end); X=D(Y~=0,1:2); Y=Y(Y~=0); %coeff = glmfit(X, Y-1, 'binomial', 'link','logit'); %phat = glmval(coeff, Xtest, 'logit'); D=load('iris.txt'); Y=D(:,end); X=D(Y~=0,1:2); Y=Y(Y~=0); lR = logRegC(X,Y-1); class2DPlot(lR,X,Y-1); lR = logRegPC(X,Y-1); class2DPlot(lR,X,Y-1); lR=logRegL1C(.1,X,Y-1) class2DPlot(lR,X,Y-1); Dtrain = load('ALL_AML_train.csv'); Dtest = load('ALL_AML_test.csv'); Ytr = Dtrain(1,:)'; Xtr = Dtrain(2:end,:)'; Yte = Dtest(1,:)'; Xte = Dtest(2:end,:)'; fit = glmnet(Xtr,Ytr+1,'binomial'); glmnetPrint(fit); % 2 features at lambda = .35 coef = glmnetCoef(fit, .35); find(coef ~= 0) % find non-zero feature coefficients lR = logRegC( Xtr(:,[3321 4848]), Ytr ); class2DPlot(lR, Xtr(:,[3321 4848]), Ytr); Decision trees: classregtree N=50000; M=20; % many examples of 20 variables in a Markov chain x=zeros(N,M); x(:,1) = rand(N,1) > .5; for j=2:M, x(:,j) = mod(x(:,j-1) + (rand(N,1) > .9) ,2); end; x = x + .1*randn(size(x)); C = cov(x); D=diag(diag(C)); C=D^(-.5) * C * D^(-.5); J=inv(C); D=diag(diag(J)); J=D^(-.5) * J * D^(-.5); figure(1); imagesc(C); figure(2); imagesc(J); N=50; M=20; % many examples of 20 variables in a Markov chain x=zeros(N,M); x(:,1) = rand(N,1) > .5; for j=2:M, x(:,j) = mod(x(:,j-1) + (rand(N,1) > .9) ,2); end; x = x + .1*randn(size(x)); C = cov(x); D=diag(diag(C)); C=D^(-.5) * C * D^(-.5); J=inv(C); D=diag(diag(J)); J=D^(-.5) * J * D^(-.5); figure(1); imagesc(C); figure(2); imagesc(J); C=cov(x); fit = glmnet(x(:,2:end),x(:,1)); % default: gaussian lasso glmnetPrint(fit); % show penalties corresponding to changes in feature set theta = glmnetCoef(fit,lambda); % get actual coefficients at some value of lambda % Regression version of network estimation idx=10; rest=[1:idx-1,idx+1:M]; fit = glmnet(x(:,rest),x(:,idx)); % default: gaussian lasso glmnetPrint(fit); % show penalties corresponding to changes in feature set lambda = .11; % gives 3 non-zero terms theta = glmnetCoef(fit,lambda); % get actual coefficients at some value of lambda rest(find(theta~=0)), % Graphical lasso |