【发布时间】:2021-07-08 02:14:44
【问题描述】:
据我了解,当我尝试传递数组而不是单个值时会发生此错误,但我认为 StandardScaler 应该接受矩阵。
对于正在提取的数据的上下文,我处理了一个包含 1,000 个垃圾箱图像的目录,我稍后会对其进行整形。我认为重塑可以解决问题,但我们到了。
编辑:更新代码以包含导入和错误消息,它应该使用提供的内容运行。
import os
from os import listdir
from os.path import isfile, join
import matplotlib as mpl
import matplotlib.pyplot as plt
from IPython.display import display
%matplotlib inline
import pandas as pd
import numpy as np
from PIL import Image
from skimage.feature import hog
from skimage.color import rgb2gray
from skimage.color import rgba2rgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.svm import SVC
from sklearn.metrics import roc_curve, auc
def returnImageArray(filename, directory = "./garbage bins/"):
filePath = os.path.join(directory, filename)
image = Image.open(filePath)
return np.array(image)
def processImageFeatures(image):
features = image.flatten()
grayscaleConversion = rgb2gray(rgba2rgb(image))
hogify = hog(grayscaleConversion, block_norm='L2-Hys', pixels_per_cell=(16, 16))
flatify = np.hstack(features)
return flatify
allFeatures = []
for file in fileNames:
image = returnImageArray(file)
feat = processImageFeatures(image)
allFeatures.append(feat)
allFeaturesArray = np.array(allFeatures, dtype=object)
print(allFeaturesArray.shape)
reshapedF = np.array(allFeaturesArray).reshape(-1, 1)
print(reshapedF.shape)
print(reshapedF[1])
scaler = StandardScaler()
##----
garbage = scaler.fit_transform(reshapedF) ##ERROR HERE: only size-1 arrays can be converted to Python scalars
##----
pca = PCA(n_components=1000)
garbagePCA = scale.fit_transform(gabrage)
print(allFeaturesArray.shape)
#(1000,)
#(1000, 1)
#[array([196, 179, 146, ..., 187, 164, 255], dtype=uint8)]
警告
<ipython-input-6-6c460760299f>:7: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
allFeaturesArray = np.array(allFeatures)
错误
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-7-4533ce454d0d> in <module>
6 scaler = StandardScaler()
7
----> 8 garbage = scaler.fit_transform(reshapedF)
9
10 pca = PCA(n_components=1000)
~\anaconda3\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params)
688 if y is None:
689 # fit method of arity 1 (unsupervised transformation)
--> 690 return self.fit(X, **fit_params).transform(X)
691 else:
692 # fit method of arity 2 (supervised transformation)
~\anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in fit(self, X, y)
665 # Reset internal state before fitting
666 self._reset()
--> 667 return self.partial_fit(X, y)
668
669 def partial_fit(self, X, y=None):
~\anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in partial_fit(self, X, y)
694 Transformer instance.
695 """
--> 696 X = self._validate_data(X, accept_sparse=('csr', 'csc'),
697 estimator=self, dtype=FLOAT_DTYPES,
698 force_all_finite='allow-nan')
~\anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
418 f"requires y to be passed, but the target y is None."
419 )
--> 420 X = check_array(X, **check_params)
421 out = X
422 else:
~\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
70 FutureWarning)
71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72 return f(**kwargs)
73 return inner_f
74
~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
596 array = array.astype(dtype, casting="unsafe", copy=False)
597 else:
--> 598 array = np.asarray(array, order=order, dtype=dtype)
599 except ComplexWarning:
600 raise ValueError("Complex data not supported\n"
~\anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
ValueError: setting an array element with a sequence.
图片集
【问题讨论】:
-
为什么是
object?np.array(allFeatures, dtype=object) -
建议提供一段工作代码,即包括导入语句,这样试图重现您遇到的错误的人就不会花时间重新创建您的问题。
-
请提供错误和堆栈跟踪
-
我使用“dtype=object”的原因是因为它向我发出了警告,我应该删除它还是有更好的方法来处理我收到的警告?具体来说,警告是:
<ipython-input-6-6c460760299f>:7: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray allFeaturesArray = np.array(allFeatures) -
但即使我删除了 dtype,它似乎也给出了相同的结果。
标签: python pandas numpy scikit-learn