def run(audio_db, prepared_dataset=None, new_song=None): # prepare data training_set, test_set = load_dataset(audio_db, new_song) if prepared_dataset: training_set = pickle.load(prepared_dataset) shuffle(training_set) print 'Train set: ' + repr(len(training_set)) print 'Test set: ' + repr(len(test_set)) # generate predictions predictions = [] if not new_song: for x in range(len(test_set)): neighbors = get_neighbors(training_set, test_set[x], K_POINTS) result = predict(neighbors) predictions.append(result) print('> predicted=' + repr(result) + ', actual=' + repr(test_set[x][-1])) accuracy = get_accuracy(test_set, predictions) # check accuracy print 'Accuracy: ' + repr(accuracy) return dump_best_accuracy( dataset=training_set, accuracy=accuracy ) # Returns True If accuracy is bigger than threshold else False else: neighbors = get_neighbors(training_set, test_set[0], K_POINTS) return predict(neighbors)
def predict_check(): try: local_dir = os.path.dirname(__file__) data_path = os.path.join(local_dir, 'data') # read in data from files data = read_in_data(data_path) data_train, data_val, data_test = split_data(data) data_train, data_val, data_test = list( map(lambda data_set: normalize(data_set), (data_train, data_val, data_test))) model = train(data_train[0], data_train[1]) coeff = model.coef_ assert coeff.shape == (3, ), "The shape of coefficient matrix should be (3, )" predict(data_test, model) except Exception: print(f'Training test failed') print(f'Exception trace back:') print(traceback.print_exc()) else: print('Training test passed.') print(f"Linear regression coefficient: ") print(coeff)
def main(): start = time() # データ構築 X_train, y_train, X_test = construct_data.construct_data() # 予測 prediction.predict(X_train, y_train, X_test, 'submission') print('time: %.2f s' % (time() - start))
def app(): html_temp = ''' <div style = "background-color:Lime;padding:13px;"> <h1 style ="color:black;text-align:center;">Pneumothorax Detector</h1></div> ''' st.markdown(html_temp, unsafe_allow_html=True) st.header("Identify Pneumothorax disease in chest x-rays") file = st.file_uploader("Please upload an image file", type=["jpg", "png", "dcm"]) if file is not None: #data = file.read() predict(file.name)
def pred_eval(): """Function used to evaluate the prediction result of the model Args: Returns: Pandas DataFrame: Root Mean Square Error value of all the three models. """ logging.info("Evaluating the Housing Value of all the three Models....") _, housing_labels = data_preprocessing.data_preprocess() ( Linear_Model_prediction, DT_Model_prediction, RF_Model_prediction, ) = prediction.predict() lin_mse = mean_squared_error(housing_labels, Linear_Model_prediction) lin_rmse = np.sqrt(lin_mse) # lin_mae = mean_absolute_error(housing_labels, Linear_Model_prediction) tree_mse = mean_squared_error(housing_labels, DT_Model_prediction) tree_rmse = np.sqrt(tree_mse) _, y_test = data_preprocessing.rfdata() final_mse = mean_squared_error(y_test, RF_Model_prediction) final_rmse = np.sqrt(final_mse) return lin_rmse, tree_rmse, final_rmse
def getvotes(): to_predict = request.json print(to_predict) pred = predict(to_predict) return jsonify({"predicted votes": pred.tolist()})
def cross_validation_p(filename, target_feature, k_range): df = filt_into_df(filename, target_feature) # apply 0-1 normalization to the filtered data normalized_df = normalize_df(df, target_feature) list_mape = [] list_sde = [] list_r_mape = [] list_r_sde = [] for k in k_range: for n in range(len(normalized_df)): test_point = dict(normalized_df.loc[n]) true_value = test_point[target_feature] del test_point[target_feature] df = normalized_df.drop([n]) predict_value = predict(df, test_point, target_feature, k) mape = abs(1 - predict_value / true_value) sde = (true_value - predict_value)**2 list_mape.append(mape) list_sde.append(sde) list_r_mape.append(sum(list_mape) / len(list_mape)) list_r_sde.append(math.sqrt(sum(list_sde) / len(list_sde))) result_dict = {'MAPE': list_r_mape, "SDE": list_r_sde} return result_dict
def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: # flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': # flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filePath = os.path.join(app.config['UPLOAD_FOLDER'], filename) try: file.save(filePath) preds = prediction.predict(filePath) except Exception as e: return e return response( preds, request.host_url.rstrip('/api/shoes/prediction')) return ''
def rebuild_pred_database(share_name, path="."): # Read databases hist_df = read_database(HIST_FILE_NAME.format(path, share_name)) # Calculate predictions pred_df = predict(hist_df, hist_df, PRED_DAYS) # Store databases write_database(pred_df, PRED_FILE_NAME.format(path, share_name))
def evaluate_test_data(): try: test_data = pd.read_csv(os.path.join(RAW_DATA_DIR, "test_data.csv"), encoding="utf-8") sentences = list(test_data["comment_text"].values) y = test_data[CLASSES].values _, y_pred = predict(sentences) with open(os.path.join(OUTPUTS_DIR, "evaluation_test.txt"), "w") as f: f.write("Shape of predicted values : {}".format(y_pred.shape)) f.write("\n") f.write("Shape of target values : {}".format(y.shape)) f.write("\n") tn, fp, fn, tp = confusion_matrix(y, y_pred).ravel() f.write("True neg : {}".format(tn)) f.write("\n") f.write("False pos : {}".format(fp)) f.write("\n") f.write("False neg : {}".format(fn)) f.write("\n") f.write("True pos : {}".format(tp)) f.write("\n") f.write("Classification report : ") f.write("\n") f.write(classification_report(y, y_pred)) f.write("\n") return True except Exception as e: logger.log("Exception in evaluating test data : {}".format(str(e))) return False
def post(): if request.method == 'POST': team1 = request.form['team1'] team2 = request.form['team2'] venue = request.form['venue'] toss_winner = request.form['tossWinner'] toss_decision = request.form['tossDecision'] winner, accuracy = predict(convert_back_to_team_names(team1), convert_back_to_team_names(team2), venue, toss_winner, toss_decision) print("Winner -> " + winner) print("Accuracy -> " + str("{0:.2f}".format(accuracy))) #print("Winning Team is -> " + winner_team) #home_team_name = convert_back_to_team_names(team1).__str__() #away_team_name = convert_back_to_team_names(team2).__str__() #flash("Predicted Winner for this match is : " + winner) #return render_template('index.html') #return render_template('results.html') a = { 'status': 1, 'msg': "Predicted Winner: " + winner + '<br>' + "Model Accuracy: " + str("{0:.2f}".format(accuracy)) + "%" } python2json = json.dumps(a) return python2json
def form(): if request.method == 'GET': return render_template('form.html') else: input_dict = {} #request was a POST input_dict['AMT_ANNUITY'] = float(request.form['amt_annuity']) input_dict['AMT_CREDIT'] = float(request.form['amt_credit']) input_dict['AMT_GOODS_PRICE'] = float(request.form['amt_goods_price']) input_dict['DAYS_BIRTH'] = float(request.form['days_birth']) input_dict['DAYS_EMPLOYED'] = float(request.form['days_employed']) input_dict['DAYS_ID_PUBLISH'] = float(request.form['days_id_publish']) input_dict['EXT_SOURCE_2'] = float(request.form['ext_source_2']) input_dict['EXT_SOURCE_3'] = float(request.form['ext_source_3']) input_dict['i_AMT_PAYMENT'] = float(request.form['i_amt_payment']) input_dict['pc_CNT_INSTALMENT_FUTURE'] = float(request.form['pc_cnt_instalment_future']) # print(input_dict) # print(predict(input_dict)) try: app.vars['result'] = predict(input_dict)[0] except ValueError: pass # app.vars['start_year'] = '' # app.vars['tag'] = 'Start year not specified/recognized' # app.vars['select'] = [feat[q] for q in range(3) if feat[q] in request.form.values()] return redirect('/result')
def start(): print("test*****************************") to_predict = request.json print(to_predict) pred = predict(to_predict) return jsonify({"predict happiness with partner":pred})
def predict_thread(data, kind, cursor, connection): prediction_table, rows_table = data["prediction_table"], data["rows_table"] station_id = data["station_id"] dump_rows = DumpRow.fetch_all(cursor, rows_table, station_id) prediction_rows = PredictionRow.fetch_all(cursor, rows_table, data["ids"]) predictions = predict(data, dump_rows, prediction_rows, kind) results = [] for prediction in predictions: timestamp = datetime.datetime.utcfromtimestamp(prediction["timestamp"]) timestamp = datetime.datetime(timestamp.year, timestamp.month, timestamp.day, timestamp.hour) now = datetime.datetime.utcnow() result = "(%d, TIMESTAMP '%s', %d, '%s', TIMESTAMP '%s', TIMESTAMP '%s')" % ( station_id, timestamp, prediction["value"], kind, now, now) results.append(result) cursor.execute( "INSERT INTO %s (station_id, datetime, available_bikes, kind, created_at, updated_at) VALUES %s" % (prediction_table, ", ".join(results))) connection.commit()
def handleData(self, data): #DO STUFF HERE print("data:" , data) predictionResult = prediction.predict(np.array(data)) print(predictionResult) print(predictionResult.shape) return int(predictionResult)
def get_labels(image): response = requests.get(image) response = response.content response = base64.b64encode(response).decode() labels = prediction.predict(response) labels = [x[1] for x in labels if x[2] > 0.2] return (image, labels)
def pred(): if request.method == 'POST': url = request.form['url'] data = prediction.predict(url) return render_template('index2.html', data=data) else: return
def game2(request): #team_name="no team input" if request.method == 'POST': print("POST!!!") form = PlayerForm(request.POST) teamA = {} teamB = {} teamA_name = request.POST.get('team_A') teamB_name = request.POST.get('team_B') try: for x in range(10): teamA[x] = splitString( request.POST.get("playerA_{}".format(x + 1))) teamB[x] = splitString( request.POST.get("playerB_{}".format(x + 1))) print(teamA) print(teamB) teamAWin = prediction.predict(teamA, teamB) except: errstr = "请按照格式填写表单" return render(request, 'black/game2.html', {'error': errstr}) s = "{}有{}%的概率胜过{}".format(teamA_name, teamAWin, teamB_name) return render(request, 'black/team_result.html', {'result': s}) else: teamAWin = None #print(team_name) return render(request, 'black/game2.html', {'error': ""})
def k_fold_cross_validation(x, y, degrees, lambdas): """ Does a k-fold cross validation to find the best degree and lambda, given a list of degrees and lambdas to test. Returns all measured accuracies as well as the best degree and lambda. """ accuracies = np.zeros((len(degrees), len(lambdas))) k_indices = build_k_indices(y) c = 1 for d, degree in enumerate(degrees): x_ = add_features(x, degree) for l, lamb in enumerate(lambdas): temp_acc = [] for indices in k_indices: tr_ind, te_ind = indices tr_x, tr_y = x_[tr_ind], y[tr_ind] te_x, te_y = x_[te_ind], y[te_ind] w = regress(tr_x, tr_y, lamb) preds = predict(w, te_x) temp_acc.append(accuracy(preds, te_y)) accuracies[d][l] = np.mean(temp_acc) print('\r%d / %d' % (c, len(lambdas) * len(degrees)), end='') c += 1 best_d, best_l = np.unravel_index(np.argmax(accuracies), accuracies.shape) best_d = degrees[best_d] best_l = lambdas[best_l] print('\n') return accuracies, best_d, best_l
def to_csv(df,test,sub): pred =predict(df,test) sub.iloc[:,-1]=pred print("### making submission to csv file ###") sub.to_csv('automlsub.csv',index=False) print("####################### Completed ###########################")
def search_sample(): query_image = config.queryImage category = predict(query_image) indexFile = config.indexDir + '/' + category + '.csv' catFile = config.dataFile + '/' + category cd = Region_Based( (8, 12, 3) ) #initialize the image descriptor. Here we specify the number of bins for hue, saturation and value. # load the query image and describe it query = cv2.imread(query_image) features = cd.describe(query) searcher = Searcher(indexFile) # perform the search results = searcher.search(features) i = 0 print("Got results") rmtree(config.resultDir) mkdir(config.resultDir) for (score, resultID) in results: print(catFile + "/" + resultID) # load the result image and display it copy2(catFile + "/" + resultID, config.resultDir) i = i + 1
def show_predictions(model, loader, device, noOfImages=8): """ Runs prediction on a sample data and plots the prediction """ data = next(iter(loader)) dep_out, seg_out = predict(model, device, data) prediction = {"dense_depth": dep_out, "fg_bg_mask": seg_out} plot_endgame_predictions(data, prediction, 8)
def get_label(self): data = self.content.get("1.0", END) data = data.strip() if not data: self.label_text.configure(text="*****") return label = prediction.predict(data) self.label_text.configure(text=f"Label: {label[0]}")
def predict_url(): url = request.json['url'] # url = '1000/70-house-detail.jpg' image = load_image_from_url(url) predicted_category, prediction = predict(image) response = {'category': predicted_category.tolist()} return jsonify(response)
def hello(): if request.method == 'GET': return render_template('index.html') if request.method == 'POST': if 'file' not in request.files: return "Image not uploaded" file = request.files['file'].read() try: img = Image.open(io.BytesIO(file)) except IOError: return "<p>Not an image</p>" img = img.convert("RGB") img.save("input.jpg") predict(img) return render_template('result.html')
def get_user_articles(self, user_id): # Get all articles conn = rdbms.connect(instance=INSTANCE_NAME, database=DATABASE_NAME) cursor = conn.cursor() SQL_GET_ARTICLES = 'SELECT * FROM articles a INNER JOIN sources s' cursor.execute(SQL_GET_ARTICLES) if cursor.rowcount == -1: # There are no article. return None recommended = [] for article in cursor.fetchall(): # Run prediction to see if this use is interested in article print article article_id = article[0] # get tags of the article SQL_GET_ARTICLE_TAGS = 'SELECT tag FROM tags WHERE article_id = %s' cursor.execute(SQL_GET_ARTICLE_TAGS, (article_id)) article_obj = { 'id': article[0], 'title': article[1], 'link': article[2], 'body': article[3], 'source_id': article[4], 'date': article[5], 'source_name': article[7] } for tag in cursor.fetchall(): tag = tag[0] result = p.predict(user_id, [tag]) print 'tag = ' + tag if result != None: if result.outputLabel == '1': # TODO:for now, If one tag passes, we pass. # keep score for r in result['outputMulti']: if r['label'] == '1': article_obj['score'] = score # Give this article to the user, in order of scores for i in range(0, recommended.len): if float(recommended[i].score) < float(score): recommended.insert(i, article_obj) break else: # If we can't predict, simply serve. logging.debug('could not predict for tag=' + tag) article_obj['score'] = 0 recommended.append(article_obj) break print recommended return recommended
def start(): to_predict = request.json print(to_predict) pred = predict(to_predict) return jsonify({ "Fraud probability": pred[0], 'Block transaction': ['NO', 'YES'][pred[1]] })
def home(): #Check for spam req = request.get_json() msg = req["message"] if msg == "i hate your face, you betray everyone": return "bully" results = predict(msg) print(results) return results
def prediction(project_id): flow = request.args.get('flow', None) day = request.args.get('day', None) if flow is None: return "Error: flow parameter is empty" if day is None: return "Error: day parameter is empty" csv = predict() return csv
def test_train_pred(target, df,input_columns): with tempfile.TemporaryDirectory() as temp_dir: path_csv = os.path.join(temp_dir, "test.csv") df.to_csv(path_csv) best_test_score, best_pipeline, input_columns = \ train(path_csv, target=target,input_columns=input_columns) assert isinstance(input_columns, list) dict_input = df[input_columns].iloc[42].T.to_dict() pred = predict(dict_input) assert isinstance(pred,float)
def __init__(self, params): self.lat = float(params['lat']) self.lon = float(params['lon']) self.alt = float(params['alt']) self.lat_threshold = float(params['lat_threshold']) self.lon_threshold = float(params['lon_threshold']) self.alt_threshold = float(params['alt_threshold']) self.ds = int(params['source']) self.IP = params['ip'] self.PORT = int(params['port']) self.max_alt = float(params['max_alt']) self.fmt = params['packet_format'] self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.queue = [] self.origin = array([self.lon, self.lat, self.alt]) self.predict = pre.predict(self.ds, self.max_alt) self.predict.prediction(self.origin)
def predict_thread(data, kind, cursor, connection): prediction_table, rows_table = data["prediction_table"], data["rows_table"] station_id = data["station_id"] dump_rows = DumpRow.fetch_all(cursor, rows_table, station_id) prediction_rows = PredictionRow.fetch_all(cursor, rows_table, data["ids"]) predictions = predict(data, dump_rows, prediction_rows, kind) results = [] for prediction in predictions: timestamp = datetime.datetime.utcfromtimestamp(prediction["timestamp"]) timestamp = datetime.datetime(timestamp.year, timestamp.month, timestamp.day, timestamp.hour) now = datetime.datetime.utcnow() result = "(%d, TIMESTAMP '%s', %d, '%s', TIMESTAMP '%s', TIMESTAMP '%s')" % (station_id, timestamp, prediction["value"], kind, now, now) results.append(result) cursor.execute("INSERT INTO %s (station_id, datetime, available_bikes, kind, created_at, updated_at) VALUES %s" % (prediction_table, ", ".join(results))) connection.commit()