<jupyter_start><jupyter_text>The first dataset: blood-transfusion-service-center<jupyter_code>ilpd = fetch_openml(name='ilpd')
x = ilpd['data']
y = ilpd['target']
n_features = x.shape[1]<jupyter_output><empty_output><jupyter_text>randomly select 80% of data as training set and remaining 20% as test set<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8)<jupyter_output><empty_output><jupyter_text>Generate 10 different subsets of the training set by randomly subsampling 10%; 20%; : : : ; 100% of the training set.<jupyter_code>i = 1
subset = {}
while(i < 10):
    subset[i] = []
    subset_x_train, _no_use_train , subset_y_train, _no_use_test = train_test_split(x_train, y_train, train_size=float(i)*0.1)
    subset[i].append(subset_x_train)
    subset[i].append(subset_y_train)
    i += 1
subset[i] = []
subset[i].append(x_train)
subset[i].append(y_train)
percentage_changes = [i for i in range(10, 110, 10)]
rf_accuracy_changes = []
rf_time_changes = []<jupyter_output><empty_output><jupyter_text>train Random forest<jupyter_code>for i in subset:
    training_set_x = subset[i][0]
    training_set_y = subset[i][1]
    model = RandomForestClassifier(n_estimators=20, max_features=2, min_samples_split=2, bootstrap=True)
    time_start = time.time()
    model.fit(training_set_x, training_set_y)
    time_end = time.time()
    time_c= time_end - time_start
    print('time cost', time_c, 's')
    print('training dataset precentage: '+ str(i)+'0%')
    print(accuracy(model.predict(x_test), y_test))
    rf_accuracy_changes.append(accuracy(model.predict(x_test), y_test))
    rf_time_changes.append(time_c)
gb_accuracy_changes = []
gb_time_changes = []
for i in subset:
    training_set_x = subset[i][0]
    training_set_y = subset[i][1]
    model = GradientBoostingClassifier(random_state=10)
    time_start = time.time()
    model.fit(training_set_x, training_set_y)
    time_end = time.time()
    time_c= time_end - time_start
    print('time cost', time_c, 's')
    print('training dataset precentage: '+ str(i)+'0%')
    print(accuracy(model.predict(x_test), y_test))
    gb_accuracy_changes.append(accuracy(model.predict(x_test), y_test))
    gb_time_changes.append(time_c)
plt.plot(percentage_changes, rf_accuracy_changes, c = 'b')
plt.plot(percentage_changes, gb_accuracy_changes, c = 'r')
plt.xlabel('subsampling of training set')
plt.ylabel('accuracy')
plt.show()
plt.plot(percentage_changes, rf_time_changes, c = 'b')
plt.plot(percentage_changes, gb_time_changes, c = 'r')
plt.xlabel('subsampling of training set')
plt.ylabel('time')
plt.show()<jupyter_output><empty_output><jupyter_text>The second dataset: electricity<jupyter_code>electricity = fetch_openml(name='electricity')
x = electricity['data']
y = electricity['target']
n_features = x.shape[1]
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8)
i = 1
subset = {}
while(i < 10):
    subset[i] = []
    subset_x_train, _no_use_train , subset_y_train, _no_use_test = train_test_split(x_train, y_train, train_size=float(i)*0.1)
    subset[i].append(subset_x_train)
    subset[i].append(subset_y_train)
    i += 1
subset[i] = []
subset[i].append(x_train)
subset[i].append(y_train)
percentage_changes = [i for i in range(10, 110, 10)]
rf_accuracy_changes = []
rf_time_changes = []
for i in subset:
    training_set_x = subset[i][0]
    training_set_y = subset[i][1]
    model = RandomForestClassifier(n_estimators=20, max_features=3, min_samples_split=2, bootstrap=True)
    time_start = time.time()
    model.fit(training_set_x, training_set_y)
    time_end = time.time()
    time_c= time_end - time_start
    print('time cost', time_c, 's')
    print('training dataset precentage: '+ str(i)+'0%')
    print(accuracy(model.predict(x_test), y_test))
    rf_accuracy_changes.append(accuracy(model.predict(x_test), y_test))
    rf_time_changes.append(time_c)
gb_accuracy_changes = []
gb_time_changes = []
for i in subset:
    training_set_x = subset[i][0]
    training_set_y = subset[i][1]
    model = GradientBoostingClassifier(random_state=10)
    time_start = time.time()
    model.fit(training_set_x, training_set_y)
    time_end = time.time()
    time_c= time_end - time_start
    print('time cost', time_c, 's')
    print('training dataset precentage: '+ str(i)+'0%')
    print(accuracy(model.predict(x_test), y_test))
    gb_accuracy_changes.append(accuracy(model.predict(x_test), y_test))
    gb_time_changes.append(time_c)
plt.plot(percentage_changes, rf_accuracy_changes, c = 'b')
plt.plot(percentage_changes, gb_accuracy_changes, c = 'r')
plt.xlabel('subsampling of training set')
plt.ylabel('accuracy')
plt.show()
plt.plot(percentage_changes, rf_time_changes, c = 'b')
plt.plot(percentage_changes, gb_time_changes, c = 'r')
plt.xlabel('subsampling of training set')
plt.ylabel('time')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13715813283981082, 'fraction_numerical': 0.01418877236273905, 'mean_word_length': 5.850704225352112, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '571521', 'license': 'MIT', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1832, 'n_words': 319, 'path': 'Assignment1/question3.ipynb', 'repo_name': 'IrohXu/NYU_Intro_to_Deep_Learning_System', 'size': '4863'}