Demo for this article can be found . here I was recently looking into how I can train a model in Python/scikit-learn and use it in another platform such as Node. I could not find a tool that works out of the box. So I tried implementing one. I will use logistic regression. Since it's a linear model, the parameters are easy to extract & interpret. Fortunately, its math is also easy and straightforward. My end goal is to make predictions on the Angular application without a backend. The classifier will run entirely on the front end application. For this example, we will do sentiment analysis on and I will skip the model training process as that's not the goal of this article. IMDB review dataset [I trained a model without paying not so much attention for the sake of the demo. Here is the .] After the training, we have a vectorizer and a trained logistic regression model. We can now extract the learned parameters. notebook The following method will write the desired model and vector parameters to a file in json format. json def save_model_params(model, vectorizer, file_name= ): feature_names = vectorizer.get_feature_names() d = dict() d[ ] = feature_names d[ ] = model.coef_.tolist() d[ ] = model.intercept_.tolist() d[ ] = model.classes_.tolist() open(file_name, ) f: f.write(json.dumps(d)) save_model_params(logRegModel, vectorizer) import 'model_params.json' 'words' 'values' 'intercept' 'classes' with "w" as We can copy the generated json file to our Angular project. And that's it! We are done with Python. Let's switch over to . Angular Angular To get intellisense, I created a model. This is schema of the json file that I created in the previous step. interface ModelParams { : string[], : number[][], : number[], : number[] } export words values intercept classes Next, I created two classes: Vectorizer and LogisticRegression as we need corresponding functionality on the Angular side. The purpose of the vectorizer is to create a count vector to be fed to the logistic regression model. I used CountVectorizer while training the model, hence this class will match the functionality: It will create a count vector. { ModelParams } ; { private punctuationRegex = ; private words: string[]; (params: ModelParams){ .words = params.words; } transform(sentence: string): number[] { sentence = sentence.replace( .punctuationRegex, ); words = sentence .split( ) .map( i.toLowerCase()) .filter( i.length); vector = .words.map( words.filter( ww === word).length); vector; } } import from './model' /** * This is a class that imitates vectorizer class in * scikit-learn library. * It has only transform method that takes the sentence, * removes punctuation, tokenizes and returns a count vector. */ export class Vectorizer /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g constructor this this '' const ' ' => i => i const this => word => ww return For logistic regression, we need and functions. Of course, method names don't need to match the scikit-learn's logistic regression. I'm giving the same names for clarity. predict predict_proba method will calculate the probability for each class and return an array (of probabilities). predict_proba predict_proba(vector: number[]){ b = / ( + .exp(- .func(vector, idx))); ( .classes.length <= ){ result = b( ); [ -result, result]; } ... } // https://realpython.com/logistic-regression-python/ // 𝑝(𝑥₁, 𝑥₂) = 1 / (1 + exp(−𝑓(𝑥₁, 𝑥₂))) const ( ) => idx: number 1 1 Math this // binary classification if this 2 const 0 return 1 private func(vector: number[], : number){ sum = .intercept[forClass]; vector.reduce( { iv = vv * .values[forClass][idx]; prev + iv; }, sum); } // intercept + w1 * x1 + w2 * x2 + ... forClass let this return ( )=> prev, vv, idx const this return predict method uses the ` ` to get the array of probabilities and returns the class that has the highest probability. We are doing a binary classification, so the result will be either 0 or 1. predict_proba predict(vector: number[]): number { probabilities = .predict_proba(vector); classes = .classes; sorted = classes .reduce( { prev.push({ class_, : probabilities[idx] }); prev; }, []) .sort( b.probability - a.probability); sorted[ ].class_; } const this const this const ( )=> prev, class_, idx probability return ( )=> a,b return 0 Angular Service We are done with ML stuff. Now, it's time to write our Angular service so that we can inject in our components and make predictions. The service is pretty simple. All it does is instantiates the Vectorizer and LogisticRegression classes with model params object that we extracted from scikit-learn. `predict` method returns true if the model predicts positive, returns false otherwise. * variables ; @Injectable({ : }) { model = variables any ModelParams; vectorizer = Vectorizer( .model); logReg = LogisticRegression( .model); predict(sentence: string): boolean { vector = .vectorizer.transform(sentence); .logReg.predict(vector) === ; } } // json file that we exported from python import as from './model_params.json' providedIn 'root' export class PredictionService as as new this new this const this return this 1 Component The component part is the easiest! Just inject the service and use it. That's all. @Component({ ... }) { comment: string = ; isPositive: boolean; ( private predictionService: PredictionService ) { } ngOnInit() { } predictIfReviewIsPositive(sentence: string){ isPositive = .predictionService.predict(sentence); .comment = sentence; .isPositive = isPositive; .log(sentence, isPositive); } } export class CommentComponent implements OnInit '' constructor const this this this console Let's test! If the model predict positive, it will put a smiling emoji next to it, frowning one if otherwise. Logs will also show whether it's predicted as positive. Conclusion In this fun application, we used the learned parameters in Angular to run predictions. Scikit-learn is a nice tool to train models however, none of my applications are running in Python and I don't want to write Python just to run predictions. With this approach, I can use it in any platform including a front end application. Link to the . app