Machine Learning A-Z: Hands-On Python and java
About Lesson
      • How they work, implementation in Python and Java.

How Decision Trees Work

Decision trees are a popular supervised learning algorithm used for classification and regression tasks. They work by recursively splitting the dataset into subsets based on feature values, forming a tree-like structure where each node represents a decision point and each branch represents an outcome.

Implementation in Python (Scikit-learn)

Step 1: Import Libraries

pythonCopier le codefrom sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import train_test_split

Step 2: Prepare Data and Split into Training/Testing Sets

pythonCopier le codeX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Step 3: Train the Model

pythonCopier le codemodel = DecisionTreeClassifier()model.fit(X_train, y_train)

Step 4: Make Predictions

pythonCopier le codepredictions = model.predict(X_test)

Implementation in Java (Weka)

Step 1: Load the Dataset

javaCopier le codeInstances data = new Instances(new BufferedReader(new FileReader(“data.arff”)));data.setClassIndex(data.numAttributes() – 1);

Step 2: Build the Decision Tree Model

javaCopier le codeJ48 tree = new J48();  // J48 is Weka’s implementation of C4.5 decision treetree.buildClassifier(data);

Step 3: Evaluate the Model

javaCopier le codeEvaluation eval = new Evaluation(data);eval.crossValidateModel(tree, data, 10, new Random(1));