<jupyter_start><jupyter_text>Data Analysis<jupyter_code>#  Displays the data correlation between the features.
#  Strong correlation between the start_of_day and the waste
plt.figure(figsize=(16, 6))
heatmap = sns.heatmap(df.corr(), annot = True)
heatmap.set_title('Correlation Heatmap', fontdict={'fontsize':18}, pad=12);
plt.show()
sns.pairplot(data=df)
sns.lmplot(x='product_id', y ='quantity_consumed', data=df)<jupyter_output><empty_output><jupyter_text>Define test data<jupyter_code>y = df['quantity_consumed']

X = df[['product_id','transaction_weekday', 'waste']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>lr = LinearRegression()
# Training test data
model = lr.fit(X_train,y_train)
# Coefficients for product_id and transaction_weekday, respectively.
coeffecients = pd.DataFrame(lr.coef_,X.columns)
coeffecients.columns = ['Coeffecient']
coeffecients
# Predicting values based on test data.
predictions_test = lr.predict(X_test)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print(np.sqrt(mean_squared_error(y_test,predictions_test)))
print(r2_score(y_test, predictions_test))
# Predicting values based on trained data.
predictions_train = lr.predict(X_train)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
print(np.sqrt(mean_squared_error(y_train,predictions_train)))
print(r2_score(y_train, predictions_train))
#Include features - underestimating
# X_test, y_test, predictions - get product_id - 69
plt.scatter(y_test, predictions_test)
plt.xlabel('Test Data')
plt.ylabel('Predicted Data')
#Checking distribution of residuals.
plt.hist((y_test-predictions_test));
#item id - 69, 
myvals = np.array([69, 3, 0]).reshape(1, -1)
print("Number of this pastry item needed on a Wednesday: " + str(model.predict(myvals)[0]))
from sklearn.linear_model import Ridge
rr = Ridge(alpha=0.01)
rr_model = rr.fit(X_train, y_train) 
pred_train_rr= rr.predict(X_test)
#Include features - underestimating
# X_test, y_test, predictions - get product_id - 69
rr_y,rr_p = [], []
for i_X_test, value in enumerate(X_test["product_id"]):
    if(value == 69):
        rr_y.append(y_test.values[i_X_test])
        rr_p.append(pred_train_rr[i_X_test])
        print("test: " + str(y_test.values[i_X_test]) + " prediction: " + str((pred_train_rr[i_X_test])))
plt.scatter(rr_y, rr_p)
plt.xlabel('Test Data')
plt.ylabel('Predicted Data')
print("Number of this pastry item needed on a Wednesday: " + str(round(rr_model.predict(myvals)[0])))
plt.scatter(lr_p, rr_p)
plt.xlabel('Linear Regression Prediction')
plt.ylabel('Ridge Regression Prediction')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12819576333089847, 'fraction_numerical': 0.01168736303871439, 'mean_word_length': 7.3, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '139007', 'license': 'MIT', 'n_tokens_mistral': 1044, 'n_tokens_neox': 988, 'n_words': 198, 'path': 'notebook/.ipynb_checkpoints/pastry-checkpoint.ipynb', 'repo_name': 'arthurwpessoa/machine_learning', 'size': '2738'}