SeveralArray.py

In[1]: L = list(range(10)) L Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[2]: type(L[0]) Out[2]: int In[3]: L2 = [str(c) for c in L] L2 Out[3]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] In[4]: type(L2[0]) Out[4]: str In[5]: L3 = [True, "2", 3.0, 4] [type(item) for item in L3] Out[5]: [bool, str, float, int] In[6]: import array L = list(range(10)) A = array.array('i', L) A Out[6]: array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) In[8]: # integer array: np.array([1, 4, 2, 5, 3]) Out[8]: array([1, 4, 2, 5, 3]) In[9]: np.array([3.14, 4, 2, 3]) Out[9]: array([ 3.14, 4. , 2. , 3. ]) In[10]: np.array([1, 2, 3, 4], dtype='float32') Out[10]: array([ 1., 2., 3., 4.], dtype=float32) In[11]: # nested lists result in multidimensional arrays np.array([range(i, i + 3) for i in [2, 4, 6]]) Out[11]: array([[2, 3, 4], [4, 5, 6], [6, 7, 8]]) In[12]: # Create a length-10 integer array filled with zeros np.zeros(10, dtype=int) Out[12]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) In[13]: # Create a 3x5 floating-point array filled with 1s np.ones((3, 5), dtype=float) Out[13]: array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]]) In[14]: # Create a 3x5 array filled with 3.14 np.full((3, 5), 3.14) Out[14]: array([[ 3.14, 3.14, 3.14, 3.14, 3.14], [ 3.14, 3.14, 3.14, 3.14, 3.14], [ 3.14, 3.14, 3.14, 3.14, 3.14]]) In[15]: # Create an array filled with a linear sequence # Starting at 0, ending at 20, stepping by 2 # (this is similar to the built-in range() function) np.arange(0, 20, 2) Out[15]: array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]) In[16]: # Create an array of five values evenly spaced between 0 and 1 np.linspace(0, 1, 5) Out[16]: array([ 0. , 0.25, 0.5 , 0.75, 1. ]) In[17]: # Create a 3x3 array of uniformly distributed # random values between 0 and 1 np.random.random((3, 3)) Out[17]: array([[ 0.99844933, 0.52183819, 0.22421193], [ 0.08007488, 0.45429293, 0.20941444], [ 0.14360941, 0.96910973, 0.946117 ]]) In[18]: # Create a 3x3 array of normally distributed random values # with mean 0 and standard deviation 1 np.random.normal(0, 1, (3, 3)) Out[18]: array([[ 1.51772646, 0.39614948, -0.10634696], [ 0.25671348, 0.00732722, 0.37783601], [ 0.68446945, 0.15926039, -0.70744073]]) In[19]: # Create a 3x3 array of random integers in the interval [0, 10) np.random.randint(0, 10, (3, 3)) Out[19]: array([[2, 3, 4], [5, 7, 8], [0, 5, 0]]) In[20]: # Create a 3x3 identity matrix np.eye(3) Out[20]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) In[21]: # Create an uninitialized array of three integers # The values will be whatever happens to already exist at that # memory location np.empty(3) Out[21]: array([ 1., 1., 1.])
Creating Arrays from Scratch Especially for larger arrays, it is more efficient to create arrays from scratch using rou‐ tines built into NumPy. Here are several examples:

1 Response

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.