numpy中的ravel()首先声明两者所要实现的功能是一致的(将多维数组降位一维)

2019年03月15日 17:03:52 Jennie_J 阅读数 187
将多维数组转换为一维数组

print(Y_underSampling_trian)
1
输出带有index的DataFrame格式

    Class

6870 1
82147 0

[688 rows x 1 columns]
1
2
3
4
5
对这个series的values进行ravel()

print(Y_underSampling_trian.values.ravel())
1
输出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]
1
之所以会关注这个,是因为,在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
numpy的ravel() 和 flatten()函数
简介
首先声明两者所要实现的功能是一致的(将多维数组降位一维)。这点从两个单词的意也可以看出来,ravel(散开,解开),flatten(变平)。两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响(reflects)原始矩阵,而numpy.ravel()返回的是视图(view,也颇有几分C/C++引用reference的意味),会影响(reflects)原始矩阵。

两者功能
In [14]: x=np.array([[1,2],[3,4]])

flattenh函数和ravel函数在降维时默认是行序优先

In [15]: x.flatten()
Out[15]: array([1, 2, 3, 4])

In [17]: x.ravel()
Out[17]: array([1, 2, 3, 4])

传入’F’参数表示列序优先

In [18]: x.flatten(‘F’)
Out[18]: array([1, 3, 2, 4])

In [19]: x.ravel(‘F’)
Out[19]: array([1, 3, 2, 4])

#reshape函数当参数只有一个-1时表示将数组降为一维
In [21]: x.reshape(-1)
Out[21]: array([1, 2, 3, 4])
#x.T表示x的转置
In [22]: x.T.reshape(-1)
Out[22]: array([1, 3, 2, 4])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
两者区别

x = np.array([[1, 2], [3, 4]])
x.flatten()[1] = 100
x
array([[1, 2],
[3, 4]])

x.ravel()[1] = 100
x
array([[ 1, 100],
[ 3, 4]])
1
2
3
4
5
6
7
8
9
通过上面的程序可以发现flatten函数返回的是拷贝。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值