我正在尝试将数据 reshape 为二维数据结构,以便我可以在 Sklear 中使用它,但我不断收到错误“numpy.ndarray”对象没有属性“值” 当我尝试从 Xtrain.values.reshape(-1, 1) 中删除值时,我收到另一个错误:如果您的数据具有单个特征或数组,请使用 array.reshape(-1, 1) reshape 您的数据。如果包含单个样本,则 reshape(1, -1)。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('loan_defaults.csv')
data = pd.get_dummies(data, drop_first=True)
data.groupby('default').mean()
data.corr()
defaultN = data.query('default == 0')
defaultY= data.query('default == 1')
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(data.balance, data.default, random_state = 0)
Xtrain = Xtrain.values.reshape(-1, 1)
Xtest = Xtest.values.reshape(-1, 1)
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(class_weight="balanced")
log_reg.fit(Xtrain, ytrain)
log_reg.intercept_
log_reg.coef_
log_reg.predict_proba(100)
log_reg.predict(100)
最佳答案
执行train_test_split
后不需要添加值,因为输出本身就是一个数组。只需尝试:
Xtrain = Xtrain.reshape(-1,1)
Xtest = Xtest.reshape(-1,1)
关于python - 使用 reshape 时,“numpy.ndarray”对象没有属性 'values',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868430/