numpy中的ravel()

将多维数组转换为一维数组

print(Y_underSampling_trian)

输出带有index的DataFrame格式

        Class
6870        1
82147       0
...
[688 rows x 1 columns]

对这个series的values进行ravel()

print(Y_underSampling_trian.values.ravel())

输出1-D数组(ndarray):

[1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 0....0 0 0 1 0 0 0]

之所以会关注这个,是因为,在sklearn.linear_model.LogisticRegression()应该过程中fit(X,Y)时,Y没有用ravel()出先如下warrning:
‘DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True)’

查阅sklearn官网https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression发现fit()的Y是数组:
fit(X, y, sample_weight=None)

Parameters:
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and
n_features is the number of features.

y : array-like, shape (n_samples,)
Target vector relative to X.

sample_weight : array-like, shape (n_samples,) optional
Array of weights that are assigned to individual samples.
If not provided, then each sample is given unit weight.

New in version 0.17: sample_weight support to LogisticRegression.

Returns:
self : object

发布了41 篇原创文章 · 获赞 14 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43685844/article/details/88578576
今日推荐