简体   繁体   中英

'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.

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. Simply try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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