In [7]:
import pandas as pd
from sklearn.metrics import classification_report
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import PolynomialFeatures
In [2]:
df = pd.read_excel('06.dataset.xlsx')
In [3]:
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
sf = np.exp(x)
sf = sf/np.sum(sf, axis=0)
return sf
In [26]:
for i in range(6):
X = pd.concat([
df.ix[df['failure_bidding'] >= i, '공유자매수신고':'rdtuch_중로한면'],
df.ix[df['failure_bidding'] >= i, '가축사육제한구역':'현상변경허가 대상구역'],
df.ix[df['failure_bidding'] >= i, 'log_est_jiga'],
df.ix[df['failure_bidding'] >= i, 'area(m2)']
], axis = 1)
y = df[df['failure_bidding'] >= i]['failure_bidding'].copy()
y[y == i] = 0
y[y > i] = 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
pipe_rf = Pipeline([
('rf', RandomForestClassifier())
])
param_grid = [
{'rf__n_estimators' : np.arange(100, 150, 10),
'rf__criterion' : ['gini', 'entropy']}
]
gs = GridSearchCV(estimator = pipe_rf, param_grid = param_grid,
scoring = 'recall', cv = 10, n_jobs = -1)
gs = gs.fit(X_train, y_train)
model = RandomForestClassifier(n_estimators = gs.best_params_['rf__n_estimators'],
n_jobs = -1,
criterion = gs.best_params_['rf__criterion'])
pred = model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print('---------------- ',i,'회 유찰 매물 ----------------')
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
In [27]:
for i in range(6):
X = pd.concat([
df.ix[df['failure_bidding'] >= i, '공유자매수신고':'rdtuch_중로한면'],
df.ix[df['failure_bidding'] >= i, '가축사육제한구역':'현상변경허가 대상구역'],
df.ix[df['failure_bidding'] >= i, 'log_est_jiga'],
df.ix[df['failure_bidding'] >= i, 'area(m2)']
], axis = 1)
y = df[df['failure_bidding'] >= i]['failure_bidding'].copy()
y[y == i] = 0
y[y > i] = 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
model1 = BernoulliNB().fit(X_train.ix[:, '공유자매수신고':'현상변경허가 대상구역'], y_train)
model2 = GaussianNB().fit(pd.DataFrame(X_train.ix[:, 'log_est_jiga':'area(m2)']), y_train)
prob1 = model1.predict_proba(X_test.ix[:, '공유자매수신고':'현상변경허가 대상구역'])
prob2 = model2.predict_proba(pd.DataFrame(X_test.ix[:, 'log_est_jiga':'area(m2)']))
y_pred = np.zeros(len(prob1))
for j in range(len(prob1)):
y_pred[j] = np.argmax(softmax((prob1 * prob2)[j]))
y_pred = y_pred.reshape(-1, 1)
print('---------------- ',i,'회 유찰 매물 ----------------')
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
In [8]:
for i in range(6):
X = pd.concat([
df.ix[df['failure_bidding'] >= i, '공유자매수신고':'rdtuch_중로한면'],
df.ix[df['failure_bidding'] >= i, '가축사육제한구역':'현상변경허가 대상구역'],
df.ix[df['failure_bidding'] >= i, 'log_est_jiga'],
df.ix[df['failure_bidding'] >= i, 'area(m2)']
], axis = 1)
y = df[df['failure_bidding'] >= i]['failure_bidding'].copy()
y[y == i] = 0
y[y > i] = 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
pipe_rf = Pipeline([
('rf', RandomForestClassifier())
])
param_grid = [
{'rf__n_estimators' : np.arange(100, 150, 10),
'rf__criterion' : ['gini', 'entropy']}
]
gs = GridSearchCV(estimator = pipe_rf, param_grid = param_grid,
scoring = 'recall', cv = 10, n_jobs = -1)
gs = gs.fit(X_train, y_train)
model0 = RandomForestClassifier(n_estimators = gs.best_params_['rf__n_estimators'],
n_jobs = -1,
criterion = gs.best_params_['rf__criterion']).fit(X_train, y_train)
prob0 = model0.predict_proba(X_test)
model1 = BernoulliNB().fit(X_train.ix[:, '공유자매수신고':'현상변경허가 대상구역'], y_train)
prob1 = model1.predict_proba(X_test.ix[:, '공유자매수신고':'현상변경허가 대상구역'])
model2 = GaussianNB().fit(pd.DataFrame(X_train.ix[:, 'log_est_jiga':'area(m2)']), y_train)
prob2 = model2.predict_proba(pd.DataFrame(X_test.ix[:, 'log_est_jiga':'area(m2)']))
y_pred = np.zeros(len(prob1))
for j in range(len(prob1)):
y_pred[j] = np.argmax(softmax((prob0 * prob1 * prob2)[j]))
y_pred = y_pred.reshape(-1, 1)
print('---------------- ',i,'회 유찰 매물 ----------------')
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
In [ ]: