I appreciate that this response is many months late. However, I have faced the same problem and I hope that others might find this response useful.
Ishaan, one of the ways to create personalised model per company is to use Partitioned ML Model. If this approach is used, then it would look like this:
from sklearn.linear_model import LinearRegression
import pickle
import random
import numpy as np
"""
Sample data preparation for the partition.
--------
This example uses linear function y=mx+b where m is the tenant / company
specific bias and b is the error. LinearRegression is then used to find
parameters to fit the generated data.
"""
samples = 1000
x = np.random.randint(100000, size=(samples, 1))
bias = np.random.randint(1000, size=3)
error = np.random.normal(loc=0, scale=2, size=(samples, 1))
Xy_a = np.hstack([np.full((samples,1), 0), x, bias[0]*x+error])
Xy_b = np.hstack([np.full((samples,1), 1), x, bias[1]*x+error])
Xy_c = np.hstack([np.full((samples,1), 2), x, bias[2]*x+error])
Xy = np.concatenate((Xy_a, Xy_b, Xy_c), axis=0)
random.shuffle(Xy)
X = Xy[:,0:2]
y = Xy[:,2]
"""
Example of how this model can be used.
--------
Part 1 shows how it is possible to set up, train and use Partition class with your underlying model.
Part 2 shows that model state can be preserved and reloaded for future predictions, this
means the model should work seamlessly with MLflow.
"""
idx_to_test = np.random.randint(low=0, high=len(X), size=5).tolist()
partitioned_linear_regression = Partition(LinearRegression, normalize=True, copy_X=True)
partitioned_linear_regression.fit(X, y)
score_per_partition = partitioned_linear_regression.score(X, y)
print("Coefficient of determination per partition:", score_per_partition)
y_hat = partitioned_linear_regression.predict(X[idx_to_test])
print("y actual:", y[idx_to_test])
print("ŷ predicted:", y_hat)
filename = 'partition.pkl'
pickle.dump(partitioned_linear_regression, open(filename, 'wb'))
partitioned_linear_regression_from_pkl = pickle.load(open(filename, 'rb'))
y_hat_from_pkl = partitioned_linear_regression_from_pkl.predict(X[idx_to_test])
print("y actual:", y[idx_to_test])
print("ŷ predicted (using pkl):", y_hat_from_pkl)
After your data is trained you can save your model and use it in the future to make predictions. This can be done by using Python pickle.
You can find the whole write up and source code for this in the Medium post "Machine learning model partitioning for multi-tenant data using Python".