国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 劃分數據集為訓練集和測試集的方法

2020-02-16 00:04:51
字體:
來源:轉載
供稿:網友

sklearn的cross_validation包中含有將數據集按照一定的比例,隨機劃分為訓練集和測試集的函數train_test_split

from sklearn.cross_validation import train_test_split#x為數據集的feature熟悉,y為label.x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3)

得到的x_train,y_train(x_test,y_test)的index對應的是x,y中被抽取到的序號。

若train_test_split傳入的是帶有label的數據,則如下代碼:

from sklearn.cross_validation import train_test_split#dat為數據集,含有feature和label.train, test = train_test_split(dat, test_size = 0.3)

train,test含有feature和label的。

自己寫了一個函數:

#X:含label的數據集:分割成訓練集和測試集#test_size:測試集占整個數據集的比例def trainTestSplit(X,test_size=0.3): X_num=X.shape[0] train_index=range(X_num) test_index=[] test_num=int(X_num*test_size) for i in range(test_num):  randomIndex=int(np.random.uniform(0,len(train_index)))  test_index.append(train_index[randomIndex])  del train_index[randomIndex] #train,test的index是抽取的數據集X的序號 train=X.ix[train_index]  test=X.ix[test_index] return train,test

以上這篇python 劃分數據集為訓練集和測試集的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城市| 乐至县| 喜德县| 东阳市| 合川市| 文登市| 昔阳县| 措勤县| 大埔区| 汝阳县| 静乐县| 武冈市| 莎车县| 南乐县| 贡觉县| 富川| 兴隆县| 钦州市| 新丰县| 梓潼县| 文昌市| 龙口市| 泉州市| 淮北市| 宾阳县| 葫芦岛市| 晋宁县| 沾化县| 图木舒克市| 钟山县| 左云县| 绵竹市| 彰化县| 牙克石市| 兴海县| 安仁县| 柳林县| 大理市| 平邑县| 三原县| 铁力市|