<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <h4>Hands-on
Step1:  <h4>Training and Evaluation Set</h4>
Export Row Number, 'x' and 'y_noisy' columns to a CSV file<br>
Row number will not be used for training the model. It is simply used as unique identifier
Step2:  <h4>Test Set</h4>
Test set should contain the same features expect for the target.<br>
Export Row Number, 'x'.
Step3:  <h4>Read the target predicted by AWS ML</h4>
df_predicted = pd.read_csv(os.path.join(data_path,
                                        'output_straight_line_noisy',
                                        'bp-oDvVSUKSpPe-straight_line_example_test_all.csv'))
Step4:  <h4>AWS ML Estimated/Predicted</h4>
Estimated/Predicted value is reported as 'score'. <br>
Tag is the row identifier
Step5:  <h4>Plot actual value and predicted value</h4>
Step6:  <h4>Predicted value has lot more noise</h4>
Default AWS ML setting transformed x from numeric to bins.<br>
In this case, binning caused more jitter.<br>
Let's rerun the model by updating recipe to treat x as numeric<br>
Model with x binned
<ASSISTANT_TASK:>
Python Code:
def straight_line(x):
    return 5 * x + 8
straight_line(25)
straight_line(1.254)
np.random.seed(5)
samples = 150
x_vals = pd.Series(np.random.rand(samples) * 20)
y_vals = x_vals.map(straight_line)
# Add random noise
y_noisy_vals = y_vals + np.random.randn(samples) * 3
df = pd.DataFrame({'x': x_vals,
                   'y':y_vals, 
                   'y_noisy': y_noisy_vals})
df.head()
# Correlation will indicate how strongly features are related to the output
df.corr()
fig = plt.figure(figsize = (12, 8))
plt.scatter(x = df.x,
            y = df.y,
            label = 'ideal fit')
plt.scatter(x = df.x,
            y = df.y_noisy, 
            color = 'r',
            marker = '+',
            label = 'Target')
plt.grid(True)
plt.xlabel('Input Feature')
plt.ylabel('Target')
plt.legend()
Explanation: <h4>Hands-on: Linear Regression with AWS ML</h4>
Input Features: x<br>
Output/Target Feature: y_noisy<br>
Objective: Simple exercise to get hands-on experience with AWSML.<br>
AWS ML Regression model to predict value of y_noisy for a given x<br>
End of explanation
data_path = '..\Data\RegressionExamples\straight_line'
df.to_csv(os.path.join(data_path, 'straight_line_example_all.csv'),
          index = True,
          index_label = 'Row')
# 130 rows for Training + Eval Set
df[df.index < 130].to_csv(os.path.join(data_path,'straight_line_noisy_example_train.csv'),
                          index = True,
                          index_label = 'Row'
                          ,columns = ['x','y_noisy'])
Explanation: <h4>Training and Evaluation Set</h4>
Export Row Number, 'x' and 'y_noisy' columns to a CSV file<br>
Row number will not be used for training the model. It is simply used as unique identifier
End of explanation
# run all the samples for prediction
df.to_csv(os.path.join(data_path, 'straight_line_example_test_all.csv'),
    index = True,
    index_label = 'Row', 
    columns = 'x')
Explanation: <h4>Test Set</h4>
Test set should contain the same features expect for the target.<br>
Export Row Number, 'x'.
End of explanation
df_predicted = pd.read_csv('./output/bp-oDvVSUKSpPe-straight_line_example_test_all.csv.gz')
Explanation: <h4>Read the target predicted by AWS ML</h4>
df_predicted = pd.read_csv(os.path.join(data_path,
                                        'output_straight_line_noisy',
                                        'bp-oDvVSUKSpPe-straight_line_example_test_all.csv'))
End of explanation
df_predicted.head()
df_predicted.columns = ["Row", "y_predicted"]
df_predicted.index = df_predicted.Row
df_predicted.head()
Explanation: <h4>AWS ML Estimated/Predicted</h4>
Estimated/Predicted value is reported as 'score'. <br>
Tag is the row identifier
End of explanation
fig = plt.figure(figsize = (12, 8))
plt.scatter(x = df.x,
            y = df.y_noisy,
            color = 'r',
            label = 'actual',)
plt.scatter(x = df.x,
            y = df_predicted.y_predicted,
            color = 'b',
            label = 'predicted')
plt.grid(True)
plt.legend()
# Training Data Residuals
residuals = (df_predicted.y_predicted - df.y_noisy)
fig = plt.figure(figsize = (12, 8))
plt.hist(residuals)
plt.grid(True)
plt.xlabel('(Predicted - Actual)')
plt.ylabel('Count')
plt.title('Residuals Distribution')
plt.axvline(color = 'g')
# left of 0 = prediction < actual
# right of 0 = prediction > actual
fig = plt.figure(figsize = (12, 8))
plt.boxplot([df.y_noisy, df_predicted.y_predicted], 
            labels = ['actual','predicted'])
plt.title('Box Plot - Actual, Predicted')
plt.ylabel('Target')
plt.grid(True)
Explanation: <h4>Plot actual value and predicted value</h4>
End of explanation
df_predicted_numeric = pd.read_csv('./output-numeric/bp-VNV4qb98Jmd-straight_line_example_test_all.csv.gz')
df_predicted_numeric.columns = ["Row", "y_predicted"]
df_predicted_numeric.head()
fig = plt.figure(figsize = (12, 8))
plt.scatter(x = df.x,
            y = df.y_noisy,
            color = 'r',
            label = 'actual',)
plt.scatter(x = df.x,
            y = df_predicted.y_predicted,
            color = 'k',
            label = 'predicted bin')
plt.scatter(x = df.x,
            y = df_predicted_numeric.y_predicted,
            color = 'b',
            label = 'predicted num')
plt.legend()
fig = plt.figure(figsize = (12, 8))
plt.boxplot([df.y_noisy, df_predicted.y_predicted, df_predicted_numeric.y_predicted], 
            labels = ['actual','predicted-bin','predicted-numeric'])
plt.title('Box Plot - Actual, Predicted')
plt.ylabel('Target')
plt.grid(True)
Explanation: <h4>Predicted value has lot more noise</h4>
Default AWS ML setting transformed x from numeric to bins.<br>
In this case, binning caused more jitter.<br>
Let's rerun the model by updating recipe to treat x as numeric<br>
Model with x binned: <br>
Training rmse=14.5376, Evaluation rmse=13.011,  Baseline rmse=30.404<br>
Model with x as numeric: <br>
Training rmse=4.4563, Evaluation rmse=2.4838, Baseline rmse=30.404
End of explanation
<END_TASK>