接下來,直接給出大家響應的代碼,并對每一行進行標注,希望能夠幫到大家。
需要用到的是庫是。numpy 、sklearn。
#導入相應的庫(對數據庫進行切分需要用到的庫是sklearn.model_selection 中的 train_test_split)
import numpy as np
from sklearn.model_selection import train_test_split
#首先,讀取.CSV文件成矩陣的形式。
my_matrix = np.loadtxt(open("xxxxxx.csv"),delimiter=",",skiprows=0)
#對于矩陣而言,將矩陣倒數第一列之前的數值給了X(輸入數據),將矩陣大最后一列的數值給了y(標簽)
X, y = my_matrix[:,:-1],my_matrix[:,-1]
#利用train_test_split方法,將X,y隨機劃分問,訓練集(X_train),訓練集標簽(X_test),測試卷(y_train),
測試集標簽(y_test),安訓練集:測試集=7:3的
概率劃分,到此步驟,可以直接對數據進行處理
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
#此步驟,是為了將訓練集與數據集的數據分別保存為CSV文件
#np.column_stack將兩個矩陣進行組合連接
train= np.column_stack((X_train,y_train))
#numpy.savetxt 將txt文件保存為。csv結尾的文件
numpy.savetxt('train_usual.csv',train, delimiter = ',')
test = np.column_stack((X_test, y_test))
numpy.savetxt('test_usual.csv', test, delimiter = ',')
完整沒解釋的代碼部分為
import numpy as np
from sklearn.model_selection import train_test_split
my_matrix = np.loadtxt(open("xxxxx.csv"),delimiter=",",skiprows=0)
X, y = my_matrix[:,:-1],my_matrix[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
train= np.column_stack((X_train,y_train))
numpy.savetxt('train_usual.csv',train, delimiter = ',')
test = np.column_stack((X_test, y_test))
numpy.savetxt('test_usual.csv', test, delimiter = ',')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

