Original_Data(原始數據)
因為程序是為實現對純數值型Excel文檔進行導入並生成矩陣,因此有必要對第五列文本值進行刪除處理。
Import_Data(數據導入)
import numpy as np
import xlrd
def import_excel_matrix(path):
table = xlrd.open_workbook(path).sheets()[0] # 獲取第一個sheet表
row = table.nrows # 獲取行
col = table.ncols # 獲取列
datamatrix = np.zeros((row, col)) # 生成一個nrows行*ncols列的初始矩陣
for i in range(col): # 對列遍歷
cols = np.matrix(table.col_values(x)) # 把list轉換為矩陣進行矩陣操作
datamatrix[:, x] = cols # 按列把數據存進矩陣中
return data_matrix
data_file = u'Path' # Excel文件存儲位置
import_excel_matrix(data_file)