【问题标题】:Importing images for manifold Isomap导入流形 Isomap 的图像
【发布时间】:2016-07-17 23:25:06
【问题描述】:

有 192 x 144 像素的图像。它们应该被导入到 Python 列表中,以便列表中的项目是 NDArray 实例。应从列表中创建新的数据框,并将该数据框提供给 Isomap。 iso.fit(df) 失败并出现错误

array = array.astype(np.float64)

ValueError: setting an array element with a sequence.

我花了一天多的时间试图弄清楚应该如何处理 NDArray 以及加载它们的数据框。没运气。任何帮助将不胜感激。

import pandas as pd
from scipy import misc
import glob
from sklearn import manifold

samples = []

for filename in glob.glob('Datasets/ALOI/32/*.png'): 
    img = misc.imread(filename, mode='I')
    samples.append(img)

df = pd.DataFrame.from_records(samples, coerce_float=True)

iso = manifold.Isomap(n_neighbors=6, n_components=3)
iso.fit(df)

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    如果这些是来自 ALOI 的灰度图像,您可能希望将每个像素的亮度视为一个特征。因此,您应该使用 img.reshape(-1) 展平 img 数组。修改后的代码如下:

    import pandas as pd
    from scipy import misc
    import glob
    from sklearn import manifold
    
    samples = []
    
    for filename in glob.glob('Datasets/ALOI/32/*.png'): 
        img = misc.imread(filename, mode='I')
        # the following line changed
        samples.append(img.reshape(-1))
    
    df = pd.DataFrame.from_records(samples, coerce_float=True)
    
    iso = manifold.Isomap(n_neighbors=6, n_components=3)
    iso.fit(df)
    

    【讨论】:

    • 谢谢霍利。 reshape() 就是这样。
    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多