0% found this document useful (0 votes)
40 views4 pages

Understanding NumPy Arrays in Python

The document discusses NumPy arrays in Python, which are homogeneous multidimensional arrays that serve as the main data structure in NumPy. It demonstrates how to create NumPy arrays with different dimensions and data types, reshape arrays, and use functions like linspace and sin on arrays. The document also shows how to flatten arrays and access elements in NumPy arrays.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Understanding NumPy Arrays in Python

The document discusses NumPy arrays in Python, which are homogeneous multidimensional arrays that serve as the main data structure in NumPy. It demonstrates how to create NumPy arrays with different dimensions and data types, reshape arrays, and use functions like linspace and sin on arrays. The document also shows how to flatten arrays and access elements in NumPy arrays.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Numpy Arrays in Python
  • Array Reshaping

Fundamentals of Data Science and Analytics

Assignment 1

[Link]() 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 [Link] is not
the same as the standard Python library class [Link]. The [Link] handles
only one-dimensional arrays and provides less functionality.

Working with Numpy Array:


 import numpy as np
 
b = [Link](2, dtype = int)
print("Matrix b : \n", b)
 
a = [Link]([2, 2], dtype = int)
print("\nMatrix a : \n", a)
 
c = [Link]([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 = [Link](8)

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


array = [Link](8).reshape(2, 4)

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


\n",array)

array = [Link](8).reshape(4 ,2)

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


array)

array = [Link](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", [Link](4).reshape(2, 2), "\n")

print("A\n", [Link](4, 10), "\n")

print("A\n", [Link](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", [Link](2.0, 3.0, num=5,


retstep=True),"\n")

x = [Link](0, 2, 10)

print("A\n", [Link](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 = [Link]([[1, 2], [3, 4]])

[Link]()

print(array)

[Link]('F')

print(array)

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

numpy.array() in Python:
The  homogeneous  multidimensional  array  is  the  main  object  of NumPy.  It  is
basically a tabl
array = np.arange(8).reshape(2, 4)
print("
array reshaped with 2 rows and 4 columns :      

",array)
array = np.arange(8).
A
 [4 5 6 7 8 9]
A
 [ 4  7 10 13 16 19]
import numpy as np
print("B
", np.linspace(2.0, 3.0, num=5, 
retstep=True),"
")
x

You might also like