SKlearning大部分的輸入數據都是M * N數組.
然而我們從數據庫或文件讀取得來的通常是Python內定的類型tuple或list
它們的優勢就不說了,但是直接把list或tuple構成的二維數組傳入scikit是會出問題的.
如:
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
下面貼上如何把list/tuple轉為scikit使用的array
首先, 准備數據如下:
讀取一行數據變為一維數組
conn = sql.connect('result_sale.db') conn.text_factory = str dataSet = conn.execute('select * from sampleData') tpRows = dataSet.fetchone() conn.close() print type(tpRows) print tpRows lstRows = list(tpRows) aryRows1 = np.array(lstRows) # 轉成數組 #aryRows2 = np.array(lstRows).reshape(1, -1) # 轉成1行N列 (二維數組) #aryRows3 = np.array(lstRows).reshape(-1, 1) # 轉成N行1列 (二維數組) print lstRows print aryRows1
輸入如下: 請留意輸入的不同點 :)
('00', '01', '02', '03', '04', '05', '06', '07', '08') (tuple) ['00', '01', '02', '03', '04', '05', '06', '07', '08'] (list) ['00' '01' '02' '03' '04' '05' '06' '07' '08'] (array) Process finished with exit code 0
一次性轉換整個數據集
conn = sql.connect('result_sale.db') conn.text_factory = str dataSet = conn.execute('select * from sampleData') tpRows = dataSet.fetchall() conn.close() aryRows1 = np.array(tpRows) # 轉成數組 #aryRows2 = np.array(tpRows).reshape(1, -1) # 轉成1行N列 (二維數組) #aryRows3 = np.array(tpRows).reshape(-1, 1) # 轉成N行1列 (二維數組) print aryRows1 #print aryRows2 #print aryRows3
輸入如下:
[['00' '01' '02' '03' '04' '05' '06' '07' '08'] ['10' '11' '12' '13' '14' '15' '16' '17' '18'] ['20' '21' '22' '23' '24' '25' '26' '27' '28'] ['30' '31' '32' '33' '34' '35' '36' '37' '38'] ['40' '41' '42' '43' '44' '45' '46' '47' '48'] ['50' '51' '52' '53' '54' '55' '56' '57' '58'] ['60' '61' '62' '63' '64' '65' '66' '67' '68'] ['70' '71' '72' '73' '74' '75' '76' '77' '78'] ['80' '81' '82' '83' '84' '85' '86' '87' '88']] Process finished with exit code 0
逐條紀錄轉換, 可以用下標來引用數組
conn = sql.connect('result_sale.db') conn.text_factory = str dataSet = conn.execute('select * from sampleData') tpRows = dataSet.fetchall() conn.close() #aryRows = np.zeros([len(tpRows), len(tpRows[0])]) aryRows = np.ones_like(tpRows) #亦可使用 empty, empty_like, zeros, zeros_like 等方法 j=0 for row in tpRows: aryRows[j][:] = row j += 1 print aryRows
輸入如下:
[['00' '01' '02' '03' '04' '05' '06' '07' '08'] ['10' '11' '12' '13' '14' '15' '16' '17' '18'] ['20' '21' '22' '23' '24' '25' '26' '27' '28'] ['30' '31' '32' '33' '34' '35' '36' '37' '38'] ['40' '41' '42' '43' '44' '45' '46' '47' '48'] ['50' '51' '52' '53' '54' '55' '56' '57' '58'] ['60' '61' '62' '63' '64' '65' '66' '67' '68'] ['70' '71' '72' '73' '74' '75' '76' '77' '78'] ['80' '81' '82' '83' '84' '85' '86' '87' '88']] Process finished with exit code 0