test_mean_max = np.max(test_mean) test_mean_min = np.min(test_mean) bins = 10 fig = plt.figure(figsize=(8, 6)) ax = plt.subplot(111) ax.hist(test_mean, bins=bins, range=(test_mean_min, test_mean_max), density=True, rwidth=0.9) mu, sigma = test_mean.mean(), test_mean.std() x = np.linspace(test_mean_min, test_mean_max, 100000) y = 1 / (sigma*np.sqrt(2*np.pi)) * np.exp(- (x-mu)**2 / (2*sigma**2)) ax.plot(x, y) plt.show()为什么已经归一化,y轴的值有大于1的值
时间: 2024-04-29 07:19:28 浏览: 13
在这段代码中,`density=True`参数会将直方图的值归一化为概率密度,即每个箱子内的计数除以箱子宽度和所有计数的总和。因此,直方图的y轴表示的是概率密度,不是计数。但是,代码中添加的曲线是正态分布的概率密度函数,它的最大值是1/sqrt(2*pi*sigma^2),也就是说,它的最大值并不是1。因此,即使直方图被归一化为概率密度,曲线的最大值仍然可以大于1。
相关问题
.map(plt.hist, 'trip_distance', bins=np.linspace(0., 40, 500))是什么
`.map(plt.hist, 'trip_distance', bins=np.linspace(0., 40, 500))` 是使用 Matplotlib 库绘制直方图的操作。
具体而言,`.map()` 是 Pandas DataFrame 的方法,它可以应用一个函数到 DataFrame 的每个元素上。在这个例子中,我们使用 `.map()` 方法将 `plt.hist` 函数应用到 DataFrame 中名为 `'trip_distance'` 的列上。
`plt.hist` 是 Matplotlib 库中的函数,用于绘制直方图。它接受一个数组或一维序列作为输入,并根据给定的 `bins` 参数将数据分成多个区间,并计算每个区间中数据的频数或频率。
`'trip_distance'` 是 DataFrame 中的列名,它包含了要绘制直方图的数据。`bins=np.linspace(0., 40, 500)` 是指定直方图的区间边界的参数。在这个例子中,我们使用 `np.linspace()` 函数生成一个从 0 到 40 的等差数列,并将其作为直方图的区间边界。
通过执行 `.map(plt.hist, 'trip_distance', bins=np.linspace(0., 40, 500))`,我们将会对 `'trip_distance'` 列中的数据绘制直方图,并使用指定的区间边界进行分组和计数。
需要注意的是,在执行这段代码之前,我们需要先导入 Matplotlib 库并将其命名为 `plt`,通常使用 `import matplotlib.pyplot as plt` 来实现。
import numpy as np import pandas as pd def localmin(points, pixel_size): x_min = np.min(points[:, 0]) y_min = np.min(points[:, 1]) x_max = np.max(points[:, 0]) y_max = np.max(points[:, 1]) w = x_max - x_min h = y_max - y_min wn = w // pixel_size + 1 hn = h // pixel_size + 1 x_bins = np.array([i * pixel_size for i in range(int(wn + 1))]) y_bins = np.array([i * pixel_size for i in range(int(hn + 1))]) df = pd.DataFrame(points, columns=['x', 'y', 'z', 'classification', 'indices']) df['x_bin'] = pd.cut(df['x'], bins=x_bins) df['y_bin'] = pd.cut(df['y'], bins=y_bins) result = df.groupby(['x_bin', 'y_bin']).apply(lambda x: x.loc[x['z'].idxmin()])[['x', 'y', 'z', 'classification', 'indices']] return result a = np.random.random([100,3]) b = np.random.random([100, 1])//0.5 c = np.arange(1000).reshape([100,1]) a = np.concatenate([a,b,c],axis=-1) d = localmin(a,0.2) 如何从a中删除d中的点
可以使用pandas的isin()方法来判断a中的点是否出现在d中,然后使用~运算符将不包含在d中的点筛选出来。具体实现代码如下:
```python
import numpy as np
import pandas as pd
def localmin(points, pixel_size):
x_min = np.min(points[:, 0])
y_min = np.min(points[:, 1])
x_max = np.max(points[:, 0])
y_max = np.max(points[:, 1])
w = x_max - x_min
h = y_max - y_min
wn = w // pixel_size + 1
hn = h // pixel_size + 1
x_bins = np.array([i * pixel_size for i in range(int(wn + 1))])
y_bins = np.array([i * pixel_size for i in range(int(hn + 1))])
df = pd.DataFrame(points, columns=['x', 'y', 'z', 'classification', 'indices'])
df['x_bin'] = pd.cut(df['x'], bins=x_bins)
df['y_bin'] = pd.cut(df['y'], bins=y_bins)
result = df.groupby(['x_bin', 'y_bin']).apply(lambda x: x.loc[x['z'].idxmin()])[['x', 'y', 'z', 'classification', 'indices']]
return result
a = np.random.random([100,3])
b = np.random.random([100, 1])//0.5
c = np.arange(1000).reshape([100,1])
a = np.concatenate([a,b,c],axis=-1)
d = localmin(a,0.2)
# 筛选出不包含在d中的点并重新赋值给a
a = a[~a[:, :2].isin(d.iloc[:, :2].values).any(axis=1)]
```
其中,isin()方法用于判断一个DataFrame或Series对象中的值是否在另一个DataFrame或Series对象中出现过。在这里我们用isin()方法来判断a中的前两列(即x和y坐标)是否出现在d中,然后使用any()方法将结果沿着列方向合并,得到一个长度为100的布尔型数组,表示a中每个点是否在d中出现过。最后使用~运算符对数组进行取反,得到不包含在d中的点的布尔型数组,然后使用该数组对a进行筛选即可。