Numpy […,None]
Numpy […,None] I have found myself needing to add features to existing numpy arrays which has led to a question around what the last portion of the following code is actually doing: np.ones(shape=feature_set.shape)[...,None] Set-up As an example, let's say I wish to solve for linear regression parameter estimates by using numpy and solving: Assume I have a feature set shape (50,1), a target variable of shape (50,), and I wish to use the shape of my target variable to add a column for intercept values. It would look something like this: # Create random target & feature set y_train = np.random.randint(0,100, size = (50,)) feature_set = np.random.randint(0,100,size=(50,1)) # Build a set of 1s after shape of target variable int_train = np.ones(shape=y_train.shape)[...,None] # Able to then add int_train to feature set X = np.concatenate((int_train, feature_set),1) What I Think I Know I see the difference in output when I include [...,None] vs when I leave it off. Here it is: The ...