-
GradientBoostingClassifierTOcorrelationTOheatmapCodingPython 2022. 1. 8. 21:41
gradient boosting regression tree는 여러 개의 decision tree를 묶어 강력한 model을 만드는 ensemble기법입니다.
random forest와 달리 gradient boosting model은 이전 tree의 오차를 보완하는 방식으로 tree를 만듭니다.
gradient boosting은 무작위성이 없어 powerful한 pre-pruning이 사용되며
1~5 정도 깊이의 tree를 사용하므로 메모리를 적게 사용하고 예측도 빠릅니다.
# clf9 = Pipeline([
# ('feature_selection', SelectFromModel(LinearSVC(penalty="l1"))),
# ('classification', RandomForestClassifier())
# ])
ett999 = ett3.reshape(-1,1)
yt999 = yt3.reshape(-1,1)
gbrt = GradientBoostingClassifier(learning_rate=0.01, random_state=0)
gbrt.fit(xt3, yt3)
print("결정 함수의 결과 형태: {}".format(gbrt.decision_function(ett999).shape))#2차원 요구--> ett3
# plot the first few entries of the decision function
print("결정 함수 결과:\n{}".format(gbrt.decision_function(ett999)[:6, :]))
print('/*/'*90)
print("가장 큰 결정 함수의 인덱스:\n{}".format(
np.argmax(gbrt.decision_function(ett999), axis=1)))
print("예측:\n{}".format(gbrt.predict(ett999)))
# 가장 큰 결정 함수의 인덱스:
# [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
# 0]
# 예측:
# [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
# 0]
print('/*/'*90)
# predict_proba 결과 중 앞부분 일부를 확인합니다
print("예측 확률:\n{}".format(gbrt.predict_proba(ett999)[:6]))
# 행 방향으로 확률을 더하면 1 이 됩니다
print("합: {}".format(gbrt.predict_proba(ett999)[:6].sum(axis=1)))axis =1 이면 행단위로 더한 것!!!!!
# 예측 확률:
# [[0.10664722 0.7840248 0.10932798]
# [0.78880668 0.10599243 0.10520089]
# [0.10231173 0.10822274 0.78946553]
# [0.10664722 0.7840248 0.10932798]
# [0.10825347 0.66344934 0.22829719]
# [0.78880668 0.10599243 0.10520089]]
# 합: [1. 1. 1. 1. 1. 1.]
print('/*/'*90)
x8x8 = pd.DataFrame(gbrt.decision_function(ett999))
x9x9 = x8x8.corr()
e9e9 = sns.heatmap(x9x9, cmap = 'viridis')
plt.show()
print('/*/'*90)l
learning rate = 0.02 일 때 아래사진
learning rate = 0.07 일 때 아래사진
learning rate = 0.14 일 때 아래사진
gradinet boosting에서 주요 parameter는 이전 트리의 오차를 얼마나 강하게 보정할 것인가를 제어하는
learning_rate가 있습니다.
learning_rate를 높이면 보정을 강하게 하기 때문에 복잡한 모델을 만듭니다.
learning rate = 0.7
learning rate = 1
learning rate = 1.7
learning rate = 9.7
아래 두 사진은 똑같은 learning rate 이지만
다른 사람과의 비교를 한 것이다.
예측 확률: [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]
예측: [62 62 62 62 62 73 73 73 73 73 70 70 68 68 68 52 76 62 62 62 62 73 73 70 62 70 73 70 68 68 52 68 76 62 62 62 62 62 73 70 62 73 70 70 68 68 68 68 76 62 62 62 62 62 70 70 73 70 70 70 68 68 68 52 62 62 62 62 62 73 73 70 73 70 70 70 68 52 68 52 76 76 76 62 62 62 62 62 62 62 62 73 70 70 68 68 63 76 76 76 76 76 76 76 76 76 62 62 62 62 62 73 76 76 76 76 76 76 62 62 62 62 62 62 62 73 70 70 62 62 62 62 62 73 70 70 62 70 70 70 68 68 68 49 62 62 62 62 73 70 70 70 70 70 70 68 68 52 52 52 76 62 62 62 62 62 62 73 73 70 70 70 68 52 52 52 63 63 76 76 76 76 76 76 76 62 62 62 62 62 62 73 63 63 63 63 63 63 63 76 76 76 76 76 76 76 76 62 63 63 76 76 76 76 76 76 76 76 62 62 62 62 62 62] 0.044642857142857144
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_multilabel_classification
from sklearn.multioutput import MultiOutputClassifier
# 데이터셋 로드하기
# [[your code]
data = rows
data2 = rows2
data9 = rows9
# 훈련용 데이터셋 나누기
# [[your code]
X_train = rows[:14]
X_test = rows[14:]
y_train = data9[:14]
y_test = data9[14:]
z_train = data9[:14]
print("y_train")
print(y_train)
print('-'*90)
print('-'*90)
print("X_train")
print(X_train)
# y_test2 = data2[:16]
# y_test2 = y_test2.reshape(16,4)
# 훈련하기
# [[your code]
print('-/'*90)
print('-/'*90)
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import make_classification
# X2, y2 = make_classification(n_samples=1000, n_features=4,
# n_informative=2, n_redundant=0,
# random_state=0, shuffle=False)
# clf = RandomForestClassifier(max_depth=2, random_state=0)
# clf.fit(X2, y2)
# print(X2)
# print('-/'*90)
# print('-/'*90)
xtt = np.array(X_train)
xt = xtt.reshape(-1,)
ytt = np.array(y_train) #numpy array 로 바꿈!!
yt = ytt.reshape(-1,) # numpy array 차원 축소!!! 1차원으로 만들어주기
ett = np.array(y_test)
ettt = ett.reshape(-1,)
zt = np.array(z_train)
print("xt : X_train 에 해당")
print(xt)
print('-'*90)
print("yt: y_train 에 해당 ")
print(yt)
print('-'*90)
print("ett : y_test 에 해당")
print(ettt)
print('-'*90)
xt = np.expand_dims(xt, axis=0) # 차원 확대
yt = np.expand_dims(yt, axis=0) # 차원 확대
np.expand_dims(xt, axis=0)
np.expand_dims(yt, axis=0)
print("xt")
print(xt)
print('-'*90)
print("yt")
print(yt)
print('-'*90)
print('-*'*90)
# clf = MultiOutputClassifier(KNeighborsClassifier()).fit(xt,yt) # 2차원 이상의 배열이 필요함...
knn = KNeighborsClassifier(n_neighbors=1)#여기에서 n_neighbers =1 이 중요!!! 입력 데이터 형태를 보면 1로 해야함
knn = MultiOutputClassifier(KNeighborsClassifier(n_neighbors=1)).fit(xt,yt) #2차원 필요
print("knn 예측치")
knnmaster1 = knn.predict(xt[-2:]) #We select the training set with the [:-1] Python syntax,
#which produces a new array that contains all but the last item from digits.data:
print("knmaster1")
print(knnmaster1)
print('-*'*90)
print("정답률=", knn.score(yt, xt)) ###########################
print('-*'*90)
model = RandomForestClassifier()
model.fit(xt, yt)
print('-'*90)
# 예측하기
# [[your code]
y_pred1 = model.predict(xt)
# 정답률 출력하기
# [[your code]
print(y_pred1)
print('-'*90)
decision_tree = DecisionTreeClassifier(random_state=1)
decision_tree.fit(xt, yt)
y_pred2 = decision_tree.predict(xt)
print("Decision Tree classifier 예측치")
print(y_pred2)
print('-'*90)
y_testt = np.transpose(y_test)
print(y_test)
print('-'*90)
#y_test = np.expand_dims(y_test, axis=0)
print('y_test')
print(y_test)
print('-'*90)
print('y_pred1')
print(y_pred1)
print('-'*90)
print('-'*90)
#svm 은 y 가 1차원이어야 한다고 한다.
yt2 = yt.reshape(-1,) # numpy array 차원 축소!!! 1차원으로 만들어주기
xt2 = xt.reshape(-1,)
xt3 = xt2.reshape(-1, 1)
yt3 = yt2.reshape(-1, 1) #조진호(사람2) 데이터
ett2 = ett.reshape(-1,)
ett3 = ett.reshape(-1, 1) #최주원(사람3) 데이터
zt3 = zt.reshape(-1,)
zt3 = zt.reshape(-1,1)
print("zt3")
print(zt3)
print('+++++'*60)
print("xt2 : xt를 reshape 으로(-1,) 한 결과 ")
print(xt2)
print(',=,='*90)
print("xt3 : xt2를 reshape 으로 (-1,1)한 결과 ")
print(xt3)
print('/./.'*90)
from sklearn import svm
print('-'*90)
print('-'*90)
print("ett3")
print(ett3)
ett4 =ett3.reshape(-1,)
yt33 = yt3.reshape(-1,) #차원축소
xt33 = xt3.reshape(-1,)
print('0-0-'*90)
print(ett4.shape)
print(yt3.shape)
yt4 = yt3.reshape(-1,)
ett3= ett3.reshape(-1,)
print(yt4)
print("ett4")
print(ett4)
yt5 = yt4[:96]
print("yt5")
print(yt5)
accuracy = accuracy_score(ett4, yt5) #같은 shape 이어야 함. accuracy 랑 다른게 있다.
print("정확도 계산중... ")
print(" 정확도는 다음과 같다 ")
print(accuracy*100)
# from sklearn.datasets import make_blobs
# import matplotlib.pyplot as plt
# X,y = make_multilabel_classification(n_classes= 3,random_state = 0)
# clf.predict(X[-2:])
# knn = KNeighborsClassifier(n_neighbors=3)
# classifier = MultiOutputClassifier(knn, n_jobs=-1)
# classifier.fit(y_test,y_pred)
# predictions = classifier.predict(y_train)
# classifier.score(y_true,np.array(y_train))
# from sklearn.linear_model import RidgeClassifierCV
# clf = RidgeClassifierCV().fit(X_train, y_train)
# y_score = clf.decision_function(X_train)
# roc_auc_score(y_test, y_train, average=None)
# from sklearn.datasets import make_multilabel_classification
# from sklearn.multioutput import MultiOutputClassifier
# from sklearn.linear_model import LogisticRegression
# from sklearn.metrics import roc_auc_score
# X, y = make_multilabel_classification(random_state=0)
# inner_clf = LogisticRegression(solver="liblinear", random_state=0)
# clf = MultiOutputClassifier(inner_clf).fit(X, y)
# y_score = np.transpose([y_pred[:, 1] for y_pred in clf.predict_proba(X)])
# roc_auc_score(y, y_score, average=None)
from sklearn.svm import LinearSVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import make_circles
from sklearn import datasets
import scipy.stats as stats
import seaborn as sns
# clf9 = Pipeline([
# ('feature_selection', SelectFromModel(LinearSVC(penalty="l1"))),
# ('classification', RandomForestClassifier())
# ])
ett999 = ett3.reshape(-1,1)
yt999 = yt3.reshape(-1,1)
gbrt = GradientBoostingClassifier(learning_rate=0.5, random_state=0)
gbrt.fit(xt3, yt3)
print("결정 함수의 결과 형태: {}".format(gbrt.decision_function(yt999).shape))#2차원 요구--> ett3
# plot the first few entries of the decision function
print("결정 함수 결과:\n{}".format(gbrt.decision_function(yt999)[:6, :]))
print('/*/'*90)
print("가장 큰 결정 함수의 인덱스:\n{}".format(
np.argmax(gbrt.decision_function(yt999), axis=1)))
print("예측:\n{}".format(gbrt.predict(yt999)))
print(gbrt.score(xt3,yt3))
# 가장 큰 결정 함수의 인덱스:
# [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
# 0]
# 예측:
# [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
# 0]
print('/*/'*90)
# predict_proba 결과 중 앞부분 일부를 확인합니다
print("예측 확률:\n{}".format(gbrt.predict_proba(yt999)[:9]))
# 행 방향으로 확률을 더하면 1 이 됩니다
print("합: {}".format(gbrt.predict_proba(yt999)[:9].sum(axis=1)))
# 예측 확률:
# [[0.10664722 0.7840248 0.10932798]
# [0.78880668 0.10599243 0.10520089]
# [0.10231173 0.10822274 0.78946553]
# [0.10664722 0.7840248 0.10932798]
# [0.10825347 0.66344934 0.22829719]
# [0.78880668 0.10599243 0.10520089]]
# 합: [1. 1. 1. 1. 1. 1.]
print('/*/'*90)
x8x8 = pd.DataFrame(gbrt.decision_function(ett999))
x9x9 = x8x8.corr()
e9e9 = sns.heatmap(x9x9, cmap = 'viridis')
# e0e0 = sns.scatterplot(xt33,yt33)
plt.show()
print('/*/'*90)
print(e0e0)
print('/*/'*90)
print(e99e99)
x88x88 = pd.DataFrame(gbrt.decision_function(yt999))
x99x99 = x88x88.corr()
e99e99 = sns.heatmap(x99x99, cmap = 'viridis')
# e0e0 = sns.scatterplot(xt33,yt33)
plt.show()
ax3 = fig.add_subplot(111, projection='3d')
ax3.scatter(ett4, xt33, yt33, c= s , marker='o', s=15, cmap='Greens')
xt39 = np.transpose(xt3)
yt39 = np.transpose(yt3)
ett39 = np.transpose(ett3)
zt39 = np.transpose(zt3)
print("zt39")
print(zt39.shape)
print(zt39)
s = [0.7*np.linalg.norm([a, b]) for a, b in zip(xt3, yt3)]
s = [a / max(s) for a in s] # scale
print("ett3.shape")
print(ett3.shape)
print("xt39.shape")
print(xt39.shape)
print(xt39)
ax= plt.scatter(xt39[:100], yt39[:100], c=s, s=30, cmap=plt.cm.Paired)
clf = make_pipeline(StandardScaler(),LinearSVC(random_state=0, tol=1e-5))
clf.fit(xt3,yt3) #2차원으로 fit X(왼쪽 xt3)에는 포인트가 있고 Y에는 해당 포인트가 속한 클래스가 있습니다.
# 초평면(Hyper-Plane) 표현
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
print("XX.shape")
print(XX.shape)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
print("xy.shape")
print(xy.shape)
xy = xy.reshape(-1,1)
xy1 = xy[:30]
xy1 = xy1.flatten()
xy1 = xy1[:899]
print("xy1")
print(xy1)
xy1 = xy1.reshape(-1,1)
xy2 = xy1[:30]
xy3 = xy2.flatten()
print("xy3")
print(xy3)
xy3 = xy3[:30]
xy99 = xy3.reshape(30,)
print("xy3")
print(xy3)
xy5= xy99.reshape(-1,1)
Z = clf.decision_function(xy5).reshape(30,30)
ax.contour(XX, YY, Z, colors='k', levels=[-1,0,1], alpha=0.2, linestyles=['--', '-', '--'])
# 지지벡터(Support Vector) 표현
ax.scatter(clf.support_vectors_[:,0], clf.support_vectors_[:,1], s=60, facecolors='r')
plt.show()
fig = plt.figure(figsize=(6, 6))
ax2 = fig.add_subplot(111, projection='3d')
ax2.scatter(xt39[:100], yt39, yt39[:100], c= s , marker='o', s=15, cmap='Greens')'CodingPython' 카테고리의 다른 글
tqqq 이번년도 50프로 하락 가능성 존재하지만... (3) 2022.01.14 얼굴인식에 대한 단상 (0) 2022.01.12 make_pipeline 의 문제점? 다양한 모델 (0) 2022.01.09 클래스, 힙 메모리, 스택 메모리 개념, 코딩도장 34.6 (0) 2022.01.05 dataframe to CNN (0) 2022.01.04