Python绘制列中具有重复值的放置线

时间:2018-12-10 16:53:54

标签: python loops matplotlib plot

我有一个像这样的.txt文件

episode:2840  score:6.0  memory length:700345  epsilon:0.5576491831247304   steps:359    recent reward:5.95
episode:2841  score:4.0  memory length:700629  epsilon:0.5574965123606621   steps:284    recent reward:5.96
episode:2842  score:5.0  memory length:700911  epsilon:0.5573449596383373   steps:282    recent reward:5.93
Max lifes :5
episode:2800  score:7.0  memory length:687331  epsilon:0.5646918805492361   steps:268    recent reward:6.04
episode:2801  score:5.0  memory length:687678  epsilon:0.5645029051613243   steps:347    recent reward:6.03
episode:2802  score:6.0  memory length:688027  epsilon:0.564312906709082   steps:349    recent reward:6.03

episode:0开始并进行更新,但是我时不时地看到这条Max lifes :5行,然后重复某些情节。我想删除我看到的情节再次出现的线条。我怎样才能做到这一点?问题是只有episode号是重复的,其余的列却没有。因此,对于episode:2800,第二次出现时,我们得到了不同的分数,ε等。这是我到目前为止的内容:

import matplotlib.pyplot as plt
import os
import numpy as np
import pandas as pd
from itertools import islice


def next_1000_lines(file_opened, N):
    return [x.strip() for x in islice(file_opened, N)]



with open("ouput_10000.txt") as f:
    lines = f.readlines()

    x, y = [], []

    for line in lines:

        if line[:3] != "Max":
            x.append(line.split()[0])
            y.append(line.split()[7])
            x = next_1000_lines(x, 1000)
            y = next_1000_lines(y, 1000)

f.close()

print(x, y)

plt.plot(x,y)
plt.show()

我想为每1000个reward创建一个episodes的图,这个next_1000_lines函数正确吗?先感谢您。

2 个答案:

答案 0 :(得分:1)

看来,您的if let attachment = notification.request.content.attachments.first { if attachment.url.startAccessingSecurityScopedResource() { if let data = NSData(contentsOfFile: attachment.url.path) as? Data { self.uiImage.image = UIImage(data: data) // <- that's the image view attachment.url.stopAccessingSecurityScopedResource() } } } 方法只是对next_1000_linesstrip的内容中的x做多达1000个元素,并且您在每一行都调用它。为什么不随便走带子,每行一次:

y

实际上没有运行此代码,因此可能有错误。

答案 1 :(得分:0)

我将问题分为几部分。除非情节编号小于之前的情节,否则每个情节/奖励数据点都会保存到列表中。现在,您应该能够在绘制列表之前检查episodesrewards列表。然后为元素0..999、1000..1999等创建新图。

import matplotlib.pyplot as plt

# Read lines from file
filename = "ouput_10000.txt"
with open(filename) as f:
    lines = f.readlines()

# Extract episode/reward data points
episodes = []
rewards = []
for line in lines:
    if not line.startswith("episode"):
        # Not a line with episode/reward data
        continue
    columns = line.split()
    try:
        episode = int(columns[0].split(":")[1])
        reward = float(columns[7].split(":")[1])
    except:
        # Not a number
        continue
    if episode in episodes:
        # We don't want duplicate episodes
        continue
    episodes.append(episode)
    rewards.append(reward)

# Plot data
N = 1000
for i in range(int((len(episodes)-1)/N)+1):
    plt.plot(episodes[i*N:i*N+N], rewards[i*N:i*N+N])
    plt.savefig("plot{}.png".format(i))
    plt.close()