当前位置: 首页>>代码示例>>Python>>正文


Python GradientBoostingRegressor.predict_proba方法代码示例

本文整理汇总了Python中sklearn.ensemble.GradientBoostingRegressor.predict_proba方法的典型用法代码示例。如果您正苦于以下问题:Python GradientBoostingRegressor.predict_proba方法的具体用法?Python GradientBoostingRegressor.predict_proba怎么用?Python GradientBoostingRegressor.predict_proba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.ensemble.GradientBoostingRegressor的用法示例。


在下文中一共展示了GradientBoostingRegressor.predict_proba方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from sklearn.ensemble import GradientBoostingRegressor [as 别名]
# 或者: from sklearn.ensemble.GradientBoostingRegressor import predict_proba [as 别名]
def main(options, args):
    if options.utildate == None:
        assert(False)
    fsampleY = open(options.input + "/" + options.utildate + "/features.csv", "r")
    l_X = []
    l_y = []

    for line in fsampleY:
        tokens = line.split(",")
        features = []
        for i in range(len(tokens)):
            if i < len(tokens)-1:
                features.append(float(tokens[i]))
            else:
                l_y.append(int(tokens[i]))
        l_X.append(features)
    print "features loaded!"
    X = np.array(l_X)
    y = np.array(l_y)
    assert(X.shape[0] == y.shape[0])
    if int(options.short) > 0:
        print "using short data for test purpose"
        X = X[0:int(options.short)]
        y = y[0:int(options.short)]
    
    print "preparing models"
    trainModel = globals()[options.trainmodel]()
    print trainModel
    if options.isregress == True:
        if options.trainmodel == "Gdbc1":
            model_predictor = GradientBoostingRegressor(max_features=0.6, learning_rate = 0.05, max_depth=5, n_estimators=300)
        else:
            model_predictor = trainModel.get_model()
    else :
        model_predictor = GradientBoostingClassifier(max_features=0.6, learning_rate=0.05, max_depth=5, n_estimators=300)
    #model_predictor = GradientBoostingClassifier()
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.3, random_state=0)
    # print cross_validation.cross_val_score(model_predictor, X, y)
    clf = model_predictor.fit(X_train, y_train)

    if options.isregress:
        if options.trainmodel != "God":
            pred = model_predictor.predict(X_test)
            r2 = r2_score(y_test, pred)
            print "the r2 score is ", r2
    else:
        pred = model_predictor.predict_proba(X_test)
    # calculate the R2score
 #   tpred = model_predictor.predict(X_test)
 #   score = model_predictor.score(X_test, tpred)
 #   print "score=", score
    #assert(len(pred) == X_test.shape[0])
    #{{{ prediction
    print "prediction ..."
    stock_predict_dir = options.output + "/"+ options.trainmodel +"/" + options.utildate
    if not os.path.exists(stock_predict_dir) : os.makedirs(stock_predict_dir)
    stock_predict_out = file(stock_predict_dir + "/predicted.csv", "w")

    metastr = file(options.input + "/" + options.utildate + "/meta.json", "r").readlines()[0]
    metajson  = json.loads(metastr)
    
    for line in file(options.input + "/" + options.utildate + "/last.csv", "r"):
        tokens = line.split(",")
        l_features = []
        for i in range(len(tokens)):
            if 0 == i:
                print >> stock_predict_out, "%s," % tokens[i],
            elif 1 == i:
                print >> stock_predict_out, "%s," % tokens[i],
                print >> stock_predict_out, "%d," % metajson["span"],
            else:
                l_features.append(float(tokens[i].strip()))
        l_features2 = []
        l_features2.append(l_features)
        np_features = np.array(l_features2)
        if np_features.shape[1] != X.shape[1] :
            assert(false)
        if options.isregress:
            if options.trainmodel == "God":
                pred = model_predictor.predict(np_features, tokens[0], tokens[1], metajson["span"])
            else:
                pred = model_predictor.predict(np_features)
            print >> stock_predict_out, "%f" % pred
        else:
            pred = model_predictor.predict_proba(np_features)
            print >> stock_predict_out, "%f" % pred[0,1]
    stock_predict_out.close()
开发者ID:greatGregLiu,项目名称:pytrade,代码行数:89,代码来源:model_tuner.py


注:本文中的sklearn.ensemble.GradientBoostingRegressor.predict_proba方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。