def recode_step2_page(): try: data_id = request.args.get('data_id') target = request.args.get('target') name = request.args.get('name') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) names=data_frame.column_names() types=data_frame.column_types() orig_data = data_frame[str(target)] norig_data = orig_data.to_numpy() target_data = data_frame[str(target)].unique() ntarget_data = target_data.to_numpy() if request.method == 'POST': mapped_values = [] data_frame = safely_add_col(str(target) + '_uncoded', data_frame[str(target)], data_frame) for x in range(0, ntarget_data.__len__()): mapped_values.append(str(request.form['new_value' + str(x)])) cross_ref = [] for x in range(0, names.__len__()): if (str(types[x].__name__) == "str"): cross_ref.append(str(names[x])) new_data = [] for field in norig_data: for y in range(0, ntarget_data.__len__()): if str(ntarget_data[y]) == str(field): new_data.append(int(mapped_values[y])) sa = SArray(new_data) data_frame[str(target)] = sa fwd_id = save_data(my_data, name, data_frame) flash('Successfully re-coded ' + target + '!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) return render_template('pages/data/transforms/code_field_step2.html', my_data=my_data, form=form, data_frame=data_frame, names=names, name=name, types=types, ntarget_data=ntarget_data, target=target) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def fill_na_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) names=data_frame.column_names() types=data_frame.column_types() if request.method == 'POST': value = str(request.form['value']) name = str(request.form['name']) for feature in request.form.getlist('features'): orig_data = data_frame[str(feature)] print(orig_data.dtype.__name__) if orig_data.dtype.__name__ == "int": try: data_frame[str(feature)] = orig_data.fillna(int(value)) except Exception as e: flash('Opps! Looks like you passed something I could not parse as an integer.', 'error') return redirect(request.referrer) if orig_data.dtype.__name__ == "float": try: data_frame[str(feature)] = orig_data.fillna(float(value)) except Exception as e: flash('Opps! Looks like you passed something I could not parse as an float.', 'error') return redirect(request.referrer) if orig_data.dtype.__name__ == "str": try: data_frame[str(feature)] = orig_data.fillna(str(value)) except Exception as e: flash('Opps! Looks like you passed something I could not parse as an string.', 'error') return redirect(request.referrer) fwd_id = save_data(my_data, name, data_frame) flash('Successfully replaced N/A values!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) return render_template('pages/data/transforms/fill_na.html', my_data=my_data, data_frame=data_frame, names=names, types=types, form=form) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def model_home_page(): try: my_models = TrainedModel.query.filter_by(user_id=current_user.id).all() return render_template('pages/models/model_page.html', my_models=my_models) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def download(filename): try: model_id = request.args.get('model_id') my_model = TrainedModel.query.filter_by(id=model_id).first() root_dir = os.path.dirname(os.getcwd()) direc = os.path.dirname(my_model.path) direc = os.path.join(direc, str(my_model.user_id)) return send_from_directory(directory=direc, filename=str(my_model.name) + str(my_model.api_key) + "_model_cross_validation.csv") except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def download_predictions(filename): try: dict_id = request.args.get('dict') my_dict = Predictions.query.filter_by(id=dict_id).first() direc = os.path.dirname(my_dict.path) direc = os.path.join(direc, str(my_dict.user_id)) return send_from_directory(directory=direc, filename=os.path.basename(my_dict.oname)) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def outlliers_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) names=data_frame.column_names() types=data_frame.column_types() if request.method == 'POST': cent = float(request.form['cent']) name = str(request.form['name']) target = str(request.form['target']) mean = data_frame[target].mean() rows = [] for row in data_frame: if row[target] is not None: diff = abs(float(row[target]) - mean) pdiff = diff/mean if pdiff < cent: rows.append(row) else: rows.append(row) sf = tc.SFrame(rows) sf = sf.unpack('X1', column_name_prefix='') print(sf) fwd_id = save_data(my_data, name, sf) flash('Successfully removed outliers!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) return render_template('pages/data/transforms/outlier.html', my_data=my_data, data_frame=data_frame, names=names, types=types, form=form) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def model_analytics_page(): try: model_id = request.args.get('model_id') my_model = TrainedModel.query.filter_by(id=model_id).first() my_data = UserData.query.filter_by(id=my_model.data_id).first() current_time = datetime.datetime.utcnow() thirty_days_ago = current_time - datetime.timedelta(days=30) runs = ModelRun.query.filter_by(model_id=my_model.id).filter(ModelRun.created_at>thirty_days_ago).limit(5000).all() values = [] for run in runs: values.append(float(run.prediction)) to_render = {} to_render['means'] = [] to_render['min'] = [] to_render['max'] = [] to_render['std'] = [] to_render['var'] = [] to_render['runs_by_day'] = [] narr = np.array([]) for x in range(1, len(values)+1): narr = np.append(narr, values[x-1]) to_render['means'].append(np.nanmean(narr)) to_render['min'].append(np.nanmin(narr)) to_render['max'].append(np.nanmax(narr)) to_render['std'].append(np.nanstd(narr)) to_render['var'].append(np.nanvar(narr)) result = db.engine.execute("SELECT count(a.id) FROM (SELECT to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date FROM generate_series(0, 31, 1) AS offs) d LEFT OUTER JOIN runs a ON d.date = to_char(date_trunc('day', a.created_at), 'YYYY-MM-DD') AND a.model_id = '" + str(my_model.id) + "' GROUP BY d.date ORDER BY d.date") for item in result: to_render['runs_by_day'].append(int(item["count"])) return render_template('pages/models/analytics.html', to_render=to_render, my_data=my_data, my_model=my_model) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def clear_error_page(): try: db.session.query(ErrorLog).delete() db.session.commit() flash('You successfully cleared your error log!', 'success') return redirect(url_for('main.error_log_page')) except Exception as e: flash( 'Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def convert_magic_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) names=data_frame.column_names() types=data_frame.column_types() if request.method == 'POST': magic = str(request.form['magic']) name = str(request.form['name']) for feature in request.form.getlist('features'): orig_data = data_frame[str(feature)] norig_data = orig_data.to_numpy() new_data = [] for item in norig_data: if str(item) == magic: new_data.append(None) else: new_data.append(item) sa = SArray(new_data) data_frame[str(feature)] = sa fwd_id = save_data(my_data, name, data_frame) flash('Successfully cleared magic values!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) return render_template('pages/data/transforms/convert_magic.html', my_data=my_data, data_frame=data_frame, names=names, types=types, form=form) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def delete_model_page(): try: model_id = request.args.get('model_id') my_model = TrainedModel.query.filter_by(id=model_id).first() project_id = my_model.project_id db.session.query(TrainedModel).filter_by(id = model_id).delete() db.session.commit() flash('You successfully deleted your model!', 'success') return redirect(url_for('main.my_project_page', project_id=project_id)) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def custom_transform_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() form = UserDataForm(request.form, obj=None) data_frame = tc.load_sframe(my_data.sname) cols = data_frame.column_names() types = data_frame.column_types() if request.method == 'POST': try: transform_code = request.form['transform_code'] target = request.form['target'] name = request.form['name'] transformed_data = [] local_space = {} for val in data_frame[str(target)]: context = {"in_var": 1, "scipy_stats": scipy_stats, "np": np} exec(transform_code) in context transformed_data.append(context['out_var']) sa = SArray(transformed_data) data_frame[str(name)] = sa fwd_id = save_data(my_data, request.form['name'], data_frame) flash('Data transform is sucessful!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) except Exception as inst: flash('Failed to run Python code! ' + str(inst), 'error') return render_template('pages/data/transforms/custom_transform.html', my_data=my_data, names=cols, types=types, form=form) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def smote_step1(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) target = None cols = [] display_cols = [] names=data_frame.column_names() types=data_frame.column_types() for x in range(0, names.__len__()): if (str(types[x].__name__) == "str"): cols.append(str(names[x])) if request.method == 'POST': target = request.form['target'] return redirect(url_for('transforms.smote_page', data_id=my_data.id, target=target, name=request.form['name'], seed=request.form['seed'], neighbors=request.form['neighbors'], algorithm=request.form['algorithm'])) return render_template('pages/data/transforms/smote_step1.html', my_data=my_data, form=form, data_frame=data_frame, names=names, types=types, target=target, cols=cols) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def split_session_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) cols = [] display_cols = [] names=data_frame.column_names() types=data_frame.column_types() print(names) for x in range(0, names.__len__()): if (str(types[x].__name__) == "int"): cols.append(str(names[x])) print(cols) if request.method == 'POST': training_set,test_set = tc.activity_classifier.util.random_split_by_session(data_frame, session_id=str(request.form['idField']), fraction=float(request.form['percent'])) save_data(my_data, request.form['train'], training_set) save_data(my_data, request.form['test'], test_set) flash('Successfully created train/test split for ' + my_data.name + '!', 'success') return redirect(url_for('main.my_project_page', project_id=my_data.project_id)) return render_template('pages/data/transforms/split_session.html', my_data=my_data, form=form, data_frame=data_frame, types=types, names=cols) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def predict_page(): try: api_key = request.args.get('api_key') model = None if api_key in _local_cache: model = _local_cache[api_key] else: my_model = TrainedModel.query.filter_by(api_key=api_key).first() tc_model = tc.load_model(my_model.mname) _local_cache[api_key] = { 'model_id': my_model.id, 'tc_model': tc_model } model = {'model_id': my_model.id, 'tc_model': tc_model} ret = "" if type(request.json) is not list: predicted_scores = predict([request.json], model) ret = {"prediction": round(predicted_scores[0], 2)} else: predicted_scores = predict(request.json, model) ret = { "predictions": np.around(predicted_scores.to_numpy(), 2).tolist() } return (jsonify(ret), 200) except Exception as e: error = ErrorLog() error.user_id = -999 error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return (jsonify({ "error": "That was unexpected. We logged it and are looking into it." }), 500)
def predictions_page(): try: model_id = request.args.get('model_id') my_model = TrainedModel.query.filter_by(id=model_id).first() my_data = UserData.query.filter_by(id=my_model.data_id).first() if my_data.user_id is not current_user.id: flash('Opps! Do data found', 'error') return redirect(request.referrer) predictions = Predictions.query.filter_by(model_id=my_model.id).all() return render_template('pages/models/predictions.html', my_data=my_data, predictions=predictions, my_model=my_model) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def transform_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() data_frame = tc.load_sframe(my_data.sname) cols = data_frame.column_names() types = data_frame.column_types() return render_template('pages/data/transforms/transform.html', my_data=my_data, data_frame=data_frame.head(), names=cols, types=types) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def rename_feature_page(): try: data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) names=data_frame.column_names() types=data_frame.column_types() if request.method == 'POST': feature_name = str(request.form['feature_name']) name = str(request.form['name']) target = str(request.form['target']) if has_column(feature_name, data_frame): flash('Opps! You appear to already have a feature with this name.', 'error') return redirect(request.referrer) sf = data_frame.rename({target: feature_name}) fwd_id = save_data(my_data, name, sf) flash('Successfully transformed the data!', 'success') return redirect(url_for('data.data_details_page', data_id=fwd_id)) return render_template('pages/data/transforms/rename_feature.html', my_data=my_data, data_frame=data_frame, names=names, types=types, form=form) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def train_model_page(): try: tc.config.set_num_gpus(0) data_id = request.args.get('data_id') my_data = UserData.query.filter_by(id=data_id).first() if my_data.user_id is not current_user.id: flash('Opps! Do data found', 'error') return redirect(request.referrer) my_model = TrainedModel() form = TrainModelForm(request.form, obj=my_model) data_frame = tc.load_sframe(my_data.sname) if request.method == 'POST': form.populate_obj(my_model) model_type = request.form['model'] model_class = request.form['model_class'] max_depth = request.form['max_depth'] max_iterations = request.form['max_iterations'] session_id = str(request.form['session_id']) time_field = str(request.form['time_field']) my_model.mtype = model_type # data_frame = data_frame.sort(str(request.form['target'])) if model_type == 'svm': label_count = data_frame[str(request.form['target'])].unique() if len(label_count) > 2: flash('SVM only supports binary classification - try another method.', 'error') return redirect(request.referrer) if model_type != 'deep': df = shuffle(data_frame.to_dataframe()) for y in range(0, 500): df = shuffle(df) data_frame = tc.SFrame(data=df) else: tfrm = data_frame.to_dataframe() tfrm = tfrm.sort_values(by=[session_id, time_field]) data_frame = tc.SFrame(data=tfrm) data_frame[session_id] = data_frame[session_id].astype(int) options_dict = {} if max_depth is not None: options_dict['max_depth'] = int(max_depth) if max_iterations is not None: options_dict['max_iterations'] = int(max_iterations) data_frame_cleaned = data_frame.dropna(str(request.form['target']), how="any") cols = [] for feature in request.form.getlist('features'): if str(feature) == str(request.form['target']): flash('You can not select the target field in your training features.', 'error') return redirect(url_for('model.train_model_page', data_id=data_id)) data_frame_cleaned = data_frame_cleaned.dropna(str(feature), how="any") cols.append(str(feature)) if data_frame_cleaned.num_rows() < 2: flash('After cleaning, there is no data left. You have a data quality issue.', 'error') return redirect(url_for('model.train_model_page', data_id=data_id)) my_model.user_id = current_user.id print("USER ID") print(my_model.user_id) old_stdout = sys.stdout sys.stdout = mystdout = StringIO() training_loss = ((float(data_frame.num_rows()) - float(data_frame_cleaned.num_rows())) / float(data_frame.num_rows())) * 100 train_data = None test_data = None if model_type != 'deep': train_data,test_data = data_frame_cleaned.random_split(.80,seed=0) else: train_data,test_data = tc.activity_classifier.util.random_split_by_session(data_frame_cleaned, session_id=session_id, fraction=0.8) tc_model = None # Setup options if model_type == 'gradient': if max_depth is None: options_dict['max_depth'] = 6 if max_iterations is None: options_dict['max_iterations'] = 10 elif model_type == 'linear': # Do nothing interesting options_dict = {} elif model_type == 'decision': if max_depth is None: options_dict['max_depth'] = 6 elif model_type == 'random': if max_depth is None: options_dict['max_depth'] = 6 if max_iterations is None: options_dict['max_iterations'] = 10 elif model_type == 'svm': if max_iterations is None: options_dict['max_iterations'] = 10 elif model_type == 'deep': if max_iterations is None: options_dict['max_iterations'] = 10 results = {} console = None if model_class == "predictor": best_run = None test_run = None working_results = {} working_train_data,working_test_data = data_frame_cleaned.random_split(.80,seed=0) num_rolls = 50 if working_train_data.num_rows() < 1000: num_rolls = 70 if working_train_data.num_rows() < 500: num_rolls = 100 if working_train_data.num_rows() < 200: num_rolls = 200 for x in range(0, num_rolls): working_train_data,working_test_data = data_frame_cleaned.random_split(.80) if model_type == 'gradient': test_run = tc.boosted_trees_regression.create(working_train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'], max_iterations = options_dict['max_iterations'] ) elif model_type == 'linear': test_run = tc.linear_regression.create(working_train_data, target=str(request.form['target']), validation_set=None, features=cols ) elif model_type == 'decision': test_run = tc.decision_tree_regression.create(working_train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'] ) elif model_type == 'random': test_run = tc.random_forest_regression.create(working_train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'], max_iterations = options_dict['max_iterations'] ) working_results = test_run.evaluate(working_test_data) if best_run is None or working_results['max_error'] < best_run: tc_model = test_run train_data = working_train_data test_data = working_test_data results = working_results best_run = results['max_error'] console = mystdout.getvalue() mystdout.truncate(0) else: if model_type == 'gradient': tc_model = tc.boosted_trees_classifier.create(train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'], max_iterations = options_dict['max_iterations']) elif model_type == 'linear': tc_model = tc.logistic_classifier.create(train_data, target=str(request.form['target']), validation_set=None, features=cols ) elif model_type == 'decision': tc_model = tc.decision_tree_classifier.create(train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'] ) elif model_type == 'random': tc_model = tc.random_forest_classifier.create(train_data, target=str(request.form['target']), validation_set=None, features=cols, max_depth = options_dict['max_depth'], max_iterations = options_dict['max_iterations'] ) elif model_type == 'svm': tc_model = tc.svm_classifier.create(train_data, target=str(request.form['target']), validation_set=None, features=cols, max_iterations = options_dict['max_iterations']) elif model_type == 'deep': tc_model = tc.activity_classifier.create(train_data, session_id=session_id, target=str(request.form['target']), validation_set=None, features=cols, max_iterations = options_dict['max_iterations']) results = tc_model.evaluate(test_data) my_model.user_id = current_user.id my_model.data_id = my_data.id my_model.project_id = my_data.project_id my_model.path = my_data.path my_model.options = options_dict my_model.api_key = str(uuid.uuid4()) imp = [] if model_type != 'linear' and model_type != 'svm' and model_type != 'deep': imp = tc_model.get_feature_importance() importance = [] for col in imp: importance.append({"name": str(col["name"]), "index": str(col["index"]), "count": str(col["count"])}) my_model.features = {"time_field": time_field, "session_id": session_id, "training_loss": training_loss, "training_rows": train_data.num_rows(), "test_rows": test_data.num_rows(), "features": cols, "target": request.form['target'], "importance": importance, "model_type": model_type, "model_class": model_class} sys.stdout = old_stdout if model_class == "predictor": my_model.results = results my_model.console = console else: print(results) if (model_type != 'svm'and model_type != 'deep'): my_model.results = {'f1_score': nan_to_null(results['f1_score']), 'auc': nan_to_null(results['auc']), 'recall': nan_to_null(results['recall']), 'precision': nan_to_null(results['precision']), 'log_loss': nan_to_null(results['log_loss']), 'accuracy': nan_to_null(results['accuracy'])} else: my_model.results = {'f1_score': nan_to_null(results['f1_score']), 'auc': "N/A", 'recall': nan_to_null(results['recall']), 'precision': nan_to_null(results['precision']), 'log_loss': "N/A", 'accuracy': nan_to_null(results['accuracy'])} my_model.console = mystdout.getvalue() my_model.mname = os.path.join(my_model.path, str(my_model.api_key) + "_model") tc_model.save(my_model.mname) my_model.console = my_model.console.strip("\x00") db.session.add(my_model) db.session.commit() flash('Model has been saved!', 'success') return redirect(url_for('model.model_details_page', model_id=my_model.id)) return render_template('pages/models/train_model_page.html', my_data=my_data, form=form, data_frame=data_frame, names=data_frame.column_names(), types=data_frame.column_types()) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP. Error: ' + str(e), 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def prediction_page(): try: dict_id = request.args.get('dict') my_dict = Predictions.query.filter_by(id=dict_id).first() my_model = TrainedModel.query.filter_by(id=my_dict.model_id).first() my_data = UserData.query.filter_by(id=my_model.data_id).first() if my_data.user_id is not current_user.id: flash('Opps! Do data found', 'error') return redirect(request.referrer) examples = len(my_dict.predictions) orig_data = [] apredictions = [] for item in my_dict.predictions: apredictions.append(float(item)) npredicted_data = np.array(apredictions) for x in range(0, len(my_dict.originals)): if my_dict.originals[x] is None: orig_data.append(npredicted_data[x]) else: orig_data.append(float(my_dict.originals[x])) norig_data = np.array(orig_data) to_render = {} results = {} variance = [] scatter = [] sorted_variance = [] truth_table = {} npredicted_data = npredicted_data[np.logical_not(np.isnan(npredicted_data))] if my_model.features['model_class'] == "predictor": for x in range(0, len(npredicted_data)): if norig_data[x] is None or npredicted_data[x] is None: variance.append(np.float64(0.0)) else: variance.append(np.absolute(norig_data[x]-npredicted_data[x])) scatter.append([norig_data[x],npredicted_data[x]]) nvariance = np.array(variance) mean, sigma = np.mean(npredicted_data), np.std(npredicted_data) std_err = sigma / (sqrt(len(npredicted_data))) to_render['confidence_99'] = std_err * 2.575 to_render['confidence_95'] = std_err * 1.96 to_render['confidence_90'] = std_err * 1.645 to_render['confidence_85'] = std_err * 1.44 to_render['confidence_80'] = std_err * 1.28 outlier = [] upper = np.percentile(nvariance,75) lower = np.percentile(nvariance,25) for item in nvariance: if item > upper or item < lower: outlier.append([0, item]) explained, unexplained = compute_explained_variance(norig_data, npredicted_data) to_render['explained_variance'] = explained to_render['unexplained'] = unexplained to_render['outliers'] = outlier to_render['orig'] = {"min": round(np.nanmin(norig_data), 2), "max": round(np.nanmax(norig_data), 2), "mean": round(np.nanmean(norig_data), 2), "median": np.median(norig_data), "upper": np.percentile(norig_data,75), "lower": np.percentile(norig_data,25)} to_render['predicted'] = {"min": round(np.nanmin(npredicted_data), 2), "max": round(np.nanmax(npredicted_data), 2), "mean": round(np.nanmean(npredicted_data), 2), "median": np.median(npredicted_data), "upper": np.percentile(npredicted_data,75), "lower": np.percentile(npredicted_data,25)} to_render['variance'] = {"min": round(np.nanmin(nvariance), 2), "max": round(np.nanmax(nvariance), 2), "mean": round(np.nanmean(nvariance), 2), "median": np.median(nvariance), "upper": np.percentile(nvariance,75), "lower": np.percentile(nvariance,25)} to_render['scatter'] = scatter sorted_variance = sorted(variance) else: total_correct = 0 total_missed = 0 my_len = len(norig_data) - 1 for x in range(0, my_len): if (str(norig_data[x]), str(npredicted_data[x])) not in truth_table: truth_table[(str(norig_data[x]), str(npredicted_data[x]))] = 1 else: truth_table[(str(norig_data[x]), str(npredicted_data[x]))] = truth_table[(str(norig_data[x]), str(npredicted_data[x]))] + 1 if norig_data[x] == npredicted_data[x]: total_correct = total_correct + 1 else: total_missed = total_missed + 1 to_render['accuracy'] = {"total_missed": total_missed, "total_correct": total_correct} try: results["f1_score"] = metrics.f1_score(norig_data, npredicted_data) except Exception as e: results["f1_score"] = None try: results["recall"] = metrics.recall_score(norig_data, npredicted_data) except Exception as e: results["recall"] = None try: results["precision"] = metrics.precision_score(norig_data, npredicted_data) except Exception as e: results["precision"] = None try: results["accuracy"] = metrics.accuracy_score(norig_data, npredicted_data) except Exception as e: results["accuracy"] = None return render_template('pages/models/prediction.html', my_data=my_data, my_dict=my_dict, my_model=my_model, col_name=my_model.features['target'], to_render=to_render, results=results, filename=os.path.basename(my_dict.oname), truth_table=truth_table, npredicted_data=npredicted_data.tolist(), norig_data=norig_data.tolist(), variance=variance, sorted_variance=sorted_variance, examples=examples) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)
def bootstrap_page(): try: model_id = request.args.get('model_id') my_model = TrainedModel.query.filter_by(id=model_id).first() my_data = UserData.query.filter_by(id=my_model.data_id).first() if my_data.user_id is not current_user.id: flash('Opps! Do data found', 'error') return redirect(request.referrer) to_render = {} variance_array = [] if my_model.bootstrap is not None: to_render = my_model.bootstrap else: data_frame = tc.load_sframe(my_data.sname) data_frame = data_frame.add_row_number(column_name='process_id') cols = [] data_frame_cleaned = data_frame.dropna(str(my_model.features['target']), how="any") for feature in my_model.features['features']: data_frame_cleaned = data_frame_cleaned.dropna(str(feature), how="any") cols.append(str(feature)) # 1. Randomly select x% of sample and train the algorithm. # 2. Predict scores for the remaining 100-x% of the sample # 3. correlate actual and predicted scores, and square the result. # 4. Repeat 2000 time # 5. arrange all outcome in ascending order # 6. report the 50th observation as lower bound of CI # 7. report 1950th observation as upper bound CI variance_array = [] r2_array = [] df = shuffle(data_frame_cleaned.to_dataframe()) for y in range(0, 100): df = shuffle(df) data_frame = tc.SFrame(data=df) old_stdout = sys.stdout sys.stdout = mystdout = StringIO() for x in range(0,2000): # Grab data df = shuffle(data_frame.to_dataframe()) data_frame = tc.SFrame(data=df) training_set,test_set = data_frame.random_split(.80,seed=0) # Train model tc_model = None if my_model.features['model_type'] == 'gradient': tc_model = tc.boosted_trees_regression.create(training_set, target=str(my_model.features['target']), validation_set=None, features=cols, max_depth = my_model.options['max_depth'], max_iterations = my_model.options['max_iterations'] ) elif my_model.features['model_type'] == 'linear': tc_model = tc.linear_regression.create(training_set, target=str(my_model.features['target']), validation_set=None, features=cols ) elif my_model.features['model_type'] == 'decision': tc_model = tc.decision_tree_regression.create(training_set, target=str(my_model.features['target']), validation_set=None, features=cols, max_depth = my_model.options['max_depth'] ) elif my_model.features['model_type'] == 'random': tc_model = tc.random_forest_regression.create(training_set, target=str(my_model.features['target']), validation_set=None, features=cols, max_depth = my_model.options['max_depth'], max_iterations = my_model.options['max_iterations'] ) predicted_scores = tc_model.predict(test_set) origs = [] for item in test_set[str(my_model.features['target'])]: origs.append(item) norigs = np.array(origs) npredicted_scores = np.array(predicted_scores) mean_var = bootstrap_ci(norigs, npredicted_scores) r2 = compute_r2(norigs, npredicted_scores) variance_array.append(mean_var) r2_array.append(r2) sys.stdout = old_stdout variance_array.sort() r2_array.sort() to_render['bootstrap_confidence_99_lower'] = variance_array[0] to_render['bootstrap_confidence_99_upper'] = variance_array[1999] to_render['bootstrap_confidence_95_lower'] = variance_array[49] to_render['bootstrap_confidence_95_upper'] = variance_array[1949] to_render['bootstrap_confidence_90_lower'] = variance_array[99] to_render['bootstrap_confidence_90_upper'] = variance_array[1899] to_render['bootstrap_confidence_85_lower'] = variance_array[149] to_render['bootstrap_confidence_85_upper'] = variance_array[1849] to_render['bootstrap_confidence_80_lower'] = variance_array[199] to_render['bootstrap_confidence_80_upper'] = variance_array[1799] to_render['variance_array'] = variance_array my_model.bootstrap = to_render db.session.commit() return render_template('pages/models/bootstrap.html', to_render=to_render, my_data=my_data, variance=to_render['variance_array'] , my_model=my_model) except Exception as e: flash('Opps! Something unexpected happened. On the brightside, we logged the error and will absolutely look at it and work to correct it, ASAP.', 'error') error = ErrorLog() error.user_id = current_user.id error.error = str(e.__class__) error.parameters = request.args db.session.add(error) db.session.commit() return redirect(request.referrer)