0

我有一个信用风险分析数据集,如下所示:

Loan_ID      Age      Income(LPA)  Employed_yr    Education    Loan_status
 1            18        2.4            1            12th              1
 2            46        43             26           Post Grad         0
 3            22       12              4            Grad              0
 4            25       17              1            Grad              1

在loan_status 中,1 表示违约,0 表示非违约。

现在默认的数量非常少,大约为 1000,非默认数量为 25,000。所以我想做过度采样或合成采样。

到这里代码运行良好

cred_loan = pd.read_csv("Credit_Risk_Analysis.csv")
from imblearn import under_sampling, over_sampling
from imblearn.over_sampling import SMOTE
y= cred_loan.loan_status
X = cred_loan.drop('loan_status', axis=1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, 
random_state=27)
sm = SMOTE(random_state=27, ratio=1.0)
from sklearn.linear_model import LogisticRegression

在此之后我执行以下操作并且出现错误

[IN] X_train, y_train = sm.fit_sample(X_train, y_train)        

[OUT]ValueError                                Traceback (most recent call 
last)
<ipython-input-39-0995f82b5705> in <module>
----> 1 X_train, y_train = sm.fit_sample(X_train, y_train)
      2 

~\Anaconda3\lib\site-packages\imblearn\base.py in fit_resample(self, X, y)
     77 
     78         check_classification_targets(y)
---> 79         X, y, binarize_y = self._check_X_y(X, y)
     80 
     81         self.sampling_strategy_ = check_sampling_strategy(

~\Anaconda3\lib\site-packages\imblearn\base.py in _check_X_y(X, y)
    135     def _check_X_y(X, y):
    136         y, binarize_y = check_target_type(y, 
indicate_one_vs_all=True)
--> 137         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc'])
    138         return X, y, binarize_y
    139 

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, 
accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, 
ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, 
y_numeric, warn_on_dtype, estimator)
    754             warnings.warn("A column-vector y was passed when a 1d 
array was"
    755                           " expected. Please change the shape of y 
to "
--> 756                           "(n_samples, ), for example using 
ravel().",
    757                           DataConversionWarning, stacklevel=2)
    758         return np.ravel(y)

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in 
check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy,         
force_all_finite, ensure_2d, allow_nd, ensure_min_samples, 
ensure_min_features, warn_on_dtype, estimator)
    565     if copy and np.may_share_memory(array, array_orig):
    566         array = np.array(array, dtype=dtype, order=order)
--> 567 
    568     if (warn_on_dtype and dtypes_orig is not None and
    569             {array.dtype} != set(dtypes_orig)):

ValueError: could not convert string to float: 'MORTGAGE'

有人可以帮忙吗?

4

0 回答 0