how to create the dotted line in the below NumPy array
import NumPy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap x=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]]) def make_figure(inp_arr: np.array, outputname): # create graphical output for visual check cmap = ListedColormap([ 'r','b','g']) plt.imshow(inp_arr, cmap=cmap) plt.grid(color='b', linestyle=':', linewidth=0.55) plt.savefig(input_folder + 'pics_' + str(outputname) + '.png', format='png', dpi=350) # plt.show() #plt.clf()
bh=make_figure(b,’gh’)
requirement: how to convert element 1 into 0 with the step of two expected outputs is like I tried with a brute force algorithm, but I am not able to find the solution
output array looks like
y=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]])
for visual representation like making a dotted line
Advertisement
Answer
Here’s one way to find the minimum weight full path, then take the first point, skip two points, and repeat until the end of the path.
import numpy as np from sklearn.neighbors import radius_neighbors_graph from scipy import sparse import networkx as nx x = np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] ] ) x_nonzeros = x.nonzero() num_points = len(x_nonzeros[0]) x_coords = [[x_nonzeros[0][k], x_nonzeros[1][k]] for k in range(num_points)] neighbors = radius_neighbors_graph(x_coords, radius=1.5, mode="distance") G = nx.Graph(neighbors) full_paths = [ {"path": path, "weight": nx.classes.path_weight(G, path, weight="weight")} for path in nx.all_simple_paths(G, 0, 40) if len(path)==num_points ] full_paths.sort(key=lambda rec: rec["weight"]) the_path = full_paths[0]["path"] y_coords = [x_coords[coord] for coord in the_path[0::3]] y = sparse.coo_array(([1]*len(y_coords),np.array(y_coords).T)).toarray() print(y) # [[1 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 1 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 1 0 0 1 0 0 1] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [1 0 0 1 0 0 1 0 0 1 0 0 1] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 0 0 0 0 0] # [0 0 0 1 0 0 1 0 0 0 0 0 0] # [0 0 0 0 0 0 0 0 1 0 0 1 0]]