示例#1
0
def test_stacked_neg_ensemble():
    stacked = [model.LinearModel()]
    evaluation.estimate(model.StackedEnsembleModel(
        learners=stacked, next_model=model.LinearModel(), min_neg_recall=0.88),
                        X_train,
                        X_test,
                        y_train,
                        y_test,
                        use_confusion_matrix=False)
示例#2
0
def test_ensemble():
    for md in [
            model.LinearEnsemble([model.LinearModel(),
                                  model.LinearModel()]),
    ]:
        try:
            evaluation.cross_validation(md,
                                        X,
                                        y,
                                        scoring='both',
                                        n_jobs=1,
                                        n_splits=2)
        except Exception as e:
            print(md.__class__)
            raise e
示例#3
0
def test_data_split_identical():
    X_, y_ = X[:40], np.array([0] * 20 + [1] * 20)
    for md in [
            model.SVM(),
            model.KNN(),
            model.XGBoost(),
            model.LinearModel(),
            model.DecisionTree()
    ]:
        a = evaluation.estimate(md, X_train, X_test, y_train, y_test)
        b = evaluation.estimate(md, X_train, X_test, y_train, y_test)
        assert a == b
        a = evaluation.cross_validation(md,
                                        X_,
                                        y_,
                                        scoring='both',
                                        n_splits=2,
                                        n_jobs=1)
        b = evaluation.cross_validation(md,
                                        X_,
                                        y_,
                                        scoring='both',
                                        n_splits=2,
                                        n_jobs=1)
        assert np.all(a['f1'] == b['f1'])
        assert np.all(a['roc_auc'] == b['roc_auc'])
示例#4
0
def TestLinearMnistModel():
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    training_set, training_label = mnist.train.next_batch(60000)
    mnist_dataset = dataset.DataSet()
    mnist_dataset.Set(training_set, training_label)

    with model.LinearModel() as mdl:
        mdl.Train(mnist_dataset, 100)
示例#5
0
def test_estimate():
    for md in [
            model.SVM(),
            model.KNN(),
            model.XGBoost(),
            model.LinearModel(),
            model.DecisionTree()
    ]:
        evaluation.estimate(md, X_train, X_test, y_train, y_test)
示例#6
0
def TestLinearModel():
    test_dataset = dataset.NpyDataSet()
    dir_path = os.path.join(common.DataDir(), "systest-prototype-small-npy")
    print(dir_path)
    test_dataset.Load(dir_path)
    with model.LinearModel() as linear_model:
        linear_model.Train(test_dataset, 10, 1)
        first_row = test_dataset.x[0]
        first_row = first_row.reshape(1, first_row.shape[0])
        infer_result = linear_model.Infer(first_row)
        logging.info("infer result: %d", infer_result)
示例#7
0
def test_clone():
    models = [
        model.LinearModel(),
        model.SVM(),
        model.DecisionTree(),
        model.MultiClassesLearner('KNN', {'n_neighbors': 1}),
        model.KNN(),
        model.XGBoost(),
    ]
    from sklearn.base import clone
    for md in models:
        clone(md)
示例#8
0
def predict():

    tournaments = nx.tournament_names()
    print(tournaments)

    # download dataset from numerai
    data = nx.download('numerai_dataset.zip', load=False)
    print('data downloaded')
    data = nx.load_zip('numerai_dataset.zip', single_precision=True)
    print('data loaded')

    for tournament_name in tournaments:
        saved_model_name = 'model_trained_' + tournament_name
        if os.path.exists(saved_model_name):
            print("using saved model for", tournament_name)
            m = model.LinearModel.load(saved_model_name)
        else:
            print("saved model not found for", tournament_name)
            m = model.LinearModel(verbose=True)

            print("training model for", tournament_name)
            m.fit(data['train'], tournament_name)

        print("running predictions for", tournament_name, flush=True)
        # fit model with train data and make predictions for tournament data
        prediction = nx.production(m, data, tournament=tournament_name)

        # save predictions to csv file
        prediction_filename = '/tmp/prediction_' + tournament_name + '.csv'
        prediction.to_csv(prediction_filename, verbose=True)

    # submit the prediction

    # Numerai API key
    # You will need to create an API key by going to https://numer.ai/account and clicking "Add" under the "Your API keys" section.
    # Select the following permissions for the key: "Upload submissions", "Make stakes", "View historical submission info", "View user info"
    public_id = os.environ["NUMERAI_PUBLIC_ID"]
    secret_key = os.environ["NUMERAI_SECRET_KEY"]

    for tournament_name in tournaments:
        prediction_filename = '/tmp/prediction_' + tournament_name + '.csv'

        api = NumerAPI(public_id=public_id, secret_key=secret_key)
        model_id = api.get_models()
        api.upload_predictions(prediction_filename,
                               model_id=model_id['akrimedes_2'])
示例#9
0
def test_cross_validation():
    for md in [
            model.SVM(),
            model.MultiClassesLearner('KNN', {'n_neighbors': 1}),
            model.KNN(),
            model.XGBoost(),
            model.LinearModel(),
            model.DecisionTree(),
    ]:
        try:
            evaluation.cross_validation(md,
                                        X,
                                        y,
                                        scoring='both',
                                        n_jobs=1,
                                        n_splits=2)
        except Exception as e:
            print(md.__class__)
            raise e
