稀疏矩阵的压缩方式以及一般性的数据处理方式
匪不君子 发布于2019-05 浏览:1230 回复:0
0
收藏

首先在数据处理的过程对于参数的理解开始解析稀疏矩阵的优化储存方式


Python中的数据缺失问题比较:

数据集

Country,Age,Salary,Purchased
France,44,72000,No
Spain,27,48000,Yes
Germany,30,54000,No
Spain,38,61000,No
Germany,40,,Yes
France,35,58000,Yes
Spain,NaN,52000,No
France,48,79000,Yes
Germany,50,83000,No
France,37,67000,Yes


作为科学技术库,那么在sklearn当中有一个包,可以专门用来处理缺失数据,名为Imputer

先来看官方对它的参数的定义:

贴代码:

 

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = “NaN”, strategy = “mean”, axis = 0)
result = imputer.fit_transform(X[ : , 1:3])
print(result)

 


Parameters
———-
missing_values : integer or “NaN”, optional (default=”NaN”)
The placeholder for the missing values. All occurrences of
`missing_values` will be imputed. For missing values encoded as np.nan,
use the string value “NaN”.


strategy : string, optional (default=”mean”)
The imputation strategy.


– If “mean”, then replace missing values using the mean along
the axis.
– If “median”, then replace missing values using the median along
the axis.
– If “most_frequent”, then replace missing using the most frequent
value along the axis.


axis : integer, optional (default=0)
The axis along which to impute.


– If `axis=0`, then impute along columns.
– If `axis=1`, then impute along rows.


verbose : integer, optional (default=0)
Controls the verbosity of the imputer.


copy : boolean, optional (default=True)
If True, a copy of X will be created. If False, imputation will
be done in-place whenever possible. Note that, in the following cases,
a new copy will always be made, even if `copy=False`:


– If X is not an array of floating values;
– If X is sparse and `missing_values=0`;
– If `axis=0` and X is encoded as a CSR matrix;
– If `axis=1` and X is encoded as a CSC matrix.
详解参数:

首先missing_values表示缺失值可以是整数或者NaN,默认为NaN

(default=”NaN”)体现

strategy = “mean”

表示替换缺失数据的策略,默认选择mean模式,也就是均值模式;可用以下说明:

If “median”, then replace missing values using the median along
the axis.
– If “most_frequent”, then replace missing using the most frequent
value along the axis.
使用median时候,用的是中间值,中位数

使用most_frequent 则利用的是最多出现次数的数字来代替缺失值

axis作为制定轴的参数:

默认为0

 

– If `axis=0`, then impute along columns.
– If `axis=1`, then impute along rows.
如果他是0的话,那么代表的是列;

如果他不是0,而是1的话,那么代表的是行

copy决定是否在原有基础上修改值:

为True,不会选择就地修改,会复制一份;

为False,则会选择源数据进行修改。

 


copy : boolean, optional (default=True)
If True, a copy of X will be created. If False, imputation will
be done in-place whenever possible. Note that, in the following cases,
a new copy will always be made, even if `copy=False`:


– If X is not an array of floating values;
– If X is sparse and `missing_values=0`;
– If `axis=0` and X is encoded as a CSR matrix;
– If `axis=1` and X is encoded as a CSC matrix.
在这里涉及到了一个所谓的CSR matrix,也就是CSR矩阵储存方式

所必须要说的是关于稀疏矩阵,稀疏矩阵原本是用来描述在一个矩阵当中0元素远多于非0元素,而且排布毫无规律,与稠密矩阵正好相反,对于矩阵的稀疏性量化表示可以用0元素counts除以总的元素counts,得到一个分式,在机器学习当中,稀疏矩阵运用的十分广泛且平凡,因为很多0元素的矩阵计算起来非常方便,计算量小

由于稀疏矩阵的特性,如果在内存中分配和稠密矩阵同样的内存给他的话,那么会导致内存极度浪费,毕竟我们没有必要拿那么多宝贵的内存来保证所有0值的储存,我们只需要知道哪里非0即可,于是我们产生了下面几种压缩储存矩阵的方式:


CSR矩阵:

一个稀疏矩阵通常有很多的0,我们学会记录它的三个参数作为三个列表进行储存,提高内存复用率,三个参数分别是:values,columns,rows

比如这个矩阵:

[[1 0 0 0 1 0]
 [0 0 1 0 1 0]
 [0 0 2 0 3 0]
 [0 2 0 3 0 4]]

在经过scipy的csr_matrix方法进行变换矩阵之后,其形式如下

前面的元组表示行和列,而后面的数字表示对应的值,这就是利用三个参数压缩储存矩阵的方式,由于它的行值有不少重复值,也就是元组的第一个元素,所以他的空间并不是最优设置,于是有一种相对优越的方法,就是我们所说的完整的csr矩阵储存方式,就是利用索引获取每一个行元素的第一个值,记录索引,总共有n个行,那么把行元素的列表替换成有n+1个元素的列表记录值,多出来的一个记录非零元素个数,具体记录方式,如下:

将行的列表换成[0,2,4,6,9]

  (0, 0)        1
  (0, 4)        1
  (1, 2)        1
  (1, 4)        1
  (2, 2)        2
  (2, 4)        3
  (3, 1)        2
  (3, 3)        3
  (3, 5)        4
在[0,2,4,6,9]中

0表示第一行的第一个元素在values列表中的索引为0

2表示第二行的第一个元素在values列表中的索引是2

以此类推,获得csr完整矩阵压缩方法。

CSC矩阵:

看看python官方的构造:

 

# 示例解读


indptr = np.array([0, 2, 3, 6])

 

indices = np.array([0, 2, 2, 0, 1, 2])

 

data = np.array([1, 2, 3, 4, 5, 6])

 

csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()

 

array([[1, 0, 4],

 

[0, 0, 5],

 

[2, 3, 6]])

 

 

像是在indices中的元素表示的是非0元素所在行
而利用indptr的索引

例如第0列,

非零元素所在行为indices[indptr[0],indptr[0+1]]

数据是data[indptr[0]:indptr[0+1]]

所以在矩阵中,第0行和第2行为非0(取自indices的[0,2]前两个元素)

数值为data里面对应的元素。

csc矩阵稍微有点复杂难以理解

不过深入观察一下

是可以渗透的。

 

文:张一极
更多人工智能相关文章:http://siligence.ai

收藏
点赞
0
个赞
TOP
切换版块