本文整理汇总了Python中sklearn.ensemble.forest.RandomForestClassifier.fit方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestClassifier.fit方法的具体用法?Python RandomForestClassifier.fit怎么用?Python RandomForestClassifier.fit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.forest.RandomForestClassifier
的用法示例。
在下文中一共展示了RandomForestClassifier.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: enemy_detection_clf
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def enemy_detection_clf():
chars = np.array(['warrior', 'warlock', 'mage', 'druid', 'rogue', 'shaman', 'paladin', 'priest', 'hunter'])
data = []
target = []
for c in chars:
p = path('images/character/new/black')
for f in os.listdir(p+'/'+c):
img = Image.open(p+'/'+c+'/'+f)
w, h = img.size
pixel = img.load()
tmp = []
for y in range(h):
for x in range(w):
tmp.append(np.float(pixel[x,y] / 255))
target.append(np.str(c))
data.append(np.array(tmp))
data = np.array(data)
#image = data.view()
#image.shape = (-1, 22, 30)
#clf = svm.SVC(gamma = 0.001)
clf = RandomForestClassifier()
clf.fit(data, target)
return clf
示例2: Random_Forest
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def Random_Forest(x_train, Y_train,n_estimators=10, criterion='gini', max_depth=None, min_samples_split=2,
min_samples_leaf=1, max_features='auto', bootstrap=True, oob_score=False, n_jobs=1,
random_state=None, verbose=0, min_density=None, compute_importances=None, *args):
clf = RandomForestClassifier()
clf.fit(x_train,Y_train)
return clf
示例3: rforest_classify
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def rforest_classify(X,Y):
#clf = RandomForestClassifier(criterion='gini',max_features=7,n_estimators=100,n_jobs=3,min_samples_leaf=5)
clf = RandomForestClassifier(n_estimators=500, \
criterion='entropy', max_depth=None, min_samples_split=2, min_samples_leaf=1 \
,max_features='auto', bootstrap=False, oob_score=False, n_jobs=-1, min_density=None)
clf.fit(X,Y)
return clf
示例4: train_rf
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def train_rf(train_vec, train_label):
from sklearn.ensemble.forest import RandomForestClassifier as RFC
# rfrclf = RFR(n_estimators=1001)
# rfrclf.fit(train_vec, train_label)
# print rfrclf.feature_importances_
trfclf = RFC(n_estimators=1001)
trfclf.fit(train_vec, train_label)
# print rfclf.feature_importances_
return trfclf
示例5: main
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def main(args):
if args.analyse != None:
train_data_x, test_data_x,train_data_y, test_data_y = process_data(args.analyse)
RT = RandomForestClassifier(n_estimators=100)
RT.fit(train_data_x, train_data_y)
print RT.score(test_data_x, test_data_y)
return
示例6: my_digits
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def my_digits():
digits = _data()
n_samples = len(digits.images)
datas = digits.images.reshape((n_samples, -1))
classifier = RandomForestClassifier()
classifier.fit(datas, digits.target)
return classifier
示例7: RandomForestClassifer
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def RandomForestClassifer(self):
'''
Function to do RandomForest Classifer.
'''
train_Array = self.titanic_train_frame.values
self.test_Array = self.titanic_test_frame.values
randomForest = RandomForestClassifier(n_estimators = 100, n_jobs = -1)
randomForest.fit(train_Array[0::,1::],train_Array[0::,0])
self.predicted_probability = randomForest.predict(self.test_Array[0::,0::])
self.predicted_probability_list = self.predicted_probability.tolist()
开发者ID:malaikannan,项目名称:Kaggle_TitanicPredictionChallenge,代码行数:13,代码来源:TitanicPrediction_LogisticRegression.py
示例8: do_training
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def do_training(processed_train_csv_file):
## Processed train samples reading
# read saved processed train samples from the given csv file
processed_train_samples = pd.read_csv(processed_train_csv_file)
# inf to nan
processed_train_samples = processed_train_samples.replace([np.inf, -np.inf], np.nan)
# nan to 0
processed_train_samples = processed_train_samples.fillna(value=0)
processed_train_samples_index_lst = processed_train_samples.index.tolist()
# 之前排过序,这里shuffle一下,效果更好
random.shuffle(processed_train_samples_index_lst)
# organize new train samples and targets
shuffled_train_samples = processed_train_samples.ix[processed_train_samples_index_lst]
col_names = shuffled_train_samples.columns.tolist()
col_names.remove("booking_bool")
features = shuffled_train_samples[col_names].values
labels = shuffled_train_samples['booking_bool'].values
# Model training
# 1 Random Forest Classifier
print("Training Random Forest Classifier")
rf_classifier = RandomForestClassifier(n_estimators=150,
verbose=2,
n_jobs=-1,
min_samples_split=10)
rf_classifier.fit(features, labels)
print("Saving the Random Forest Classifier")
data_io.save_model(rf_classifier, model_name='rf_classifier.pkl')
# 2 Gradient Boosting Classifier
print("Gradient Boosting Classifier")
gb_classifier = GradientBoostingClassifier(n_estimators=150,
verbose=2,
learning_rate=0.1,
min_samples_split=10)
gb_classifier.fit(features, labels)
print("Saving the Gradient Boosting Classifier")
data_io.save_model(gb_classifier, model_name='gb_classifier.pkl')
# 3 SGD Classifier
print("SGD Classifier")
sgd_classifier = SGDClassifier(loss="modified_huber", verbose=2,
n_jobs=-1)
sgd_classifier.fit(features, labels)
print("saved the SGD Classifier")
data_io.save_model(sgd_classifier, model_name='sgd_classifier.pkl')
示例9: RF
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def RF(pth):
train_desc=np.load(pth+'/training_features.npy')
nbr_occurences = np.sum( (train_desc > 0) * 1, axis = 0)
idf = np.array(np.log((1.0*len(image_paths)+1) / (1.0*nbr_occurences + 1)), 'float32')
# Scaling the words
stdSlr = StandardScaler().fit(train_desc)
train_desc = stdSlr.transform(train_desc)
modelRF=RandomForestClassifier(n_estimators=10,
max_depth=5,max_features=1,random_state=0)
modelRF.fit(train_desc,np.array(train_labels))
joblib.dump((modelRF, img_classes, stdSlr), pth+"/rf-bof.pkl", compress=3)
test(pth, "rf-")
示例10: try_model
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def try_model(train):
print(train.shape)
features = ["phone_brand", "device_model", "event_count", "action_radius_max", "medianTime", "minTime", "maxTime", "weekday", "appcounts1"]
encoder = LabelEncoder()
train["group"] = encoder.fit_transform(train["group"].values)
rf = RandomForestClassifier(n_estimators=50, max_depth=15, max_features=6, bootstrap=True, n_jobs=4, random_state=2016, class_weight=None)
rf.fit(train[features].values, train["group"].values)
feature_importance(rf, features)
skf = StratifiedKFold(train["group"].values, n_folds=5, shuffle=True, random_state=2016)
scores = cross_val_score(rf, train[features].values, train["group"].values, scoring="log_loss", cv=skf, n_jobs=1)
print(scores)
print("RF Score: %0.5f" %(-scores.mean())) # RF Score: 2.39884
示例11: just_pred
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def just_pred(x, y):
xlen = len(x)
i = range(xlen)
np.random.shuffle(i)
trainpct = 0.7
trainlen = int(trainpct * xlen)
testlen = xlen - trainlen
xtrain = x.ix[:trainlen,:]
ytrain = y.ix[:trainlen]
xtest = x.ix[trainlen:,:]
ytest = y.ix[trainlen:]
rf = RandomForestClassifier()
rf.fit(xtrain, ytrain)
ypred = rf.predict(xtest)
return ytest, ypred
示例12: crossval
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def crossval(x, y, k=5):
for i in range(k):
i = range(len(X))
np.random.shuffle(i)
xlen = len(x)
trainpct = 0.7
trainlen = int(trainpct * xlen)
testlen = xlen - trainlen
xtrain = x.ix[:trainlen,:]
ytrain = y.ix[:trainlen]
xtest = x.ix[trainlen:,:]
ytest = y.ix[trainlen:]
rf = RandomForestClassifier()
rf.fit(xtrain, ytrain)
ypred = rf.predict(xtest)
print ypred
示例13: test_RandomForest
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def test_RandomForest(self):
X = [[0, 1], [1, 1]]
Y = [0, 1]
regression = RandomForestClassifier(n_estimators=10)
regression = regression.fit(X, Y)
regression.predict_proba(X)
示例14: model_pred
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def model_pred(trainX,trainY,testX,model_type):
if model_type == "rf":
clf = RandomForestClassifier(n_estimators = 500,n_jobs = 20)
clf.fit(trainX,trainY)
pred = clf.predict(testX)
if model_type == "gbdt":
clf = GradientBoostingClassifier(n_estimators=6,learning_rate=0.9,random_state=0)
clf.fit(trainX,trainY)
pred = clf.predict(testX)
if model_type == "fusion":
prob = np.zeros(len(testX))
params = [100,200,300,400,500]
for param in params:
clf = RandomForestClassifier(n_estimators = param,n_jobs = 20,bootstrap=True)
clf.fit(trainX,trainY)
prob += clf.predict(testX)
'''
params = [1,2,3,4,5,6,7,8,9,10]
for param in params:
clf = GradientBoostingClassifier(n_estimators=param,learning_rate=0.9,random_state=0)
clf.fit(trainX,trainY)
prob += clf.predict(testX)
'''
pred = list(prob >= 3)
print "the pos rate is:",float(sum(pred))/len(pred)
return pred
示例15: initDecTrees
# 需要导入模块: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.forest.RandomForestClassifier import fit [as 别名]
def initDecTrees(self, path):
for filename in os.listdir(path):
if filename=='train.csv':
with open(os.path.join(path,filename)) as infile:
f = csv.reader(infile)
aux = f.next() # skip the header
x = []
y = []
for line in f:
if size(line) > 1:
if self.option == 1:
data = [converter(line[2]), converter(line[3]), converter(line[4]), converter(line[7]), converter(line[9])]
y.append(converter(line[6]))
x.append(data)
elif self.option == 2:
auxDeputy = fetchDeputyParty(line[2])
data = [converter(line[2]), converter(line[3]), converter(line[4]), converter(line[7]), converter(line[9]), encodeParty(auxDeputy['party']), encodeState(auxDeputy['state'])]
y.append(converter(line[6]))
x.append(data)
clf = RandomForestClassifier(n_estimators=5)
clf.fit(x, y)
return clf