Skip to main content

linearTrain

warning

The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.

What is linearTrain component ?

linearTrain component is used to train the linear regression model on 2 parameters and saves the learnt parameters in trainedValues.txt file.

  • Description : linearTrain() takes an input(through STDIN) as .csv file and column name 1 and column name 2 with values, which we train using linear regression algorithm and trained parameters are stored in .txt file for further use. Check Input and output parameters for details.
  • Parameters :
    • Input(Via STDIN) : A JSON String with following contents:
      • Input1 : .csv file name (which container 2 columns with values)
      • Input2 : column1 name
      • Input3 : column2 name
    • Output(Via STDOUT) : A JSON string with following contents
      • trainedValues.txt file which contains trained values i.e. the model

List of linearTrain features in shunya stack

  1. linearTrain component train the linear-regression model on csv file and stored the trained values in .txt file.

Using linearTrain features in Shunya stack

1. Getting trained values after linear-regression using linearTrain component

  • linearTrain give trainedValues.txt which contains predicted values.

Lets look into code to understand how to output.

C++

// call linearTrain API
subprocess::popen linearTrain("/usr/bin/linearTrain", {});
linearTrain.stdin() << jsonDoc2Text(inputJson) << std::endl;
linearTrain.close();
std::string linearTrainOut;
// Get output from STDOUT
linearTrain.stdout() >> linearTrainOut;

with above c++ program, you will get trainedValues.txt file stored in your system, which has trained values.

Python

# calling linearTrain API
linearTrain = Popen(['/usr/bin/linearTrain'], stdout=PIPE, stdin=PIPE)
# Passing input to STDIN and getting an output from STDOUT
linearTrainOut = linearTrain.communicate(input=inputJson1_data.encode('utf-8'))[0]

with above python program, you will get trainedValues.txt file stored in your system, which has trained values.

Understand this component with an example (ready to use code)

  • This is an example for linear-regression training and predicton and here we will be using 2 components: linearTrain and linearPredict
  • Check this ready to use example in c++ and python
  • C++ Example
    git clone https://gitlab.iotiot.in/shunya/products/shunya-ai-examples.git
    cd shunya-ai-examples/ml-examples/cpp-examples/linear-reg
  • In this folder there is a file, linear-reg.cpp
  • linearTrain Components used
    subprocess::popen linearTrain("/usr/bin/linearTrain", {});
  • linearPredict component used
     subprocess::popen linearPredict("/usr/bin/linearPredict", {});
  • Run code by yourself
    mkdir build && cd build
    cmake .. && make
    ./linearRegCpp
    • You will get a new image stored in system.

Facing errors with the component?