C/C++教程

先输入,后 np.column_stack的一个奇怪错误

本文主要是介绍先输入,后 np.column_stack的一个奇怪错误,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我前面尝试使用以下代码创建路网上的乘客,但是在使用时候发现,部分乘客不在对应的线上(‘O_wgs_lng’, ‘O_wgs_lat’ 不在线段’起点路段节点1编号’, ‘起点路段节点2编号’),尝试多次未找到原因

def generate_customer_data(**kwargs):
    p_num = kwargs['p_num']
    run_times = kwargs['run_times']
    columns_cus_name = np.zeros(shape=(p_num, 1), dtype=object)
    columns_O_wgs_lng = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_O_wgs_lat = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_O_point_name1 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_O_point_name2 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_D_wgs_lng = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_D_wgs_lat = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_D_point_name1 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_D_point_name2 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_appear_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_state = np.zeros(shape=(p_num, 1), dtype=object)
    columns_car_name = np.zeros(shape=(p_num, 1), dtype=object)
    columns_get_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_on_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_off_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    adjacent_data = pd.read_csv(r'..\Map\adjacent_inf.csv')
    for i in range(p_num):
        order_time = random.randint(1, run_times + 1)
        road_row_1 = random.randint(0, len(adjacent_data) - 1)
        O_point_name1 = adjacent_data.loc[road_row_1, 'fir_point_name']
        O_point_name2 = adjacent_data.loc[road_row_1, 'lst_point_name']
        A1 = [adjacent_data.loc[road_row_1, 'start_x'], adjacent_data.loc[road_row_1, 'start_y']]
        B1 = [adjacent_data.loc[road_row_1, 'end_x'], adjacent_data.loc[road_row_1, 'end_y']]
        P1 = sub_way.Get_Mid_Point(fir_point=A1, lst_point=B1)
        road_row_2 = random.randint(0, len(adjacent_data) - 1)
        D_point_name1 = adjacent_data.loc[road_row_2, 'fir_point_name']
        D_point_name2 = adjacent_data.loc[road_row_2, 'lst_point_name']
        A2 = [adjacent_data.loc[road_row_2, 'start_x'], adjacent_data.loc[road_row_2, 'start_y']]
        B2 = [adjacent_data.loc[road_row_2, 'end_x'], adjacent_data.loc[road_row_2, 'end_y']]
        P2 = sub_way.Get_Mid_Point(fir_point=A2, lst_point=B2)
        columns_cus_name[i, 0] = 'cus_' + str(i + 1)
        columns_O_wgs_lng[i, 0] = P1[0]
        columns_O_wgs_lat[i, 0] = P1[1]
        columns_O_point_name1[i, 0] = O_point_name1
        columns_O_point_name2[i, 0] = O_point_name2
        columns_D_wgs_lng[i, 0] = P2[0]
        columns_D_wgs_lat[i, 0] = P2[1]
        columns_D_point_name1[i, 0] = D_point_name1
        columns_D_point_name2[i, 0] = D_point_name2
        columns_appear_time[i, 0] = order_time
        columns_state[i, 0] = '未出现'
    customer_data = np.column_stack((
        columns_cus_name,
        columns_O_wgs_lng, columns_O_wgs_lat, columns_O_point_name1, columns_O_point_name2,
        columns_D_wgs_lng, columns_D_wgs_lat, columns_D_point_name1, columns_D_point_name2,
        columns_appear_time, columns_state, columns_car_name,
        columns_get_car_time, columns_on_car_time, columns_off_car_time
    ))
    # np.save(r'..\Data\config\customer_data.npy', customer_data)
    fi_customer_data = pd.DataFrame(customer_data)
    fi_customer_data.columns = [
        '乘客编号',
        'O_wgs_lng', 'O_wgs_lat', '起点路段节点1编号', '起点路段节点2编号',
        'D_wgs_lng', 'D_wgs_lat', '终点路段节点1编号', '终点路段节点2编号',
        '出现时间', '状态', '搭乘车的车牌号',
        '打车时间', '搭车时间', '下车时间'
    ]
    fi_customer_data.to_csv(r'..\Data\config\customer_data.csv', index=False, encoding='utf_8_sig')

而后我尝试先拼接,再输入数据,错误就解决了

