前记
最近在做机器学习数据的预处理,用到了一些矩阵的处理,非常方便简单,在此记录一下。
主要是numpy包的使用。
矩阵初始化
mixtraxs = numpy.zeros([3, 3]) #sentence 矩阵初始化 3×3的0矩阵
矩阵的点乘
m = numpy.array(wordvec_column) //矩阵竖列
n = numpy.array(wordvec_row) //矩阵横列
lm = numpy.sqrt(m.dot(m)) //竖列的绝对值
ln = numpy.sqrt(n.dot(n)) //横列的绝对值
result = numpy.dot(m, n) //点乘
cos = result / (lm * ln) //cos值
矩阵的拓展
b = [[2,2,3],[1,2,5],[2,4,6]]
a = [1,1,1]
mixtraxs = numpy.row_stack((a, b)) //加行
print mixtraxs
result :
[[1 1 1]
[2 2 3]
[1 2 5]
[2 4 6]]
b = [[2,2,3],[1,2,5],[2,4,6]]
a = [1,1,1]
mixtraxs = numpy.column_stack((a, b)) //加列
print mixtraxs
result:
[[1 2 2 3]
[1 1 2 5]
[1 2 4 6]]