def menu2(companydata, companylist, comindex): utils.cls() print("=" * 26, "You selected", "=" * 26) print((companylist.iloc[int(comindex)])) loop = True while loop: print("\n" + "=" * 25 + "Stock Options" + "=" * 25) print("\nSelect options (1-6):") print("1. Summary") print("2. Graph") print("3. Historical Data") print("4. Future Prediction") print("5. Go back to previous menu") print("6. Exit") option = input( "\nYour option: ").lstrip() if option == '1': summary(companydata) elif option == '2': graph(companylist, comindex), elif option == '3': historicaldata(companydata), elif option == '4': prediction(companydata), elif option == '5': menu() elif option == '6': print("\nThanks for coming. Please visit again ...\n") loop = False exit() else: print("\nWrong option. Try again ...")
def main(): args = parseArgs() function = args.function input_file = args.input output_file = args.output true_file = args.trueout result_file = args.result method = args.method if function == ("Pred" or "Predict"): if input_file == "": print("Error - Read inputs") elif output_file == "": print("Error - Write output") else: prediction(input_file, output_file, method) elif function == ("Eval" or "Evaluate"): if input_file == "" or true_file == "": print("Error - Read inputs") elif output_file == "": print("Error - Write output") else: evaluate(input_file, output_file, true_file, result_file) else: print("Please choose 'Pred' function or 'Eval' function")
def determine_lambda(self, data, results): reg = regression.regression() pred = prediction.prediction() data = np.array_split(data, self.k) # Split array into k folds results = np.array_split(results, self.k) for index in range(10): # Iterate through all lambdas rmse = 0 for combo in self.combos: # Find avg RMSE from all combinations data_matrices_to_stack = [] results_matrices_to_stack = [] for fold_index in combo: data_matrices_to_stack.append(data[fold_index]) results_matrices_to_stack.append(results[fold_index]) training_data = np.vstack(tuple(data_matrices_to_stack)) results_data = np.vstack(tuple(results_matrices_to_stack)) weights = reg.closed_form_lin_regression(training_data, results_data, l=self.l) single_rmse = pred.linear_regression(training_data, results_data, weights) rmse += single_rmse # Calculate the avg RMSE for all combos and log with lambda value rmse /= len(self.combos) self.rmse[self.l] = rmse # Divide lambda by 2 then try combinations again self.l = float(self.l / 2) # Return lambda with smallest average RMSE return min(self.rmse, key=self.rmse.get)
def index(): if request.method == 'POST': data = request.form data = data.to_dict() data['NUM_UNITS'] = int(data['NUM_UNITS']) data['BLDG_NUM'] = int(data['BLDG_NUM']) data['ROOMS'] = int(data['ROOMS']) data['BEDRM'] = int(data['BEDRM']) data['BATHRM'] = int(data['BATHRM']) data['HF_BATHRM'] = int(data['HF_BATHRM']) data['KITCHENS'] = int(data['KITCHENS']) data['FIREPLACES'] = int(data['FIREPLACES']) data['LANDAREA'] = int(data['LANDAREA']) data['GBA'] = int(data['GBA']) data['YR_RMDL'] = int(data['YR_RMDL']) hasil = prediction(data) return render_template('result.html', hasil_pred=hasil) # return render_template('result.html',hasil_pred=hasil) # kita jalanin function predict # Render reulst.html return render_template('prediction.html', data_quadrant=sorted(quadrant), data_ward=sorted(ward), data_neighborhood=sorted(neighborhood), data_style=sorted(style), data_struct=sorted(structure), data_ext=sorted(extwall), data_int=sorted(intwall), data_roof=sorted(roof), data_qualified=sorted(qualified), data_condition=sorted(condition), data_grade=sorted(grade), data_ac=sorted(ac), data_heat=sorted(heat), data_property=sorted(property_type))
def model_predict_and_evaluate(self, dataset): """Train model with subset of the features and evalaute the performance. Args: - dataset: dataset with subset of temporal features Returns: - performance: performance with subset of temporal features """ # Build model pred_class = prediction(self.model_parameters['model_type'], self.model_parameters, self.task) # Train the model pred_class.fit(dataset) # Test the model test_y_hat = pred_class.predict(dataset) # Extract the labels _, _, test_y, _, _ = dataset.get_fold(fold=0, split='test') # Evaluate the performance temp_performance = Metrics([self.metric_name], self.metric_parameters).evaluate( test_y, test_y_hat) performance = np.mean(list(temp_performance.values())[0]) return performance
def prediccion(estacion, data, dirData, dirTrain, contaminant): """ function that sends the data to the neural network for the prediction of the pollutant :param estacion: name the station :type estacion: String :param data: information for the prediction :type data : list float32 :param dirData: address of the files with training information :type dirData: String :param dirTrain: address of the training files of the neural network :type dirTrain: String :return: prdiction values :type return : float32 """ temp = data.ix[0].values temp = temp[1:] dataPred = pre.normalize(temp, estacion, contaminant, dirData) dataPred = convert(dataPred) prediccion = pre.prediction(estacion, contaminant, [dataPred], dirTrain, dirData) print(prediccion) columnContaminant = findTable2(contaminant) prediccion1 = pre.desNorm(prediccion, estacion, contaminant, dirData, columnContaminant + '_') return prediccion1
def forecast_month(month, year, dirData, dirTotalCsv, dirTrain,estacion, contaminant): lastDay = calendar.monthrange(year,month)[1] fechaInicio = str(year) + '-' + numString(month) + '-01 00:00:00' fechaFinal = str(year) + '-' + numString(month) + '-'+ numString(lastDay) +' 23:00:00' #print(fechaInicio) #print(fechaFinal) data = fd.readData(fechaInicio, fechaFinal, [estacion], contaminant) data = separateDate(data) data = unionMeteorologia(data,dirTotalCsv) data = data.fillna(value=-1) #print(data) #sys.out frame_dates = data['fecha'].values data = filterData(data, dirData + estacion + "_" + contaminant + ".csv") data = data.fillna(value=-1) index = data.index.values arrayPred = [] for x in index: pred = data.ix[x].values valPred= pred[1:] valNorm = pre.normalize(valPred,estacion, contaminant, dirData) arrayPred.append(convert(valNorm)) result = pre.prediction(estacion, contaminant, arrayPred, dirTrain, dirData) nameCont = findTable2(contaminant) real = pre.desNorm(result, estacion,contaminant, dirData, nameCont + '_') for xs in range(len(frame_dates)): fecha = frame_dates[xs] ts = df.to_datetime(str(fecha)) fecha_string = ts.strftime('%Y-%m-%d %H:%M:%S') pronostico = real[xs] guardarPrediccion(estacion, fecha_string,[pronostico],contaminant,4)
def fall_or_not(data, frameNum): global FLAG_FALL global FLAG_WARNING global fall_cancel global fall_count global warning_timestamp if (frameNum % 15 == 0): # if FLAG_WARNING: # warning_status() if x == 0 or x + w == 320: pass else: res = pre.prediction(data) if (res[0] == 0 and res[1] > 0.80): FLAG_FALL = True fall_count += 1 # fall_cancel = 0.4 # if (FLAG_WARNING == False): # warning_timestamp = timestamp() # FLAG_WARNING = True # else: print('fall: {}'.format(res[1])) elif (res[0] == 1): print('lying: {}'.format(res[1])) elif (res[0] == 2 and res[1] > fall_cancel): FLAG_FALL = False fall_count = 0 # FLAG_WARNING = False # fall_cancel = 0.4 print('normal: {}'.format(res[1])) elif (res[0] == 3): pass
def fall_or_not(): global FLAG_FALL global VIDEO_GETTING if (frameNum % 5 == 0): if (x == 0) or (y == 0) or (x + w == 320): pass else: res = pre.prediction(data) if (res[0] == 0 and res[1] > 0.89): FLAG_FALL = True # print(window_name, 'fall: {}'.format(res[1])) cv2.putText(frame, 'Status: Fall', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) if not VIDEO_GETTING: VIDEO_GETTING = True try: path_to_file = video.get_video( list(frames), 'output') if upload.upload(path_to_file) == 200: print('upload success!') else: print('upload error!') except Exception as e: print(e) pass elif (res[0] == 2 and res[1] > fall_cancel): FLAG_FALL = False VIDEO_GETTING = False
def fall_or_not(data, frameNum): global FLAG_FALL global FLAG_WARNING global fall_cancel global warning_timestamp if (frameNum % 20 == 0): if FLAG_WARNING: warning_status() res = pre.prediction(data) if (res[0] == 0 and res[1] > 0.86): # FLAG_FALL = True if (FLAG_WARNING == False): warning_timestamp = timestamp() FLAG_WARNING = True # else: print('fall: {}'.format(res[1])) elif (res[0] == 1): print('lying: {}'.format(res[1])) elif (res[0] == 2 and res[1] > fall_cancel): FLAG_FALL = False FLAG_WARNING = False fall_cancel = 0.7 print('normal: {}'.format(res[1]))
def detect(self, image): """ Perform object detction on image and returns the result as a list of detections.prediction Args: image: numpy array with shape [height,width,#channels] in RGB order Returns: list of detected object in detections.prediciton format """ assert len(image.shape) == 3 and (image.shape[2] == 1 or image.shape[2] == 3) #image preprocessing image = image.astype(np.uint8) image = np.expand_dims(image, axis=0) #detections (boxes, scores, classes, num_detections) = self._sess.run( [self._boxes, self._scores, self._classes, self._num_detections], feed_dict={self._image_tensor: image}) #remove extra dimensions boxes = np.squeeze(boxes) scores = np.squeeze(scores) classes = np.squeeze(classes) #convert to detections.prediction result = [None] * int(num_detections) for i in range(num_detections): result[i] = prediction.prediction(boxes[i], classes[i], scores[i], self._getClassName(classes[i])) return result
def pronostico_normal(data,dirFestivos,dataMet,estacion,contaminant,dirData,dirTrain): data = data.reset_index(drop=True) data = separateDate(data) data = totalUnionData(data, dirFestivos) data = df.concat([data, dataMet], axis=1, join='inner') #data = data.merge(dataMet, how='left', on='fecha') data = filterData(data, dirData + estacion + "_" + contaminant + ".csv") data = data.fillna(value=-1) index = data.index.values arrayPred = [] for x in index: pred = data.ix[x].values valPred = pred[2:] valNorm = pre.normalize(valPred, estacion, contaminant, dirData) arrayPred.append(convert(valNorm)) result = pre.prediction(estacion, contaminant, arrayPred, dirTrain, dirData) columnContaminant = findTable2(contaminant) real = pre.desNorm(result, estacion, contaminant, dirData, columnContaminant+ '_') for xs in range(len(real)): fechaPronostico = data['fecha'].iloc[xs].values fechaPronostico = datetime.strptime(fechaPronostico[1], '%Y-%m-%d %H:%M:%S') fechaPronostico = fechaPronostico - timedelta(days=1) pronostico = real[xs] guardarPrediccion(estacion, fechaPronostico, [pronostico],contaminant,2) return 1
def get(self): query = self.get_argument("q", default="") res = { "input": query, "output": "" } if query != "": parser = argparse.ArgumentParser() parser.add_argument('--seed', type=int, default=None) parser.add_argument('--sample', type=int, default=1) parser.add_argument('--primetext', type=str, default='') parser.add_argument('--length', type=int, default=2000) parser.add_argument('--gpu', type=int, default=-1) args = parser.parse_args([]) args.primetext = query args.sample = 1 args.length = 500 output = prediction(args, vocab=self.VOCAB, model=self.MODEL) output = output[len(query):].lstrip("?\n").lstrip("?\n").lstrip("??\n").lstrip("??\n").lstrip().split("\n\n")[0] res["output"] = output self.set_header("Content-Type", "application/json; charset=utf-8") self.write( json.dumps(res, ensure_ascii=False))
def fall_or_not(): global FLAG_FALL global VIDEO_GETTING if (frameNum % 5 == 0): if True: res = pre.prediction(data) # print(res) if (res[0] == 0 and res[1] > 0.92): FLAG_FALL = True print('FLAG_FALL') cv2.putText(tempframe, 'Status: Fall', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) elif (res[0] == 2 and res[1] > fall_cancel): FLAG_FALL = False print('Nomal') cv2.putText(tempframe, 'Status: Nomal', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2) else: print('Nomal') cv2.putText(tempframe, 'Status: Nomal', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
def evaluate_fitness(self, actuators, history_array, models, prediction_horizon, history_length, initial_tick): predictions = prediction(models, actuators, history_array, prediction_horizon, history_length, initial_tick) self.predictions.append(predictions) # Minimize the distance from the center - this fitness function must be replaced according to the context # fitness = np.amax(np.abs(np.array(predictions[uwabami.Tick.center_index]))) # return fitness pass # placeholder
def result(): body = request.form # user input # 'male' 'age' 'currentSmoker' 'cigsPerDay' 'BPMeds' 'prevalentStroke' 'prevalentHyp' # 'diabetes' 'totChol' 'sysBP' 'diaBP' 'BMI' 'heartRate' user = body['user'] male = int(body['sex']) age = int(body['age']) smoker = int(body['smoker']) cigs = int(body['cigs']) BP = int(body['BP']) stroke = int(body['stroke']) hyp = int(body['hyp']) dia = int(body['dia']) chol = int(body['chol']) sysBP = int(body['sysBP']) diaBP = int(body['diaBP']) BMI = int(body['BMI']) heart = int(body['heart']) glucose = int(body['glucose']) X = [[ male, age, smoker, cigs, BP, stroke, hyp, dia, chol, sysBP, diaBP, BMI, heart ]] predict = prediction.prediction(X) if predict == 0: result = 'Congratulations {}! You are health, please continue to maintain your condition.'.format( user) elif predict == 1: result = 'We\'re sorry {}. You have a risk of coronary heart disease, please consult a doctor immediately.'.format( user) Z = { 'totChol': chol, 'sysBP': sysBP, 'diaBP': diaBP, 'BMI': BMI, 'heartRate': heart, 'glucose': glucose } graphic = graph.create_graphic(Z) data = dict(body) data['TenYearCHD'] = int(predict) query = """UPDATE users SET male = %(sex)s, age = %(age)s, currentSmoker = %(smoker)s, cigsPerDay = %(cigs)s, BPMeds = %(BP)s, prevalentStroke = %(stroke)s, prevalentHyp = %(hyp)s, diabetes = %(dia)s, totChol = %(chol)s, sysBP = %(sysBP)s, diaBP = %(diaBP)s, BMI = %(BMI)s, heartRate = %(heart)s, glucose = %(glucose)s, TenYearCHD = %(TenYearCHD)s WHERE username = %(user)s""" mycursor.execute(query, data) sqldb.commit() return render_template('result.html', result=result, graphic=graphic)
def run_code(train_image, train_label, n_dim, save_dir, weights, train=False, valid=False, importance=[1, 5]): print("Getting images for training/testing...") train_images, train_labels, test_images, test_labels = get_data( train_image, train_label, n_dim, augment=True) # reshaping / formating of labels train_labels = np.expand_dims(train_labels, axis=2) test_labels = np.expand_dims(test_labels, axis=2) sample_weights = np.zeros((train_labels.shape[0], train_labels.shape[1])) x = np.where(np.squeeze(train_labels) == 1) sample_weights[x] = importance[1] # weight of class 1 (i.e. roofs) sample_weights[np.where(np.squeeze(train_labels) == 0)] = importance[ 0] # weight of class 0 (i.e. non-roofs) if train == True: lrate = LearningRateScheduler(step_decay) callbacks_list = [lrate] train_model(train_images,train_labels,n_dim,valid=valid, numepochs = 50, \ sgd = SGD(lr=0.001, momentum=0.9, decay=1e-6, nesterov=True), \ metrics_list = ['accuracy'], callbacks_list = [lrate], sample_weights = sample_weights) weights = "Weights/weights.h5" test_pred = prediction(test_images, test_labels, weights, n_dim) test_pred = np.reshape( test_pred, (test_images.shape[0], test_images.shape[1], test_images.shape[2], 1)) #Convert confidences to 0 or 1, use 70% threshold test_pred = test_pred[:, :, :, 0] test_pred[np.where(test_pred >= 0.7)] = 1 test_pred[np.where(test_pred < 0.7)] = 0 for i in range(test_pred.shape[0]): pred = test_pred[i] pred = np.expand_dims(pred, axis=2) image_np = np.multiply( np.array(np.concatenate((pred, pred, pred), axis=2), dtype='uint8'), 255) image = Image.fromarray(image_np) image.save(save_dir + 'test' + str(i) + '.png') return test_pred
def index(): if request.method == 'POST': data = request.form data = data.to_dict() data['Bathrooms'] = int(data['Bathrooms']) data['Rooms'] = int(data['Rooms']) data['Size Num'] = int(data['Size Num']) hasil = prediction(data) return render_template('result.html', hasil_pred=hasil) # Kita jalanini Function Predict # Render Result.html return render_template('prediction.html', data_location=sorted(locations), prop=property_type)
def dataCorrelacion(contaminant, estacion, fechaInicio, fechaFin, dataMet, dirData, dirTrain, dirFestivos): print('COrrelacion') data_Corr = df.read_csv( '/media/storageBK/AirQualityForecast/Scripts/ContaminationForecast/Data/Correlacion_table.csv', index_col=0) corr_est = data_Corr[estacion].sort_values(ascending=False) estacion_corr = corr_est.index[1] data = fd.readData_corr(fechaInicio, fechaFin, [estacion_corr], contaminant) if data.empty: useClimatology(contaminant, estacion, fechaUltima, fechaFin, dataMet, dirData, dirTrain, dirFestivos) else: data = data.drop_duplicates(keep='first') data = data.reset_index(drop=True) index_values = data.columns.values[1:] for xs in index_values: data.rename(columns={ xs: xs.replace(estacion_corr.lower(), estacion.lower()) }, inplace=True) data = separateDate(data) data = totalUnionData(data, dirFestivos) data = df.concat([data, dataMet], axis=1, join='inner') print(data) #data = data.merge(dataMet, how='left', on='fecha') data = filterData(data, dirData + estacion + "_" + contaminant + ".csv") data = data.fillna(value=-1) index = data.index.values arrayPred = [] for x in index: pred = data.ix[x].values valPred = pred[2:] print(valPred) valNorm = pre.normalize(valPred, estacion, contaminant, dirData) arrayPred.append(convert(valNorm)) result = pre.prediction(estacion, contaminant, arrayPred, dirTrain, dirData) columnContaminant = findTable2(contaminant) real = pre.desNorm(result, estacion, contaminant, dirData, columnContaminant + '_') for xs in range(len(real)): fechaPronostico = data['fecha'].iloc[xs].values fechaPronostico = datetime.strptime(fechaPronostico[1], '%Y-%m-%d %H:%M:%S') pronostico = real[xs] guardarPrediccionRep(estacion, fechaPronostico, [pronostico], contaminant, 5)
def useClimatology(contaminant, estacion, fechaInicio, fechaFinal, dataMet, dirData, dirTrain, dirFestivos): """ function to make the forecast using climatologies :param contaminant: name of the pollutant :type contaminant: String :param estacion: name of the weather station :type estacion: String :param fechaInicio: range of data wit wich the vaues of tue query are extracted :type fechaInicio: datetime :param fechaFinal: range of data wit wich the vaues of tue query are extracted :type fechaFinal: datetime :param dataMet: dataframe with the climatological information :type dataMet: DataFrame """ data = fd.get_climatology(fechaInicio, fechaFinal, estacion) print(data) data = makeDates(fechaInicio, fechaFinal, data) #sys.out print(data) data = data.reset_index(drop=True) print(data) data = separateDate(data) data = totalUnionData(data, dirFestivos) data = df.concat([data, dataMet], axis=1, join='inner') #data = data.merge(dataMet, how='left', on='fecha') data = data.fillna(value=-1) data = filterData(data, dirData + estacion + "_" + contaminant + ".csv") data = data.fillna(value=-1) index = data.index.values arrayPred = [] for x in index: pred = data.ix[x].values valPred = pred[2:] valNorm = pre.normalize(valPred, estacion, contaminant, dirData) arrayPred.append(convert(valNorm)) result = pre.prediction(estacion, contaminant, arrayPred, dirTrain, dirData) columnContaminant = findTable2(contaminant) real = pre.desNorm(result, estacion, contaminant, dirData, columnContaminant + '_') fechaPronostico = fechaInicio for xs in real: print(fechaPronostico) fechaUpdate = fechaPronostico fechaUpdate = fechaUpdate - timedelta(days=1) guardarPrediccion(estacion, fechaUpdate, [xs], contaminant, 1) fechaPronostico = fechaPronostico + timedelta(hours=1) print('Climatologia:' + estacion)
def test_predict(): """Test predict.py for array length and data type.""" # Create a row of data and run prediction. home = 'Arsenal' away = 'Chelsea' stats = pd.read_sql_query("select * from stats;", engine) model = joblib.load('./model.pkl') result = prediction.prediction(home, away, stats, model) # Check type of output. assert isinstance(result, np.ndarray) # Check array length. assert len(result) == 3
def index_prediction(): if request.method == 'POST': data = request.form data = data.to_dict() data['humidity'] = int(data['humidity']) data['wind'] = int(data['wind']) data['rain'] = int(data['rain']) data['clouds'] = int(data['clouds']) data['temp'] = int(data['temp']) hasil = prediction(data) #hasilIDR = hasil*3564.67 return render_template('result.html', hasil_prediction=hasil) return render_template('prediction.html', data_location=locations, data_property=sorted(property_type))
def forecastDate2(station, dirData, dirrDataC, dirTrain, contaminant, columnContaminant, fechaInicio, fechaFin, dirTotalCsv): sta = station name = sta + '_' + contaminant tempData = baseContaminantes(fechaInicio, fechaFin, station, contaminant) if tempData.empty: dataBackup = back(dirData, contaminant) data = dataBackup data = data.fillna(value=-1) data = filterData(data, dirData + name + ".csv") data = data.fillna(value=-1) temp = data.ix[0].values temp = temp[1:] dataPred = pre.normalize(temp, sta, contaminant, dirData) dataPred = convert(dataPred) prediccion = pre.prediction(sta, contaminant, [dataPred], dirTrain, dirData) else: data = tempData.dropna(axis=1, how = 'all') data = data.fillna(value = -1) data = data.reset_index(drop = True) data = separateDate(data) data = unionData(data,dirTotalCsv) data = data.drop_duplicates(keep='first') data = filterData(data,dirData + name + '.csv') data = data.fillna(value = -1) dataTemp = data['fecha'] index = data.index.values arrayPred = [] for x in index: pred = data.ix[x].values valPred = pred[1:] valNorm = pre.normalize(valPred, sta, contaminant, dirData) arrayPred.append(convert(valNorm)) result = pre.prediction(sta,contaminant,arrayPred, dirTrain,dirData) real = desNorm(result, sta, contaminant, dirData, columnContaminant) dataPrediccion = real savePrediccion(station, dataPrediccion, contaminant, dataTemp)
def main(): image = np.load('test_images_1.npy') image_final_height = 48 image_final_width = 96 test_image = fun_img_preprocessing(image[0], image_final_height, image_final_width) logs_path = os.getcwd( ) + '/tensorflow_logs/opt=GDS,lr=1E-05,fc=2,drop=0.5,img=48x96,batch=100/' + 'train-900' pred = prediction(logs_path, test_image) print pred[0], pred[1]
def run_prediction(data_red, data_white): pred_obj = prediction(data_red, data_white) # Decision Tree Classifier processed_data_red = pred_obj.remove_column_spaces(data_red) processed_data_white = pred_obj.remove_column_spaces(data_red) print "Red Wine Decision Tree" pred_obj.decision_tree(processed_data_red) print "White Wine Decision Tree" pred_obj.decision_tree(processed_data_white) # Random Forest Classifier print "Red Wine Random Forest" pred_obj.random_forest(processed_data_red) print "White Wine Random Forest" pred_obj.random_forest(processed_data_white)
def prediction_html(): if request.method == 'POST': data = request.form data=data.to_dict() data['deposit_type']=int(data['deposit_type']) data['lead_time']=float(data['lead_time']) data['country']=int(data['country']) data['price']=float(data['price']) data['total_of_special_requests']=int(data['total_of_special_requests']) data['arrival_date_day_of_month']=int(data['arrival_date_day_of_month']) data['arrival_date_week_number']=int(data['arrival_date_week_number']) data['market_segment']=int(data['market_segment']) data['previous_cancellations']=int(data['previous_cancellations']) result = prediction(data) return render_template('result.html', hasil_prediksi=result) return render_template('prediction.html')
def model_train(self, dataset, model_parameters, task): """Train predictive model. Args: - dataset: training dataset - model_parameters: parameters for training model - task: classification or regression Returns: - pred_class: class of predictive model - pred_model: trained predictive model """ # Build model pred_class = prediction(model_parameters["model_type"], model_parameters, task) # Train the model pred_model = pred_class.fit(dataset) return pred_class, pred_model
def index_prediction(): if request.method == "POST": data = request.form data = data.to_dict() for i in data: if data[i] == 'Yes': data[i] = 1 elif data[i] == 'No': data[i] = 0 data['Price range'] = int(data['Price range']) data['Aggregate rating'] = float(data['Aggregate rating']) data['Votes'] = int(data['Votes']) data = pd.DataFrame(data, index=[0]) hasil = prediction(data) return render_template('result.html', hasil_prediction=round(hasil, 2)) return render_template('prediction.html', data_location=sorted(price_range), data_boolean=sorted(boolean))
def evaluate(model, data_loader, scorer_list, ts_writer, cuda, average='macro'): model.eval() for scorer_name, scorer in scorer_list.items(): valid_y, y_pred = prediction(model, data_loader, cuda, label=True) try: score = scorer(valid_y, y_pred) except ValueError: score = scorer(valid_y, y_pred, average=average) try: # change the sign according to your usage if score > model.config[scorer_name]: model.config[scorer_name] = score except KeyError: model.config[scorer_name] = score ts_writer["tensorboard_writer"].add_scalar(scorer_name, score, model.config["global_step"]) logging.info(f"{scorer_name}: {score}") model.train()
def predict(): skills = request.json['skill'] headers = [str(i) for i in skills.split(',')] payload = request.json['data'] values = [float(i) for i in payload.split(',')] df = data_reader() col_list = list(df.columns) df_new = pd.DataFrame(columns=col_list) X = pd.DataFrame([values], columns=headers, dtype=float) X_te = pd.concat([df_new, X], axis=0, sort=False) X_te.fillna(0, inplace=True) X_tr, X_te, X_tr_sc, X_te_sc = preprocess(df, X_te) my_model = knn(X_tr_sc, 5, 0.4) prediction_results = prediction(my_model, X_tr, X_te, X_tr_sc, X_te_sc) prediction_results = prediction_results.loc[:, (prediction_results != 0).any( axis=0)] return json.dumps(json.loads(prediction_results.to_json(orient='index')))
def recommend(self, bot, chat_id, telegram_user): telegram_id = telegram_user.telegram_id if not LearningPredictionForUser.objects.filter(telegram_user=telegram_user): bot.sendMessage( chat_id=chat_id, text="It seems like I see you for the first time," " please answer a few questions, so I'll be know more about you", ) prediction.get_test_courses(telegram_id) test_courses = LearningPredictionForUser.objects.get(telegram_user=telegram_user).get_list() if len(test_courses) > 0: course_id = MatrixEdxCoursesId.objects.filter(course_index=test_courses[0]).first().course_key course_key = CourseKey.from_string(course_id) reply_markup = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="I like it!", callback_data=json.dumps({"method": "learning", "kwargs": {}}) ) ], [ InlineKeyboardButton( text="Hmmm. I don't like at all!", callback_data=json.dumps({"method": "learning", "kwargs": {"is_positive": False}}), ) ], ] ) else: predicted_course_id = prediction.prediction(telegram_id) if predicted_course_id == -1: bot.sendMessage(chat_id=chat_id, text="It seems like you have enrolled to all courses we have for now") return predicted_course_key = ( MatrixEdxCoursesId.objects.filter(course_index=predicted_course_id).first().course_key ) bot.sendMessage(chat_id=chat_id, text="Now I'm going to recommend you some great courses") course_key = CourseKey.from_string(predicted_course_key) course_for_user = PredictionForUser.objects.get_or_create(telegram_user=telegram_user)[0] course_for_user.prediction_course = predicted_course_key course_for_user.save() reply_markup = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="I like it and I want to enroll", callback_data=json.dumps({"method": "enroll", "kwargs": {}}), ) ], [ InlineKeyboardButton( text="I like it but will eroll another time", callback_data=json.dumps({"method": "not_enroll", "kwargs": {}}), ) ], [ InlineKeyboardButton( text="What the shit is this (I don't like it)", callback_data=json.dumps({"method": "wrong_predict", "kwargs": {}}), ) ], ] ) course_description = CourseDetails.fetch_about_attribute(course_key, "overview") course_title = modulestore().get_course(course_key).display_name_with_default bot.sendMessage(chat_id=chat_id, text="*%s*" % course_title, parse_mode=telegram.ParseMode.MARKDOWN) bot.sendMessage(chat_id=chat_id, text=truncate_course_info(course_description), reply_markup=reply_markup)