How to standardize an array if standard deviation is zero? I am trying to standardize dataset columns for linear regression. 
One of the columns have standard deviation = 0. 
def standardize(X):
    return (X - mean(X)) / std(X) 

So this code doesn't work. 
Are there any tricks to solve this problem? I have tried two things


*

*Throw aways the column with standard deviation 0 because it's a useless parameter.

*Add some very small noise like $10^{-10}$ to one of the elements of the column so that the standardize function works. 


Thank you! 
 A: The situation you describe will arise as a result of one of these two scenarios:  


*

*The column you're referring to is the column of 1's which is added to your matrix of covariates so that your linear regression has an intercept term.

*The column is a different column than the previously-mentioned column of ones, giving you two columns of constants [****].


For Scenario 1: skip that column, standardize all the other columns, and then run the regression as you normally would. 
For Scenario 2, however, you'll have to get rid of that additional constant column entirely. In fact, regardless of the question of Standardization, you'll never be able to run the regression with two constant columns since then you would have perfect collinearity. The result is that even if you try running the regression, the computer program will spit out an error message and quit halfway through [Note: this is because an OLS regression requires the matrix X'X to be non-singular for things to work out correctly].
Anyway, good luck with your, um, regressing!  
[****] Just to clarify: What I mean by "two columns of constants" is that you have one column in which every element is '1' and a second column in which every element is some constant 'k'... 
A: The right way would be to delete the feature column from the data. But as a temporary hack -
You could just replace the 0 std to 1 for that feature. This would basically mean that the scaled value would be zero for all the data points for that feature. This makes sense as this implies that the feature values do not deviate even a bit form the mean(as the values is constant, the constant is the mean.)
FYI- This is what sklearn does! https://github.com/scikit-learn/scikit-learn/blob/7389dbac82d362f296dc2746f10e43ffa1615660/sklearn/preprocessing/data.py#L70
A: The feature that has zero variance is useless, remove it.
Consider this, if this was the only feature, you wouldn't learn anything about the response to this feature from the data. In multivariate case, it takes linear algebra to come to the same conclusion, but the idea's the same.
