<jupyter_start><jupyter_text>This homework must hand with numpy !!!<jupyter_code>print(data_train.index)
print(data_test.columns)
print(data_train.shape)
print(data_test.shape)
data_train.head()
data_train[['price','bedrooms']].values
def get_numpy_data(data_sframe, features_name, output_name):
    data_sframe['constant'] = 1 # add a constant column to an SFrame
    features_matrix = data_sframe[ (['constant'] + features_name) ].values
    output_array = data_sframe[output_name].values
    return(features_matrix, output_array)
def predict_outcome(feature_matrix, weights):
    predictions = feature_matrix.dot(weights)
    return(predictions)
def feature_derivative(errors, feature):
    derivative = (-2)*np.sum(errors * feature)
    return(derivative)
def regression_gradient_descent(feature_matrix, y_true, initial_weights, step_size, tolerance):
    converged = False
    weights = np.array(initial_weights)
    while not converged:
#     for ii in range(10):
        # compute the predictions based on feature_matrix and weights:
        # compute the errors as predictions - output:
#         errors = predict_outcome(feature_matrix, weights) - y_true 
        # !!! 就是这个地方害的
        errors = -predict_outcome(feature_matrix, weights) + y_true 
        # 本来应该是 y - y_predict 的，这样才对应 w = w - alpha*grad
        gradient_sum_squares = 0 # initialize the gradient
        # while not converged, update each weight individually:
        for i in range(len(weights)):
            # Recall that feature_matrix[:, i] is the feature column associated with weights[i]
            # compute the derivative for weight[i]:
            derivative = feature_derivative(errors, feature_matrix[:, i])
            # add the squared derivative to the gradient magnitude
            gradient_sum_squares += derivative**2
            # update the weight based on step size and derivative:
            weights[i] = weights[i] - step_size*derivative
        rss = len(y_true) * sklearn.metrics.mean_squared_error(y_true, predict_outcome(feature_matrix, weights))
        print(rss)
        gradient_magnitude = np.sqrt(gradient_sum_squares)
        print('weights[i]:',weights[i], ' derivative:', derivative, ' :gradient_magnitude', gradient_magnitude)
        if gradient_magnitude < tolerance:
            converged = True
    return(weights)<jupyter_output><empty_output><jupyter_text>homework1,2:<jupyter_code>np.array([-47000., 1.])
simple_features_name = ['sqft_living']
my_output_name= 'price'
(simple_feature_matrix, y_true) = get_numpy_data(data_train, simple_features_name, my_output_name)
initial_weights = np.array([-47000., 1.])
step_size = 7e-12
tolerance = 2.5e7
y_true
weights = regression_gradient_descent(simple_feature_matrix, y_true, initial_weights, step_size, tolerance)
weights
(x_test, y_test) = get_numpy_data(data_test, simple_features_name, my_output_name)
predict_outcome(x_test, weights)
len(y_test) * sklearn.metrics.mean_squared_error(y_test, predict_outcome(x_test, weights))
y_test<jupyter_output><empty_output><jupyter_text>homework3,4:<jupyter_code>model_features_name = ['sqft_living', 'sqft_living15']
my_output_name = 'price'
(feature_matrix, y_true) = get_numpy_data(data_train, model_features,my_output)
initial_weights = np.array([-100000., 1., 1.])
step_size = 4e-12
tolerance = 1e9
weights = regression_gradient_descent(feature_matrix, y_true, initial_weights, step_size, tolerance)
weights
(x_test, y_test) = get_numpy_data(data_test, model_features_name, my_output_name)
predict_outcome(x_test, weights)
len(y_test) * sklearn.metrics.mean_squared_error(y_test, predict_outcome(x_test, weights))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10194042088002186, 'fraction_numerical': 0.011751844766329598, 'mean_word_length': 5.049586776859504, 'pattern_counts': {'":': 0, '<': 14, '<?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': '517716', 'license': 'Apache-2.0', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1182, 'n_words': 242, 'path': 'Course2week2/assignment2.ipynb', 'repo_name': 'bluove/Machine-Learning-Specialization', 'size': '3659'}