NumPy Array Creation. In this lesson, I will show how to create various NumPy array. Such as an array of zeros, array of ones, array of numbers. I used Jupyter Notebook for running the codes.
Code:
import numpy as np
Code:
a = np.array([20,30,40])
a
Output:
array([20, 30, 40])
Code:
a.dtype
Output:
dtype('int64')
Code:
b = np.array([10.2, 30.5, 50.1])
b
Output:
array([10.2, 30.5, 50.1])
Code:
b.dtype
Output:
dtype('float64')
Code:
b = np.array([(10.5,20,30), (40,50,60)])
b
Output:
array([[10.5, 20. , 30. ],
[40. , 50. , 60. ]])
Code:
c = np.array( [ [10,20], [30,40] ], dtype=complex )
c
Output:
array([[10.+0.j, 20.+0.j],
[30.+0.j, 40.+0.j]])
Code:
np.zeros( (4,5) )
Output:
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Code:
np.ones((2, 4, 5), dtype=np.int16)
Output:
array([[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]],
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]], dtype=int16)
Code:
np.empty((2, 3))
Output:
array([[10.5, 20. , 30. ],
[40. , 50. , 60. ]])
Code:
np.arange(10 ,50, 5)
Output:
array([10, 15, 20, 25, 30, 35, 40, 45])
Code:
np.arange(0, 3, .4)
Output:
array([0. , 0.4, 0.8, 1.2, 1.6, 2. , 2.4, 2.8])
Code:
from numpy import pi
np.linspace(0, 2, 9)
Output:
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
Code:
x = np.linspace(0, 2*pi, 50)
f = np.sin(x)
x
Output:
array([0. , 0.12822827, 0.25645654, 0.38468481, 0.51291309,
0.64114136, 0.76936963, 0.8975979 , 1.02582617, 1.15405444,
1.28228272, 1.41051099, 1.53873926, 1.66696753, 1.7951958 ,
1.92342407, 2.05165235, 2.17988062, 2.30810889, 2.43633716,
2.56456543, 2.6927937 , 2.82102197, 2.94925025, 3.07747852,
3.20570679, 3.33393506, 3.46216333, 3.5903916 , 3.71861988,
3.84684815, 3.97507642, 4.10330469, 4.23153296, 4.35976123,
4.48798951, 4.61621778, 4.74444605, 4.87267432, 5.00090259,
5.12913086, 5.25735913, 5.38558741, 5.51381568, 5.64204395,
5.77027222, 5.89850049, 6.02672876, 6.15495704, 6.28318531])
Code:
f
Output:
array([ 0.00000000e+00, 6.34239197e-02, 1.26592454e-01,
1.89251244e-01, 2.51147987e-01, 3.12033446e-01,
3.71662456e-01, 4.29794912e-01, 4.86196736e-01,
5.40640817e-01, 5.92907929e-01, 6.42787610e-01,
6.90079011e-01, 7.34591709e-01, 7.76146464e-01,
8.14575952e-01, 8.49725430e-01, 8.81453363e-01,
9.09631995e-01, 9.34147860e-01, 9.54902241e-01,
9.71811568e-01, 9.84807753e-01, 9.93838464e-01,
9.98867339e-01, 9.99874128e-01, 9.96854776e-01,
9.89821442e-01, 9.78802446e-01, 9.63842159e-01,
9.45000819e-01, 9.22354294e-01, 8.95993774e-01,
8.66025404e-01, 8.32569855e-01, 7.95761841e-01,
7.55749574e-01, 7.12694171e-01, 6.66769001e-01,
6.18158986e-01, 5.67059864e-01, 5.13677392e-01,
4.58226522e-01, 4.00930535e-01, 3.42020143e-01,
2.81732557e-01, 2.20310533e-01, 1.58001396e-01,
9.50560433e-02, 3.17279335e-02, -3.17279335e-02,
-9.50560433e-02, -1.58001396e-01, -2.20310533e-01,
-2.81732557e-01, -3.42020143e-01, -4.00930535e-01,
-4.58226522e-01, -5.13677392e-01, -5.67059864e-01,
-6.18158986e-01, -6.66769001e-01, -7.12694171e-01,
-7.55749574e-01, -7.95761841e-01, -8.32569855e-01,
-8.66025404e-01, -8.95993774e-01, -9.22354294e-01,
-9.45000819e-01, -9.63842159e-01, -9.78802446e-01,
-9.89821442e-01, -9.96854776e-01, -9.99874128e-01,
-9.98867339e-01, -9.93838464e-01, -9.84807753e-01,
-9.71811568e-01, -9.54902241e-01, -9.34147860e-01,
-9.09631995e-01, -8.81453363e-01, -8.49725430e-01,
-8.14575952e-01, -7.76146464e-01, -7.34591709e-01,
-6.90079011e-01, -6.42787610e-01, -5.92907929e-01,
-5.40640817e-01, -4.86196736e-01, -4.29794912e-01,
-3.71662456e-01, -3.12033446e-01, -2.51147987e-01,
-1.89251244e-01, -1.26592454e-01, -6.34239197e-02,
-2.44929360e-16])