Adding a complete custom Project

Linear Regression on Global GDP by Year

GDP of all countries of the world combined by year, from 1960 to the latest official figures (2017) published by the World Bank. GDP Real refers to GDP adjusted for inflation (base year: 2010). In this example of linear regression using Python we will build a model and determine a line which best fits the data.

Required data for this exercise can be downloaded from https://www.worldometers.info/gdp/#gdpyear

Design of the Project

This Project is divided in 3 stages namely Data Entry, Calculation and Prediction.

Data Entry stage will be used to get the input data (Year and GDP) and also for normalization of Year Property.

Calculation stage will be used to create a Linear regression model after getting the Data from stage 1 using GET APIs.

Prediction stage will be used to get the predicted values after getting the Parameters from stage 2 using GET APIs. These Predicted values will also be shared on Web Portal/App using POST API.

Since Data form from Prediction stage have been shared to User on Web Portal/Wynum App a user con submit the form by providing the value of year and receive a message on the predicted value for the world GDP (in Bn Dollars)

Data Entry Stage

Data Entry stage will be used to get the input data (Year and GDP) and also for normalization of Year Property.

Adding Data

Data is added to the stage after defining all the input and output variables (Properties).

Adding Properties

Clicking on Properties will give a pop up form listing all the properties associated with a Stage. A user can then add a new property by giving a Title and Type of property. There are predefined Types of properties (like Text, Number, Upload File, Barcode etc) which a user can use for adding a Data Property.

Suppose a User want to create a simple Linear Regression model for Real GDP (world) vs Year as a variable then a User can add 2 new Properties namely Year and GDP. Keeping TYPE as Number for both the variables. User can also add a new Property namely newYear (keep TYPE as Computed) for getting the normalized values later.

Adding Data

Clicking on Data will give a Pop up form to manage the data. A User can Add Data, View Data and check Data Spread here. Data can be added in the form or can also be uploaded using a Excel Sheet. A User can also Download a Sample file for uploading the data.

Adding a Python Script

Clicking on Python Scripts will give a Pop up window showing all the Scripts related the particular Stage. Clicking on a Script Tab a user can Click on I/O , Edit , Delete, Info and Test to check Input/Output, Edit, Delete, get information and test the script.

A User can add a new script by clicking on + Icon on this Pop up window. User can provide Script Title and Description and Submit the form to create a new script. User is shown the new script in Edit Tab and here a user can select and add input properties, output properties and Global variables.

For Linear regression on GDP data exercise a User can add Year as input property and newYear as output property.

After adding these Properties a user can use I/O Tab to edit the script. For this example we have normalized Year variable by subtracting 1960 from the value of Year.

A default Python script is generated when a User adds a new script. We have just added line no. 7 in the script to normalize the Year variable.

# access property of abc using input["abc"]
# print(input["abc"])
# save computed property xyz to the database using output["xyz"] = 1234
​
# stuff to run always here such as class/def
def main():
output["newYear"] = input["year"]-1960
pass
​
if __name__ == "__main__":
# stuff only to run when not called via 'import' here
main()
​

A User can run the unittest on the script on the Test Tab and get the test response as Passed or Failed.

Test Script

import unittest
# You can accesss your script using 'mainscript'
import script_5e06f21aba09a728d800021a as mainscript

​Calculation stage

Calculation stage will be used to create a Linear regression model after getting the Data from Data Entry stage using GET APIs.

Adding GET API on Data Entry Stage

In this example we will be sharing data from Data Entry stage to Calculation Stage. To share a data form a Stage a user can create a new Generic API on that particular Stage (Data Entry)and submit all the required fields namely API Title, API request Type (Here we select it as GET) and Description (Optional.)

On submission of the required data a Pop up window will give Secret Key and API Token and it will ask User to select Input Properties and Response Properties. User can submit the required Properties to create new API.

Adding Python Script on Calculation Stage

