我的以下代码输出似乎是每个人口普查区的字典列表,这基本上就像一个指定的土地区域。我能够计算出人口和几种不同土地覆盖类型的百分比。现在我想计算每种土地覆盖类型的人口和百分比之间的皮尔逊相关系数。
我要做的是提取/过滤字典列表,以便我可以将人口与每种土地覆盖类型进行比较。因此,皮尔逊相关性:
- 人口和开发土地
- 人口荒地
- 人口和林地
- ...
这是代码:
import geopandas as gpd
from rasterstats import zonal_stats
from rasterio.mask import mask
from rasterio.plot import show
import matplotlib.pyplot as plt
import numpy as np
import fiona
import rasterio
from scipy import stats
from rasterio.warp import calculate_default_transform, reproject, Resampling
mass_fp = r"New_Massachusetts.tif"
mass_tracts = gpd.read_file("Massachusetts/Massachusetts.shp");
dst_crs = 'EPSG:4269';
with rasterio.open('Massachusetts.tif') as src:
transform, width, height = calculate_default_transform(
src.crs, mass_tracts.crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': mass_tracts.crs,
'transform': transform,
'width': width,
'height': height
})
with rasterio.open('New_Mass.tif', 'w', **kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rasterio.band(src, i),
destination=rasterio.band(dst, i),
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=dst_crs,
resampling=Resampling.nearest)
#Getting zonal stats
stats = zonal_stats("Massachusetts/Massachusetts.shp", "New_Mass.tif",stats="count",geojson_out=True, copy_properties=True,nodata_value=0,categorical=True);
#Variables for our loop below
total_pop=0.0;
total_pixel_count=0.0;
total_developed = 0.0;
total_water_ice = 0.0;
total_barren_land = 0.0;
total_forest = 0.0;
#Array to store our census track
census_tract_land_percentages = [];
#Looping through each tract in the stats data and getting the data we need and then storing it in a array with dictionaries
#[11,12], [21, 22, 23,24], 31, [41,42,43] 5
for x in stats:
total_pixel_count=x["properties"]["count"];
total_census_population = x["properties"]["DP0010001"]
total_developed= (float(x["properties"].get(21,0)+x.get(22,0)+x["properties"].get(23,0) + x["properties"].get(24,0))/total_pixel_count)*100;
total_water_ice = (float(x["properties"].get(11,0)+x["properties"].get(12,0))/total_pixel_count)*100;
total_barren_land=float(x["properties"].get(31,0)/total_pixel_count)*100;
total_forest = (float(x["properties"].get(41,0)+x["properties"].get(42,0)+x["properties"].get(43,0))/total_pixel_count)*100;
census_tract_land_percentages.append({"Total Population:":total_census_population,"Total Water Ice Cover":total_water_ice,"Total Developed":total_developed,
"Total Barren Land":total_barren_land,"Total Forest":total_forest});
print(census_tract_land_percentages);
#Getting the total population for all census tracts
for x in mass_tracts["DP0010001"]:
total_pop+=x
np_census_arr = np.asarray(census_tract_land_percentages);
运行此代码后,我会得到以下字典列表,我想知道如何提取/过滤人口并将其与每个土地覆盖百分比进行比较,并最终计算 Pearson r 相关性。
[{'Total Population:': 4585, 'Total Water Ice Cover': 2.848142234497044, 'Total Developed': 17.205368316575324, 'Total Barren Land': 0.22439908514219134, 'Total Forest': 34.40642126612868},
{'Total Population:': 4751, 'Total Water Ice Cover': 1.047783534830167, 'Total Developed': 37.27115716753022, 'Total Barren Land': 0.11514104778353484, 'Total Forest': 19.11341393206678},
{'Total Population:': 3214, 'Total Water Ice Cover': 0.09166603009701321, 'Total Developed': 23.50469788404247, 'Total Barren Land': 0.2597204186082041, 'Total Forest': 20.418608204109695},
{'Total Population:': 5005, 'Total Water Ice Cover': 0.0, 'Total Developed': 66.37545713124746, 'Total Barren Land': 0.0, 'Total Forest': 10.68671271840715},
...
]
有什么想法可以循环遍历,然后根据每种土地覆盖类型的百分比计算总人口变量的 Pearson r?
谢谢