<jupyter_start><jupyter_text>Linear Regression<jupyter_code>X = df['Signal'].values.reshape(-1,1)
y = df['Density']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.1, random_state = 101)
from sklearn.linear_model import LinearRegression
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
lr_preds = lr_model.predict(X_test)
lr_preds
from sklearn.metrics import mean_absolute_error, mean_squared_error
mean_absolute_error(y_test, lr_preds)
np.sqrt(mean_squared_error(y_test, lr_preds))
signal_range = np.arange(0,100)
signal_preds = lr_model.predict(signal_range.reshape(-1,1))
signal_preds
sns.scatterplot(x = 'Signal', y = 'Density', data =df)
plt.plot(signal_range, signal_preds)<jupyter_output><empty_output><jupyter_text>Polynomial Regression<jupyter_code>def run_model(model, X_train, y_train, X_test, y_test):

    #Fit the model to training dataset
    model.fit(X_train, y_train)
    #performance metric
    preds = model.predict(X_test)
    rmse = np.sqrt(mean_squared_error(y_test, preds))
    mae =  mean_absolute_error(y_test, preds)
    print(f'RMSE: {rmse}')
    print(f'MAE: {mae}')

    # Plot Results Model signal range
    signal_range = np.arange(0,100)
    signal_preds = model.predict(signal_range.reshape(-1,1))

    sns.scatterplot(x = 'Signal', y = 'Density', data =df, color = 'green')
    plt.plot(signal_range, signal_preds)

model = LinearRegression() # It is not applicable
run_model(model, X_train, y_train, X_test, y_test)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
pipe = make_pipeline(PolynomialFeatures(degree = 2), LinearRegression()) #It is not performing well
run_model(pipe, X_train, y_train, X_test, y_test )
pipe = make_pipeline(PolynomialFeatures(degree = 6), LinearRegression()) #performing well
run_model(pipe, X_train, y_train, X_test, y_test )
from sklearn.neighbors import KNeighborsRegressor
k_values = [1,5,10,30]

for n in k_values:
    model = KNeighborsRegressor(n_neighbors=n)
    run_model(model, X_train, y_train, X_test, y_test)
    plt.show()
    
from sklearn.tree import DecisionTreeRegressor
model =  DecisionTreeRegressor() # Not performing well(high variance)
run_model(model, X_train, y_train, X_test, y_test)
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
svr = SVR()
param_grid = {'C': [0.01, 0.1, 1, 10, 100, 100], 'gamma': ['auto', 'scale']}
grid_model = GridSearchCV(svr, param_grid)
run_model(grid_model, X_train, y_train, X_test, y_test) #Performing well
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators = 10)
run_model(rf,X_train, y_train, X_test, y_test) #Not performing well
from sklearn.ensemble import GradientBoostingRegressor, AdaBoostRegressor
model = GradientBoostingRegressor()
run_model(model, X_train, y_train, X_test, y_test)
model = AdaBoostRegressor()
run_model(model, X_train, y_train, X_test, y_test)<jupyter_output>RMSE: 0.13168728487848655
MAE: 0.11181872511305284
{'fraction_non_alphanumeric': 0.10806398955272609, 'fraction_numerical': 0.025791707476330396, 'mean_word_length': 6.756962025316455, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '384510', 'license': 'MIT', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1138, 'n_words': 242, 'path': 'RFRegression/RF_Regression.ipynb', 'repo_name': 'deepanshu03091995/ml_live_class', 'size': '3063'}