Ejemplo n.º 1
0
def train():

    clf = request.form['train']
    if allowed_classifier(clf):
        string = str('train')
        hist_n = string + "hist.jpeg"
        cnmt_n = string + "cnmt.jpeg"
        pkl_hnd = store(app.config['static_path'], app.root_path)

        # Feature extraction
        data = utils.file_parser(
            os.path.join(app.config['upload_path'], "data.txt"))
        features = utils.feature_extractor(data['text'], 5000).todense()
        sh = data.shape

        # Preprocessing features and labels
        data_x = utils.preprocess_features(features, 2500)
        data_y, enc = utils.label_encoder(data['label'], False, None)
        pkl_hnd.dump(enc, 'enc')  # storing encoder

        # Splitting data into training set and validation set
        train_x, train_y, valid_x, valid_y = utils.train_valid(
            data_x, data_y, 0.2)

        #Balancing data with SMOTE
        text, label = utils.balance_data(train_x, train_y)

        # Selecting model and tuning hyperparameters
        tr = model(clf, text[:sh[0], :], label[:sh[0]], valid_x, valid_y)
        comb_mod = tr.model_selection()

        # Fitting model and predicting
        mod = tr.build_model(comb_mod)
        pkl_hnd.dump(mod, 'model')  # storing the model
        pr = predict_model(valid_x)
        pred = pr.predict_model(mod)

        #Training Statistics
        st = stats(pred, valid_y)
        acc, f1 = st.train_stats()

        #Plotting histogram and confusion matrix
        pkl_hnd.plot_hist(data['label'], hist_n)
        n_labels = np.unique(np.asarray(data['label']))
        pkl_hnd.dump(n_labels, 'n_labels')  # storing labels
        cnf_matrix = st.cnf_mtx()
        pkl_hnd.plot_confusion_matrix(
            cnf_matrix,
            n_labels,
            cnmt_n,
            normalize=True,
            title='Confusion matrix',
            cmap=plt.cm.Blues,
        )

        return render_template("train_result.html",
                               accuracy=acc,
                               img_hist=url_for(app.config['static_path'],
                                                filename=hist_n),
                               img_cfmt=url_for(app.config['static_path'],
                                                filename=cnmt_n),
                               f1=f1)
    else:
        flash('Please enter a valid classifier')
        return redirect(url_for('index'))
Ejemplo n.º 2
0
 def test_xml_parse_to_dict(self):
     testxml = 'testxml.xml'
     xmltodict = file_parser('xml', testxml)
     self.assertEqual(type(xmltodict), type({}))
     self.assertTrue(xmltodict['shapes'][0]['type'] == 'circle')
Ejemplo n.º 3
0
 def test_json_parse_to_dict(self):
     testjson = 'testjson.json'
     jsontodict = file_parser('json', testjson)
     self.assertEqual(type(jsontodict), type({}))
     self.assertTrue(jsontodict['shapes'][0]['type'] == 'circle')
Ejemplo n.º 4
0
from Rectangle import Rectangle
from Triangle import Triangle
from Ellipse import Ellipse
from utils import file_parser, area_organizer, file_exporter


filetype = raw_input("What file type (json/xml)? ")
filename = raw_input("filename: ")

data = file_parser(filetype, filename)
shape_list = []
if(data):
    for shape in data['shapes']:
        if shape['type'] == 'circle' or shape['type'] == 'non-circle':
            shape_list.append(Ellipse(shape['type'], int(shape['height']), int(shape['width'])))
        elif shape['type'] == 'square'or shape['type'] == 'rectangle':
            shape_list.append(Rectangle(shape['type'], int(shape['height']), int(shape['width'])))
        elif shape['type'] in ['scalene','isosceles','equillateral']:
            shape_list.append(Triangle(shape['type'], shape['angles'], int(shape['base']), int(shape['height'])))


    area_dict = area_organizer(shape_list)

    output = raw_input("output to (f)ile or (s)creen ")
    if 's' in output:
        file_exporter('screen', area_dict)
    elif 'f' in output:
        file_exporter('file', area_dict)
    else:
        print 'invalid destination'