在console里换行输入命令:ctrl+enter
change list to matrix: np.arrary([1,2,3])
** means exponentiation, np.arrary([1,2,3])**2=[1,4,6]
20180901
Shape
一维数组或list: np.shape(a)会返回(integer, )
For example:
a=np.zeros([3])
print(a)
np.shape(a)
output:
[0. 0. 0.]
(3,)
所以,看到shape(n,)就要自然联想到一维数组
二维及以上维度的数组:
Shape(a)会返回(integer, integer,…) 很正常
20180903
def excel2m(path):
data = xlrd.open_workbook(path)
table = data.sheets()[0]
nrows = table.nrows # 行数
ncols = table.ncols # 列数
datamatrix = np.zeros((nrows, ncols))
for x in range(1,ncols):
cols = table.col_values(x)
cols1 = np.matrix(cols) # 把list转换为矩阵进行矩阵操作
datamatrix[:, x-1] = cols1 # 把数据进行存储
return datamatrix
path=u'E:/master/project/20180413figures_from yi/20180415/1.xls'
def file2list(filename):
fr = open(filename)
array = fr.readlines() #以文件中的每行为一个元素,形成一个list列表
num = len(array)
returnMat = np.zeros((num,2))#初始化元素为0的,行号数个列表,其中每个元素仍是列表,元素数是3,在此表示矩阵
index = 0
for line in array:
line = line.strip()#去掉一行后的回车符号
linelist = line.split(',')#将一行根据分割符,划分成多个元素的列表
returnMat[index,:] = linelist[9:11]#向矩阵赋值,注意这种赋值方式比较笨拙
index +=1
return returnMat