A User can then write Python Script to get the Data in the Calculation Stage. Secret Key and API Token of the GET API created earlier are used in the script (Line no. 11 and 12) to get the data from Data Entry Stage. In the script we have converted Dict data from Data Entry stage to array X and Y and using sklearn we have found intercept and coef for out Linear Regression Model.

# access property of abc using input["abc"]
# print(input["abc"])
# save computed property xyz to the database using output["xyz"] = 1234
​
# stuff to run always here such as class/def
from wynum import Client
import numpy as np
from sklearn.linear_model import LinearRegression
def main():
#output['alpha'] = input['year'] * input['GDP']
#output['beta'] = input['year'] + input['GDP']
secret = "6b85efb96cb1eb45179f9ed419d2d3439t"
token = "3212395"
client = Client(secret, token)
getdata = client.getdata()
X = np.zeros(57)
Y = np.zeros(57)
count = 0
while (count < 57):
X[count] = getdata[count]['year']
Y[count] = getdata[count]['GDP']
count = count + 1
X = X.reshape((-1, 1))
model = LinearRegression().fit(X, Y)
output['alpha'] = model.intercept_
output['beta'] = 1168.067
# output['beta'] = model.coef_
​
if __name__ == "__main__":
# stuff only to run when not called via 'import' here
main()
​

Prediction Stage

Prediction stage will be used to get the predicted values after getting the Parameters from Calculation stage using GET APIs. These Predicted values will also be shared on Web Portal/App using POST API.

Adding GET API on Calculation Stage

In this example we will be sharing data from Calculation stage to Prediction Stage. To share a data from Calculation stage a user can create a new Generic API and submit all the required fields namely API Title, API request Type (Here we select it as GET) and Description (Optional.)

On submission of the required data a Pop up window will give Secret Key and API Token and it will ask User to select Input Properties and Response Properties. User can submit the required Properties to create new API.

Adding POST API on Prediction Stage

Share data form from a Stage on Web Wynum Portal or App

To share a data form on web wynum portal or App a user can create a new User API and submit all the required fields namely API Title, API request Type (Here we select it as POST) and Description (Optional.)

On submission of the required data it will ask User to select Input Properties and Response Properties. User can submit the required Properties to create new API.

A user can share the Data form (exposed through the Post API) by providing the User Email. Clicking on the Share Project Button will open a Pop up window containing links for adding Collaborators and Users. A User can share the data form by clicking on Users Link and submitting the Select Stage, Title, Form Title, Component URL and POST URL to generate the required Tokens.

A User can then add Process User by entering the Email Address of the select Users.

Adding Python Script on Prediction Stage

A User can then write Python Script to get the Data in the Prediction Stage. Secret Key and API Token are used in the script (Line no. 21 and 22) to get the data from Calculation Stage.

from wynum import Client
import requests
​
def send_mesage(sender_email, receiver_email, message, api_token,components_url, post_url, submission_id, response):
data = {
"Sender_email": sender_email,
"Receiver_email": receiver_email,
"Message": message,
"APIToken": api_token,
"Payload": {
"Components_url": components_url,
"Post_url": post_url,
"Submission_id": submission_id,
"Response": response
}
}
res = requests.post("https://api.wynum.com/apimessages", json=data)
return res.json()
def main():
secret = "6b85efb96cb1eb45179f9ed419d2d3439t"
token = "8992174"
client = Client(secret, token)
getdata = client.getdata()
print(getdata)
alpha = getdata[0]['alpha']
beta = getdata[0]['beta']
output['prediction'] = alpha + beta*input['year']
res = send_mesage(sender_email = 'hello@wynum.com',
receiver_email = input['user_id'],
message = "Prediction Result " + str(output['prediction']) ,
api_token = '3257293',
components_url = "https://api.wynum.com/getComponent/4993bac5aba569405d23f42e6f48a604",
post_url = "https://api.wynum.com/postStage/779b903316106aadd9c5b295e910d01f",
submission_id = input['id'],
response = ''
)
​
if __name__ == "__main__":
# stuff only to run when not called via 'import' here
main()
​