zeros_like dtype=np.bool
时间: 2023-10-21 13:18:19 浏览: 71
`np.bool` 表示布尔类型,`zeros_like` 函数会返回一个和输入数组 `shape` 相同且元素全部为 False 的数组,数据类型为 `np.bool`。下面是一个示例:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.zeros_like(a, dtype=np.bool)
print(b)
```
输出:
```
[[False False]
[False False]]
```
这里 `b` 和 `a` 的形状相同,元素全部为 False。由于指定了 dtype 为 `np.bool`,所以 `b` 数组的数据类型为布尔类型。
相关问题
# 找出相关程度 plt.figure(figsize=(20, 16)) # 指定绘图对象宽度和高度 colnm = data_train1.columns.tolist() # 列表头 mcorr = data_train1[colnm].corr(method="spearman") # 相关系数矩阵,即给出了任意两个变量之间的相关系数 mask = np.zeros_like(mcorr, dtype=np.bool) # 构造与mcorr同维数矩阵 为bool型 mask[np.triu_indices_from(mask)] = True # 角分线右侧为True cmap = sns.diverging_palette(220, 10, as_cmap=True) # 返回matplotlib colormap对象 g = sns.heatmap(mcorr, mask=mask, cmap=cmap, square=True, annot=True, fmt='0.2f') # 热力图(看两两相似度) plt.show()根据这段代码写出正确的代码
这段代码是用于绘制热力图展示数据集中各个变量之间的相关性,可以按照以下步骤进行编写:
1. 导入必要的库
```python
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
```
2. 读取数据集
```python
data_train1 = pd.read_csv('data_train1.csv')
```
3. 计算相关系数矩阵
```python
colnm = data_train1.columns.tolist()
mcorr = data_train1[colnm].corr(method="spearman")
```
4. 构造相似度矩阵和掩码矩阵
```python
mask = np.zeros_like(mcorr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
```
5. 绘制热力图
```python
plt.figure(figsize=(20, 16))
cmap = sns.diverging_palette(220, 10, as_cmap=True)
g = sns.heatmap(mcorr, mask=mask, cmap=cmap, square=True, annot=True, fmt='0.2f')
plt.show()
```
其中,`np.triu_indices_from(mask)` 是用于构造掩码矩阵的函数,将对角线及其左下方的元素设为 False,右上方的元素设为 True。`sns.diverging_palette(220, 10, as_cmap=True)` 是用于生成颜色渐变的函数。其他参数解释如下:
- `square=True`:将每个单元格绘制为正方形。
- `annot=True`:在每个单元格中显示相关系数。
- `fmt='0.2f'`:以浮点数格式显示相关系数,保留两位小数。
def extract_building_shadow(image, dsm, ground_spacing, radius): shadow_mask = np.zeros_like(image, dtype=np.bool) for i in range(0, image.shape[0], ground_spacing): for j in range(0, image.shape[1], ground_spacing): if not np.any(shadow_mask[i, j]): center = (i, j) ground_height = dsm[i, j] for x, y in spiral_search(center, radius, ground_spacing): if x < 0 or x >= image.shape[0] or y < 0 or y >= image.shape[1]: continue if np.any(shadow_mask[x, y:]): continue height = dsm[x, y] if height > ground_height: shadow_mask[x, y] = True elif height == ground_height: if np.linalg.norm(np.array([x, y]) - np.array(center)) < \ np.linalg.norm(np.array([i, j]) - np.array(center)): shadow_mask[x, y] = True cv2.imwrite(output_path, shadow_mask.astype(np.uint8) * 255) return shadow_mask image = cv2.imread('C:\yingxiang\DJI_20230322140516_0026_V.JPG') dsm_path = 'C:/sanwei/jianmo/Productions/Production_2/Production_2_DSM_part_2_2.tif' dsm_dataset = gdal.Open(dsm_path) output_path = 'C:\yingxiang\mask.png'这段代码为什么运行后不能将掩膜结果保存
这可能是由于在保存 PNG 文件之前,尝试在代码中访问 `output_path` 变量,而此时该变量尚未定义。您可以将 `output_path` 变量的定义移动到函数之前,以确保在访问变量之前它已经存在。例如:
```
output_path = 'C:\yingxiang\mask.png'
def extract_building_shadow(image, dsm, ground_spacing, radius):
shadow_mask = np.zeros_like(image, dtype=np.bool)
for i in range(0, image.shape[0], ground_spacing):
for j in range(0, image.shape[1], ground_spacing):
if not np.any(shadow_mask[i, j]):
center = (i, j)
ground_height = dsm[i, j]
for x, y in spiral_search(center, radius, ground_spacing):
if x < 0 or x >= image.shape[0] or y < 0 or y >= image.shape[1]:
continue
if np.any(shadow_mask[x, y:]):
continue
height = dsm[x, y]
if height > ground_height:
shadow_mask[x, y] = True
elif height == ground_height:
if np.linalg.norm(np.array([x, y]) - np.array(center)) < \
np.linalg.norm(np.array([i, j]) - np.array(center)):
shadow_mask[x, y] = True
cv2.imwrite(output_path, shadow_mask.astype(np.uint8) * 255)
return shadow_mask
image = cv2.imread('C:\yingxiang\DJI_20230322140516_0026_V.JPG')
dsm_path = 'C:/sanwei/jianmo/Productions/Production_2/Production_2_DSM_part_2_2.tif'
dsm_dataset = gdal.Open(dsm_path)
# 调用函数
extract_building_shadow(image, dsm_dataset.ReadAsArray(), 10, 10)
```
请注意,此代码还将 `dsm` 数据集转换为数组,并将其传递给函数。