def generate_customer_data(**kwargs):
    f = open(r'..\Data\config\Graph_layer.pkl', 'rb')
    G = pickle.load(f)
    p_num = kwargs['p_num']
    run_times = kwargs['run_times']
    columns_cus_name = np.zeros(shape=(p_num, 1), dtype=object)
    columns_O_wgs_lng = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_O_wgs_lat = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_O_point_name1 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_O_point_name2 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_D_wgs_lng = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_D_wgs_lat = np.zeros(shape=(p_num, 1), dtype=np.float32)
    columns_D_point_name1 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_D_point_name2 = np.zeros(shape=(p_num, 1), dtype=object)
    columns_appear_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_state = np.zeros(shape=(p_num, 1), dtype=object)
    columns_car_name = np.zeros(shape=(p_num, 1), dtype=object)
    columns_get_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_on_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    columns_off_car_time = np.zeros(shape=(p_num, 1), dtype=int)
    adjacent_data = pd.read_csv(r'..\Map\adjacent_inf.csv')
    customer_data = np.column_stack((
        columns_cus_name,
        columns_O_wgs_lng, columns_O_wgs_lat, columns_O_point_name1, columns_O_point_name2,
        columns_D_wgs_lng, columns_D_wgs_lat, columns_D_point_name1, columns_D_point_name2,
        columns_appear_time, columns_state, columns_car_name,
        columns_get_car_time, columns_on_car_time, columns_off_car_time
    ))
    # np.save(r'..\Data\config\customer_data.npy', customer_data)
    fi_customer_data = pd.DataFrame(customer_data)
    fi_customer_data.columns = [
        '乘客编号',
        'O_wgs_lng', 'O_wgs_lat', '起点路段节点1编号', '起点路段节点2编号',
        'D_wgs_lng', 'D_wgs_lat', '终点路段节点1编号', '终点路段节点2编号',
        '出现时间', '状态', '搭乘车的车牌号',
        '打车时间', '搭车时间', '下车时间'
    ]
    for customer_index in fi_customer_data.index:
        order_time = random.randint(1, run_times + 1)
        road_row_1 = random.randint(0, len(adjacent_data) - 1)
        O_point_name1 = adjacent_data.loc[road_row_1, 'fir_point_name']
        O_point_name2 = adjacent_data.loc[road_row_1, 'lst_point_name']
        A1 = [adjacent_data.loc[road_row_1, 'start_x'], adjacent_data.loc[road_row_1, 'start_y']]
        B1 = [adjacent_data.loc[road_row_1, 'end_x'], adjacent_data.loc[road_row_1, 'end_y']]
        P1 = sub_way.Get_Mid_Point(fir_point=A1, lst_point=B1)
        sub_way.Get_Pro(fir_point=A1, lst_point=B1, mid_point=P1, accuracy=7)
        road_row_2 = random.randint(0, len(adjacent_data) - 1)
        D_point_name1 = adjacent_data.loc[road_row_2, 'fir_point_name']
        D_point_name2 = adjacent_data.loc[road_row_2, 'lst_point_name']
        A2 = [adjacent_data.loc[road_row_2, 'start_x'], adjacent_data.loc[road_row_2, 'start_y']]
        B2 = [adjacent_data.loc[road_row_2, 'end_x'], adjacent_data.loc[road_row_2, 'end_y']]
        P2 = sub_way.Get_Mid_Point(fir_point=A2, lst_point=B2)
        sub_way.Get_Pro(fir_point=A2, lst_point=B2, mid_point=P2, accuracy=7)
        fi_customer_data.loc[customer_index, '乘客编号'] = 'cus_' + str(int(customer_index) + 1)
        fi_customer_data.loc[customer_index, ['O_wgs_lng', 'O_wgs_lat', '起点路段节点1编号', '起点路段节点2编号']] = \
            P1[0], P1[1], O_point_name1, O_point_name2
        fi_customer_data.loc[customer_index, ['D_wgs_lng', 'D_wgs_lat', '终点路段节点1编号', '终点路段节点2编号']] = \
            P2[0], P2[1], D_point_name1, D_point_name2
        fi_customer_data.loc[customer_index, ['出现时间', '状态']] = order_time, '未出现'

        # columns_cus_name[i, 0] = 'cus_' + str(i + 1)
        # columns_O_wgs_lng[i, 0] = P1[0]
        # columns_O_wgs_lat[i, 0] = P1[1]
        # columns_O_point_name1[i, 0] = O_point_name1
        # columns_O_point_name2[i, 0] = O_point_name2
        # columns_D_wgs_lng[i, 0] = P2[0]
        # columns_D_wgs_lat[i, 0] = P2[1]
        # columns_D_point_name1[i, 0] = D_point_name1
        # columns_D_point_name2[i, 0] = D_point_name2
        # columns_appear_time[i, 0] = order_time
        # columns_state[i, 0] = '未出现'
    fi_customer_data.to_csv(r'..\Data\config\customer_data.csv', index=False, encoding='utf_8_sig')

我只能说,阿巴巴

这篇关于先输入,后 np.column_stack的一个奇怪错误的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!