TypeError:只有大小为1的数组可以转换为Python标量-Earth Transactions

时间:2019-01-25 12:37:13

标签: python numpy

我正在尝试创建一个脚本,该脚本将一个.GTiff文件作为参数输入,然后从文件中提取一些信息以创建一个stats.txt文件,该文件将为我提供classID,分数覆盖率和总数该classID的像素数。

到目前为止,我相信我已经拥有了我所需要的一切,但是我一直遇到相同的错误,并且纠正错误的尝试还没有被证明是卓有成效的。

import requests
#from urllib.parse import urljoin
from bs4 import BeautifulSoup

# URL to scrape
url = "https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0"

# Make request and Initialize BS4 with request content
req = requests.get(url)
soup = BeautifulSoup(req.content, "lxml")

# Tag that contains "Top Experiences" and "More Experiences"
soup.find_all(class_="_l8g1fr")

# Test Code
#Prints title of links and the href
links = soup.find_all(class_="_l8g1fr")
for link in links:
    print(link.find("a").get_text())
    print(link.find("a").get('href'))

运行此命令时,它会阻塞以下回溯:

#!/usr/bin/env python

import sys
import calendar
import os

import gdal
import numpy as np
from scipy.stats import mode

from IPython import embed

GDAL2NUMPY = {  gdal.GDT_Byte      :   np.uint8,
                gdal.GDT_UInt16    :   np.uint16,
                gdal.GDT_Int16     :   np.int16,
                gdal.GDT_UInt32    :   np.uint32,
                gdal.GDT_Int32     :   np.int32,
                gdal.GDT_Float32   :   np.float32,
                gdal.GDT_Float64   :   np.float64,
                gdal.GDT_CInt16    :   np.complex64,
                gdal.GDT_CInt32    :   np.complex64,
                gdal.GDT_CFloat32  :   np.complex64,
                gdal.GDT_CFloat64  :   np.complex128
              }


#Open the original training data .tif map file.

fname = sys.argv[1]
lc_dataset = gdal.Open(fname)
lc = lc_dataset.ReadAsArray()
lc = np.array(lc)

#Calculating total number of pixels with a valid Land Cover ID.

fill_value = 0
number_of_pixels = np.where(lc != fill_value)[0].shape[0]

#Get the number of classes and corresponding IDs.

lc_classes = np.unique(lc)

#Split each class into its contituante pixel and write result to file.

for classID in range(1, lc_classes):
    lc_class_pixels = np.where(lc == classID)[0].shape[0]
    FractionalCover = lc_class_pixels/number_of_pixels
    f.write(classID, FractionalCoverage, lc_class_pixels)

f.close()

我已经尝试了一些更改,因为我确定错误与numpy数据和本机python数据交互有关,但是将我所有的数组转换为numpy数组并尝试重新格式化代码是徒劳的,因为同一错误持续存在。

如果有人可以提出建议,将不胜感激!

谢谢。

1 个答案:

答案 0 :(得分:1)

好吧,函数lc_classes = np.unique(lc)返回一个数组。当您尝试将for循环编写为

for classID in range(1, lc_classes)

在这里,lc_classes是一个数组,尝试将其作为range的绑定会导致错误。如果要遍历数组的长度,可以将代码修改为:

for classID in range(1, len(lc_classes))