<jupyter_start><jupyter_text>Polynomial Regression<jupyter_code>x1 = df.drop(['sales', 'total_spent'], axis=1)
x1.head()
from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=2,include_bias=False)
poly.fit(x1)
poly_features = poly.transform(x1)

print(x1.shape)
poly_features.shape#there for we got 9 features by 
x1.iloc[0]
poly_features[0]
X_train, X_test, y_train, y_test = train_test_split(
                                    poly_features, y, test_size=0.33, random_state=101)
model2 = LinearRegression()
model2.fit(X_train, y_train)
test_pred = model2.predict(X_test)
test_pred
print(mae(y_test,test_pred))
print(mse(y_test,test_pred))
print(mse(y_test,test_pred,squared=False))
model2.coef_
from sklearn.metrics import mean_absolute_error , mean_squared_error
#lest check the best model by model complexity .

train_rmse_errors = []
test_rmse_errors = []

for d in range(1,10):
    poly_converter = PolynomialFeatures(degree=d, include_bias=False)
    poly_features = poly_converter.fit_transform(x1)
    X_train, X_test, y_train, y_test = train_test_split(
                                    poly_features, y, test_size=0.33, random_state=101)

    model = LinearRegression()
    model.fit(X_train, y_train)

    train_pred = model.predict(X_train)
    test_pred = model.predict(X_test)

    train_rmse = np.sqrt(mean_squared_error(y_train, train_pred))
    test_rmse = np.sqrt(mean_squared_error(y_test, test_pred))
    

    train_rmse_errors.append(train_rmse)
    test_rmse_errors.append(test_rmse)
train_rmse_errors
test_rmse_errors #overfitting is happening after the 5th degree(error exploding)
plt.plot(range(1,6), train_rmse_errors[:5], label = 'TRAIN_RMSE') #You will be selecting the 2nd degree polynomial model
plt.plot(range(1,6), test_rmse_errors[:5], label = 'TEST_RMSE')
plt.xlabel("Model Complexity/ Degree of Polynomial")
plt.ylabel("RMSE")
plt.legend()
plt.plot(range(1,10), train_rmse_errors, label = 'TRAIN_RMSE')
plt.plot(range(1,10), test_rmse_errors, label = 'TEST_RMSE')
plt.xlabel("Model Complexity/ Degree of Polynomial")
plt.ylabel("RMSE")
plt.legend()
#coz after degreee 5 error are more than 10000
#in that we can see all linear untill 8 but it is not true
#from that we can say that degree 3 is best 
final_poly_converter = PolynomialFeatures(degree=3, include_bias=False)

final_model = LinearRegression()

full_converted_x = final_poly_converter.fit_transform(x)
final_model.fit(full_converted_x, y)
model_dir = 'models' 
os.makedirs(model_dir, exist_ok=True)
filepath = os.path.join(model_dir, 'final_poly_converter.joblib')
dump(final_poly_converter, filepath)

model_dir = "models"
os.makedirs(model_dir, exist_ok=True)
filepath = os.path.join(model_dir, 'poly.joblib')
dump(final_model, filepath)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1083392729864576, 'fraction_numerical': 0.01924447612259444, 'mean_word_length': 5.780193236714976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '545262', 'license': 'MIT', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1018, 'n_words': 203, 'path': 'Regression/LinearRegrssion.ipynb', 'repo_name': 'yokesh492/Ml_class_ineuron', 'size': '2806'}