<jupyter_start><jupyter_text>Scaling<jupyter_code># import packages
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import warnings 
warnings.filterwarnings('ignore')

pd.options.display.float_format = '{:,.2f}'.format
# read dataframe in
df = pd.read_csv('data/kickstarter_preprocess.csv')
df.columns<jupyter_output><empty_output><jupyter_text>features to keep: preparation, duration_days, goal, pledged_per_backer, parent_name, blurb_len_w, slug_len_w, 'launched_month', 'usd_pledged'<jupyter_code># drop unimportant features
df.drop(['backers_count', 'country', 'blurb_len_c', 'slug_len_c', 'cat_in_slug', 
         'category_parent_id', 'category_id', 'category_name', 'created_year', 'created_month', 'deadline_year', 
         'deadline_month', 'launched_year', 'rel_pledged_goal', 'filled_parent', 'staff_pick'], 
        axis=1, inplace=True)
df.columns
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 177593 entries, 0 to 177592
Data columns (total 11 columns):
 #   Column              Non-Null Count   Dtype  
---  ------              --------------   -----  
 0   goal                177593 non-null  float64
 1   staff_pick          177593 non-null  bool   
 2   state               177593 non-null  object 
 3   usd_pledged         177593 non-null  int64  
 4   blurb_len_w         177593 non-null  int64  
 5   slug_len_w          177593 non-null  int64  
 6   launched_month      177593 non-null  int64  
 7   duration_days       177593 non-null  int64  
 8   preparation         177593 non-null  int64  
 9   pledged_per_backer  177593 non-null  int64  
 10  parent_name         177593 non-null  object 
dtypes: bool(1), float64(1), int64(7), object(2)
memory usage: 13.7+ MB<jupyter_text>drop rows with state == canceled, rows with wrong categories<jupyter_code>df = df.drop(df[df['state'] == "canceled" ].index)
df.shape
df = df.drop(df[df['state'] == "failed" ].index)
df.shape
categories = ["Games", "Art", "Photography", "Film & Video", "Design", "Technology"]
df = df[df.parent_name.isin(categories)]
df.shape<jupyter_output><empty_output><jupyter_text>make dummies (staff_pick, state, category_name)<jupyter_code>#df.staff_pick = df.staff_pick.astype('int')
df['state'] = np.where(df['state'] == 'successful', 1, 0)
# convert the categorical variable parent_name into dummy/indicator variables
df_dum2 = pd.get_dummies(df.parent_name, prefix='parent_name')
df = df.drop(['parent_name'], axis=1)
df = pd.concat([df, df_dum2], axis=1)
# making a categorical variable for launched_month q1, q2, q3, q4 
df.loc[df['launched_month'] <  4, 'time_yr'] = 'q1'
df.loc[(df['launched_month'] >=  4) & (df['launched_month'] <  7), 'time_yr'] = 'q2'
df.loc[(df['launched_month'] >=  7) & (df['launched_month'] <  10), 'time_yr'] = 'q3'
df.loc[df['launched_month'] >  9, 'time_yr'] = 'q4'
df_dum3 = pd.get_dummies(df.time_yr, prefix='time_yr')
df = df.drop(['time_yr'], axis=1)
df = df.drop(['launched_month'], axis=1)
df = pd.concat([df, df_dum3], axis=1)
df.columns
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>Train-Test-Split<jupyter_code>from sklearn.model_selection import train_test_split, cross_val_score
y = df['usd_pledged']
X = df.drop('usd_pledged', axis=1)
y
# Train-test-split
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>remove outliers<jupyter_code>new = pd.concat([X_train, y_train], axis=1)
new.shape
lower = new['usd_pledged'].quantile(0.025)
upper = new['usd_pledged'].quantile(0.975)
new_2 = new[(new['usd_pledged'] < upper) & (new['usd_pledged'] > lower)]

