Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Fundamentals of Data Science and Analytics

Assignment 1

numpy.array() in Python:
The homogeneous multidimensional array is the main object of NumPy. It is
basically a table of elements which are all of the same type and indexed by a tuple of
positive integers. The dimensions are called axis in NumPy.

The NumPy's array class is known as ndarray or alias array. The numpy.array is not
the same as the standard Python library class array.array. The array.array handles
only one-dimensional arrays and provides less functionality.

Working with Numpy Array:


 import numpy as np
 
b = np.empty(2, dtype = int)
print("Matrix b : \n", b)
 
a = np.empty([2, 2], dtype = int)
print("\nMatrix a : \n", a)
 
c = np.empty([3, 3])
print("\nMatrix c : \n", c)

Output:

Matrix b :
[ 0 1079574528]
Matrix a :
[[0 0]
[0 0]]
Matrix a :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]

 import numpy as np

array = np.arange(8)

print("Original array : \n", array)


array = np.arange(8).reshape(2, 4)

print("\narray reshaped with 2 rows and 4 columns :


\n",array)

array = np.arange(8).reshape(4 ,2)

print("\narray reshaped with 2 rows and 4 columns : \n",


array)

array = np.arange(8).reshape(2, 2, 2)
print("\nOriginal array reshaped to 3D : \n", array)

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]]]

 import numpy as np

print("A\n", np.arange(4).reshape(2, 2), "\n")

print("A\n", np.arange(4, 10), "\n")

print("A\n", np.arange(4, 20, 3), "\n")

A
[[0 1]
[2 3]]
A
[4 5 6 7 8 9]

A
[ 4 7 10 13 16 19]

 import numpy as np

print("B\n", np.linspace(2.0, 3.0, num=5,


retstep=True),"\n")

x = np.linspace(0, 2, 10)

print("A\n", np.sin(x))

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]
 import numpy as np

array = np.array([[1, 2], [3, 4]])

array.flatten()

print(array)

array.flatten('F')

print(array)

[1, 2, 3, 4]
[1, 3, 2, 4]

You might also like