mingyk commited on
Commit
7b4c18a
1 Parent(s): 9d2ec8d
Files changed (3) hide show
  1. README.md +9 -4
  2. model.py +27 -9
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,17 @@
1
  # Linear Regression Model
2
 
3
- This is a simple linear regression model that returns coefficients and intercept when provided with column names.
 
 
4
 
5
  ## Usage
6
 
7
- ### Input
8
- The model takes JSON input with the following structure:
 
9
  ```json
10
  {
11
- "columns": ["feature1", "feature2", "feature3"]
 
 
12
  }
 
1
  # Linear Regression Model
2
 
3
+ This model provides two main functions:
4
+ 1. **Retrieve Coefficients and Intercept**: Return the model's coefficients and intercept based on input column names.
5
+ 2. **Predict Values**: Given column names and data values, return predictions.
6
 
7
  ## Usage
8
 
9
+ ### 1. Retrieve Coefficients and Intercept
10
+ Send a JSON payload with just column names:
11
+
12
  ```json
13
  {
14
+ "inputs": {
15
+ "columns": ["feature1", "feature2", "feature3"]
16
+ }
17
  }
model.py CHANGED
@@ -1,22 +1,40 @@
1
  # model.py
2
  import joblib
 
 
3
 
4
  class LinearRegressionModel:
5
  def __init__(self):
6
- # Load the model
7
  self.model = joblib.load("linear_regression_model.joblib")
8
 
9
- def get_coefficients(self, columns):
10
- # Return coefficients as a dictionary with column names as keys
11
  coefficients = dict(zip(columns, self.model.coef_.tolist()))
12
  intercept = self.model.intercept_
13
  return {"coefficients": coefficients, "intercept": intercept}
14
 
15
- # Instantiate the model for inference
 
 
 
 
16
  model = LinearRegressionModel()
17
 
18
- # Sample usage function to demonstrate
19
- def predict(inputs):
20
- # Assume 'columns' is a key in the inputs dict with a list of column names
21
- columns = inputs.get("columns", [])
22
- return model.get_coefficients(columns)
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # model.py
2
  import joblib
3
+ from typing import List, Dict, Union
4
+ from transformers import Pipeline
5
 
6
  class LinearRegressionModel:
7
  def __init__(self):
8
+ # Load the trained linear regression model
9
  self.model = joblib.load("linear_regression_model.joblib")
10
 
11
+ def get_coefficients(self, columns: List[str]) -> Dict[str, float]:
12
+ # Return coefficients with column names as keys
13
  coefficients = dict(zip(columns, self.model.coef_.tolist()))
14
  intercept = self.model.intercept_
15
  return {"coefficients": coefficients, "intercept": intercept}
16
 
17
+ def predict(self, columns: List[str], data: List[List[float]]) -> List[float]:
18
+ # Predict values for given input data
19
+ return self.model.predict(data).tolist()
20
+
21
+ # Instantiate the model
22
  model = LinearRegressionModel()
23
 
24
+ # Define a custom pipeline class for inference
25
+ class CustomLinearRegressionPipeline(Pipeline):
26
+ def __init__(self, model):
27
+ super().__init__()
28
+ self.model = model
29
+
30
+ def __call__(self, inputs: Dict[str, Union[List[str], List[List[float]]]]) -> Dict[str, Union[Dict[str, float], List[float]]]:
31
+ columns = inputs.get("columns", [])
32
+ data = inputs.get("data", [])
33
+
34
+ if not data: # If no data, return coefficients and intercept
35
+ return self.model.get_coefficients(columns)
36
+ else: # If data is provided, return predictions
37
+ return {"predictions": self.model.predict(columns, data)}
38
+
39
+ # Instantiate the custom pipeline
40
+ pipeline = CustomLinearRegressionPipeline(model)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ scikit-learn
2
+ joblib
3
+ transformers