Numpy | Array Creation - GeeksforGeeks

15 Nov, 2018
Array creation using List : Arrays are used to store multiple values in one single variable.Python does not have built-in support for Arrays, but Python lists can be used instead.Example :
arr = [1, 2, 3, 4, 5]
arr1 = ["geeks", "for", "geeks"]

Output:
1
2
3
4
5

 Array creation using array functions :array(data type, value list) function is used to create an array with data type and value list specified in its arguments.Example :
Output:
The new created array is : 1 2 3 1 5

 Array creation using numpy methods :NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros,np.empty etc.
numpy.empty(shape, dtype = float, order = ‘C’) : Return a new array of given shape and type, with random values.
Output :
Matrix b : 
 [         0 1079574528]

Matrix a : 
 [[0 0]
 [0 0]]

Matrix a : 
 [[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
numpy.zeros(shape, dtype = None, order = ‘C’) : Return a new array of given shape and type, with zeros.
Output :
Matrix b : 
 [0 0]

Matrix a : 
 [[0 0]
 [0 0]]

Matrix c : 
 [[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

 Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, ..., aN). We can reshape and convert it into another array with shape (b1, b2, b3, ..., bM).The only required condition is: a1 x a2 x a3 ... x aN = b1 x b2 x b3 ... x bM . (i.e original size of array remains unchanged.)
numpy.reshape(array, shape, order = ‘C’) : Shapes an array without changing data of array.
Output :
Original array : 
 [0 1 2 3 4 5 6 7]

array reshaped with 2 rows and 4 columns : 
 [[0 1 2 3]
 [4 5 6 7]]

array reshaped with 2 rows and 4 columns : 
 [[0 1]
 [2 3]
 [4 5]
 [6 7]]

Original array reshaped to 3D : 
 [[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.arange returns evenly spaced values within a given interval. step size is specified.linspace returns evenly spaced values within a given interval. num no. of elements are returned.
arange([start,] stop[, step,][, dtype]) : Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop)
Output :
A
 [[0 1]
 [2 3]]

A
 [4 5 6 7 8 9]

A
 [ 4  7 10 13 16 19]


numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) : Returns number spaces evenly w.r.t interval. Similiar to arange but instead of step it uses sample number.
Output :
B
 (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

A
 [ 0.          0.22039774  0.42995636  0.6183698   0.77637192  0.8961922
  0.9719379   0.99988386  0.9786557   0.90929743]
 Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order.
numpy.ndarray.flatten(order = ‘C’) : Return a copy of the array collapsed into one dimension.
Output :
[1, 2, 3, 4]
[1, 3, 2, 4]

 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Comments
Old Comments
Difference between sh and bash
Python - Add values to Dictionary of List
How to Install Scala IDE For Eclipse?
How to Test React Components using Jest ?
Spring @Controller Annotation with Example
What is External JavaScript ?
Create a dictionary with list comprehension in Python
Converting an image to a Torch Tensor in Python
How to Calculate Cosine Similarity in Python?
10 Best C and C++ Books For Beginners & Advanced Programmers