csv 数组索引太多:数组是1维的,但索引了2个

fbcarpbf  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(110)

我是Python代码的初学者。如果有人能帮我解答这个问题,我将不胜感激。我运行下面给出的代码来生成数据,并将测试和训练数据分开。我得到了200行2列的训练数据,但我得到了100行1列的测试数据。我不明白为什么训练数据有2列,而测试数据是1列。
最后,这个问题导致了这个问题
数组索引太多:数组是1维的,但索引了2个
当我运行下面给出的代码部分时。

feature_1_test = X_test[:,[0]].reshape(-1,1)
feature_2_test = X_test[:,[1]].reshape(-1,1) 

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from pyod.models.knn import KNN
from pyod.utils.data import generate_data
outlier_fraction = 0.1  
n_train = 200 
n_test = 100
X_train, y_train, X_test, y_test = generate_data(n_train=n_train, n_test=n_test, contamination=outlier_fraction)

#plot train and test set
feature_1_train = X_train[:,[0]].reshape(-1,1)
feature_2_train = X_train[:,[1]].reshape(-1,1)
feature_1_test = X_test[:,[0]].reshape(-1,1)
feature_2_test = X_test[:,[1]].reshape(-1,1)

我真的很感谢你的帮助。

f87krz0w

f87krz0w1#

根据'pyod'文档,函数'generate_data'以不同的顺序返回。这就是为什么你有尺寸问题。您可以通过访问此页面检查函数参数和返回。下面,你可以看到我成功运行的版本。

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from pyod.models.knn import KNN
from pyod.utils.data import generate_data

outlier_fraction = 0.1  
n_train = 200 
n_test = 100
X_train, X_test, y_train, y_test = generate_data(n_train=n_train, n_test=n_test, contamination=outlier_fraction)

print(X_train.shape)
print(X_test.shape)

#plot train and test set
feature_1_train = X_train[:,[0]].reshape(-1,1)
feature_2_train = X_train[:,[1]].reshape(-1,1)
feature_1_test = X_test[:,[0]].reshape(-1,1)
feature_2_test = X_test[:,[1]].reshape(-1,1)

相关问题