python - numpy arrays dimension mismatch -
i using numpy , pandas attempt concatenate number of heterogenous values single array.
np.concatenate((tmp, id, freqs))
here exact values:
tmp = np.array([u'dnmt3a', u'p.m880v', u'chr2', 25457249], dtype=object) freqs = np.array([0.022831050228310501], dtype=object) id = "id_23728"
the dimensions of tmp
, 17232
, , freqs
follows:
[in] tmp.shape [out] (4,) [in] np.array(17232).shape [out] () [in] freqs.shape [out] (1,)
i have tried casting them numpy arrays no avail.
although variable freqs
have more 1 value.
however, both np.concatenate
, np.append
functions following error:
*** valueerror: input arrays must have same number of dimensions
these have same number of columns (0)
, why can't concatenate them either of above described numpy methods?
all i'm looking obtain is[(tmp), 17232, (freqs)]
in 1 single dimensional array, appended onto end of pandas dataframe.
thanks.
update
it appears can concatenate 2 existing arrays:
np.concatenate([tmp, freqs],axis=0) array([u'dnmt3a', u'p.m880v', u'chr2', 25457249, 0.022831050228310501], dtype=object)
however, integer, when casted cannot used in concatenate.
np.concatenate([tmp, np.array(17571)],axis=0) *** valueerror: input arrays must have same number of dimensions
what work, nesting append , concatenate
np.concatenate((np.append(tmp, 17571), freqs),) array([u'dnmt3a', u'p.m880v', u'chr2', 25457249, 17571, 0.022831050228310501], dtype=object)
although kind of messy. have better solution concatenating number of heterogeneous arrays?
the problem id
, , later integer
np.array(17571)
, not array_like
object. see here how numpy decides whether object can converted automatically numpy array or not.
the solution make id
array_like
, i.e. element of list
or tuple
, numpy understands id
belongs 1d
array_like
structure
it boils down to
concatenate((tmp, (id,), freqs))
or
concatenate((tmp, [id], freqs))
to avoid sort of problems when dealing input variables in functions using numpy
, can use atleast_1d
, pointed out @askewchan. see this question/answer.
basically, if unsure if in different scenarios variable id
single str
or list of str
, better off using
concatenate((tmp, atleast_1d(id), freqs))
because 2 options above fail if id
list/tuple of strings.
edit: may not obvious why np.array(17571)
not array_like
object. happens because np.array(17571).shape==()
, not iterable has no dimensions.
Comments
Post a Comment