本文整理汇总了Python中sklearn.ensemble.RandomForestClassifier.n_classes_方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestClassifier.n_classes_方法的具体用法?Python RandomForestClassifier.n_classes_怎么用?Python RandomForestClassifier.n_classes_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestClassifier
的用法示例。
在下文中一共展示了RandomForestClassifier.n_classes_方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Train1
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import n_classes_ [as 别名]
def Train1(X, y):
rfc = RandomForestClassifier(n_estimators=10, oob_score=True)
rfc.n_classes_ = 3
model = rfc.fit(X, y)
return model
示例2: Train_Kfold
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import n_classes_ [as 别名]
def Train_Kfold(X, y, K):
#y = np.array(y)
kf = KFold(X.shape[0], n_folds = K)
record = {}
k = 0
for train_index, test_index in kf:
k = k + 1
rfc = RandomForestClassifier(n_estimators=5, oob_score=True)
rfc.n_classes_ = 3
model = rfc.fit(X[train_index], y[train_index])
pred = model.predict(X[test_index])
count = 0
#AB: predicted as "1" while really is "2"
AA, AB, AC, BA, BB, BC, CA, CB, CC = 0,0,0,0,0,0,0,0,0
TA, TB, TC, PA, PB, PC = 0,0,0,0,0,0
for i in range(len(pred)):
if pred[i] == '1' and y[test_index][i] == '1':
AA = AA + 1
PA = PA + 1
TA = TA + 1
if pred[i] == '1' and y[test_index][i] == '2':
AB = AB + 1
PA = PA + 1
TB = TB + 1
if pred[i] == '1' and y[test_index][i] == '3':
AC = AC + 1
PA = PA + 1
TC = TC + 1
if pred[i] == '2' and y[test_index][i] == '1':
BA = BA + 1
PB = PB + 1
TA = TA + 1
if pred[i] == '2' and y[test_index][i] == '2':
BB = BB + 1
PB = PB + 1
TB = TB + 1
if pred[i] == '2' and y[test_index][i] == '3':
BC = BC + 1
PB = PB + 1
TC = TC + 1
if pred[i] == '3' and y[test_index][i] == '1':
CA = CA + 1
PC = PC + 1
TA = TA + 1
if pred[i] == '3' and y[test_index][i] == '2':
CB = CB + 1
PC = PC + 1
TB = TB + 1
if pred[i] == '3' and y[test_index][i] == '3':
CC = CC + 1
PC = PC + 1
TC = TC + 1
if pred[i] != y[test_index][i]:
count = count + 1
record[str(k)] = [count, AA, AB, AC, BA, BB, BC, CA, CB, CC, TA, TB, TC, PA, PB, PC, len(pred)]
err, Aerr, Berr, Cerr = 0, 0, 0, 0
for key in record:
Aerr = Aerr + (record[key][2]+record[key][3])/float(record[key][13])
Berr = Berr + (record[key][4]+record[key][5])/float(record[key][14])
#Cerr = Cerr + (record[key][7]+record[key][8])/float(record[key][15])
err = err + record[key][0]/float(record[key][16])
err = err/float(K)
Aerr = err/float(K)
Berr = err/float(K)
#Cerr = err/float(K)
#err = float(count)/K
#AA, AB, AC, BA, BB, BC, CA, CB, CC = float(AA)/K, float(AB)/K, float(AC)/K, float(BA)/K, float(BB)/K, float(BC)/K, float(CA)/K, float(CB)/K, float(CC)/K
return (err, Aerr, Berr)