X = X.toarray() NameError: name 'X' is not defined. when loading dataset using load_svmlight_file() trying to convert X to ndarray

X = X.toarray() NameError: name 'X' is not defined. when loading dataset using load_svmlight_file() trying to convert X to ndarray

from sklearn.datasets import load_svmlight_file
def get_data(dn):
    # load_svmlight_file loads dataset into sparse CSR matrix
    X,Y = load_svmlight_file(dn)
    print(type(X)) # you will get numpy.ndarray
    return X,Y


# convert X to ndarray
X = X.toarray()
print(type(X))
    
# As you are going to implement logistic regression, you have to convert the labels into 0 and 1 
Y = np.where(Y == -1, 0, 1)

当运行代码出现以下错误X = X.toarray() NameError: name 'X' is not defined时,代码是为了转换此数据集url= 'https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/diabetes' wget.download(url,'Assingment1')

您没有先调用 get_data(dn) 函数,您需要在将 X 转换为数组之前先调用它。

应该是这样的:

from sklearn.datasets import load_svmlight_file
def get_data(dn):
    # load_svmlight_file loads dataset into sparse CSR matrix
    X,Y = load_svmlight_file(dn)
    print(type(X)) # you will get numpy.ndarray
    return X,Y

# X, Y = get_data(dn) uncomment this code and pass the dn parameter you want.
# convert X to ndarray
X = X.toarray()
print(type(X))

# As you are going to implement logistic regression, you have to convert the 
labels into 0 and 1 
Y = np.where(Y == -1, 0, 1)

取消第8行函数调用的注释,将dn参数传给它,然后定义X和Y。