One - One Code All

Blog Content

python中pandas的dataframe和numpy的ndarray转换为矩阵

Python 统计学-科学计算   2013-10-10 21:53:04

pandas的dataframe转换为矩阵


# 转换为矩阵
diag_mat = np.mat(dataframe_mat.as_matrix())


numpy的array转换为矩阵用asmatrix()


ndarray转换成matrix

import numpy as np
from numpy import random,mat

r_arr=random.rand(4,4)
print('r_arr',r_arr)
r_mat=mat(r_arr)
print(r_mat.I)#求逆


mat1 = np.mat(A)     #数组转矩阵,不能用A.mat(),数组没有mat属性


array1 = mat1.getA()      #矩阵转数组,不能用getA(M),getA是属性,不是函数



上一篇:pandas读取csv文件使用第一列作为索引index_col=0
下一篇:Python时间,日期,时间戳之间转换

The minute you think of giving up, think of the reason why you held on so long.