1 Array Creation Routines
title: "Array creation routines" date: 2026-05-24T13:51:21Z
Array creation routines
Ones and zeros
1import numpy as np
Create a new array of 2*2 integers, without initializing entries.
array([[0, 0],
[0, 0]])
Let X = np.array([1,2,3], [4,5,6], np.int32). Create a new array with the same shape and type as X.
1X = np.array([[1,2,3], [4,5,6]], np.int32)
array([[1, 2, 3],
[4, 5, 6]])
Create a 3-D array with ones on the diagonal and zeros elsewhere.
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Create a new array of 3*2 float numbers, filled with ones.
array([[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])
Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X.
1x = np.arange(4, dtype=np.int64)
array([1, 1, 1, 1], dtype=int64)
Create a new array of 3*2 float numbers, filled with zeros.
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X.
1x = np.arange(4, dtype=np.int64)
array([0, 0, 0, 0], dtype=int64)
Create a new array of 2*5 uints, filled with 6.
array([[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6]], dtype=uint32)
Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X.
1x = np.arange(4, dtype=np.int64)
array([6, 6, 6, 6], dtype=int64)
From existing data
Create an array of [1, 2, 3].
array([1, 2, 3])
Let x = [1, 2]. Convert it into an array.
1x = [1,2]
array([1, 2])
Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix.
1X = np.array([[1, 2], [3, 4]])
matrix([[1, 2],
[3, 4]])
Let x = [1, 2]. Conver it into an array of float.
1x = [1, 2]
array([ 1., 2.])
Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30.
1x = np.array([30])
30
Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x.
1x = np.array([1, 2, 3])
70140352 [1 2 3]
70140752 [1 2 3]
Numerical ranges
Create an array of 2, 4, 6, 8, ..., 100.
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,
28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,
54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,
80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])
Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive.
array([ 3. , 3.14285714, 3.28571429, 3.42857143,
3.57142857, 3.71428571, 3.85714286, 4. ,
4.14285714, 4.28571429, 4.42857143, 4.57142857,
4.71428571, 4.85714286, 5. , 5.14285714,
5.28571429, 5.42857143, 5.57142857, 5.71428571,
5.85714286, 6. , 6.14285714, 6.28571429,
6.42857143, 6.57142857, 6.71428571, 6.85714286,
7. , 7.14285714, 7.28571429, 7.42857143,
7.57142857, 7.71428571, 7.85714286, 8. ,
8.14285714, 8.28571429, 8.42857143, 8.57142857,
8.71428571, 8.85714286, 9. , 9.14285714,
9.28571429, 9.42857143, 9.57142857, 9.71428571,
9.85714286, 10. ])
Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive.
array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,
2.63026799e+03, 3.63078055e+03, 5.01187234e+03,
6.91830971e+03, 9.54992586e+03, 1.31825674e+04,
1.81970086e+04, 2.51188643e+04, 3.46736850e+04,
4.78630092e+04, 6.60693448e+04, 9.12010839e+04,
1.25892541e+05, 1.73780083e+05, 2.39883292e+05,
3.31131121e+05, 4.57088190e+05, 6.30957344e+05,
8.70963590e+05, 1.20226443e+06, 1.65958691e+06,
2.29086765e+06, 3.16227766e+06, 4.36515832e+06,
6.02559586e+06, 8.31763771e+06, 1.14815362e+07,
1.58489319e+07, 2.18776162e+07, 3.01995172e+07,
4.16869383e+07, 5.75439937e+07, 7.94328235e+07,
1.09647820e+08, 1.51356125e+08, 2.08929613e+08,
2.88403150e+08, 3.98107171e+08, 5.49540874e+08,
7.58577575e+08, 1.04712855e+09, 1.44543977e+09,
1.99526231e+09, 2.75422870e+09, 3.80189396e+09,
5.24807460e+09, 7.24435960e+09])
Building matrices
Let X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]). Get the diagonal of X, that is, [0, 5, 10].
1X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
array([ 0, 5, 10])
Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere.
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
Create an array which looks like below. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])
array([[ 0., 0., 0., 0., 0.],
[ 1., 0., 0., 0., 0.],
[ 1., 1., 0., 0., 0.]])
Create an array which looks like below. array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]])
array([[ 0, 0, 0],
[ 4, 0, 0],
[ 7, 8, 0],
[10, 11, 12]])
Create an array which looks like below. array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]])
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 0, 8, 9],
[ 0, 0, 12]])