分類演算法:lr/rf/gbdt/adaboost
python包:sklearn
# -*- coding: utf-8 -*-
"""created on wed may 9 10:37:12 2018
比較不同分類演算法效果
分類演算法:lr/rf/gbdt/adaboost
@author: dell
"""from sklearn.linear_model import logisticregression
from sklearn.ensemble import gradientboostingclassifier
from sklearn.ensemble import randomforestclassifier
from sklearn.ensemble import adaboostclassifier
import pandas as pd
import numpy as np
# 讀入資料,資料格式為 user,feature,pv,label
data = pd.read_csv('e:\\creditcard\\rf_data\\train_500.csv') # 正樣本資料
data1 = pd.read_csv('e:\\creditcard\\rf_data\\train_1w.csv') # 負樣本資料
# 整理資料,將資料整理成 使用者-特徵 矩陣
grp=data.groupby(['user','feature'])
grp_1 = grp.agg(np.sum) # pv 累加
grp_2 = grp_1.reset_index() # 重置行索引
grp_3 = grp_2.pivot_table(index='user',columns='feature',values='pv') # 以使用者為行索引,特徵為列索引,pv為矩陣內容
grp_4 = grp_3.fillna(0) # 將na替換成0
# 獲取所有特徵名稱
features = grp_4.columns
# 將資料整理成 使用者-標籤 矩陣
user_label_1 = data[['user','label']].groupby(['user']).agg('max') # 標籤為"y"或"n",當使用者分組後,取較大值作為標籤
# 將標籤列加入 使用者-特徵 矩陣
grp_4['label'] = user_label_1['label']
# 劃分訓練集和測試集,隨機劃分,75%為訓練集
grp_4['is_train'] = np.random.uniform(0,1,len(grp_4)) <=0.75 # 生成0-1的隨機數,按0.75劃分
train,test = grp_4[grp_4['is_train']==true],grp_4[grp_4['is_train']==false] # 拆分測試集和訓練集
# 邏輯回歸
lr = logisticregression(random_state=10)
lr.fit(train[features],train['label']) # 訓練模型
label_predict1 = lr.predict(test[features]) # **測試集分類
result1 = pd.crosstab(test['label'],label_predict1) # 生成真實標籤-**標籤矩陣
print("lr")
print(result1)
print("")
# 隨機森林
clf = randomforestclassifier(n_jobs=2,n_estimators=10)
clf.fit(train[features],train['label'])
label_predict2 = clf.predict(test[features])
result2 = pd.crosstab(test['label'],label_predict2)
print("randomforst")
print(result2)
print("")
# gbdt
gbc = gradientboostingclassifier(random_state=10)
gbc.fit(train[features],train['label'])
label_predict3 = gbc.predict(test[features])
result3 = pd.crosstab(test['label'],label_predict3)
print("gbdt")
print(result3)
print("")
# adaboost
abc = adaboostclassifier()
abc.fit(train[features],train['label'])
label_predict4 = abc.predict(test[features])
result4 = pd.crosstab(test['label'],label_predict4)
print("adaboost")
print(result4)
# 將 分類錯誤的負樣本使用者 輸出到檔案
f = open('e:\\creditcard\\rf_data\\user2\\user_nf.csv','w');
test["pre"]=label_predictpreusers=test[(test["pre"]=='y')&(test['label']=='n')]
for i in preusers.index:
f.write(i+"\n")
f.close();
print("finished!!!")
結果:
runfile('d:/pythonspace/randomforst/lr_userfilter.py', wdir='d:/pythonspace/randomforst')
lrcol_0 n y
label
n 13280 20
y 66 94
randomforst
col_0 n y
label
n 13292 8
y 56 104
gbdt
col_0 n y
label
n 13285 15
y 57 103
adaboost
col_0 n y
label
n 13284 16
y 57 103
分類 KNN分類演算法之Python實現
knn稱為k最近鄰。對於待分類資料,它先計算出與其最相近的k個的樣本,然後判斷這k個樣本中最多的類標籤,並將待分類資料標記為這個最多的類標籤。python樣例 import numpy as np from sklearn.neighbors import kneighborsclassifier ...
機器學習python分類演算法
from pandas import read csv from sklearn.linear model import linearregression from sklearn.linear model import logisticregression from sklearn.model s...
python實現logistic分類演算法
最近在看吳恩達的機器學習課程,自己用python實現了其中的logistic演算法,並用梯度下降獲取最優值。logistic分類是乙個二分類問題,而我們的線性回歸函式 的取值在負無窮到正無窮之間,對於分類問題而言,我們希望假設函式的取值在0 1之間,因此logistic函式的假設函式需要改造一下 由...