MachineLearning中numpy 常用操作技巧

数组移位

import numpy as np
a = np.arange(10)
b = np.roll(a, -1)
print(a)
print(b)
'''
[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9 0]
'''

筛选array中出现次数最多的元素

c = np.array([1,2,5,9,9,9,3])
a = np.bincount(c)
# 它返回的是 [0,序列最大值] 在这个array中出现的次数。
d = np.argmax(np.bincount(c))
index = list(c).index(d)
'''
a:[0 1 1 1 0 1 0 0 0 3]
d:9
index:3
'''

删除某列数据

dataset=[[1,2,3],[2,3,4],[4,5,6]] 
import numpy as np 
dataset = np.delete(dataset, -1, axis=1) 
''' 
array([[1, 2], 
  [2, 3], 
  [4, 5]])
'''

快速判断两个N*1的矩阵是否相等

import numpy as np
a = np.array([
    [1], [3], [3]
])
b = np.array([
    [1], [2], [3]
])
m = a.shape[0]
aggErrors = np.multiply(np.mat(a) != np.mat(b), np.ones(a.shape))
print('wrong index:', aggErrors)
print('wrong nums:', aggErrors.sum())
'''
wrong index: 
[[0.]
 [1.]
 [0.]]
wrong nums: 1.0
'''

生成满足多元高斯分布的数据

import numpy as np
# 各个维度的均值
mu1_fact = (0, 0, 0)
# 协方差矩阵
cov_fact = np.identity(3)
positive_data = np.random.multivariate_normal(mu1_fact, cov_fact, 400)
'''
[[-1.84565134  0.64334784  0.07752541]
 [ 0.42888124  1.26704032  1.0359175 ]
 [-1.73487344  0.12276851 -0.50163476]
 ...
 [-0.82389061 -0.35710593 -0.01775249]
 [ 0.49466037  0.03923352  0.69746271]
 [-0.83899052 -0.73179449  0.60391842]]
'''

将维度为(3,)的一维ndarray数组扩展为(n*3)维的矩阵

import numpy as np

n = 3
y1 = np.array([
    2, 2, 5
])
print(np.tile(y1, (n, 1)))
'''
[[2 2 5]
 [2 2 5]
 [2 2 5]]
'''

你可能感兴趣的:(MachineLearning中numpy 常用操作技巧)