Ch-1 Numpy: Ndarray Ndarray

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

Ch-1 NumPy

NumPy was created in 2005 by Travis Oliphant. NumPy stands for Numerical Python. NumPy is a
python library used for working with arrays. It also has functions for working in domain of linear
algebra and matrices. It is an open source project. The array object in NumPy is called ndarray, it
provides a lot of supporting functions that make working with ndarray very easy.

Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is an
object of data-type object (called dtype). Any item extracted from ndarray object (by slicing) is
represented by a Python object of one of array scalar types.

NumPy arrays are stored at one continuous place in memory. NumPy is a Python library and is written
partially in Python, but most of the parts that require fast computation are written in C or C++. The
basic ndarray is created using an array function in NumPy.

Example 1

import numpy as np

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

print a

output

[1, 2, 3]

Example 2

# Two dimensions

import numpy as np

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

print a

output

[[1, 2]

[3, 4]]

The ndarray object consists of contiguous one-dimensional segment of computer


memory, combined with an indexing scheme that maps each item to a location in the
memory block. The memory block holds the elements in a row-major order or a
column-major order.
NumPy − Data Types

Data Types Description

bool_ Boolean (True or False) stored as a byte

int_ Default integer type int32

int8 Byte (-128 to 127)

int16 Integer (-32768 to 32767)

int32 Integer (-2147483648 to 2147483647)

uint8 Unsigned integer (0 to 255)

NumPy − Array Attributes

NumPy provides a reshape function to resize an array.


import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print b
output
[[1 2]
[3 4]
[5 6]]

Assignment:

Q1. WAP Enter any two no. to swap the value.

Q2. Create an ndarry with value ranging from 10 to 99 each spaced with a
difference of 5.

Q3. Predict the output of the following code

(a) import numpy as np

arr=np.arange(9).reshape(3,3)

print arr

(b) import numpy as np

arr=np.add(75,-98)
print arr

You might also like