MCPcopy Create free account
hub / github.com/numpy/numpy / atleast_2d

Function atleast_2d

numpy/core/shape_base.py:82–132  ·  view source on GitHub ↗

View inputs as arrays with at least two dimensions. Parameters ---------- arys1, arys2, ... : array_like One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have two or more dimensions are preserved. Returns

(*arys)

Source from the content-addressed store, hash-verified

80
81@array_function_dispatch(_atleast_2d_dispatcher)
82def atleast_2d(*arys):
83 """
84 View inputs as arrays with at least two dimensions.
85
86 Parameters
87 ----------
88 arys1, arys2, ... : array_like
89 One or more array-like sequences. Non-array inputs are converted
90 to arrays. Arrays that already have two or more dimensions are
91 preserved.
92
93 Returns
94 -------
95 res, res2, ... : ndarray
96 An array, or list of arrays, each with ``a.ndim >= 2``.
97 Copies are avoided where possible, and views with two or more
98 dimensions are returned.
99
100 See Also
101 --------
102 atleast_1d, atleast_3d
103
104 Examples
105 --------
106 >>> np.atleast_2d(3.0)
107 array([[3.]])
108
109 >>> x = np.arange(3.0)
110 >>> np.atleast_2d(x)
111 array([[0., 1., 2.]])
112 >>> np.atleast_2d(x).base is x
113 True
114
115 >>> np.atleast_2d(1, [1, 2], [[1, 2]])
116 [array([[1]]), array([[1, 2]]), array([[1, 2]])]
117
118 """
119 res = []
120 for ary in arys:
121 ary = asanyarray(ary)
122 if ary.ndim == 0:
123 result = ary.reshape(1, 1)
124 elif ary.ndim == 1:
125 result = ary[_nx.newaxis, :]
126 else:
127 result = ary
128 res.append(result)
129 if len(res) == 1:
130 return res[0]
131 else:
132 return res
133
134
135def _atleast_3d_dispatcher(*arys):

Callers 10

test_0D_arrayMethod · 0.90
test_1D_arrayMethod · 0.90
test_2D_arrayMethod · 0.90
test_3D_arrayMethod · 0.90
test_r2arrayMethod · 0.90
multi_dotFunction · 0.90
test_emptyMethod · 0.90
vstackFunction · 0.85
test_atleast_2dMethod · 0.85
test_shape_scalarMethod · 0.85

Calls 2

asanyarrayFunction · 0.85
reshapeMethod · 0.80

Tested by 8

test_0D_arrayMethod · 0.72
test_1D_arrayMethod · 0.72
test_2D_arrayMethod · 0.72
test_3D_arrayMethod · 0.72
test_r2arrayMethod · 0.72
test_emptyMethod · 0.72
test_atleast_2dMethod · 0.68
test_shape_scalarMethod · 0.68