在Python中绘制线性图和对数图。类似于R中的mfrow = c(2,1)

用户4933

我试图在python中绘制两个相邻的图,一个是实验的线性结果,另一个是对数变换。我们的目标是像par(mfrow=c(1,2))R中那样将地块彼此相邻放置

if __name__ == '__main__':
  c_1 = run_experiment(1.0, 2.0, 3.0, 0.1, 100000) # run_experiment(m1, m2, m3, eps, N):
  c_05 = run_experiment(1.0, 2.0, 3.0, 0.05, 100000)
  c_01 = run_experiment(1.0, 2.0, 3.0, 0.01, 100000)

  # log scale plot
  plt.plot(c_1, label='eps = 0.10')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.xscale('log')
  plt.title(label="Log Multi-Arm Bandit")
  plt.show()


  # linear plot
  plt.plot(c_1, label='eps = 0.10')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.show()

我尝试了很多方法,但似乎总是遇到错误。有人可以用代码实现它。我在Python中相对较新,并且大多数人都具有R方面的经验,但是任何帮助对我来说都是至关重要的。下面,我将提供一些我尝试的更改代码。

  # log scale plot
  fig, axes = plt.subplots(122)
  ax1, ax2 = axes[0], axes[1]
  ax1.plot(c_1, label='eps = 0.10')
  ax1.plot(c_05,label='eps = 0.05')
  ax1.plot(c_01,label='eps = 0.01')
  ax1.legend()
  ax1.xscale('log')
  #plt.title(label="Log Multi-Arm Bandit")
  #plt.show()

  # linear plot
  ax2.plot(c_1, label='eps = 0.10')
  ax2.plot(c_05,label='eps = 0.05')
  ax2.plot(c_01, label='eps = 0.01')
  ax2.legend()
  plt.show()

但是我收到一个错误。

下面是我用来进行实验的完整代码。这是强化学习中的多臂强盗问题。

# Premable
from __future__ import print_function, division
from builtins import range
import numpy as np
import matplotlib.pyplot as plt


class Bandit:
  def __init__(self, m):  # m is the true mean
    self.m = m
    self.mean = 0
    self.N = 0

  def pull(self): # simulated pulling bandits arm
    return np.random.randn() + self.m

  def update(self, x):
    self.N += 1
    # look at the derivation above of the mean
    self.mean = (1 - 1.0/self.N)*self.mean + 1.0/self.N*x  


def run_experiment(m1, m2, m3, eps, N):
  bandits = [Bandit(m1), Bandit(m2), Bandit(m3)]

  data = np.empty(N)

  for i in range(N): # Implement epsilon greedy shown above
    # epsilon greedy
    p = np.random.random()
    if p < eps:
      j = np.random.choice(3) # Explore
    else:
      j = np.argmax([b.mean for b in bandits]) # Exploit
    x = bandits[j].pull()  # Pull and update
    bandits[j].update(x)

    # Results for the plot
    data[i] = x  # Store the results in an array called data of size N
    # Calculate cumulative average
  cumulative_average = np.cumsum(data) / (np.arange(N) + 1)


  # plot moving average ctr
  plt.plot(cumulative_average) # plot cumulative average
  # Plot bars with each of the means so we can see where are 
  # cumulative averages relative to means
  plt.plot(np.ones(N)*m1) 
  plt.title('Slot Machine ')
  plt.plot(np.ones(N)*m2)
  plt.title('Slot Machine ')
  plt.plot(np.ones(N)*m3)
  plt.title('Slot Machine ')
  # We do this on a log scale so that you can see the 
  # fluctuations in earlier rounds more clearly
  plt.xscale('log') 
  plt.show()

  for b in bandits:
    print(b.mean)

  return cumulative_average

if __name__ == '__main__':
  c_1 = run_experiment(1.0, 2.0, 3.0, 0.1, 100000) # run_experiment(m1, m2, m3, eps, N):
  c_05 = run_experiment(1.0, 2.0, 3.0, 0.05, 100000)
  c_01 = run_experiment(1.0, 2.0, 3.0, 0.01, 100000)

  # log scale plot
  plt.plot(c_1, label='eps = 0.10')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.xscale('log')
  plt.title(label="Log Multi-Arm Bandit")
  plt.show()


  # linear plot
  plt.plot(c_1, label='eps = 0.10')
  plt.plot(c_05, label='eps = 0.05')
  plt.plot(c_01, label='eps = 0.01')
  plt.legend()
  plt.show()
威廉·米勒

正如@JohanC在评论中指出的那样,您混淆了plt.subplots()and的语法plt.subplot()线

fig, axes = plt.subplots(122)

在单个列中创建122个子图。这应该是

fig, axes = plt.subplots(1, 2)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 par(mfrow=c(2,1)) 时,图表在创建时不显示

在Numpy中添加(2,)和(2,1)数组

Ignite 2,1 中的数据库视图支持?

使用 LOOKUP(Google Sheets 中的 2,1/ 函数)

R:估算Carma(2,1)参数(使用yuima包)

为什么语句[1,2] <[2,1]在python中评估为True

两个限制 [1,2] 和 [2,1] 的循环不可接受的python

如何简化检查一对数字是(1,2)还是(2,1)?

计算并绘制[-1,5]区间中的理论正态分布N(2,1)

使用hist2d在matplotlib中创建对数线性图

ValueError:形状(20,1)和(2,1)不对齐:1(dim 1)!= 2(dim 0)

无论顺序如何,都需要从PIG中的关系中提取不同的元组,即(1,2)=(2,1)

如何在python中修复sklearn多元线性回归ValueError(样本数量不一致:[2,1])

如何在ggplot2或R中绘制雷达图

使用ggplot2为R中的离散变量创建小提琴图,飞船图或类似图

matMul中的错误:具有形状684,1和2,1且transposeA = false和transposeB = false的张量的内部形状(1)和(2)必须匹配

我正在尝试在python中绘制5 * 2图

par(mfrow = c(A,B))未绘制:使用软件包ggpubr和ggdensity()和ggqqplot()函数进行密度图和QQ图

在ggplot2中绘制饼图

在循环Gunplot中绘制1,2个多图

在2-1配置中绘制3张图

在 1*2 数组中绘制两个图

在python中在顶部轴上绘制3个图2并在底部轴上绘制一个图?

Networkx:绘制类似于 igraph R 中的子组件的子图

ggplot2和R中的简单饼图问题

ValueError:logits 和標籤必須具有相同的形狀,但得到了形狀 [2] 和 [2,1]

C ++-对象图的哈希值类似于boost :: serialization

Python中的方法/库,类似于C#中的NewtonSoft

在C ++中是否有类似于Python中的help()的函数