def run(): fruits = CData(gyumpath, gyumindeps, feature=TAXLEVEL, cross_val=CROSSVAL) fruits.transformation = (TRANSFORMATION, TRANSFORMATION_PARAM) network = build_net(*fruits.neurons_required) testing = fruits.table("testing") zsind = CData(zsindpath, zsindeps, cross_val=0.0, feature=TAXLEVEL) vx, vy = zsind.learning, zsind.lindeps vx = fruits.transformation(vx) vy = fruits.embed(vy) initc, initacc = network.evaluate(*testing, verbose=0) initc, initacc = round(initc, 5), round(initacc, 5) print("Initial cost: {}\tacc: {}".format(initc, initacc)) X, y = fruits.table("learning") network.fit(X, y, batch_size=20, nb_epoch=400, validation_data=testing, verbose=0) tacc = network.evaluate(*testing, batch_size=fruits.n_testing, verbose=0)[-1] vacc = network.evaluate(vx, vy, verbose=0)[-1] # batchgen = fruits.batchgen(100, infinite=True) # log = network.fit_generator(batchgen, fruits.N, nb_epoch=15, validation_data=valid, verbose=verbose) print("T: {}\tV: {}".format(tacc, vacc)) return tacc, vacc
def full_training(validate=True, dump_weights=False): fruits = CData(gyumpath, gyumindeps, feature=TAXLEVEL, cross_val=0.0) fruits.transformation = (TRANSFORMATION, TRANSFORMATION_PARAM) network = build_net(*fruits.neurons_required) X, y = fruits.table("learning") network.fit(X, y, batch_size=30, nb_epoch=500, verbose=0) if dump_weights: weights = network.layers[0].get_weights() wghts = open("weights.csv", "w") wghts.write("\n".join([ "\t".join([str(float(cell)) for cell in line]) for line in weights[0].T ]).replace(".", ",")) wghts.close() if validate: vx, vy = CData(zsindpath, zsindeps, cross_val=0.0, feature=TAXLEVEL) vx = fruits.transformation(vx) vy = fruits.embed(vy) vacc = network.evaluate(vx, vy, batch_size=len(vy), verbose=0)[-1] probs = network.predict_proba(vx, verbose=0) preds = network.predict_classes(vx, verbose=0) print("ANN validation accuracy:", vacc) return probs, preds, vy, fruits
def get_data(): fruits = CData(gyumpath, gyumindeps, feature=TAXLEVEL, cross_val=CROSSVAL) fruits.transformation = (AFTER_TREATMENT, TRANSFORM_PARAM) zsind = CData(zsindpath, zsindeps, feature=TAXLEVEL, cross_val=0, transformation=None, param=None) lX, lY = fruits.learning, fruits.lindeps tX, tY = fruits.testing, fruits.tindeps vX, vY = zsind.learning, zsind.lindeps vX = fruits.transformation(vX) return lX, lY, tX, tY, vX, vY
def pull_data(crossval): from csxdata import CData, roots from csxdata.utilities.parser import parse_csv table = parse_csv(roots["csvs"] + "sum_ntab.csv", indeps=7) data, labels, headers = table labels = labels[..., -1].astype("float32") data = CData((data[..., -11:], labels), cross_val=crossval) data.transformation = "std" return data
def experiment(mode): mode = mode.lower()[0] if mode.lower()[0] == "c": chain = "Convolutional" elif mode.lower()[0] == "f": chain = "Fully Connected" else: raise RuntimeError("Wrong mode definition!") print("Experiment: MNIST classification with {} Neural Network!".format(chain)) net = get_fcnn() if mode == "f" else get_cnn() mnist = CData(mnist_to_lt(miscpath, (True if mode == "c" else False))) mnist.transformation = "standardization" net.fit(mnist.data, mnist.indeps, batch_size=20, nb_epoch=30, verbose=1, validation_split=0.2, show_accuracy=True)
def get_frame(pretreat="std", param=0): path, indps = paths["grapes"] frame = CData(path, indps, headers=1, feature="borregio", lower=True) frame.transformation = (pretreat, param) return frame
ae.fit(grapes.testing, grapes.testing, epochs=300, verbose=0) ae.prediction(grapes.learning) eX = ae.layers[2].output trGrapes = CData((eX, grapes.lindeps), headers=None) full_run(trGrapes) def neural_switcharoo(grapes): model = Network(input_shape=grapes.neurons_required[0], name="GrapesNet") model.add(DenseLayer(60, activation="tanh")) model.add(DenseLayer(grapes.neurons_required[1], activation="sigmoid")) model.finalize(cost="xent", optimizer="adam") model.describe(1) model.fit(*grapes.table("testing"), epochs=300, monitor=["acc"], verbose=0) model.prediction(grapes.learning) trX = model.layers[-2].output trGrapes = CData((trX, grapes.lindeps), headers=None) full_run(trGrapes) if __name__ == '__main__': dframe = CData(path, indepsn, headers=1, feature="borregio", lower=True) dframe.transformation = "std" autoencoder(dframe)
# from sklearn.ensemble import AdaBoostClassifier as Boost from sklearn.ensemble.bagging import BaggingClassifier as Boost from sklearn.naive_bayes import GaussianNB from csxdata import CData from SciProjects.grapes import path, indepsn if __name__ == '__main__': data = CData(path, indepsn, feature="evjarat", headers=1, cross_val=0.2, lower=True) data.transformation = "std" model = Boost(GaussianNB(), n_estimators=100) model.fit(data.learning, data.lindeps) preds = model.predict(data.testing) eq = [left == right for left, right in zip(preds, data.tindeps)] print("Acc:", sum(eq) / len(eq))
def pull_mnist_data(): mnist = CData(roots["misc"] + "mnist.pkl.gz", cross_val=0.18) mnist.transformation = "std" return mnist
from sklearn.ensemble import RandomForestClassifier as RF from csxdata import CData from SciProjects.fruits import * LABEL = "Familia" rf = RF(n_estimators=100, n_jobs=1) fruits = CData(gyumpath, gyumindeps, cross_val=0.2, feature=LABEL) fruits.transformation = ("lda", 5) zsind = CData(zsindpath, zsindeps, cross_val=0.0) valid = fruits.transformation(zsind.learning, zsind.lindeps) rf.fit(fruits.learning, fruits.lindeps) tpredict = rf.predict(fruits.testing) vpredict = rf.predict(valid[0]) tacc = [right == left for right, left in zip(tpredict, fruits.tindeps)] vacc = [right == left for right, left in zip(vpredict, valid[1])] print("TPredictions ({}%):\n{}".format(int(100 * sum(tacc) / len(tacc)), tpredict)) print("TestingY:\n", fruits.tindeps) print("VPredictions ({}%):\n{}".format(int(100 * sum(vacc) / len(vacc)), vpredict)) print("ValidY:\n", valid[1])
def get_frame(transf, params): path, indepsn = paths["grapes"] grapes = CData(path, indepsn, headers=1, feature=FEATURE, lower=True) grapes.transformation = (transf, params) return grapes