#new.drop(new[new['usd_pledged'] > (new['usd_pledged'].quantile(0.975)) and 
#new.new['usd_pledged'] < (new['usd_pledged'].quantile(0.025))].index, inplace=True)
new_2.shape
y_train = new_2['usd_pledged']
X_train = new_2.drop('usd_pledged', axis=1)<jupyter_output><empty_output><jupyter_text>log-transformation target value<jupyter_code>#y_train_log = y_train + 0.00001
#y_train_log = np.log(y_train_log)
#y_train_log<jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
# we have to define which columns we want to scale.
col_scale = ['goal', 'blurb_len_w', 'slug_len_w', 'duration_days', 'preparation', 'pledged_per_backer']<jupyter_output><empty_output><jupyter_text>Data standardization<jupyter_code># Scaling with standard scaler
scaler = StandardScaler()
X_train_scaled_st = scaler.fit_transform(X_train[col_scale])
X_test_scaled_st = scaler.transform(X_test[col_scale])
# Concatenating scaled and dummy columns 
X_train_preprocessed_st = np.concatenate([X_train_scaled_st, X_train.drop(col_scale, axis=1)], axis=1)
X_test_preprocessed_st = np.concatenate([X_test_scaled_st, X_test.drop(col_scale, axis=1)], axis=1)<jupyter_output><empty_output><jupyter_text>Data normalization  Scaling with MinMaxScaler Try to scale you data with the MinMaxScaler() from sklearn.  It follows the same syntax as the StandardScaler. Don't forget: you have to import the scaler at the top of your notebook. from sklearn.preprocessing import MinMaxScalerscaler = MinMaxScaler()X_train_scaled_nor = scaler.fit_transform(X_train[col_scale])X_test_scaled_nor = scaler.transform(X_test[col_scale])  Concatenating scaled and dummy columns X_train_preprocessed_nor = np.concatenate([X_train_scaled_nor, X_train.drop(col_scale, axis=1)], axis=1)X_test_preprocessed_nor = np.concatenate([X_test_scaled_nor, X_test.drop(col_scale, axis=1)], axis=1)  Model Regression Polynomial Regression<jupyter_code>from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X_train_preprocessed_st)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y_train)
X_poly_test = poly_reg.fit_transform(X_test_preprocessed_st)
X_poly_test.shape
#y_pred = np.exp(pol_reg.predict(X_poly_test)) - 0.001
#y_pred = pd.DataFrame(y_pred)
#y_pred
pol_reg.predict(X_poly_test)
print("RMSE:", np.sqrt(mean_squared_error(y_test, pol_reg.predict(X_poly_test))))
print("R2:", r2_score(y_test, pol_reg.predict(X_poly_test)))
plt.hist(y_test - (pol_reg.predict(X_poly_test)))
y_test_2 = y_test['usd_pledged']
y_test_2.shape
pol_reg.predict(X_poly_test)
plt.scatter(y_test, pol_reg.predict(X_poly_test))
res = y_test - (pol_reg.predict(X_poly_test))
res
plt.hist(res)
plt.hist(res['usd_pledged'])
res.describe()
#y_pred = np.exp(pol_reg.predict(X_poly_test)) - 0.001
#y_pred
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("R2:", r2_score(y_test, y_pred))
plt.hist(y_test - y_pred)
y_test
y_pred
def mean_absolute_percentage_error(y_true, y_pred): 
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
from sklearn.linear_model import LinearRegression
# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=2)
X_poly = poly_reg.fit_transform(X_train_preprocessed_st)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y_train)
X_poly_test = poly_reg.fit_transform(X_test_preprocessed_st)
print("RMSE:", np.sqrt(mean_squared_error(y_test, pol_reg.predict(X_poly_test))))
print("R2:", r2_score(y_test, pol_reg.predict(X_poly_test)))
from sklearn.linear_model import LinearRegression
# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=3)
X_poly = poly_reg.fit_transform(X_train_preprocessed_st)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y_train)
X_poly_test = poly_reg.fit_transform(X_test_preprocessed_st)
print("RMSE:", np.sqrt(mean_squared_error(y_test, pol_reg.predict(X_poly_test))))
print("R2:", r2_score(y_test, pol_reg.predict(X_poly_test)))
from sklearn.linear_model import LinearRegression
# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X_train_preprocessed_st)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y_train)
X_poly_test = poly_reg.fit_transform(X_test_preprocessed_st)
print("RMSE:", np.sqrt(mean_squared_error(y_test, pol_reg.predict(X_poly_test))))
print("R2:", r2_score(y_test, pol_reg.predict(X_poly_test)))<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestRegressor
# Create the model with 100 trees
model = RandomForestRegressor(n_estimators=100, 
                               random_state=42, 
                               max_features = 'sqrt',
                               n_jobs=-1, verbose = 1)
# Fit on training data
model.fit(X_train_preprocessed_st, y_train)
y_pred = model.predict(X_test_preprocessed_st)
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("R2:", r2_score(y_test, y_pred))
res = y_test - y_pred
res<jupyter_output><empty_output><jupyter_text>Adaboost<jupyter_code>from sklearn.ensemble import AdaBoostRegressor
from sklearn.datasets import make_regression
X, y = make_regression(n_features=50, n_informative=2,
                       random_state=42, shuffle=False)

regr = AdaBoostRegressor(random_state=42, n_estimators=5)
regr.fit(X_train_preprocessed_st, y_train)

y_pred = regr.predict(X_test_preprocessed_st)
y_train_pred = regr.predict(X_train_preprocessed_st)
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("R2:", r2_score(y_test, y_pred))
print("RMSE:", np.sqrt(mean_squared_error(y_train, y_train_pred)))
#print("R2:", r2_score(y_train, y_train_pred))
from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import make_hastie_10_2   
# TODO: Import the three supervised learning models from sklearn
from sklearn.ensemble import AdaBoostRegressor

# TODO: Initialize the three models, the random states are set to 101 so we know how to reproduce the model later
clf_C = AdaBoostClassifier(random_state = 101)

# TODO: Calculate the number of samples for 1%, 10%, and 100% of the training data
samples_1 = int(round(len(X_train) / 100))
samples_10 = int(round(len(X_train) / 10))
samples_100 = len(X_train)

# Collect results on the learners
results = {}
for clf in clf_C:
    clf_name = clf.__class__.__name__
    results[clf_name] = {}
    for i, samples in enumerate([samples_1, samples_10, samples_100]):
        results[clf_name][i] = \
        train_predict(clf, samples, X_train, y_train, X_test, y_test)
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
print("R2:", r2_score(y_test, y_pred))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1225190131700983, 'fraction_numerical': 0.025505472083101464, 'mean_word_length': 6.179094540612517, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '162361', 'license': 'MIT', 'n_tokens_mistral': 4225, 'n_tokens_neox': 3992, 'n_words': 749, 'path': 'Regression_scaled.ipynb', 'repo_name': 'olehansen538/MPO-Kickstarter', 'size': '10782'}