【问题标题】:Numpy reshape issuesNumpy重塑问题
【发布时间】:2017-01-13 10:45:03
【问题描述】:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, cross_validation, svm


df = pd.read_csv('table.csv') 
print (df.head())

df = df[['Price', 'Asset']]
x = np.array(df.Price)
y = np.array(df.Asset)

x_train, x_test, y_train, y_test = cross_validation.train_test_split(
x, y, test_size=0.2)


x_train = np.pad(x, [(0,0)], mode='constant')
x_train.reshape((23,1))


y_train = np.pad(y, [(0,0)], mode ='constant')
y_train.reshape((23,1))

np.reshape(-1, 1)

错误:

runfile('C:/Users/HP/Documents/linear.py', wdir='C:/Users/HP/Documents')
       Price     Asset
0    87.585859    191
1    87.839996    232
2    87.309998    245
3    88.629997    445
4    88.379997    393
C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: 

    DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
Traceback (most recent call last):

  File "<ipython-input-124-030ffa933525>", line 1, in <module>
    runfile('C:/Users/HP/Documents/linear.py', wdir='C:/Users/HP/Documents')

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/HP/Documents/linear.py", line 38, in <module>
    clf.fit(x_train, y_train)

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 427, in fit
    y_numeric=True, multi_output=True)

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 520, in check_X_y
    check_consistent_length(X, y)

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 176, in check_consistent_length
    "%s" % str(uniques))

ValueError: Found arrays with inconsistent numbers of samples: [ 1 23]

我的 DataFrame 大小:23、2。

我将我的 x_train 和 y_train 填充到 [23,1],因为我得到了这个初始错误 ValueError:发现样本数量不一致的数组:[1 18]。 填充后我的错误消息:ValueError:发现样本数量不一致的数组:[1 23]。

然后我尝试对其进行重塑,但仍然收到错误消息:ValueError:发现样本数量不一致的数组:[1 23]。

我该如何解决这个问题?

【问题讨论】:

  • 你想重塑什么? np.reshape 不知道你想重塑什么。像array.reshape((x, y))一样使用它。
  • 正如@mwormser 所说,您需要在数组对象上调用reshape,例如x_trainy_trainx_train.reshape((23,1))
  • 试过仍然收到错误消息:ValueError:找到样本数量不一致的数组:[1 23]
  • 真的不清楚你想在这里做什么。填充前x_trainy_train 的大小是多少?您是否尝试打印它们的形状以检查它们是否一致?我们无法运行您的代码来测试它,因为我们没有您的数据。您能否尝试发布一个minimal reproducible example,我们可以运行它以便能够重现您的错误?
  • @Praveen 问题已解决。我不得不垫和重塑是它归结为。我不得不填充,因为我得到不一致的数字并重塑,因为 scikit(机器学习)需要读取行。它也不是 1 dim 数组,因此转置不起作用。感谢您的帮助!

标签: python numpy


【解决方案1】:

如果您只想将数组大小从(x, 1) 重塑为(1, x),您可以使用np.transposenumpy.ndarray.T 函数:

x_train = x_train.T
y_train = np.transpose(y_train)

两者都达到相同的结果。

编辑:这仅适用于一维数组。对高维数组使用 reshape。

如果您不向我们提供显示错误发生在哪一行的完整回溯,我们将无法为您提供更详细的帮助。

【讨论】:

  • 转置和重塑不要做同样的事情!试试a = np.arange(6).reshape((2, 3)); b = a.reshape((3, 2)); c = a.T; np.all(b == c)。请在发布之前尝试您的答案!
  • @Praveen 当然你是对的,我只是想一维是1的情况,我会编辑它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 2015-05-07
相关资源
最近更新 更多