Skip to content

Commit

Permalink
Add logistic_regression nodebook.
Browse files Browse the repository at this point in the history
  • Loading branch information
only4sim committed Nov 1, 2023
1 parent 45b99aa commit 32e9587
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions logistic_regression.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Logistic regression is a commonly used binary classification model, and it can be translated into o1js.\n",
"\n",
"Here's a step-by-step example of how to translate a scikit-learn logistic regression model to o1js:\n",
"\n",
"**Step 1: Export Model Parameters**\n",
"\n",
"In scikit-learn, a logistic regression model has two main parameters: the coefficients (`coef_`) and the intercept (`intercept_`). These parameters define the decision boundary of the model.\n",
"\n",
"Suppose we have the following scikit-learn model:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"import numpy as np\n",
"\n",
"# Sample data\n",
"X_train = np.array([[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]])\n",
"y_train = np.array([0, 1, 0])\n",
"\n",
"# Create and fit a logistic regression model\n",
"clf = LogisticRegression()\n",
"clf.fit(X_train, y_train)\n",
"\n",
"# Export model parameters\n",
"coefficients = clf.coef_\n",
"intercept = clf.intercept_"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1.75964519e-06 2.03090830e-06]]\n",
"[-0.69312133]\n"
]
}
],
"source": [
"print(coefficients)\n",
"print(intercept)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Step 2: Map Features**\n",
"\n",
"Ensure you have a mapping between the feature names (if available) and their corresponding positions or indices in the input data. In this example, we have two features: Feature 0 and Feature 1.\n",
"\n",
"**Step 3: Initialize o1js Model**\n",
"\n",
"In o1js, we can represent a logistic regression model as follows:\n",
"\n",
"```javascript\n",
"class LogisticRegressionModel {\n",
" constructor(coefficients, intercept) {\n",
" this.coefficients = coefficients; // Coefficients array\n",
" this.intercept = intercept; // Intercept value\n",
" }\n",
"\n",
" predict(input) {\n",
" // Compute the dot product of coefficients and input, add the intercept, and apply sigmoid function\n",
" const dotProduct = this.coefficients.reduce((sum, coeff, index) => {\n",
" return sum + coeff * input[index];\n",
" }, 0);\n",
" const z = dotProduct + this.intercept;\n",
" return sigmoid(z); // Apply sigmoid activation\n",
" }\n",
"}\n",
"```\n",
"\n",
"**Step 4: Translate Activation Functions**\n",
"\n",
"Ensure you have an equivalent implementation of the sigmoid activation function in o1js. You can refer to previous responses for the implementation.\n",
"\n",
"**Step 5: Perform Predictions**\n",
"\n",
"Implement the prediction logic in o1js using the `predict` method of the `LogisticRegressionModel` class. The prediction involves computing the dot product of coefficients and input features, adding the intercept, and applying the sigmoid activation.\n",
"\n",
"**Step 6: Write Tests**\n",
"\n",
"Develop tests to verify that the translated o1js model produces the same or similar predictions as the scikit-learn model on a set of test data. This step is crucial to validate the accuracy of the translation.\n",
"\n",
"Here's a simple test case in o1js:\n",
"\n",
"```javascript\n",
"const model = new LogisticRegressionModel(coefficients, intercept);\n",
"const input = [2.5, 3.5]; // Test input features\n",
"\n",
"const prediction = model.predict(input);\n",
"\n",
"console.log(`Prediction: ${prediction}`);\n",
"```\n",
"\n",
"This code sets up the o1js logistic regression model and performs a prediction on a test input.\n",
"\n",
"**Step 7: Optimize and Validate**\n",
"\n",
"Optimize the o1js model for efficiency and validate its accuracy against the scikit-learn model on various datasets. Ensure that the translated model behaves consistently with the original scikit-learn model."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 32e9587

Please sign in to comment.