python “numpy.ndarray”对象没有属性“columns”

yjghlzjz  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(234)

我遵循machine learning tutorial on youtube并使用this dataset。然而,虽然视频中的人运行代码没有问题,但我收到了一个错误,即numpy.ndarray对象没有属性“columns”
下面是我运行的代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from imblearn.over_sampling import RandomOverSampler
cols = ['integrated_mean','integrated_standard_deviation','integrated_excess_kurtosis','integrated_skewness','DM_mean','DM_standard_deviation','DM_excess_kurtosis','DM_skewness','class']
df = pd.read_csv("HTRU_2.data", names = cols)
train, valid, test = np.split(df.sample(frac = 1), [int(0.6*len(df)), int(0.8*len(df))])
def scale_dataset(dataframe, oversample = False):
    X = dataframe[dataframe.columns[:-1]].values
    y = dataframe[dataframe.columns[-1]].values

    scaler = StandardScaler()
    X = scaler.fit_transform(X)

    if oversample:
        ros = RandomOverSampler()
        X, y = ros.fit_resample(X, y)

    data = np.hstack((X, np.reshape(y, (-1, 1))))

    return data, X, y
train, X_train, y_train = scale_dataset(train, oversample = True)
valid, X_train, y_train = scale_dataset(train, oversample = False)
test, X_train, y_train = scale_dataset(train, oversample = False)

我不知道发生了什么事,如何解决它,我已经尝试寻找其他地方,但我不知道。如果有人能帮助它将不胜感激。

ilmyapht

ilmyapht1#

代替

X = dataframe[dataframe.columns[:-1]].values
y = dataframe[dataframe.columns[-1]].values

是的

X = dataframe[:, :-1]
y = dataframe[:, -1]

现在所有的代码都正常工作了

相关问题