ValueError: x 和 y 必须具有相同的第一个维度,但形状分别为 (5,) 和 (1,)。

huangapple go评论122阅读模式
英文:

ValueError: x and y must have same first dimension, but have shapes (5,) and (1,)

问题

  1. import time
  2. import random
  3. import matplotlib.pyplot as plt
  4. def brute_force(values, weights, capacity):
  5. num_items = len(values)
  6. max_value = 0
  7. subset = []
  8. for i in range(2**num_items):
  9. current_value = 0
  10. current_weight = 0
  11. current_subset = []
  12. for j in range(num_items):
  13. if(i & (1 << j)) != 0:
  14. current_value += values[j]
  15. current_weight += weights[j]
  16. current_subset.append(j)
  17. if current_weight <= capacity and current_value > max_value:
  18. max_value = current_value
  19. subset = current_subset
  20. return max_value, subset
  21. input_sizes = [10, 20, 30, 40, 50]
  22. running_times = []
  23. for size in input_sizes:
  24. values = [random.randint(1, 100) for _ in range(size)]
  25. weights = [random.randint(1, 100) for _ in range(size)]
  26. capacity = sum(weights) // 2
  27. start_time = time.time()
  28. max_value, subset = brute_force(values, weights, capacity)
  29. end_time = time.time()
  30. running_time = end_time - start_time
  31. running_times.append(running_time)
  32. print(f"Input size: {size}")
  33. print(f"Chosen subset: {subset}")
  34. print(f"Chosen weight: {sum(weights[i] for i in subset)}")
  35. print(f"Running time: {running_time:.6f} seconds")
  36. plt.plot(input_sizes, running_times, marker='o')
  37. plt.xlabel('Input size')
  38. plt.ylabel('Running time (s)')
  39. plt.title('Brute force Knapsack')
  40. plt.show()
英文:

