简体   繁体   English

X.reshape() 的问题

[英]Problems with X.reshape()

I am trying to use predict (as can be seen below) to predict data on a model after training it.我正在尝试使用预测(如下所示)在训练后预测 model 上的数据。 However all the steps I take I get an error.但是,我采取的所有步骤都会出错。

I tried:我试过了:

X = data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]
X = X.values.reshape(X.shape)
X = X.transpose()
y_pred=model.predict(X)
n_scores = cross_val_score(model, X,y, scoring='accuracy', cv=cv, n_jobs=-1)
# report the model performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

but then get the error: ValueError: X has 17016 features, but LogisticRegression is expecting 4 features as input.但随后得到错误: ValueError: X has 17016 features, but LogisticRegression is expecting 4 features as input.

So then I put in the dimensions of the data:然后我输入数据的维度:

X = X.values.reshape(X.shape(4,17016))
X = X.transpose()
y_pred=model.predict(X)
n_scores = cross_val_score(model, X,y, scoring='accuracy', cv=cv, n_jobs=-1)
# report the model performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

Only to be told: TypeError: 'tuple' object is not callable只是被告知: TypeError: 'tuple' object is not callable

Then I tried:然后我尝试了:

X = data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]
X = X.values.reshape(X.shape[4,17016])
X = X.transpose()
y_pred=model.predict(X)
n_scores = cross_val_score(model, X,y, scoring='accuracy', cv=cv, n_jobs=-1)
# report the model performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

But I am still getting an error: tuple indices must be integers or slices, not tuple但我仍然收到一个错误: tuple indices must be integers or slices, not tuple

So then I make it slices:所以我把它切片:

X = data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]
X = X.values.reshape(X.shape[4:17016])
X = X.transpose()
y_pred=model.predict(X)
n_scores = cross_val_score(model, X,y, scoring='accuracy', cv=cv, n_jobs=-1)
# report the model performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

but still get an error of: ValueError: cannot reshape array of size 68064 into shape () Which leads me down the same rabbit hole.但仍然得到一个错误: ValueError: cannot reshape array of size 68064 into shape ()这导致我进入同一个兔子洞。

I feel like I have gone in circles with all the posts I have been looking at, none which seem to solve my problem.我觉得我一直在看的所有帖子都在兜圈子,似乎没有一个可以解决我的问题。

Here is the head of the data: {'IID': {0: 'NESARC00001', 1: 'NESARC00003', 2: 'NESARC00005', 3: 'NESARC00006', 4: 'NESARC00007'}, 'SEX': {0: 1, 1: 2, 2: 1, 3: 1, 4: 2}, 'NAGE': {0: 79, 1: 49, 2: 76, 3: 51, 4: 36}, 'LIFEAUD5': {0: 0, 1: 0, 2: 0, 3: 0, 4: 1}, 'YES': {0: 'Yes', 1: 'Yes', 2: 'Yes', 3: 'Yes', 4: 'Yes'}, 'PRS': {0: 0.000738316709, 1: -0.0027634842900000002, 2: 0.000455826588, 3: -0.000639326695, 4: -0.0028217993}}这是数据的头部: {'IID': {0: 'NESARC00001', 1: 'NESARC00003', 2: 'NESARC00005', 3: 'NESARC00006', 4: 'NESARC00007'}, 'SEX': {0: 1, 1: 2, 2: 1, 3: 1, 4: 2}, 'NAGE': {0: 79, 1: 49, 2: 76, 3: 51, 4: 36}, 'LIFEAUD5': {0: 0, 1: 0, 2: 0, 3: 0, 4: 1}, 'YES': {0: 'Yes', 1: 'Yes', 2: 'Yes', 3: 'Yes', 4: 'Yes'}, 'PRS': {0: 0.000738316709, 1: -0.0027634842900000002, 2: 0.000455826588, 3: -0.000639326695, 4: -0.0028217993}}

THe dimensions are 17016,6.尺寸为 17016,6。

update, I have changed the dimensions to 6 11344 but still seem to be getting the same errors.更新,我已将尺寸更改为 6 11344,但似乎仍然遇到相同的错误。

When calling predict and fit, an array or arraylike object of dimension n_samples x n_features is expected.调用预测和拟合时,需要一个数组或类似数组的 object,维度为 n_samples x n_features。 Looking at those lines看着那些线条

X = data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]
X = X.values.reshape(X.shape)
X = X.transpose()

You are first extracting an array of n_samples x 4 with n_samples probably being 17016. The second line is not needed and the transpose() in the end is the problem as you are swapping the dimensions.您首先提取 n_samples x 4 的数组,其中 n_samples 可能为 17016。不需要第二行,最后的 transpose() 是您交换尺寸时的问题。 This should explain the error you get.这应该解释你得到的错误。 I would assume that y_pred=model.predict(data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]) should just work as sklearn classes accept both numpy arrays and pandas DataFrames. I would assume that y_pred=model.predict(data2[["SEX", "NAGE", "LIFEAUD5", "PRS"]]) should just work as sklearn classes accept both numpy arrays and pandas DataFrames.

Apart from this, X = X.values.reshape(X.shape(4,17016)) gives you an error as X.shape returns a tuple and you are trying to call a method with 2 arguments so to say using braces.除此之外, X = X.values.reshape(X.shape(4,17016))给你一个错误,因为 X.shape 返回一个元组,你试图用 2 arguments 调用一个方法,可以说使用大括号。 This X.shape[4,17016] gives you an error as you are trying to index into it with a wrong syntax.这个 X.shape[4,17016] 给你一个错误,因为你试图用错误的语法索引它。 If you want to call reshape, just pass in a tuple of the required and allowed dimensions like X.values.reshape((4,17016)),如果要调用 reshape,只需传入所需和允许的维度的元组,例如 X.values.reshape((4,17016)),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM