Skip to content

Instantly share code, notes, and snippets.

@GermanCM
Created December 19, 2018 10:47
Show Gist options
  • Save GermanCM/b72f5b4d329ce573398336484db7bb84 to your computer and use it in GitHub Desktop.
Save GermanCM/b72f5b4d329ce573398336484db7bb84 to your computer and use it in GitHub Desktop.
normalization and prediction
# returns the dependent variable (y axis) value which the model assigns to a certain independent variable (x axis) value
def predict_output(feature_matrix, coefficients):
'''
inputs:
* feature_matrix: two-dimensions array of the data points, where each columns is a feature and a row a point
* coefficients: one-dimension array of estimated feature coefficients
output:
* one-dimension array of predictions
'''
predictions = np.dot(feature_matrix, coefficients)
return predictions
# normalize features and returns predictions for the normalized features
def normalize_and_predict(feature_values, train_mu, train_sigma, coefficients):
X = [feature_values]
H = pd.DataFrame({'X': X})
# for a single point:
n = 1
feature_to_predict = np.zeros(n*2)
feature_to_predict.shape = (n, 2)
feature_to_predict[:,0] = 1
feature_to_predict[:,1] = H['X']
#normalize test feature with the same values as in the training set:
feature_to_predict = (feature_to_predict - train_mu) / train_sigma
return predict_output(feature_to_predict, coefficients)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment