def predict(): if request.method == 'POST': try: file_object = open("logs/GeneralLogs.txt", 'a+') log_writer.log(file_object, 'Start getting data from UI') CRIM = float(request.form['CRIM']) ZN = float(request.form['ZN']) INDUS = float(request.form['INDUS']) CHAS = float(request.form['CHAS']) NOX = float(request.form['NOX']) RM = float(request.form['RM']) AGE = float(request.form['AGE']) DIS = float(request.form['DIS']) RAD = float(request.form['RAD']) TAX = float(request.form['TAX']) PTRATIO = float(request.form['PTRATIO']) B = float(request.form['B']) LSTAT = float(request.form['LSTAT']) log_writer.log(file_object, 'Complete getting data from UI') mydict = { 'CRIM': CRIM, 'ZN': ZN, 'INDUS': INDUS, 'CHAS': CHAS, 'NOX': NOX, 'RM': RM, 'AGE': AGE, 'DIS': DIS, 'RAD': RAD, 'TAX': TAX, 'PTRATIO': PTRATIO, 'B': B, 'LSTAT': LSTAT } log_writer.log(file_object, 'Passing mydict to prediction.predict_data') prediction = predict_data(mydict, log_writer) return render_template('results.html', prediction=prediction) log_writer.log( file_object, '=================================================') except Exception as e: print('The Exception message is: ', e) return 'something is wrong' # return render_template('results.html') else: return render_template('index.html')
def predict(): if request.method == 'POST': try: file_object = open("logs/GeneralLogs.txt", 'a+') log_writer.log(file_object, 'Start getting data from UI') fixed_acidity = float(request.form['fixed_acidity']) volatile_acidity = float(request.form['volatile_acidity']) citric_acid = float(request.form['citric_acid']) residual_sugar = float(request.form['residual_sugar']) chlorides = float(request.form['chlorides']) free_sulfur_dioxide = float(request.form['free_sulfur_dioxide']) total_sulfur_dioxide = float(request.form['total_sulfur_dioxide']) density = float(request.form['density']) pH = float(request.form['pH']) sulphates = float(request.form['sulphates']) alcohol = float(request.form['alcohol']) log_writer.log(file_object, 'Complete getting data from UI') mydict = { 'fixed acidity': fixed_acidity, 'volatile acidity': volatile_acidity, 'citric acid': citric_acid, 'residual sugar': residual_sugar, 'chlorides': chlorides, 'free sulfur dioxide': free_sulfur_dioxide, 'total sulfur dioxide': total_sulfur_dioxide, 'density': density, 'pH': pH, 'sulphates': sulphates, 'alcohol': alcohol } log_writer.log(file_object, 'Passing mydict to prediction.predict_data') prediction = predict_data(mydict, log_writer) return render_template('results.html', prediction=prediction) log_writer.log( file_object, '=================================================') except Exception as e: print('The Exception message is: ', e) return 'something is wrong' # return render_template('results.html') else: return render_template('index.html')
def predict(): if request.method == 'POST': try: # file_object = open("logs/GeneralLogs.txt", 'a+') # log_writer.log(file_object, 'Start getting data from UI') age = float(request.form['age']) workclass = (request.form['workclass']) fnlwgt = float(request.form['fnlwgt']) education = (request.form['education']) education_num = float(request.form['education_num']) marital_status = (request.form['marital_status']) occupation = (request.form['occupation']) relationship = (request.form['relationship']) race = (request.form['race']) sex = (request.form['sex']) capital_gain = float(request.form['capital_gain']) capital_loss = float(request.form['capital_loss']) hours_per_week = float(request.form['hours_per_week']) native_country = (request.form['native_country']) # log_writer.log(file_object, 'Complete getting data from UI') mydict = {'age':age, 'workclass':workclass, 'fnlwgt':fnlwgt, 'education':education, 'education_num':education_num, 'marital_status':marital_status, 'occupation':occupation, 'relationship':relationship, 'race':race, 'sex':sex, 'capital_gain':capital_gain, 'capital_loss':capital_loss, 'hours_per_week':hours_per_week, 'native_country':native_country} # log_writer.log(file_object, 'Passing mydict to prediction.predict_data') prediction = predict_data(mydict) if prediction == 0: result = '<=50K' else: result = '>50K' return render_template('results.html', result=result) except Exception as e: print('The Exception message is: ', e) return 'something is wrong' # return render_template('results.html') else: return render_template('index.html')
'lat_var_skew', 'lon_var_skew', 'velocity_var_skew', 'velocity_var_covar', 'angle_var_mean', 'computed_angle_var_mean', 'computed_angle_var_skew', 'computed_angle_var_covar', #'angle_var_velocity_cov' ], axis=1, inplace=True) #构建测试集数据 #test_x,test_id = predict_data(est_kbin) test_x, test_id = predict_data() test_x.drop( [ 'lat_var_skew', 'lon_var_skew', 'velocity_var_skew', 'velocity_var_covar', 'angle_var_mean', 'computed_angle_var_mean', 'computed_angle_var_skew', 'computed_angle_var_covar', #'angle_var_velocity_cov' ], axis=1, inplace=True)
def run(self): try: #Connections if (useServer): self.connectToServer() print("Connected to test server") data = Data(self.sock) self.connectToArduino() #Handshaking with Arduino while (self.isHandshakeDone == False): self.serial_port.write('H') print("H sent") time.sleep(0.5) reply = self.serial_port.read(1) if (reply == 'B'): self.isHandshakeDone = True self.serial_port.write('F') print("Connected to Arduino") self.serial_port.readline() time.sleep(1) else: time.sleep(0.5) #Receive data from Arduino(periodically) and save to CSV send_flag = True while (collect_test_data): if (send_flag == True): self.serial_port.write('R') send_flag = False time.sleep(0.1) if (self.serial_port.inWaiting() > 0): arduino_data = readlineCR(self.serial_port) print(arduino_data) cs.save_data(arduino_data) send_flag = True cs.calc_checksum(arduino_data) i = 0 #Read and Predict the results while (testing_samples): if (send_flag == True): self.serial_port.write('R') send_flag = False time.sleep(0.1) if (self.serial_port.inWaiting() > 0): send_flag = True arduino_data = readlineCR(self.serial_port) #print(arduino_data) if (cs.calc_checksum(arduino_data) ): #if checksum is correct last_comma_index = arduino_data.rfind(",") arduino_data = arduino_data[: last_comma_index] #strip out the checksum arduino_data_list = arduino_data.split( "\n")[:-1] #split into 4 rows of samples for item in arduino_data_list: data.sample_queue.appendleft(item) self.retrieve_data(data, arduino_data) if (len(data.sample_queue) == 20): if (i % 2 == 1): self.result_queue.appendleft( predict.predict_data(data.sample_queue)) #print(list(self.result_queue)) data.sample_queue.clear() if (len(list(self.result_queue)) == 3 and len(set(list(self.result_queue))) == 1): predictedMove = (list(self.result_queue)[0]) self.result_queue.clear() if (predictedMove != "Idle"): data.sendData(predictedMove) i += 1 #test communication with server. ## while(True): ## name = raw_input("What is the dance move?") ## data.voltage = 12 ## data.current = 1 ## data.power = 100 ## data.cumpower ## data.sendData(name) except KeyboardInterrupt: sys.exit(1)
def predict(model_id): token = DataServer.extract_token(request) # check if user sent token if token is None: return jsonify({"detail": "Authentication credentials were not provided."}), 401 # send a request to the data server to authenticate the user via the token user = DataServer.auth_user(token) # none is returned if the token is invalid if user is None: return jsonify({"detail": "Invalid token."}), 401 user_id = user['user_id'] # get data information from the data server user_model = DataServer.get_user_model(user_id=user_id, model_id=model_id) if user_model is None: return jsonify({"detail": "The model specified was not found."}), 404 # check if the user owns the data to be uploaded if user_model['owner'] != user['user_id']: return jsonify({"detail": "You do not have permission to perform this action."}), 403 # check if the file for the model exists user_model_dir = os.path.join(MODEL_DIR, f"{user_id}") if not os.path.exists(user_model_dir): return jsonify({'detail': 'Model was never trained before.'}), 404 if not check_file(get_model_dir(user_id=user_id, model_id=model_id)): return jsonify({'detail': 'Model was never trained before.'}), 404 if not check_file(get_model_info_dir(user_id=user_id, model_id=model_id)): return jsonify({'detail': 'Model was never trained before.'}), 404 # create directories for the user if it doesn't exist user_prediction_dir = os.path.join(PREDICT_DIR, f"{user['user_id']}") if not os.path.exists(user_prediction_dir): os.makedirs(user_prediction_dir) model_info_dir = get_model_info_dir(user_id=user_id, model_id=model_id) model_info_file = open(model_info_dir, mode='r') model_info_text = model_info_file.read() model_info_file.close() model_info = json.loads(model_info_text) dataset_type = model_info['dataset_info']['type'] dataset_column_info = model_info['dataset_info']['columns'] # file is sent in request body, open a temp file and start writing to it temp_file_dir = os.path.join(PREDICT_DIR, f"{user['user_id']}", f'temp.{dataset_type}') with open(temp_file_dir, "wb") as file: file.write(request.data) # try to open the file and get some information about it before overwriting the existing file try: prediction_dataset_column_info = get_columns(temp_file_dir, dataset_type) except (UnicodeDecodeError, EmptyDataError): os.remove(temp_file_dir) return jsonify({'detail': f"Invalid {dataset_type} file."}), 415 # check columns of the prediction data compared to the training data invalid_columns = [] dataset_column_info_dict = {column['name']: column for column in dataset_column_info} for column in prediction_dataset_column_info: if column['name'] in dataset_column_info_dict: if column['type'] != dataset_column_info_dict[column['name']]['type']: invalid_columns.append({ 'detail': 'Unexpected type', 'name': column['name'], 'expected_type': dataset_column_info_dict[column['name']]['type'] }) else: if column['name'] not in model_info['to_drop'] and column['name'] != model_info['y_col']: invalid_columns.append({ 'detail': 'Missing column that was not dropped during training', 'name': column['name'], }) if len(invalid_columns) != 0: return jsonify({'detail': 'The columns of the prediction file do not match the columns used for training', 'invalid_columns': invalid_columns}), 400 to_drop = [col_name for col_name in model_info['to_drop'] if col_name in dataset_column_info_dict] from predict import predict as predict_data predict_data(user_id=user_id, model_id=model_id, to_drop=to_drop, predict_data_dir=temp_file_dir, model_info=model_info,) # return status code CREATED return jsonify({ 'detail': 'Prediction done. You can access the data from the given endpoint.', 'endpoint': f'/download/p/{model_id}' }), 201
from predict import predict_data from train import train_model if __name__ == "__main__": # mode in which programme runs(training a new model or using an existing one) # can be set to 'predict' to predict or to 'train' to train mode = 'predict' # checking 'train' mode if mode == 'train': train_model('models/sunday') # checking 'predict' mode elif mode == 'predict': predict_data('models/sunday', 'data/comments_klsh.csv', 'output/klsh_predicted.csv') # mode error else: print("Error: unknown mode") exit(1)