Skip to content

Commit Live PR #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added q01_bagging/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added q01_bagging/__pycache__/build.cpython-36.pyc
Binary file not shown.
58 changes: 58 additions & 0 deletions q01_bagging/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %load q01_bagging/build.py
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
@@ -15,5 +16,62 @@


# Write your code here
# def bagging(X_train,X_test,y_train,y_test,n_est):

# dt = DecisionTreeClassifier()
# score_test = []
# score_train = []
# for x in range(1,50):

# bag = BaggingClassifier(base_estimator = dt, n_estimators = x,random_state=9, bootstrap= True, max_features=0.67,max_samples=0.67)

# bag.fit(X_train,y_train)
# score_test.append(bag.score(X_test,y_test))
# score_train.append(bag.score(X_train,y_train))


# plt.plot(score_test)
# plt.plot(score_train)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score

# Data Loading
dataframe = pd.read_csv('data/loan_prediction.csv')

X = dataframe.iloc[:, :-1]
y = dataframe.iloc[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=9)


# Write your code here

def bagging(X_train, X_test, y_train, y_test, n_est):
acc_train, acc_test = [], []
ranges = range(1, n_est, 2)
for n_est in ranges:
clf_bagging_20 = BaggingClassifier(DecisionTreeClassifier(), n_estimators=n_est, max_samples=0.67,
max_features=0.67, bootstrap=True, random_state=9)
clf_bagging_20.fit(X_train, y_train)
X_pred_bagging = clf_bagging_20.predict(X_train)
y_pred_bagging = clf_bagging_20.predict(X_test)
acc_score_train = accuracy_score(y_train, X_pred_bagging)
acc_score_test = accuracy_score(y_test, y_pred_bagging)
acc_train.append(acc_score_train)
acc_test.append(acc_score_test)
plt.figure(figsize=(10, 6))
plt.plot(ranges, acc_train, c='b', label='Train set')
plt.plot(ranges, acc_test, c='g', label='Test set')
plt.legend(loc='upper right')
plt.xlabel('n_estimators')
plt.ylabel('Accuracy')
plt.show()






Binary file not shown.
Binary file not shown.