示例#10
0
def pf_run(infile, outfile, posnoise, velnoise, PARTICLEN, FRAMEN, NOISE, log):
    np.random.seed(0)
    print "Loading data..."
    d = pickle.load(open(infile))
    print "done!"
    env = util.Environmentz((1.5, 2), (240, 320))

    eo = likelihood.EvaluateObj(240, 320)
    eo.set_params(10, 4, 2)
    le = likelihood.LikelihoodEvaluator(env, eo, log)

    model_inst = model.LinearModel(env,
                                   le,
                                   POS_NOISE_STD=posnoise,
                                   VELOCITY_NOISE_STD=velnoise)

    y = d['video'][:FRAMEN]

    weights, particles = pf.particle_filter(y, model_inst, len(y), PARTICLEN)
    np.savez_compressed(outfile, weights=weights, particles=particles)
示例#11
0
def main():
    # 取数据
    mgr = BusinessManager('data/')

    # 生成训练模型需要的train_data, test_data
    train_data, test_data = mgr.generate_model_data()
    #
    feature_builder = model.FeatureBuilder('tfidf')
    X_train, y_train, X_test, y_test = feature_builder.get_feature(train_data,\
                      test_data)

    lrmodel = model.LinearModel()
    lrmodel.train(X_train, y_train)
    lrmodel.save(model_path)

    mgr.set_sentiment_model(lrmodel)

    business_ids = mgr.get_business_ids()  # return list
    for bid in business_ids:
        summary = mgr.aspect_based_summary(bid)
        print(summary)
示例#12
0
def test_get_params():
    models = [
        model.LinearModel(),
        model.SVM(),
        model.DecisionTree(),
        model.MultiClassesLearner('KNN', {'n_neighbors': 1}),
        model.KNN(),
        model.XGBoost(),
    ]

    for md in models:
        params = md.get_params()
        try:
            assert 'normalizer_name' in params
            assert 'sample_method' in params
            if not isinstance(md, model.KNN):
                assert 'balanced_learning' in params
        except AssertionError as e:
            print(params)
            print(md.__class__)
            raise e
示例#13
0
def test_logistic_regression():
    m = model.LinearModel()
    X = np.random.normal(size=(1000, 10))
    y = np.array(X.sum(axis=1) > 0, dtype=np.int8)
    scores = evaluation.cross_validation(m, X, y, n_jobs=1)
    assert np.all(scores > 0.9)
示例#14
0
# None

# Project-specific Imports

import model

# Start the actual test

# Declare a system model:
spec = {
    "use_library"   : 0,
    "model_name"    : 'Ballbot',
    "time_sample"   : 0.01,
    "disc_flag"     : 1
}
ball_model = model.LinearModel(spec)
print(ball_model.model_name) # print out the model name
print(ball_model.u) # print out the symbolic vector of model control
print(ball_model.cont_model) # print out the continous dynamic equations
print(ball_model.disc_model) # print out the discrete time dynamic equations
print(ball_model.measure_func) # print out the measurement function

print('Testing the process time')
start1 = time.process_time() # get an initial start time to check to the see the computational time needed for subs-based system evaluation
evaled = ball_model.evaluate_dynamics([1, 2, 3, 4], [1,1], [2]) # Evaluate using the subs command
end1 = (time.process_time()-start1) # get the end time to check computational cost
print(' ') # Blank line
print('Number of Seconds for Subs Method')
print(end1)    
print('Value of the state when placed in the dynamics')
print(evaled)
示例#15
0
from business import BusinessManager
import model


def main(): 

	mgr = BusinessManager('data/')


	train_data, test_data = mgr.generate_model_data()


	feature_builder = model.FeatureBuilder('tfidf')
    X_train, y_train, X_test, y_test = feature_builder.get_feature(train_data, test_data)

    lrmodel = model.LinearModel()
    lrmodel.train(X_train, y_train)
    lrmodel.save(model_path)

    mgr.set_sentiment_model(lrmodel)

	
	business_ids = get_business_ids()
	for bid in business_ids:
		summary = mgr.aspect_based_summary(business_ids)
		print(summary)

if __name__ == "__main__":
	main()

示例#16
0
import model

tournaments = nx.tournament_names()
print(tournaments)

# download dataset from numerai
data = nx.download('numerai_dataset.zip')

for tournament_name in tournaments:
    saved_model_name = 'model_trained_' + tournament_name
    if os.path.exists(saved_model_name):
        print("using saved model for", tournament_name)
        m = model.LinearModel.load(saved_model_name)
    else:
        print("saved model not found for", tournament_name)
        m = model.LinearModel(verbose=True)

        print("training model for", tournament_name)
        m.fit(data['train'], tournament_name)

    print("running predictions for", tournament_name, flush=True)
    # fit model with train data and make predictions for tournament data
    prediction = nx.production(m, data, tournament=tournament_name)

    # save predictions to csv file
    prediction_filename = '/tmp/prediction_' + tournament_name + '.csv'
    prediction.to_csv(prediction_filename, verbose=True)

# submit the prediction

# Numerai API key
示例#17
0
import likelihood
import util
import model

np.random.seed(0)

d = pickle.load(open('simulate.pickle'))

env = util.Environment((1.5, 2), (240, 320))

eo = likelihood.EvaluateObj(240, 320)
eo.set_params(10, 4, 2)
le = likelihood.LikelihoodEvaluator(env, eo)

model_inst = model.LinearModel(env, le)

PARTICLEN = 4000

for frameno in range(1, 300, 30):
    print "frame", frameno
    prior_states = model_inst.sample_latent_from_prior(PARTICLEN)

    img = d['video'][frameno]

    scores = np.zeros(PARTICLEN)

    for si, state in enumerate(prior_states):
        scores[si] = model_inst.score_obs(img, state)

    score_sort_idx = np.argsort(scores)