def SVM(X, features, LOS_pos, classOfLOS): y = [None] * len(X) testPercentage = 0 # plot.plot_column(np.array(X), icu_LOS, LOS, "icu_LOS", "LOS") # classify target classOfLOSNum = [0] * len(classOfLOS) for i in range(0, len(X)): for j in range(0, len(classOfLOS) - 1): if X[i][LOS_pos] >= classOfLOS[j] and X[i][LOS_pos] <= classOfLOS[ j + 1]: classOfLOSNum[j] += 1 X[i][LOS_pos] = j break if X[i][LOS_pos] >= classOfLOS[len(classOfLOS) - 1]: classOfLOSNum[len(classOfLOS) - 1] += 1 X[i][LOS_pos] = len(classOfLOS) - 1 print("classOfLOSNum: ", classOfLOSNum) # plot.plot_column(np.array(X), icu_LOS, LOS, "icu_LOS", "LOS classes") # select features for i in range(0, len(X)): arr = [0] * len(features) y[i] = X[i][LOS_pos] for j in range(0, len(features)): arr[j] = X[i][features[j]] X[i] = arr X_train = [] y_train = [] X_test = [] y_test = [] testNum = [] start = 0 end = 0 for j in range(0, len(classOfLOSNum)): end += classOfLOSNum[j] train_len = (end - start) * (1 - testPercentage) testNum.append(end - int(train_len) - start) for k in range(start, start + int(train_len)): X_train.append(X[k]) y_train.append(y[k]) for k in range(start + int(train_len), end): X_test.append(X[k]) y_test.append(y[k]) start = end print("===Test - SVM") clf = svm.SVC() clf.fit(X_train, y_train) prediction = clf.predict(X_test) # test test.testSVM(prediction, y_test, testNum) # plot plot.plot_map(y_test, prediction, "y_test_truelabel", "prediction", "SVM final result") return clf
def render_timepoint(data_type, plot_stations, timestamp, location, ext): """ creates an plot in "interface/data/temp/image" to be shown on the interface """ clear_dir(INTERFACE_IMAGE_FOLDER) save_to = os.path.join(INTERFACE_IMAGE_FOLDER, f"result.{ext}") plot_map( save_to = save_to, data_type = data_type, plot_stations = plot_stations, time = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc), location = location )
def cross_validation(X, y, kfold): kf = KFold(n_splits=kfold, shuffle=True) k = 0 for train_index, test_index in kf.split(X): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] print("k =", k) k += 1 clf = svm.SVR() clf.fit(X_train, y_train) prediction = clf.predict(X_test) # test test.testSVR(prediction, y_test) # plot plot.plot_map(y_test, prediction, "y_test", "prediction", "cross validation")
def SVR(X, features, LOS_pos): print("===Test - SVR") y = [None] * len(X) for i in range(0, len(X)): arr = [0] * len(features) y[i] = X[i][LOS_pos] for j in range(0, len(features)): arr[j] = X[i][features[j]] X[i] = arr X = np.array(X) y = np.array(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) cross_validation(X_train, y_train, 3) # final model clf = svm.SVR() clf.fit(X, y) prediction = clf.predict(X_test) plot.plot_map(y_test, prediction, "y_test", "prediction", "SVR final result") return clf
def render_timerange(data_type, plot_stations, timestamp1, timestamp2, location, ext): """ creates mutiple plots and fuses them to a video in "interface/data/temp/video" to be shown on the interface """ clear_dir(TEMP_IMAGE_FOLDER) for timestamp in range(timestamp1, timestamp2, 3600): plot_map( save_to = os.path.join(TEMP_IMAGE_FOLDER, str(timestamp).zfill(12) + ".png"), data_type = data_type, plot_stations = plot_stations, time = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc), location = location ) clear_dir(INTERFACE_VIDEO_FOLDER) image_to_video( [os.path.join(TEMP_IMAGE_FOLDER, file) for file in sorted(os.listdir(TEMP_IMAGE_FOLDER))], os.path.join(INTERFACE_VIDEO_FOLDER, f"result.{ext}") )
world_generation.terrains.generate_terrains(world) print("terrains", next(checkpoint)) world_generation.fixes.remove_artifacts_before_clustering(world) print("remove_artifacts_before_clustering", next(checkpoint)) world_generation.clustering.cluster_terrains(world) print("clustering", next(checkpoint)) world_generation.fixes.remove_artifacts_after_clustering(world) print("remove_artifacts_after_clustering", next(checkpoint)) # dump = exporter.export(world.terrain_blobs) # with open("/tmp/terrains.json", "w") as file: # file.write(dump) # print("export", next(checkpoint)) plot.plot_map(world, True) print("plot", next(checkpoint)) file_id = random.randint(0, 1000000) file_name = "generated_map_{}_{}".format(number_of_points, file_id) shutil.copyfile("map.png", "rendered_maps/" + file_name) print("Generated map with name generated_map_" + str(number_of_points) + "_" + str(file_id)) pickle.dump(world, open("dumps/" + file_name, "wb")) print("Finished in {} seconds".format(time.time() - start))
np.mean(env) for env_i, env in enumerate(zero_shot) if envs_to_avg[env_i] ]) * 100) + '%') plt.show() # Plot rate maps for all cells plot.plot_cells(p[env_to_plot], g[env_to_plot], environments[env_to_plot], n_f_ovc=(params['n_f_ovc'] if 'n_f_ovc' in params else 0), columns=25) # Plot accuracy separated by location plt.figure() ax = plt.subplot(1, 2, 1) plot.plot_map(environments[env_to_plot], np.array(to_acc[env_to_plot]), ax) ax.set_title('Accuracy to location') ax = plt.subplot(1, 2, 2) plot.plot_map(environments[env_to_plot], np.array(from_acc[env_to_plot]), ax) ax.set_title('Accuracy from location') # Plot occupation per location, then add walks on top ax = plot.plot_map(environments[env_to_plot], np.array(occupation[env_to_plot]) / sum(occupation[env_to_plot]) * environments[env_to_plot].n_locations, min_val=0, max_val=2, ax=None, shape='square', radius=1 / np.sqrt(environments[env_to_plot].n_locations))
def combined_SVM_SVR(X, features_class, features_regr, LOS_pos, classOfLOS, test_percentage): print("===Test - Combined SVM SVR") X_features_class = [None] * len(X) X_features_regr = [None] * len(X) y = [None] * len(X) for i in range(0, len(X)): arr_class = [0] * len(features_class) arr_regr = [0] * len(features_regr) y[i] = X[i][LOS_pos] for j in range(len(features_class)): arr_class[j] = X[i][features_class[j]] arr_regr[j] = X[i][features_regr[j]] X_features_class[i] = arr_class X_features_regr[i] = arr_regr X_features_class = np.array(X_features_class) X_features_regr = np.array(X_features_regr) y = np.array(y) y_class = np.copy(y) classOfLOSNum = [0] * len(classOfLOS) for i in range(len(y)): for j in range(len(classOfLOS) - 1): if y[i] >= classOfLOS[j] and y[i] <= classOfLOS[j + 1]: y_class[i] = j classOfLOSNum[j] += 1 break if y[i] >= classOfLOS[len(classOfLOS) - 1]: y_class[i] = len(classOfLOS) - 1 classOfLOSNum[len(classOfLOS) - 1] += 1 print("classOfLOSNum =", classOfLOSNum) X_train_class, X_test_class, X_train_regr, X_test_regr, y_train, y_test, y_train_class, y_test_class = train_test_split( X_features_class, X_features_regr, y, y_class, test_size=test_percentage) print("===SVM") clf = svm.SVC() clf.fit(X_train_class, y_train_class) class_prediction = clf.predict(X_test_class) test.test_simple(y_test_class, class_prediction, len(classOfLOS)) pickle.dump(clf, open("model/svm_model", 'wb')) print("===SVR") prediction = np.empty((0)) y_test_rearrange = np.empty((0)) for i in range(len(classOfLOS) - 1): print("SVR training set", i) indices = [] for j in range(len(y_train_class)): if y_train_class[j] == i: indices.append(j) X_train_sub = np.array(X_train_regr[indices]) y_train_sub = np.array(y_train[indices]) indices = [] for j in range(len(y_test_class)): if class_prediction[j] == i: indices.append(j) X_test_sub = np.array(X_test_regr[indices]) y_test_sub = np.array(y_test[indices]) clf = svm.SVR() clf.fit(X_train_sub, y_train_sub) fname = "model/svr_model_" + str(i) pickle.dump(clf, open(fname, 'wb')) cross_validation(X_train_sub, y_train_sub, 3) regression_pred = clf.predict(X_test_sub) prediction = np.append(prediction, regression_pred) y_test_rearrange = np.append(y_test_rearrange, y_test_sub) test.testSVR(regression_pred, y_test_sub) plot.plot_map(y_test_sub, regression_pred, "y_test", "prediction", "regression subset") print("Overall:") indices = [] for i in range(len(y_test_rearrange)): if y_test_rearrange[i] <= 30: indices.append(i) prediction = np.array(prediction[indices]) y_test_rearrange = np.array(y_test_rearrange[indices]) test.testSVR(prediction, y_test_rearrange) plot.plot_map(y_test_rearrange, prediction, "y_test", "prediction", "final regression result") interval = 5 for i in range(classOfLOS[0], classOfLOS[len(classOfLOS) - 1], interval): indices = [] for j in range(len(prediction)): if y_test_rearrange[j] >= i and y_test_rearrange[j] <= i + interval: indices.append(j) y_test_rearrange_sub = np.array(y_test_rearrange[indices]) prediction_sub = np.array(prediction[indices]) print("class", i, "-", i + interval) test.testSVR(prediction_sub, y_test_rearrange_sub)