Hexo

凡事预则立,不预则废


  • Home

  • Tags

  • Archives

  • Navigation

  • Search

Numpy——transpose函数

用于矩阵维度的转换


transpose函数的使用

  • 用法说明

    1
    nums.transpose(dims)
    • nums为np.ndarray类型的对象
    • dims为表示维度排列的tuple
      • 对于二维矩阵,只有0, 1两个维度,所以只能是(0,1), (1,0)
      • 三维矩阵,有0,1,2三个维度,所以可以是是(0,1,2)的所有全排列,共6个

简单易懂的解释

  • 只改变维度, 每一维度对应的数据不会变, 比如图片的shape为: (28, 28, 3),那么无论怎么变化维度, 变换后的数据中维度大小为3的那个维度都是表示通道, (28,28)总是表示图片的每个通道的数据
  • 原始numpy.ndarray,shape为(2,3,4)
    • .transpose((0,1,2)): shape为(2,3,4), 不变
    • .transpose((1,0,2)): shape为(3,2,4)
    • .transpose((2,1,0)): shape为(4,3,2)
  • 测试代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    import numpy as np

    origin = np.random.random_integers(1, 100, (2,3,4))
    print("-"*20 + "origin")
    print(origin)
    print("-"*20 + "(0,1,2)")
    print(origin.transpose((0,1,2)))
    print("-"*20 + "(1,0,2)")
    print(origin.transpose((1,0,2)))
    print("-"*20 + "(2,1,0)")
    print(origin.transpose((2,1,0)))

    # Output:
    --------------------origin
    [[[50 36 80 53]
    [45 45 94 91]
    [49 29 69 53]]

    [[85 83 61 18]
    [16 89 80 60]
    [99 13 36 40]]]
    --------------------(0,1,2)
    [[[50 36 80 53]
    [45 45 94 91]
    [49 29 69 53]]

    [[85 83 61 18]
    [16 89 80 60]
    [99 13 36 40]]]
    --------------------(1,0,2)
    [[[50 36 80 53]
    [85 83 61 18]]

    [[45 45 94 91]
    [16 89 80 60]]

    [[49 29 69 53]
    [99 13 36 40]]]
    --------------------(2,1,0)
    [[[50 85]
    [45 16]
    [49 99]]

    [[36 83]
    [45 89]
    [29 13]]

    [[80 61]
    [94 80]
    [69 36]]

    [[53 18]
    [91 60]
    [53 40]]]

与reshape的区别

reshape的用法:
1
nums.reshape(shape)
* `nums`为`np.ndarray`类型的对象
* `shape`为表示维度排列的`tuple`
    * `shape`必须与原始`nums.shape`兼容,即每个`shape`元素相乘的结果相等
  • 从numpy中矩阵的存储开始讲起,numpy存储数据是C++的方式存储为一行的,然后分为不同shape以不同方式进行索引
  • reshape相当于是把原始数据先还原成一行(实际上没操作),然后再转变成指定shape的数据,本质上数据存储没变化,只是shape变了,以后索引方式也就变了
区别
  • reshape修改的只是shape,每个维度的意义变了
  • transpose修改的只是维度,同时shape跟着变化而已,每个维度的顺序变了,但是意义不会变

Sklearn——CountVectorizer使用介绍


导入

1
from sklearn.feature_extraction.text import CountVectorizer

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 新建一个CountVectorizer对象,以下简称CV对象
vectoerizer = CountVectorizer(min_df=min_df, max_df=max_df, stop_words=stop_words, token_pattern=r"(?u)\b[\w-][\w-]+\b")

# 使用文本集合(一个文本的列表)训练当前CountVectorizer对象
# texts = ["this is a text", "this is another text"]
vectoerizer.fit(texts)

# 训练后可以读取当前CV对象信息
bag_of_words = vectoerizer.get_feature_names()

# 使用CV对象生成任意文本集合的信息,返回对象是文档到字典的数组统计(统计单词数量)
# 这里的texts不必要是之前使用过的,可以是新的文本集合,但是词典已经确定,不能再拓展了,不存在字典中的词直接忽略
X = vectoerizer.transform(texts)

# 使用X对象,toarray()将返回 term/token counts 矩阵,可直接用于TfidfTransformer等对象的训练
X.toarray()

重要参数说明

  • min_df: 用于排除出现次数太少的terms

    • min_df = 0.01 意味着将忽略出现在少于%1的文档中的词
    • min_df = 5 意味着将忽略只出现在5篇以下文档中的词,不包括5篇
  • max_df:

    • max_df = 0.50 意味着将忽略出现在多于%50的文档中的词
    • max_df = 25 意味着将忽略出现在25篇以上文档中的词,不包括25篇
1…266267268…352
San Ye

San Ye

Stay Hungry. Stay Foolish.

704 posts
53 tags
© 2026 San Ye
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4