代码生成
为 Statistics and Machine Learning Toolbox™ 函数生成 C/C++ 代码和 MEX 函数
MATLAB® Coder™ 可从支持代码生成的 Statistics and Machine Learning Toolbox 函数中生成可读且可移植的 C 代码和 C++ 代码。例如,您可以利用代码生成,将经过训练的支持向量机 (SVM) 分类模型部署到不能运行 MATLAB 的硬件设备上,在这些硬件设备上对新观测值进行分类。
您可以通过多种方式为这些函数生成 C/C++ 代码:
对机器学习模型的对象函数使用
saveLearnerForCoder
、loadLearnerForCoder
和codegen
(MATLAB Coder)。对机器学习模型的
predict
和update
对象函数使用由learnerCoderConfigurer
创建的代码生成器配置器。使用配置器配置代码生成选项,并在生成代码中更新模型参数。对于支持代码生成的其他函数,使用
codegen
。
您还可以生成定点 C/C++ 代码,用于预测一些机器学习模型。这种类型的代码生成需要 Fixed-Point Designer™。
要将机器学习模型的预测集成到 Simulink® 中,请使用 Statistics and Machine Learning Toolbox 库中的 MATLAB Function 模块或 Simulink 模块。
要了解代码生成,请参阅Introduction to Code Generation。
有关支持代码生成的函数的列表,请参阅函数列表(C/C++ 代码生成)。
函数
对象
模块
主题
代码生成工作流
- Introduction to Code Generation
Learn how to generate C/C++ code for Statistics and Machine Learning Toolbox functions. - General Code Generation Workflow
Generate code for Statistics and Machine Learning Toolbox functions that do not use machine learning model objects. - Code Generation for Prediction of Machine Learning Model at Command Line
Generate code for the prediction of a classification or regression model at the command line. - Code Generation for Incremental Learning
Generate code that implements incremental learning for binary linear classification at the command line. - Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App
Generate code for the prediction of a classification or regression model by using the MATLAB Coder app. - Code Generation for Prediction and Update Using Coder Configurer
Generate code for the prediction of a model using a coder configurer, and update model parameters in the generated code. - Specify Variable-Size Arguments for Code Generation
Generate code that accepts input arguments whose size might change at run time. - Generate Code to Classify Data in Table
Generate code for classifying data in a table containing numeric and categorical variables. - Create Dummy Variables for Categorical Predictors and Generate C/C++ Code
Convert categorical predictors to numeric dummy variables before fitting an SVM classifier and generating code. - Fixed-Point Code Generation for Prediction of SVM
Generate fixed-point code for the prediction of an SVM classification or regression model. - Code Generation and Classification Learner App
Train a classification model using the Classification Learner app, and generate C/C++ code for prediction. - Code Generation for Nearest Neighbor Searcher
Generate code for finding nearest neighbors using a nearest neighbor searcher model. - Code Generation for Probability Distribution Objects
Generate code that fits a probability distribution object to sample data and evaluates the fitted distribution object. - Code Generation for Binary GLM Logistic Regression Model Trained in Classification Learner
This example shows how to train a binary GLM logistic regression model using Classification Learner, and then generate C code that predicts labels using the exported classification model. - Code Generation for Anomaly Detection
Generate single-precision code that detects anomalies in data using a trained isolation forest model or one-class SVM.
分类和回归预测模块
- Predict Class Labels Using ClassificationSVM Predict Block
This example shows how to use the ClassificationSVM Predict block for label prediction in Simulink®. - Predict Class Labels Using ClassificationTree Predict Block
Train a classification decision tree model using the Classification Learner app, and then use the ClassificationTree Predict block for label prediction. - Predict Class Labels Using ClassificationLinear Predict Block
This example shows how to use the ClassificationLinear Predict block for label prediction in Simulink®. (自 R2023a 起) - Predict Class Labels Using ClassificationECOC Predict Block
Train an ECOC classification model, and then use the ClassificationECOC Predict block for label prediction. (自 R2023a 起) - Predict Class Labels Using ClassificationEnsemble Predict Block
Train a classification ensemble model with optimal hyperparameters, and then use the ClassificationEnsemble Predict block for label prediction. - Predict Class Labels Using ClassificationNaiveBayes Predict Block
Train a naive Bayes classification model, and then use the ClassificationNaiveBayes Predict block for label prediction. (自 R2024a 起) - Predict Class Labels Using ClassificationNeuralNetwork Predict Block
Train a neural network classification model, and then use the ClassificationNeuralNetwork Predict block for label prediction. - Predict Class Labels Using ClassificationKNN Predict Block
Train a nearest neighbor classification model, and then use the ClassificationKNN Predict block for label prediction. - Predict Class Labels Using ClassificationDiscriminant Predict Block
Train a discriminant analysis classification model, and then use the ClassificationDiscriminant Predict block for label prediction. (自 R2023b 起) - Predict Class Labels Using ClassificationKernel Predict Block
Train a Gaussian kernel classification model, and then use the ClassificationKernel Predict block for label prediction. (自 R2024b 起) - Predict Responses Using RegressionSVM Predict Block
Train a support vector machine (SVM) regression model using the Regression Learner app, and then use the RegressionSVM Predict block for response prediction. - Predict Responses Using RegressionTree Predict Block
This example shows how to use the RegressionTree Predict block for response prediction in Simulink®. - Predict Responses Using RegressionLinear Predict Block
This example shows how to use the RegressionLinear Predict block for response prediction in Simulink®. (自 R2023a 起) - Predict Responses Using RegressionEnsemble Predict Block
Train a regression ensemble model with optimal hyperparameters, and then use the RegressionEnsemble Predict block for response prediction. - Predict Responses Using RegressionNeuralNetwork Predict Block
Train a neural network regression model, and then use the RegressionNeuralNetwork Predict block for response prediction. - Predict Responses Using RegressionGP Predict Block
Train a Gaussian process (GP) regression model, and then use the RegressionGP Predict block for response prediction. - Predict Responses Using RegressionKernel Predict Block
This example shows how to use the RegressionKernel Predict block for response prediction in Simulink®. (自 R2024b 起)
增量学习模块
- Perform Incremental Learning Using IncrementalClassificationLinear Fit and Predict Blocks
Perform incremental learning with the IncrementalClassificationLinear Fit block and predict labels with the IncrementalClassificationLinear Predict block. (自 R2023b 起) - Perform Incremental Learning Using IncrementalRegressionLinear Fit and Predict Blocks
Perform incremental learning with the IncrementalRegressionLinear Fit block and predict responses with the IncrementalRegressionLinear Predict block. (自 R2023b 起) - Perform Incremental Learning Using IncrementalClassificationKernel Fit and Predict Blocks
Perform incremental learning with the IncrementalClassificationKernel Fit block and predict labels with the IncrementalClassificationKernel Predict block. (自 R2024b 起) - Perform Incremental Learning Using IncrementalRegressionKernel Fit and Predict Blocks
Perform incremental learning with the IncrementalRegressionKernel Fit block and predict responses with the IncrementalRegressionKernel Predict block. (自 R2024b 起) - Perform Incremental Learning and Track Performance Metrics Using Update Metrics Block
Perform incremental learning and track performance metrics with the Update Metrics block. (自 R2023b 起) - Monitor Drift Using Detect Drift Block
This example shows how to use the Detect Drift block for monitoring drift in a data stream in Simulink®. (自 R2024b 起) - Configure Simulink Template for Rate-Based Incremental Linear Regression
Configure the Simulink Rate-Based Incremental Learning template to perform incremental linear regression. (自 R2024a 起) - Configure Simulink Template for Rate-Based Incremental Linear Classification
Configure the Simulink Rate-Based Incremental Learning template to perform incremental linear classification. (自 R2024a 起) - Configure Simulink Template for Conditionally Enabled Incremental Linear Classification
Configure the Simulink Enabled Execution Incremental Learning template to perform incremental linear classification. (自 R2024a 起) - Configure Simulink Template for Conditionally Enabled Incremental Linear Regression
Configure the Simulink Enabled Execution Incremental Learning template to perform incremental linear regression. (自 R2024a 起)
聚类分析模块
- Find Nearest Neighbors Using KNN Search Block
Train a nearest neighbor searcher model, and then use the KNN Search block for label prediction. (自 R2023b 起)
代码生成应用程序
- Predict Class Labels Using MATLAB Function Block
Generate code from a Simulink model that classifies data using an SVM model. - System Objects for Classification and Code Generation
Generate code from a System object™ for making predictions using a trained classification model, and use the System object in a Simulink model. - Predict Class Labels Using Stateflow
Generate code from a Stateflow® model that classifies data using a discriminant analysis classifier. - Human Activity Recognition Simulink Model for Fixed-Point Deployment
Generate code from a classification Simulink model prepared for fixed-point deployment. - Identify Punch and Flex Hand Gestures Using Machine Learning Algorithm on Arduino Hardware (Simulink)
This example shows how to use the Simulink® Support Package for Arduino® Hardware to identify punch and flex hand gestures using a machine learning algorithm. - Deploy Neural Network Regression Model to FPGA/ASIC Platform
Predict in Simulink using a neural network regression model, and deploy the Simulink model to an FPGA/ASIC platform by using HDL code generation.