ASCATEGORICAL must have one element for each variable
Show older comments
Marketing Campaign Value Count Percent 0 2 66.67% 1 1 33.33% Training Set Value Count Percent 0 2 100.00% Test Set Value Count Percent 1 1 100.00%
Error using classreg.regr.FitObject/checkAsCat (line 912)
ASCATEGORICAL must have one element for each variable.
Error in classreg.regr.FitObject/assignData (line 314) viIsCategorical = classreg.regr.FitObject.checkAsCat(viIsCategorical,asCat,nvars,false,viName);
Error in classreg.regr.TermsRegression/assignData (line 349) model = assignData@classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in GeneralizedLinearModel/assignData (line 800) model = assignData@classreg.regr.TermsRegression(model,X,y,w,asCat,dummyCoding,varNames,excl);
Error in GeneralizedLinearModel.fit (line 1297) model = assignData(model,X,y,weights,offset,binomN,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in crater (line 235) glm = GeneralizedLinearModel.fit(Xtrain,double(Ytrain)-1,'linear','Distribution','binomial','link','logit','CategoricalVars',catPred);
if true
% code
clc clear all
% Load Datasets
Dataset = 'F:\Mtech\Mtech-3rd\Research\Matlab\CDA\Machine_Learning\data'; Testset = 'F:\Mtech\Mtech-3rd\Research\Matlab\CDA\Machine_Learning\data';
% we need to process the images first. % Convert your images into grayscale % Resize the images
width=100; height=100; DataSet = cell([], 1);
for i=1:length(dir(fullfile(Dataset,'*.jpg')))
% Training set process
k = dir(fullfile(Dataset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Dataset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Dataset,filesep,k{j}));
% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
% tempfeaturepoints = detectSURFFeatures(ReadImage);
% [f,vpts] = extractFeatures(ReadImage, %tempfeaturepoints);
% DataSet{j} = f;
DataSet{j} = double(imresize(tempImage,[width height])); % array of images
else
% tempImage = rgb2gray(ReadImage);
% tempfeaturepoints = detectSURFFeatures(tempImage);
% [f,vpts] = extractFeatures(tempImage, tempfeaturepoints);
% DataSet{j} = f;
DataSet{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images
end
end
end
TestSet = cell([], 1);
for i=1:length(dir(fullfile(Testset,'*.jpg')))
% Training set process
k = dir(fullfile(Testset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Testset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Testset,filesep,k{j}));
% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
TestSet{j} = double(imresize(tempImage,[width height])); % array of images
else
TestSet{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images
end
end
end
% Prepare class label for first run of svm % I have arranged labels 1 & 2 as per my convenience. % It is always better to label your images numerically % Please note that for every image in our Dataset we need to provide one label. % we have 30 images and we divided it into two label groups here. train_label = zeros(size(30,1),1); train_label(1:15,1) = 1; % 1 = Airplanes train_label(16:30,1) = 2; % 2 = Faces
% Prepare numeric matrix for svmtrain Training_Set=[]; for i=1:length(DataSet) Training_Set_tmp = reshape(DataSet{i},1, 100*100); Training_Set=[Training_Set;Training_Set_tmp]; end
Test_Set=[]; for j=1:length(TestSet) Test_set_tmp = reshape(TestSet{j},1, 100*100); Test_Set=[Test_Set;Test_set_tmp]; end bank=Training_Set; Response=zeros(3,1); for i=1:length(Response) if(i<2) Response(i,1)=1; %1=crater% else Response(i,1)=0; %0=non-crater% end end
%% Prepare the Data: Response and Predictors % We can segregate the data into response and predictors. This will make it % easier to call subsequent functions which expect the data in this format.
% Response Y = Response; disp('Marketing Campaign') tabulate(Y) % Predictor matrix X = double(bank(:,1:end-1));
%% Category Predictor
S = ['rays ';'peak ';'walls']; C = cellstr(S); [bank_row,bank_col]= size(bank); p=1;
for i=1:bank_row if(p>3) p=1; end crater_attrib{i}= C{p}; p=p+1; end attribute=[crater_attrib]';
%% category predictor %bank=cell2dataset(bank); catPred=false(bank_row,1); for i=1:bank_row if(strcmp(bank(i,1),C(1)) strcmp(bank(i,1),C(3))) catPred(i)=true; end end bank=(bank)'; catPred=(catPred)'; %% Adding category to data
bank=vertcat(crater_attrib, num2cell(bank));
%% Cross Validation
% Cross validation is almost an inherent part of machine learning. Cross % validation may be used to compare the performance of different predictive % modeling techniques. In this example, we use holdout validation. Other % techniques including k-fold and leave-one-out cross validation are also % available. % % In this example, we partition the data into training set and test set. % The training set will be used to calibrate/train the model parameters. % The trained model is then used to make a prediction on the test set. % Predicted values will be compared with actual data to compute the % confusion matrix. Confusion matrix is one way to visualize the % performance of a machine learning technique.
% In this example, we will hold 40% of the data, selected randomly, for % test phase. cv = cvpartition(length(bank(1, 1:end)),'holdout',0.40); Vtest=training(cv); % Training set Xtrain = X(training(cv),:); Ytrain = Y(training(cv),:); % Test set Xtest = X(test(cv),:); Ytest = Y(test(cv),:);
disp('Training Set') tabulate(Ytrain) disp('Test Set') tabulate(Ytest) % % %% Prepare Predictors/Response for Neural Networks % % When using neural networks the appropriate way to include categorical % % predictors is as dummy indicator variables. An indicator variable has % % values 0 and 1. % % [XtrainNN, YtrainNN, XtestNN, YtestNN] = preparedataNN(bank, catPred, cv); % [XtrainNN, YtrainNN, XtestNN, YtestNN] = preparedataNN(bank, cv);
% %% Speed up Computations using Parallel Computing % % If Parallel Computing Toolbox is available, the computation will be % % distributed to 2 workers for speeding up the evaluation. % % if matlabpool('size') == 0 % matlabpool open 2 % end % % %% Neural Networks % % Neural Network Toolbox supports supervised learning with feedforward, % % radial basis, and dynamic networks. It supports both classification and % % regression algorithms. It also supports unsupervised learning with % % self-organizing maps and competitive layers. % % % % One can make use of the interactive tools to setup, train and validate a % % neural network. It is then possible to auto-generate the code for the % % purpose of automation. In this example, the auto-generated code has been % % updated to utilize a pool of workers, if available. This is achieved by % % simply setting the useParallel flag while making a call to train. % % % % [net,~] = train(net,inputs,targets,'useParallel','yes'); % % % % If a GPU is available, it may be utilized by setting the useGPU flag. % % % % The trained network is used to make a prediction on the test data and % % confusion matrix is generated for comparison with other techniques. % % % Use modified autogenerated code to train the network % [~, net] = NNfun(XtrainNN,YtrainNN); % % % Make a prediction for the test set % Y_nn = net(XtestNN'); % Y_nn = round(Y_nn'); % % % Compute the confusion matrix % C_nn = confusionmat(YtestNN,Y_nn); % % Examine the confusion matrix for each class as a percentage of the true class % C_nn = bsxfun(@rdivide,C_nn,sum(C_nn,2)) * 100 %#ok<*NOPTS>
% %% Other Machine Learning Techniques % % Statistics Toolbox features a number of supervised and unsupervised % % machine learning techniques. It supports both classification and % % regression algorithms. The supervised learning techniques range from % % non-linear regression, generalized linear regression, discriminant % % analysis, SVMs to decision trees and ensemble methods. % % % % In this example, we make use of some of these techniques to perform % % predictive modeling. Observe that once the data has been prepared, the % % syntax to utilize the different modeling techniques is very similar. Most % % of these techniques can handle categorical predictors. The user can % % conveniently supply information about different parameters associated % % with the different algorithms. % % %% Generalized Linear Model - Logistic Regression % % In this example, a logistic regression model is leveraged. Response may % % follow normal, binomial, Poisson, gamma, or inverse Gaussian % % distribution. % % % % Since the response in this data set is binary, binomial distribution is % % suitable. % % % Train the classifier glm = GeneralizedLinearModel.fit(Xtrain,double(Ytrain)-1,'linear','Distribution','binomial','link','logit','CategoricalVars',catPred);
% Make a prediction for the test set Y_glm = glm.predict(Xtest); Y_glm = round(Y_glm) + 1;
% Compute the confusion matrix C_glm = confusionmat(double(Ytest),Y_glm); % Examine the confusion matrix for each class as a percentage of the true class C_glm = bsxfun(@rdivide,C_glm,sum(C_glm,2)) * 100
%% Discriminant Analysis % Discriminant analysis is a classification method. It assumes that % different classes generate data based on different Gaussian % distributions. Linear discriminant analysis is also known as the Fisher % discriminant. % % Here, a quadratic discriminant classifier is used.
% Train the classifier da = ClassificationDiscriminant.fit(Xtrain,Ytrain,'discrimType','linear');
% Make a prediction for the test set Y_da = da.predict(Xtest);
% Compute the confusion matrix C_da = confusionmat(Ytest,Y_da); % Examine the confusion matrix for each class as a percentage of the true class C_da = bsxfun(@rdivide,C_da,sum(C_da,2)) * 100
%% Classification Using Nearest Neighbors % Categorizing query points based on their distance to points in a training % dataset can be a simple yet effective way of classifying new points. % Various distance metrics such as euclidean, correlation, hamming, % mahalonobis or your own distance metric may be used.
% Train the classifier knn = ClassificationKNN.fit(Xtrain,Ytrain,'Distance','seuclidean');
% Make a prediction for the test set Y_knn = knn.predict(Xtest);
% Compute the confusion matrix C_knn = confusionmat(Ytest,Y_knn); % Examine the confusion matrix for each class as a percentage of the true class C_knn = bsxfun(@rdivide,C_knn,sum(C_knn,2)) * 100 % % %% Naive Bayes Classification % % Naive Bayes classification is based on estimating P(X|Y), the probability % % or probability density of features X given class Y. The Naive Bayes % % classification object provides support for normal (Gaussian), kernel, % % multinomial, and multivariate multinomial distributions % % % The multivariate multinomial distribution (mvmn) is appropriate for % % categorical features dist = repmat({'normal'},1,bank_col-1); dist(catPred) = {'mvmn'};
% Train the classifier Nb = NaiveBayes.fit(Xtrain,Ytrain,'Distribution',dist);
% Make a prediction for the test set Y_Nb = Nb.predict(Xtest);
% Compute the confusion matrix C_nb = confusionmat(Ytest,Y_Nb); % Examine the confusion matrix for each class as a percentage of the true class C_nb = bsxfun(@rdivide,C_nb,sum(C_nb,2)) * 100
%% Support Vector Machines % Support vector machine (SVM) is supported for binary response variables. % An SVM classifies data by finding the best hyperplane that separates all % data points of one class from those of the other class.
opts = statset('MaxIter',30000); % Train the classifier svmStruct = svmtrain(Xtrain,Ytrain,'kernel_function','rbf','kktviolationlevel',0.1,'options',opts);
% Make a prediction for the test set Y_svm = svmclassify(svmStruct,Xtest); C_svm = confusionmat(Ytest,Y_svm); % Examine the confusion matrix for each class as a percentage of the true class C_svm = bsxfun(@rdivide,C_svm,sum(C_svm,2)) * 100
% %% Decision Trees % % Classification trees and regression trees are two kinds of decision % % trees. A decision tree is a flow-chart like structure in which internal % % node represents test on an attribute, each branch represents outcome of % % test and each leaf node represents a response (decision taken after % % computing all attributes). Classification trees give responses that are % % nominal, such as 'true' or 'false'. Regression trees give numeric % % responses. % % tic % % Train the classifier % t = ClassificationTree.fit(Xtrain,Ytrain,'CategoricalPredictors',catPred); % toc % % % Make a prediction for the test set % Y_t = t.predict(Xtest); % % % Compute the confusion matrix % C_t = confusionmat(Ytest,Y_t); % % Examine the confusion matrix for each class as a percentage of the true class % C_t = bsxfun(@rdivide,C_t,sum(C_t,2)) * 100
% %% Ensemble Learning: TreeBagger % % Bagging stands for bootstrap aggregation. Every tree in the ensemble is % % grown on an independently drawn sample of input data. To compute % % prediction for the ensemble of trees, TreeBagger takes an average of % % predictions from individual trees (for regression) or takes votes from % % individual trees (for classification). Ensemble techniques such as % % bagging combine many weak learners to produce a strong learner. % % % % From a marketing perspective, as we are creating this predictive model, % % it may be more important for us to classify yes correctly than a % % no. If that is the case, we can include our opinion using the cost % % matrix. Here, cost matrix specifies that it is 5 times more costly to % % classify a yes as a no.
% Cost of misclassification % cost = [0 1 % 5 0]; % opts = statset('UseParallel',true); % % Train the classifier % tb = TreeBagger(150,Xtrain,Ytrain,'method','classification','categorical',catPred,'Options',opts,'OOBVarImp','on','cost',cost); % % % Make a prediction for the test set % [Y_tb, classifScore] = tb.predict(Xtest); % Y_tb = nominal(Y_tb); % % % Compute the confusion matrix % C_tb = confusionmat(Ytest,Y_tb); % % Examine the confusion matrix for each class as a percentage of the true class % C_tb = bsxfun(@rdivide,C_tb,sum(C_tb,2)) * 100 % % %% Compare Results % % This visualization function is making use of a couple files downloaded % % from MATLAB Central, the user % % community website. We are leveraging social computing along the way to % % help us in our effort. % % Cmat = [C_nn C_glm C_da C_knn C_nb C_svm C_t C_tb]; % labels = {'Neural Net ', 'Logistic Regression ', 'Discriminant Analysis ',... % 'k-nearest Neighbors ', 'Naive Bayes ', 'Support VM ', 'Decision Trees ', 'TreeBagger '}; Cmat = [ C_da C_knn C_nb C_svm ]; labels = { 'Discriminant Analysis ',... 'k-nearest Neighbors ', 'Naive Bayes ', 'Support VM '};
comparisonPlot( Cmat, labels )
%% ROC Curve for Classification by TreeBagger % Another way of exploring the performance of a classification ensemble is % to plot its Receiver Operating Characteristic (ROC) curve.
% [xx,yy,~,auc] = perfcurve(Ytest, classifScore(:,2),'yes'); % figure; % plot(xx,yy) % xlabel('False positive rate'); % ylabel('True positive rate') % title('ROC curve for ''yes'', predicted vs. actual response (Test Set)') % text(0.5,0.25,{'TreeBagger with full feature set',strcat('Area Under Curve = ',num2str(auc))},'EdgeColor','k');
% %% Simplify Model - Optional % % One may choose to examine the models further. One may even be able to % % improve the performance of the models. It is also possible to estimate % % importance of the different features, reduce the dimensionality of % % feature set etc. % % %% Estimating a Good Ensemble Size % % Examining the out-of-bag error may give an insight into determining a % % good ensemble size. % % figure; % plot(oobError(tb)); % xlabel('Number of Grown Trees'); % ylabel('Out-of-Bag Classification Error/Misclassification Probability'); % % %% Estimating Feature Importance % % Feature importance measures the increase in prediction error if the % % values of that variable are permuted across the out-of-bag observations. % % This measure is computed for every tree, then averaged over the entire % % ensemble and divided by the standard deviation over the entire ensemble. % % figure; % bar(tb.OOBPermutedVarDeltaError); % ylabel('Out-Of-Bag Feature Importance'); % set(gca,'XTick',1:16) % names2 = names; % names2{5} = ' default'; % set(gca,'XTickLabel',names2) % % Use file submitted from a user at MATLAB Central to rotate labels % rotateXLabels( gca, 60 ) % [~,idxvarimp] = sort(tb.OOBPermutedVarDeltaError, 'descend'); % % %% Sequential Feature Selection % % Feature selection reduces the dimensionality of data by selecting only a % % subset of measured features (predictor variables) to create a model. % % Selection criteria involves the minimization of a specific measure of % % predictive error for models fit to different subsets. % % % % Sequential feature selection can be computationally intensive. It can % % benefit significantly from parallel computing. % % opts = statset('UseParallel',true); % critfun = @(Xtr,Ytr,Xte,Yte)featureImp(Xtr,Ytr,Xte,Yte,'TreeBagger'); % % The top 5 features determined in the previous step have been included, % % to reduce the number of combinations to be tried by sequentialfs % [fs,history] = sequentialfs(critfun,Xtrain,Ytrain,'options',opts,'keepin',idxvarimp(1:5)); % disp('Included features:'); % disp(names(fs)'); % % %% TreeBagger with Reduced Feature Set % % opts = statset('UseParallel',true); % tb_r = TreeBagger(120, Xtrain(:,fs),Ytrain,'method','classification','categorical',catPred(:,fs),'Options',opts,'cost',cost); % [Y_tb_r, classifScore] = tb_r.predict(Xtest(:,fs)); % Y_tb_r = nominal(Y_tb_r); % C_tb_r = confusionmat(Ytest,Y_tb_r); % C_tb_r = bsxfun(@rdivide,C_tb_r,sum(C_tb_r,2)) * 100 % % %% Compare Results % % Cmat = [C_nn C_glm C_da C_knn C_nb C_svm C_t C_tb C_tb_r]; % labels = {'Neural Net ', 'Logistic Regression ', 'Discriminant Analysis ',... % 'k-nearest Neighbors ', 'Naive Bayes ', 'Support VM ', 'Decision Trees ', 'TreeBagger ', 'Reduced TB '}; % % comparisonPlot( Cmat, labels ) % % %% ROC Curve for Classification by Reduced TreeBagger % % [xx,yy,~,auc] = perfcurve(Ytest, classifScore(:,2),'yes'); % figure; % plot(xx,yy) % xlabel('False positive rate'); % ylabel('True positive rate') % title('ROC curve for ''yes'', predicted vs. actual response (Test Set)') % text(0.5,0.25,{'TreeBagger with reduced feature set',strcat('Area Under Curve = ',num2str(auc))},'EdgeColor','k'); % % %% Shut Down Workers % % Release the workers if there is no more work for them % % if matlabpool('size') > 0 % matlabpool close % end % %% References % % # [Moro et al., 2011] S. Moro, R. Laureano and P. Cortez. Using Data Mining for Bank Direct Marketing: An Application of the CRISP-DM Methodology. % % In P. Novais et al. (Eds.), Proceedings of the European Simulation and Modelling Conference - ESM'2011, pp. 117-121, Guimarães, Portugal, October, 2011. EUROSIS.

%code
end
1 Comment
Greg Heath
on 12 Feb 2014
Please
1. Explain your problem.
2. Please clarify what your inputs and outputs are.
3. Please format
Answers (0)
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!