Dear all,
I am trying to add a column to a numpy matrix. I can make a running example as follows:
Thank you
I am trying to add a column to a numpy matrix. I can make a running example as follows:
import numpy as np X = np.random.uniform(size=(10,3)) X.shape Out[3]: (10, 3) n,m = X.shape X0 = np.ones((n,1)) X0.shape Out[6]: (10, 1) Xnew = np.hstack((X,X0)) Xnew.shape Out[8]: (10, 4)The data I got comes from a Pandas dataframe which I converted to numpy with .to_numpy, but then:
import pandas as pd df = pd.read_csv("Data.tsv", delimiter='\t') X = df[['mr', 'fcn', 'weight']] x = X.to_numpy() S = df[['serial']] sa = S.to_numpy() s = sa.ravel() x.shape Out[14]: (15744, 3) s.shape Out[15]: (15744,) z = np.hstack((x, s)) Traceback (most recent call last): File "<ipython-input-16-c88a113c6e49>", line 1, in <module> z = np.hstack((x, s)) File "/home/gigiux/.local/lib/python3.6/site-packages/numpy/core/shape_base.py", line 340, in hstack return _nx.concatenate(arrs, 1) ValueError: all the input arrays must have same number of dimensionsThis is also the case for stack, vstack, concatenate(axis=0,1,2). Nevertheless, all these objects are numpy arrays:
type(X) Out[6]: numpy.ndarray type(X0) Out[7]: numpy.ndarray type(Xnew) Out[8]: numpy.ndarray type(x) Out[2]: numpy.ndarray type(s) Out[4]: numpy.ndarrayWhat am I missing? Why
X0
has two dimensions (10, 1) but s
has only one (15744)? Is that the problem?Thank you