如何解决ValueError:预期的二维数组,而是得到标量数组:array=1.0
我刚刚修复了一个错误,指出“只有一个元素张量可以转换为 Python 标量”,现在我收到了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-130-1555e7bdb655> in <module>
4
5 pca = PCA(n_components=0.95)
----> 6 pca.fit(X)
7 cov_matrix = np.dot(X.T,X) / len(X)
8
~\anaconda3\lib\site-packages\sklearn\decomposition\_pca.py in fit(self,X,y)
349 Returns the instance itself.
350 """
--> 351 self._fit(X)
352 return self
353
~\anaconda3\lib\site-packages\sklearn\decomposition\_pca.py in _fit(self,X)
395 'TruncatedSVD for a possible alternative.')
396
--> 397 X = self._validate_data(X,dtype=[np.float64,np.float32],398 ensure_2d=True,copy=self.copy)
399
~\anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self,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)
71 FutureWarning)
72 kwargs.update({k: arg for k,arg in zip(sig.parameters,args)})
---> 73 return f(**kwargs)
74 return inner_f
75
~\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)
611 # If input is scalar raise error
612 if array.ndim == 0:
--> 613 raise ValueError(
614 "Expected 2D array,got scalar array instead:\narray={}.\n"
615 "Reshape your data either using array.reshape(-1,1) if "
ValueError: Expected 2D array,got scalar array instead:
array=1.0.
Reshape your data either using array.reshape(-1,1) if your data has a single feature or array.reshape(1,-1) if it contains a single sample.
我不知道如何修复它! 我使用 .reshape(1,-1) 重塑了我的目标张量,这就是我收到的错误。
可以看出,我正在尝试将 PCA 应用于 FashionMNIST 的 2 个类上的张量。
我也尝试重塑张量,
reshaped = torch.reshape(target,[16,2])
带输出
reshaped
tensor([[1,1],[1,7],[7,1]])
但我仍然面临完全相同的错误!请帮帮我
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。