简体   繁体   English

'numpy.ndarray' object 没有使用 reshape 的属性 'values'

[英]'numpy.ndarray' object has no attribute 'values' using reshape

I'm trying to reshape data into two-dimensional data structure so I can use it in Sklear, I keep getting an error 'numpy.ndarray' object has no attribute 'values' and when I tried removing values from-- Xtrain.values.reshape(-1, 1) I get another error saying: Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.我正在尝试将数据重塑为二维数据结构,以便可以在 Skear 中使用它,我不断收到错误“numpy.ndarray”object 没有属性“值”,当我尝试从 Xtrain.values 中删除值时.reshape(-1, 1) 我收到另一个错误消息:如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 或 array.reshape(1, -1) 如果它包含单个特征,则使用 array.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)

No real need to add values after performing train_test_split as the output is an array itself.执行train_test_split后无需添加值,因为 output 本身就是一个数组。 Simply try with:只需尝试:

Xtrain = Xtrain.reshape(-1,1)
Xtest = Xtest.reshape(-1,1)

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

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