In this post, we'll learn how to build a logistic regression model using TensorFlow.js and Node.js.
Logistic regression is a statistical method for predicting binary outcomes. That is, it can be used to predict whether an event will occur or not. For example, we can use logistic regression to predict whether a patient will develop a disease, based on certain characteristics.
Logistic regression is a type of linear regression, but with a few important differences. First, the outcome variable is binary, which means it can only take two values (0 or 1). Second, the model is fit using a maximum likelihood estimation, rather than least squares.
To get started, we need to install TensorFlow.js and Node.js. We can do this using the following commands:
npm install -g tensorflow
npm install -g node
Once TensorFlow.js and Node.js are installed, we can create a new file called logistic-regression.js
and start coding.
The first step is to load our data. We'll use the tensorflow.js
library to load the data into a tf.tensor
object.
const tf = require('tensorflow');
// Load the data
const data = tf.tensor([
[1, 2],
[2, 3],
[3, 4],
[4, 5],
]);
Next, we need to define our model. We'll use a logistic regression model with two input features (x1
and x2
) and one output (y
).
// Define the model
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [2] }));
model.compile({ loss: 'binaryCrossentropy', optimizer: 'sgd' });
Now that we have our data and model, we can train our model. We'll use the fit
method to train our model on the data.
// Train the model
model.fit(data, tf.tensor([1, 1, 0, 0]), {
epochs: 10,
callbacks: {
onEpochEnd: (epoch, log) => {
console.log(`Epoch ${epoch}: loss = ${log.loss}`);
},
},
});
Once our model is trained, we can use it to make predictions. We'll use the predict
method to predict the output for new data.
// Make predictions
model.predict(tf.tensor([[5, 6]])).print(); // [[0.5]]
model.predict(tf.tensor([[6, 7]])).print(); // [[0.5]]
As we can see, our model predicts the output as 0.5 for both inputs. This means that our model is predicting a 50% chance of the event occurring.
In this post, we've learned how to build a logistic regression model using TensorFlow.js and Node.js. We've also seen how to use our model to make predictions on new data.