Python model.predict() Examples
The following are 8
code examples of model.predict().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
model
, or try the search function
.
Example #1
Source File: predict.py From kaos with Apache License 2.0 | 6 votes |
def invocations(): """ A flask handler for predictions Returns: A flask response with either a prediction or an error """ # pre-process request data = flask.request.get_data() # read data # make predictions try: out = predict(data, ctx) # extract prediction logging.info("Predicted digit: {}".format(out)) return flask.jsonify(result=out) except Exception as ex: logging.error(ex) return flask.Response(response='Error while processing the request', status=500, mimetype='text/plain')
Example #2
Source File: predict.py From kaos with Apache License 2.0 | 6 votes |
def invocations(): """ A flask handler for predictions Returns: A flask response with either a prediction or an error """ # pre-process request data = flask.request.get_json() # read data # make predictions try: out = predict(data, ctx) # extract prediction logging.info("Predict: {}".format(out)) return flask.jsonify(result=out) except Exception as ex: logging.error(ex) return flask.Response(response='Error while processing the request', status=500, mimetype='text/plain')
Example #3
Source File: app.py From mlapp with MIT License | 5 votes |
def predict(data: ModelData) -> str: """ Pass the request data as ModelData object, as this can be customised in the model.py file to adapt based on deployed model to make predictions Parameters: data: Parse the request body data based on your model schema and pass this to predict method to make prediction """ return model.predict(data)
Example #4
Source File: app.py From mlapp with MIT License | 5 votes |
def feedback(data: FeedbackData) -> str: """ Pass the request data as FeedbackData object, as this can be customised in the model.py file to adapt based on deployed model to make predictions Parameters: data: Parse the request body data based on your model schema and pass this to predict method to make prediction """ return model.feedback(data) # Load our pre trained model
Example #5
Source File: environ.py From DrugEx with MIT License | 5 votes |
def RF(X, y, X_ind, y_ind, is_reg=False): """Cross Validation and independent set test for Random Forest model Arguments: X (ndarray): Feature data of training and validation set for cross-validation. m X n matrix, m is the No. of samples, n is the No. of fetures y (ndarray): Label data of training and validation set for cross-validation. m-D vector, and m is the No. of samples. X_ind (ndarray): Feature data of independent test set for independent test. It has the similar data structure as X. y_ind (ndarray): Feature data of independent set for for independent test. It has the similar data structure as y out (str): The file path for saving the result data. is_reg (bool, optional): define the model for regression (True) or classification (False) (Default: False) Returns: cvs (ndarray): cross-validation results. The shape is (m, ), m is the No. of samples. inds (ndarray): independent test results. It has similar data structure as cvs. """ if is_reg: folds = KFold(5).split(X) alg = RandomForestRegressor else: folds = StratifiedKFold(5).split(X, y) alg = RandomForestClassifier cvs = np.zeros(y.shape) inds = np.zeros(y_ind.shape) for i, (trained, valided) in enumerate(folds): model = alg(n_estimators=500, n_jobs=1) model.fit(X[trained], y[trained]) if is_reg: cvs[valided] = model.predict(X[valided]) inds += model.predict(X_ind) else: cvs[valided] = model.predict_proba(X[valided])[:, 1] inds += model.predict_proba(X_ind)[:, 1] return cvs, inds / 5
Example #6
Source File: environ.py From DrugEx with MIT License | 5 votes |
def SVM(X, y, X_ind, y_ind, is_reg=False): """Cross Validation and independent set test for Support Vector Machine (SVM) Arguments: X (ndarray): Feature data of training and validation set for cross-validation. m X n matrix, m is the No. of samples, n is the No. of fetures y (ndarray): Label data of training and validation set for cross-validation. m-D vector, and m is the No. of samples. X_ind (ndarray): Feature data of independent test set for independent test. It has the similar data structure as X. y_ind (ndarray): Feature data of independent set for for independent test. It has the similar data structure as y out (str): The file path for saving the result data. is_reg (bool, optional): define the model for regression (True) or classification (False) (Default: False) Returns: cvs (ndarray): cross-validation results. The shape is (m, ), m is the No. of samples. inds (ndarray): independent test results. It has similar data structure as cvs. """ if is_reg: folds = KFold(5).split(X) model = SVR() else: folds = StratifiedKFold(5).split(X, y) model = SVC(probability=True) cvs = np.zeros(y.shape) inds = np.zeros(y_ind.shape) gs = GridSearchCV(model, {'C': 2.0 ** np.array([-5, 15]), 'gamma': 2.0 ** np.array([-15, 5])}, n_jobs=5) gs.fit(X, y) params = gs.best_params_ print(params) for i, (trained, valided) in enumerate(folds): model = SVC(probability=True, C=params['C'], gamma=params['gamma']) model.fit(X[trained], y[trained]) if is_reg: cvs[valided] = model.predict(X[valided]) inds += model.predict(X_ind) else: cvs[valided] = model.predict_proba(X[valided])[:, 1] inds += model.predict_proba(X_ind)[:, 1] return cvs, inds / 5
Example #7
Source File: environ.py From DrugEx with MIT License | 5 votes |
def KNN(X, y, X_ind, y_ind, is_reg=False): """Cross Validation and independent set test for KNN. Arguments: X (ndarray): Feature data of training and validation set for cross-validation. m X n matrix, m is the No. of samples, n is the No. of fetures y (ndarray): Label data of training and validation set for cross-validation. m-D vector, and m is the No. of samples. X_ind (ndarray): Feature data of independent test set for independent test. It has the similar data structure as X. y_ind (ndarray): Feature data of independent set for for independent test. It has the similar data structure as y out (str): The file path for saving the result data. is_reg (bool, optional): define the model for regression (True) or classification (False) (Default: False) Returns: cvs (ndarray): cross-validation results. The shape is (m, ), m is the No. of samples. inds (ndarray): independent test results. It has similar data structure as cvs. """ if is_reg: folds = KFold(5).split(X) alg = KNeighborsRegressor else: folds = StratifiedKFold(5).split(X, y) alg = KNeighborsClassifier cvs = np.zeros(y.shape) inds = np.zeros(y_ind.shape) for i, (trained, valided) in enumerate(folds): model = alg(n_jobs=1) model.fit(X[trained], y[trained]) if is_reg: cvs[valided] = model.predict(X[valided]) inds += model.predict(X_ind) else: cvs[valided] = model.predict_proba(X[valided])[:, 1] inds += model.predict_proba(X_ind)[:, 1] return cvs, inds / 5
Example #8
Source File: environ.py From DrugEx with MIT License | 4 votes |
def DNN(X, y, X_ind, y_ind, out, is_reg=False): """Cross Validation and independent set test for fully connected deep neural network Arguments: X (ndarray): Feature data of training and validation set for cross-validation. m X n matrix, m is the No. of samples, n is the No. of fetures y (ndarray): Label data of training and validation set for cross-validation. m X t matrix if it is for multi-task model, m is the No. of samples, n is the No. of tasks or classes; m-D vector if it is only for single task model, and m is the No. of samples. X_ind (ndarray): Feature data of independent test set for independent test. It has the similar data structure as X. y_ind (ndarray): Feature data of independent set for for independent test. It has the similar data structure as y out (str): The file path for saving the result data. is_reg (bool, optional): define the model for regression (True) or classification (False) (Default: False) Returns: cvs (ndarray): cross-validation results. If it is single task, the shape is (m, ), m is the No. of samples, it contains real label and probability value; if it is multi-task, the shape is m X n, n is the No. of tasks. inds (ndarray): independent test results. It has similar data structure as cvs. """ if 'mtqsar' in out or is_reg: folds = KFold(5).split(X) NET = model.MTFullyConnected else: folds = StratifiedKFold(5).split(X, y[:, 0]) NET = model.STFullyConnected indep_set = TensorDataset(T.Tensor(X_ind), T.Tensor(y_ind)) indep_loader = DataLoader(indep_set, batch_size=BATCH_SIZE) cvs = np.zeros(y.shape) inds = np.zeros(y_ind.shape) for i, (trained, valided) in enumerate(folds): train_set = TensorDataset(T.Tensor(X[trained]), T.Tensor(y[trained])) train_loader = DataLoader(train_set, batch_size=BATCH_SIZE) valid_set = TensorDataset(T.Tensor(X[valided]), T.Tensor(y[valided])) valid_loader = DataLoader(valid_set, batch_size=BATCH_SIZE) net = NET(X.shape[1], y.shape[1], is_reg=is_reg) net.fit(train_loader, valid_loader, out='%s_%d' % (out, i), epochs=N_EPOCH, lr=LR) cvs[valided] = net.predict(valid_loader) inds += net.predict(indep_loader) cv, ind = y == y, y_ind == y_ind return cvs[cv], inds[ind] / 5