<jupyter_start><jupyter_text>Reading the data<jupyter_code>data = pd.read_csv('../data/kc_house_data.csv')<jupyter_output><empty_output><jupyter_text>Extracting Features<jupyter_code>features = ['bedrooms','bathrooms','sqft_living','sqft_lot','floors']
X = data[features].values
y = data['price'].values<jupyter_output><empty_output><jupyter_text>Splitting the data<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>Creating and training the model<jupyter_code>#Creating the model 
reg = BaggingRegressor(n_estimators=100, random_state=42)
#Training the model 
reg.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predicting on the model<jupyter_code>X_train[0].reshape(-1,1)
train_pred = reg.predict(X_train)
test_pred = reg.predict(X_test)

individual_pred = reg.predict(X_train[0].reshape(1,5))
print(f'The prediction on individual example was {individual_pred[0]} whereas actual value was {y_train[0]}')<jupyter_output>The prediction on individual example was 354688.66666666674 whereas actual value was 325000.0<jupyter_text>Evaluating the model<jupyter_code>r_sq_train = reg.score(X_train, y_train)
r_sq_test = reg.score(X_test, y_test)

print(f'The R squared for train set is {r_sq_train} and for test set is {r_sq_test}')<jupyter_output>The R squared for train set is 0.939532741609383 and for test set is 0.5037027481232426<jupyter_text>Hyperparameter tuning<jupyter_code>train_rsquared = []
test_rsquared = []

for i in range(1,151):
    reg_loop = BaggingRegressor(n_estimators=i, random_state=42) #building a regressor with specified depth
    reg_loop.fit(X_train, y_train) #fitting the regressor 
    train_rsq_loop = reg_loop.score(X_train, y_train) #seeing r^2 for train set 
    test_rsq_loop = reg_loop.score(X_test, y_test) #seeing r^2 for test set 
    train_rsquared.append(train_rsq_loop) #appending the r^2 for train set 
    test_rsquared.append(test_rsq_loop) #appending the r^2 for test set<jupyter_output><empty_output><jupyter_text>Seeing the score for individual n_estimators<jupyter_code>estimators = [i for i in range(1,151)]
fig, ax = plt.subplots(figsize=(16,6),nrows=1, ncols=2)
ax[0].plot(estimators, train_rsquared,color='blue')
ax[0].set_xlabel('Number of estimators')
ax[0].set_ylabel(r'$R^2$')
ax[0].set_title(r'$R^2$ for various number of estimators on train set')
ax[1].plot(estimators, test_rsquared,color='black')
ax[1].set_xlabel('Number of estimators')
ax[1].set_ylabel(r'$R^2$')
ax[1].set_title(r'$R^2$ for various number of estimators on test set')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12135922330097088, 'fraction_numerical': 0.039208364451082896, 'mean_word_length': 7.531847133757962, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '47006', 'license': 'MIT', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1029, 'n_words': 193, 'path': 'LinkedInLearning/MachineLearningWithScikitlearn/BaggedTreesPractice.ipynb', 'repo_name': 'aayush1036/OnlineCourses', 'size': '2678'}