Introduction to Machine Learning - Java Code Sheet
1. Introduction to Machine Learning
Definition: Machine Learning is a branch of AI that allows systems to learn and improve from
experience without being explicitly programmed.
Key Types of Machine Learning:
- Supervised Learning
- Unsupervised Learning
- Reinforcement Learning
Common Algorithms:
- Linear Regression
- Logistic Regression
- Decision Trees
- K-Nearest Neighbors (KNN)
- Support Vector Machines (SVM)
- Neural Networks
2. Linear Regression
Definition: Linear regression is used to model the relationship between a dependent variable (y) and
one or more independent variables (X).
Code Example (Simple Linear Regression):
import [Link];
public class LinearRegressionExample {
public static void main(String[] args) {
SimpleRegression regression = new SimpleRegression();
[Link](new double[][] { {1, 1}, {2, 2}, {3, 2.5}, {4, 3.5} });
[Link]("Slope: " + [Link]());
[Link]("Intercept: " + [Link]());
double predicted = [Link](5);
[Link]("Predicted value for x=5: " + predicted);
Key Operations: Fitting, Prediction, Error Calculation
3. Logistic Regression
Definition: Logistic regression is used for binary classification problems. It predicts the probability of
a binary outcome.
Code Example (Logistic Regression using Weka):
import [Link];
import [Link];
import [Link];
public class LogisticRegressionExample {
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
[Link](new [Link]("[Link]"));
Instances data = [Link]();
[Link]([Link]() - 1);
Logistic logistic = new Logistic();
[Link](data);
[Link]("Logistic Regression Model: " + logistic);
Key Operations: Training, Prediction, Model Evaluation
4. K-Nearest Neighbors (KNN)
Definition: KNN is a simple algorithm used for both classification and regression, where the output is
based on the majority class or average of k nearest points.
Code Example (KNN Classification using Weka):
import [Link];
import [Link];
import [Link];
public class KNNExample {
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
[Link](new [Link]("[Link]"));
Instances data = [Link]();
[Link]([Link]() - 1);
IBk knn = new IBk();
[Link](3);
[Link](data);
[Link]("KNN Model: " + knn);
Key Operations: Training, Prediction, Model Evaluation
5. Decision Trees
Definition: A decision tree is a flowchart-like structure where each internal node represents a
decision on an attribute, and each leaf node represents a class label.
Code Example (Decision Tree using Weka):
import [Link].J48;
import [Link];
import [Link];
public class DecisionTreeExample {
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
[Link](new [Link]("[Link]"));
Instances data = [Link]();
[Link]([Link]() - 1);
J48 tree = new J48();
[Link](data);
[Link]("Decision Tree Model: " + tree);
Key Operations: Training, Prediction, Model Evaluation
6. Neural Networks
Definition: A neural network is a series of algorithms that attempt to recognize underlying
relationships in a set of data through a process that mimics the way the human brain operates.
Code Example (Simple Neural Network using Deeplearning4j):
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Nd4j;
public class NeuralNetworkExample {
public static void main(String[] args) {
MultiLayerNetwork model = new MultiLayerNetwork(new [Link]()
.list()
.layer(0, new [Link]().nIn(3).nOut(3).build())
.layer(1, new [Link]().nIn(3).nOut(1).build())
.build());
[Link]();
// Sample data
INDArray input = [Link](new double[] {1.0, 0.0, 1.0}, new int[]{1, 3});
INDArray output = [Link](input);
[Link](output);
Key Operations: Training, Prediction, Evaluation
7. Model Evaluation
Definition: The process of assessing the performance of a machine learning model using various
metrics.
Common Evaluation Metrics:
- Accuracy
- Precision
- Recall
- F1-Score
- ROC Curve and AUC
Code Example (Evaluating a Model):
import [Link];
import [Link];
import [Link];
import [Link];
public class ModelEvaluationExample {
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
[Link](new [Link]("[Link]"));
Instances data = [Link]();
[Link]([Link]() - 1);
Classifier classifier = new [Link].J48();
[Link](data);
Evaluation eval = new Evaluation(data);
[Link](classifier, data);
[Link]("Model Evaluation: " + [Link]());
Key Operations: Accuracy, Precision, Recall, F1-Score