【问题标题】:How to Normalize Values in a Column of a GeoDataFrame如何规范化 GeoDataFrame 列中的值
【发布时间】:2019-11-17 12:14:12
【问题描述】:

有两个 shapefile 作为 GeoDataFrames 读入。对于每个 gdf ​​中的一列,我需要将值标准化为介于 0 和 1 之间。

我尝试使用scaler.fit_transform 函数对值进行规范化,但在传入二维数组而不是一维数组时引发了错误。所以我开始尝试(未成功)在规范化之前将列重塑(使用np.reshape)为一维数据帧。


    output = gpd.read_file(r"C:\Users\mrich\OneDrive\GMU\Summer 2019 Comp Migration\output_3_simOutput.shp")
    val = gpd.read_file(r"C:\Users\mrich\OneDrive\GMU\CSS 645 (Spring 2019)\Final Project\Other_geo_data\gadm36_TUR_1_val.shp")

    # Reshape attributes
    output.simEnd = np.reshape(output.simEnd, (928, -1)
    val.val_mar19 = np.reshape(val.val_mar19, (928, -1)

    # Normalize both actual and predicted REFPOP
    scaler = preprocessing.StandardScaler()
    scaled_actual = scaler.fit_transform(val.val_mar19)
    scaled_predicted = scaler.fit_transform(output.simEnd)

要归一化的两列是 simEnd(在输出中)和 val_mar19(在 val 中)。每个条目有 928 个条目。我相信他们可能是 GeoSeries。

在重塑线中,Exception: Data must be 1-dimensional

另一个错误但无法判断是否相关:AttributeError: 'Series' object has no attribute 'reshape.'

【问题讨论】:

    标签: python-3.x numpy normalization sklearn-pandas geopandas


    【解决方案1】:

    如果没有mcve,很难更具体地提供帮助,但如果我理解正确,这似乎是一个简单的pandas 问题:

    df = pd.DataFrame({'x':np.random.rand(20) // .01})
    df['x_norm'] = (df['x'] - df['x'].min()) / (df['x'].max() - df['x'].min())
    

    返回

           x    x_norm
    0   38.0  0.426966
    1   77.0  0.865169
    2   61.0  0.685393
    3   48.0  0.539326
    4   88.0  0.988764
    5   74.0  0.831461
    6   49.0  0.550562
    7    0.0  0.000000
    8   47.0  0.528090
    9   60.0  0.674157
    10  22.0  0.247191
    11  74.0  0.831461
    12  34.0  0.382022
    13  48.0  0.539326
    14  69.0  0.775281
    15   3.0  0.033708
    16  89.0  1.000000
    17  83.0  0.932584
    18  57.0  0.640449
    19  26.0  0.292135
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 2021-02-16
      • 1970-01-01
      • 2015-10-04
      • 2016-03-06
      • 2017-09-26
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多