df.Neighbourhood[show_up].count().plot(kind='bar') 'numpy.int64' object has no attribute 'plot'
时间: 2023-09-29 07:06:17 浏览: 49
这个错误是因为你正在尝试使用一个 numpy.int64 对象的 plot 方法,而这个方法不存在。
通常情况下,我们使用 pandas 库来读取和操作数据,而不是 numpy 库。因此,我猜测你的代码中可能存在问题。
请确保你已经正确导入 pandas 库并正确加载了数据。然后,你可以使用 pandas DataFrame 的 plot 方法绘制数据。
例如,如果你想绘制 `df.Neighbourhood[show_up].count()` 的柱状图,可以使用以下代码:
```python
import pandas as pd
# 加载数据
df = pd.read_csv('your_data.csv')
# 统计并绘制柱状图
df.Neighbourhood[show_up].value_counts().plot(kind='bar')
```
在这里,`value_counts()` 方法将返回每个邻里出现的次数,并且我们可以使用 `plot()` 方法来绘制柱状图。如果 `show_up` 是一个布尔数组,则 `df.Neighbourhood[show_up]` 将返回一个 Series 对象,其中包含符合条件的行,我们可以使用 `value_counts()` 方法来计算每个邻里出现的次数。
相关问题
cv2.filter2D
cv2.filter2D is a function in the OpenCV library used for convolving a kernel with an image. It performs a 2D spatial convolution between the input image and the kernel. The function is used for various image processing tasks such as blurring, sharpening, edge detection, and noise reduction.
Syntax:
cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])
Parameters:
1. src: Input image (numpy array).
2. ddepth: Desired depth of the output image (e.g. cv2.CV_8U, cv2.CV_32F, etc).
3. kernel: Convolution kernel (numpy array).
4. dst (optional): Output image (numpy array).
5. anchor (optional): Anchor point from where the kernel is applied (by default (-1,-1) which means that the kernel is centred).
6. delta (optional): Value added to the output after the convolution.
7. borderType (optional): Border mode used when the kernel goes beyond the image boundaries.
Example:
import cv2
import numpy as np
img = cv2.imread('image.jpg', 0)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
cv2.imshow('Original Image', img)
cv2.imshow('Filtered Image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we are applying a 5x5 averaging kernel to an input image and displaying the original and filtered images. The output is a blurred image as the kernel is designed to average the pixel values in the neighbourhood.
Traceback (most recent call last): File "D:/Python_For_Beginners/7.12草稿.py", line 367, in <module> variable_neighbourhood_search(100, neighbourhood_funcs, k_max=3, l_max=3) File "D:/Python_For_Beginners/7.12草稿.py", line 342, in variable_neighbourhood_search neighbour_solution = neighbour_func_sets[l](x_0[0]) File "D:/Python_For_Beginners/7.12草稿.py", line 260, in exchange_M2 cost = solution(M2_neigh) File "D:/Python_For_Beginners/7.12草稿.py", line 75, in solution plant_fix = np.sum(np.multiply(M1, w[:len(M1)])) + np.sum(np.multiply(M2, w[len(M1):])) # 固定成本1 ValueError: operands could not be broadcast together with shapes (0,) (4,)
根据你提供的错误信息,看起来是在 `solution()` 函数中出现了一个广播操作的错误。
具体来说,错误信息中指出的 `ValueError` 是由以下代码引起的:
```python
plant_fix = np.sum(np.multiply(M1, w[:len(M1)])) + np.sum(np.multiply(M2, w[len(M1):]))
```
根据错误信息,operands 的形状分别为 (0,) 和 (4,),无法进行广播操作。
这个问题可能是由于 `M1` 的长度为 0,而 `w` 的长度为 4,导致无法进行元素级别的运算。请检查你的代码,确保在使用 `M1` 之前它被正确初始化,并且具有正确的形状。
另外,你还可以检查一下 `w` 数组是否正确初始化,并且是否具有与 `M1` 和 `M2` 相匹配的长度。
如果你能提供更多关于你的代码和数据的信息,我可以给予更详细的帮助。