本文整理汇总了Python中model.predict方法的典型用法代码示例。如果您正苦于以下问题:Python model.predict方法的具体用法?Python model.predict怎么用?Python model.predict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model
的用法示例。
在下文中一共展示了model.predict方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: invocations
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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')
示例2: invocations
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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')
示例3: predict
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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)
示例4: feedback
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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
示例5: RF
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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
示例6: SVM
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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
示例7: KNN
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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
示例8: DNN
# 需要导入模块: import model [as 别名]
# 或者: from model import predict [as 别名]
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