I'm creating a brute force approach for the knapsack problem, unfortunately when trying to graph the results I get this error: ValueError: x and y must have same first dimension, but have shapes (5,) and (1,)

  1. import time
  2. import random
  3. import matplotlib.pyplot as plt
  4. def brute_force(values, weights, capacity):
  5. num_items = len(values)
  6. max_value = 0
  7. subset = []
  8. for i in range(2**num_items):
  9. current_value = 0
  10. current_weight = 0
  11. current_subset = []
  12. for j in range(num_items):
  13. if(i &amp; (1 &lt;&lt; j)) != 0:
  14. current_value += values[j]
  15. current_weight += weights[j]
  16. current_subset.append(j)
  17. if current_weight &lt;= capacity and current_value &gt; max_value:
  18. max_value = current_value
  19. subset = current_subset
  20. return max_value, subset
  21. input_sizes = [10, 20, 30, 40, 50]
  22. running_times = []
  23. for size in input_sizes:
  24. values = [random.randint(1,100) for _ in range(size)]
  25. weights = [random.randint(1, 100) for _ in range(size)]
  26. capacity = sum(weights) // 2
  27. start_time = time.time()
  28. max_value, subset = brute_force(values, weights, capacity)
  29. end_time = time.time()
  30. running_time = end_time - start_time
  31. running_times.append(running_time)
  32. print(f&quot;Input size: {size}&quot;)
  33. print(f&quot;Chosen subset: {subset}&quot;)
  34. print(f&quot;Chosen weight: {sum(weights[i] for i in subset)}&quot;)
  35. print(f&quot;Running time: {running_time: .6f} seconds&quot;)
  36. plt.plot(input_sizes, running_times, marker = &#39;o&#39;)
  37. plt.xlabel(&#39;Input size&#39;)
  38. plt.ylabel(&#39;Running time (s)&#39;)
  39. plt.title(&#39;Brute force Knapsack&#39;)
  40. plt.show()

答案1

得分: 0

  1. # 在循环内部有 `plt`,这意味着 `running_times` 只有一个值,而 `input_sizes` 有 5 个值...
  2. 这样应该可以工作
  3. ```python
  4. import time
  5. import random
  6. import matplotlib.pyplot as plt
  7. def brute_force(values, weights, capacity):
  8. num_items = len(values)
  9. max_value = 0
  10. subset = []
  11. for i in range(2**num_items):
  12. current_value = 0
  13. current_weight = 0
  14. current_subset = []
  15. for j in range(num_items):
  16. if(i &amp; (1 &lt;&lt; j)) != 0:
  17. current_value += values[j]
  18. current_weight += weights[j]
  19. current_subset.append(j)
  20. if current_weight &lt;= capacity and current_value &gt; max_value:
  21. max_value = current_value
  22. subset = current_subset
  23. return max_value, subset
  24. input_sizes = [10, 20, 30, 40, 50]
  25. running_times = []
  26. for size in input_sizes:
  27. values = [random.randint(1,100) for _ in range(size)]
  28. weights = [random.randint(1, 100) for _ in range(size)]
  29. capacity = sum(weights) // 2
  30. start_time = time.time()
  31. max_value, subset = brute_force(values, weights, capacity)
  32. end_time = time.time()
  33. running_time = end_time - start_time
  34. running_times.append(running_time)
  35. print(f&quot;Input size: {size}&quot;)
  36. print(f&quot;Chosen subset: {subset}&quot;)
  37. print(f&quot;Chosen weight: {sum(weights[i] for i in subset)}&quot;)
  38. print(f&quot;Running time: {running_time: .6f} seconds&quot;)
  39. plt.plot(input_sizes, running_times, marker = &#39;o&#39;)
  40. plt.xlabel(&#39;Input size&#39;)
  41. plt.ylabel(&#39;Running time (s)&#39;)
  42. plt.title(&#39;Brute force Knapsack&#39;)
  43. plt.show()

我将 input_list 减小为 [1, 2, 3, 4, 5](为了速度,你可以将其改回),输出如下:

ValueError: x 和 y 必须具有相同的第一个维度,但形状分别为 (5,) 和 (1,)。

  1. <details>
  2. <summary>英文:</summary>
  3. The `plt` was inside the loop meaning that `running_times` has only one value whereas `input_sizes` has 5 values...
  4. This should work:
  5. ```python
  6. import time
  7. import random
  8. import matplotlib.pyplot as plt
  9. def brute_force(values, weights, capacity):
  10. num_items = len(values)
  11. max_value = 0
  12. subset = []
  13. for i in range(2**num_items):
  14. current_value = 0
  15. current_weight = 0
  16. current_subset = []
  17. for j in range(num_items):
  18. if(i &amp; (1 &lt;&lt; j)) != 0:
  19. current_value += values[j]
  20. current_weight += weights[j]
  21. current_subset.append(j)
  22. if current_weight &lt;= capacity and current_value &gt; max_value:
  23. max_value = current_value
  24. subset = current_subset
  25. return max_value, subset
  26. input_sizes = [10, 20, 30, 40, 50]
  27. running_times = []
  28. for size in input_sizes:
  29. values = [random.randint(1,100) for _ in range(size)]
  30. weights = [random.randint(1, 100) for _ in range(size)]
  31. capacity = sum(weights) // 2
  32. start_time = time.time()
  33. max_value, subset = brute_force(values, weights, capacity)
  34. end_time = time.time()
  35. running_time = end_time - start_time
  36. running_times.append(running_time)
  37. print(f&quot;Input size: {size}&quot;)
  38. print(f&quot;Chosen subset: {subset}&quot;)
  39. print(f&quot;Chosen weight: {sum(weights[i] for i in subset)}&quot;)
  40. print(f&quot;Running time: {running_time: .6f} seconds&quot;)
  41. plt.plot(input_sizes, running_times, marker = &#39;o&#39;)
  42. plt.xlabel(&#39;Input size&#39;)
  43. plt.ylabel(&#39;Running time (s)&#39;)
  44. plt.title(&#39;Brute force Knapsack&#39;)
  45. plt.show()

i reduced the input_list to [1, 2, 3, 4, 5] (for speed, so you can put it back) and the output looks like this:

ValueError: x 和 y 必须具有相同的第一个维度,但形状分别为 (5,) 和 (1,)。

huangapple
  • 本文由 发表于 2023年6月5日 19:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定