2021-05-21

这段代码展示了如何使用Python的matplotlib库进行数据可视化,具体是模拟随机漫步过程。通过RandomWalk类生成随机漫步点,然后在图表上展示这些点,并突出起点和终点。用户可以选择是否继续生成新的随机漫步路径。
摘要由CSDN通过智能技术生成

数据可视化

15.3.4模拟随机漫步-15.3.5设置随机漫步样式
rw_visual.py:

import matplotlib.pyplot as plt

from random_walk import RandomWalk

#只要程序处于获得活动状态,就不断地模拟随机漫步
while True:
    #创建一个RandomWalk实例
    rw = RandomWalk(50_000)#创建实例并存储到rw
    rw.fill_walk()#调用

    #将所有的点都绘制出来
    plt.style.use('classic')
    fig,ax = plt.subplots(figsize=(15,9))#figsize:图的尺寸
    point_numbers = range(rw.num_points)#range生成一个列表,包含的数与漫步包含的点的数量
    ax.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap = plt.cm.Blues,#camp:颜色映射
               edgecolors='none',s=1)#将随机包含的x值和y值传递给scatter(),并选择合适的尺寸,edgecolors:删除每个点轮廓
    #突出起点与终点
    ax.scatter(0,0,c='green',edgecolors='none',s=100)
    ax.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none')#c:颜色,edgecolors:删除每个点轮廓,s:大小

    #隐藏坐标轴。
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    plt.show()

    keep_running = input("Make anther walk? (y/n)")
    if keep_running == 'n':
        break

random_walk.py

from random import choice

class RandomWalk:
    '一个生成随机漫步德属性'

    def __init__(self,num_points=5000):
        '初始化随机漫步的属性'
        self.num_points = num_points

        #所有随机漫步都开始于(0,0)
        self.x_values = [0]
        self.y_values = [0]

    def fill_walk(self):
        '计算随机漫步包含的所有点'
        '一个决定向左或向右,向上或向下走,指定方向走多远的类'
        #不断漫步,直到列表达到指定的长度
        while len(self.x_values) < self.num_points:#循环到所需要的点数
            '决定前进方向以及沿这个方向前进的距离。'
            x_direction = choice([1,-1])#方向:1向右,-1向左

            x_directance = choice([0,1,2,3,4])#走的距离随机0——4
            x_setp = x_direction * x_directance#确定x轴和y轴移动的距离,x_setp为正向右,,为负向左,为0垂直移动

            y_direction = choice([1,-1])
            y_distance = choice([0,1,2,3,4])
            y_setp = y_direction *y_distance#y_setp为正向上,,为负向下,为0水平移动

            #拒绝原地踏步
            if x_setp == 0 and y_setp == 0:
                continue

            #计算下一个点的x值和y值
            x = self.x_values[-1] + x_setp
            y =self.y_values[-1] + y_setp

            self.x_values.append(x)
            self.y_values.append(y)

结果:
在这里插入图片描述

2021年5月21日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值