国产探花免费观看_亚洲丰满少妇自慰呻吟_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 劃分數據集為訓練集和測試集的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 满城县| 泌阳县| 东源县| 长汀县| 宁安市| 永康市| 安新县| 杭州市| 定州市| 萨迦县| 京山县| 芦溪县| 巨鹿县| 洛隆县| 屏东县| 镇康县| 宝坻区| 安泽县| 舞钢市| 晋宁县| 尖扎县| 景东| 定兴县| 鄂托克前旗| 北票市| 苏尼特右旗| 丰顺县| 庆元县| 新余市| 仁寿县| 五寨县| 玉林市| 凤城市| 黄龙县| 阿克陶县| 崇仁县| 大石桥市| 扎囊县| 甘孜| 陵水| 台前县|