This is a simple implementation of linear regression
Project description
Simple Linear Regression
This is a simple implementation of linear regression.
- Install package
simplelinearregresspackage bypip. All dependent packages have been already installed.
pip install simplelinearregress ==0.0.1
- Train model with diabetes dataset, evaluate the trained model, save the trained model to disk
import os import pickle from linear_reg.simple_linear_regr import SimpleLinearRegression from linear_reg.simple_linear_regr_utils import generate_data, evaluate if __name__ == "__main__": # load diabetes data X_train, y_train, X_test, y_test = generate_data() # load model model = SimpleLinearRegression() # trained model model.fit(X_train, y_train) # evaluation model predicted = model.predict(X_test) evaluate(model, X_test, y_test, predicted) # save trained model if not os.path.exists('saved_model'): os.makedirs('saved_model') model_path = os.path.join("saved_model", "linear_model.dat") with open(model_path, 'wb') as saved_model: pickle.dump(model, saved_model)
- The evaluated result is as follows:
Slope: [[937.18913157]]; Intercept: [152.9193589] Mean squared error: 2549.27 Coefficient of determination: 0.47 ****** Success ****** - Create a RESTful API server with Flask
- Create a configure file in
configsfolder, named.env. This file includes all configurations of servers.# ip and port of server HOST=0.0.0.0 PORT=8080 MODEL_DIR= "saved_model" MODEL_NAME="linear_model.dat" API_KEYS=I1TIISEUKJBTJVNO3M24UC7L71CAW3T0,AY814XUOHKNI4R0DIPXYRRO07L5EIQFX,REEOBZZ6FL03VDIAX6LR71RFNFIJYTJJ,FRY7XKAGFZG3WGNX9V4MK7A7O6T8VORK,AU39QYVZIKCUITCOYMZ6STUDC6R9GBKF - Run API server. Note that you must import
SimpleLinearRegressionfrom linear_reg.main import main from linear_reg.simple_linear_regr import SimpleLinearRegression if __name__ == '__main__': main()
- Server ready started as follows:
* Serving Flask app 'linear_reg.main' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:8080 * Running on http://192.168.10.102:8080 Press CTRL+C to quit * Restarting with stat * Debugger is active! * Debugger PIN: 236-046-492
- Create a client to request RESTful API server
import requests import ast from linear_reg.simple_linear_regr_utils import generate_data if __name__ == '__main__': # load data X_train, y_train, X_test, y_test = generate_data() api_key = "I1TIISEUKJBTJVNO3M24UC7L71CAW3T0" # Header input headers = {"Content-Type": "application/json", "regression-api-key": api_key} # API url url = 'http://localhost:8080/batch' # sending data data = {'X_test': X_test.tolist()} # Request server response = requests.post(url, json=data, headers=headers) # Convert server response into JSON format response_content = response.content response_content = response.content.decode("utf-8") response_content = ast.literal_eval(response_content) print("Predict results:") print(response_content["results"])
- The response prediction is as follows:
Predict results: [225.89220475078582, 115.78961465717714, 163.26504341313685, 114.77949915173122, 120.8401921844069, 158.2144658859071, 235.99335980524535, 121.85030768985287, 99.62776657004196, 123.87053870074476, 204.67977913642085, 96.5974200537041, 154.1740038641233, 130.9413472388664, 83.46591848290672, 171.34596745670444, 138.01215577698807, 138.01215577698807, 189.5280465547316, 84.47603398835268]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simplelinearregress-0.0.1.tar.gz.
File metadata
- Download URL: simplelinearregress-0.0.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
405236015685ab77c80b45da61c4bdd9f91c4d031015ed7f33b79c5efbfaa19e
|
|
| MD5 |
88a6e6ab1eb001ec567fd72a31a203c6
|
|
| BLAKE2b-256 |
a607fcfa08d766d43697c890e28b111b3dab7af1b9d77070b32b212bd06c2d38
|
File details
Details for the file simplelinearregress-0.0.1-py3-none-any.whl.
File metadata
- Download URL: simplelinearregress-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7500faf8db25d6bcf554c8f87e4c27dd5f414fcf1454e9de0070cda0b6716d41
|
|
| MD5 |
13fac0c81349d041ac283bb396e9f3ff
|
|
| BLAKE2b-256 |
cfe8374ccc34b8d51f5e752b22c12fc2239f2a64b0cc6daa594035393338419e
|