Learn WEKA with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
Weka Classification Example
// Using Weka GUI:
// 1. Open Weka Explorer
// 2. Load dataset (e.g., iris.arff)
// 3. Select 'Classify' tab
// 4. Choose classifier (e.g., J48 decision tree)
// 5. Click 'Start' to train and evaluate
// Using Java API:
// Instances data = new Instances(new BufferedReader(new FileReader("iris.arff")));
// data.setClassIndex(data.numAttributes() - 1);
// Classifier cls = new J48();
// cls.buildClassifier(data);
An example showing how to perform a simple classification task using Weka's GUI or Java API.
2
Weka Regression Example
// Using GUI:
// 1. Load dataset with numeric target attribute
// 2. Go to 'Classify' tab
// 3. Select 'LinearRegression' classifier
// 4. Click 'Start'
// Using Java API:
// Instances data = new Instances(new BufferedReader(new FileReader("housing.arff")));
// data.setClassIndex(data.numAttributes() - 1);
// LinearRegression lr = new LinearRegression();
// lr.buildClassifier(data);
Using Weka to perform a regression task with Linear Regression.
3
Weka Clustering Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Cluster' tab
// 3. Select 'SimpleKMeans'
// 4. Set number of clusters
// 5. Click 'Start'
// Using Java API:
// Instances data = new Instances(new BufferedReader(new FileReader("iris.arff")));
// SimpleKMeans kmeans = new SimpleKMeans();
// kmeans.setNumClusters(3);
// kmeans.buildClusterer(data);
Perform clustering using K-Means in Weka GUI or API.
4
Weka Attribute Selection Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Select attributes' tab
// 3. Choose 'CfsSubsetEval' and 'BestFirst'
// 4. Click 'Start'
// Using Java API:
// AttributeSelection attrSel = new AttributeSelection();
// CfsSubsetEval eval = new CfsSubsetEval();
// BestFirst search = new BestFirst();
// attrSel.setEvaluator(eval);
// attrSel.setSearch(search);
// attrSel.SelectAttributes(data);
Selecting important attributes using Weka's AttributeSelection GUI or API.
5
Weka Cross Validation Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Classify' tab
// 3. Choose classifier (e.g., J48)
// 4. Select 'Cross-validation', set folds to 10
// 5. Click 'Start'
// Using Java API:
// Evaluation eval = new Evaluation(data);
// eval.crossValidateModel(cls, data, 10, new Random(1));
// System.out.println(eval.toSummaryString());
Performing k-fold cross-validation using Weka classifiers.
6
Weka Data Preprocessing Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Preprocess' tab
// 3. Apply filters such as 'ReplaceMissingValues', 'Normalize'
// 4. Save preprocessed data
// Using Java API:
// ReplaceMissingValues filter = new ReplaceMissingValues();
// filter.setInputFormat(data);
// Instances newData = Filter.useFilter(data, filter);
Cleaning and normalizing data using Weka filters.
7
Weka Ensemble Learning Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Classify' tab
// 3. Select 'RandomForest' or 'Bagging'
// 4. Click 'Start'
// Using Java API:
// RandomForest rf = new RandomForest();
// rf.buildClassifier(data);
Using ensemble methods like Bagging or RandomForest in Weka.
8
Weka Text Classification Example
// Using GUI:
// 1. Load text dataset
// 2. Go to 'Preprocess' tab
// 3. Apply 'StringToWordVector' filter
// 4. Go to 'Classify' tab and select classifier
// 5. Click 'Start'
// Using Java API:
// StringToWordVector filter = new StringToWordVector();
// filter.setInputFormat(data);
// Instances newData = Filter.useFilter(data, filter);
Processing text data using StringToWordVector filter and a classifier.
9
Weka Association Rule Mining Example
// Using GUI:
// 1. Load dataset
// 2. Go to 'Associate' tab
// 3. Choose 'Apriori' algorithm
// 4. Click 'Start'
// Using Java API:
// Apriori model = new Apriori();
// model.buildAssociations(data);
Discover association rules using the Apriori algorithm.
10
Weka Model Saving and Loading Example
// Save model:
// SerializationHelper.write("model.model", cls);
// Load model:
// Classifier loadedCls = (Classifier) SerializationHelper.read("model.model");
// Evaluation eval = new Evaluation(data);
// eval.evaluateModel(loadedCls, data);
Saving and loading trained Weka models using Java API.