DataWhale 組隊學習數據挖掘實踐 任務三

任務3 - 建模

用邏輯迴歸、svm和決策樹;隨機森林和XGBoost進行模型構建,評分方式任意,如準確率等。(不需要考慮模型調參)

一、 使用邏輯迴歸

from sklearn.linear_model import SGDClassifier

1  初始化模型

log_reg = SGDClassifier(loss='log',penalty='l2',max_iter = 200)

2  訓練

log_reg.fit(X_train, y_train)

3  預測

y_pred_lgr = log_reg.predict_proba(X_test)
print (y_pred_lgr[:5])

4  評估

from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import classification_report,confusion_matrix

train 和 test 的精確度

acc_lgr_train = accuracy_score(y_train, y_train_lgr)
acc_lgr = accuracy_score(y_test, y_pred_lgr)

print('train acc:%.2f, test acc:%.2f'%(acc_lgr_train, acc_lgr),'\n')

 

 

二、 使用支持向量機 SVM

from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline

1  初始化模型

SVC, PCA, make_pipeline

pca = PCA(n_components = 13, whiten=True)
svc = SVC(kernel='rbf', class_weight='balanced',C=3)
model_svc = make_pipeline(pca, svc)

2  訓練

GridSearchCV

# 嘗試各個svc的C參數和gamma參數

from sklearn.model_selection import GridSearchCV
param_grid = {'svc__C':[3,4],
              'svc__gamma':[5,10]}
grid = GridSearchCV(model_svc, param_grid)
grid.fit(X_train, y_train)

print(grid.best_params_)

或者 用model_svc.fit(x,y)

3  預測

4  評估

acc_svm_train = accuracy_score(y_train, y_pred_svc_train)
acc_svm = accuracy_score(y_test, y_pred_svc)

print('SVM train acc:{0:.4f}, SVM test acc:{1:.4f}'\
      .format(acc_svm_train, acc_svm))

看看多少個0類結果

len(y_pred_svc[y_pred_svc==0])

準確性

precision, recall, F1, _ = precision_recall_fscore_support(y_test, y_pred_svc, average="binary")
# precision, recall, F1, _ = precision_recall_fscore_support(y_train, y_pred_svc_train, average="binary")


print ("precision: {0:.2f}. recall: {1:.2f}, F1: {2:.2f}"
       .format(precision, recall, F1))

 

 

三、 隨機森林

# 初始化隨機森林
forest = RandomForestClassifier(
    n_estimators=10, criterion="entropy",
    max_depth=8,
    min_samples_leaf=2)

# 訓練
forest.fit(X_train, y_train)

# 預測
y_pred_train = forest.predict(X_train)
y_pred = forest.predict(X_test)

# 評估
# 準確率

acc_dtree_train = accuracy_score(y_train, y_pred_train)
acc_tree = accuracy_score(y_test, y_pred)

print('train acc:%.2f, test acc:%.2f'%(acc_dtree_train, acc_tree),'\n')

 

 

 

 

 

 

 

 

 

--- End ---

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章