0% found this document useful (0 votes)
57 views

NUMPY Basics: Computation and File I/O Using Arrays

NUMPY Basics NumPy is a fundamental package for scientific computing in Python that provides multidimensional array objects and tools for working with these arrays. NumPy arrays have fixed sizes and homogeneous data types, unlike Python lists. NumPy facilitates fast computations and operations on large datasets. NumPy allows both element-by-element operations and an object-oriented approach to working with arrays. NumPy provides functions for creating arrays, attributes and methods to work with arrays, and tools for input/output of arrays to and from files.

Uploaded by

Tushar Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

NUMPY Basics: Computation and File I/O Using Arrays

NUMPY Basics NumPy is a fundamental package for scientific computing in Python that provides multidimensional array objects and tools for working with these arrays. NumPy arrays have fixed sizes and homogeneous data types, unlike Python lists. NumPy facilitates fast computations and operations on large datasets. NumPy allows both element-by-element operations and an object-oriented approach to working with arrays. NumPy provides functions for creating arrays, attributes and methods to work with arrays, and tools for input/output of arrays to and from files.

Uploaded by

Tushar Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NUMPY Basics

Computation and File I/O using Arrays


What is NumPy

• NumPy is the fundamental package for scientific computing in Python

• Python library that provides a multidimensional array object, various


derived objects (such as masked arrays and matrices)

• NumPy package, is the ndarray object

• This encapsulates n-dimensional arrays of homogeneous data types


Difference between NumPy and Python lists
• NumPy arrays have a fixed size at creation, unlike Python lists (which
can grow dynamically).
• Changing the size of an ndarray will create a new array and delete the
original.
• The elements in a NumPy array are all required to be of the same
data type, and thus will be the same size in memory. The exception:
one can have arrays of (Python, including NumPy) objects, thereby
allowing for arrays of different sized elements.
• NumPy arrays facilitate advanced mathematical and other types of
operations on large numbers of data. Typically, such operations are
executed more efficiently and with less code than is possible using
Python’s built-in sequences.
Why NumPy
• NumPy gives us the best of both worlds: element-by-element operations
are the “default mode” when an ndarray is involved

• NumPy fully supports an object-oriented approach, starting, once again,


with ndarray.

• Reduced memory usage

• Faster computations

• Broadcasting

• Sliceability
Basics in NumPy
Creating arrays Zeros and Ones Function
>>>from numpy as * >>>zeros((2,4))
>>>ones((3,3,3))
>>>a=array([[1,2,3],[4,5,6]])
>>>a
>>>a.shape() Diagonal Matrix
>>>d=diag([2,4,5])
Creating one-Dimensional Space
>>>b=arange(0,11)
>>>c=arange(0,2.0,0.4)
>>>d=linspace(0,3.5,8)
Use as array.attribute

Attribute Output
shape tuple showing the array shape; setting this at- tribute re-shapes the array

strides tuple showing how many bytes must be jumped in the data segment to get from
one entry to the next

ndim number of dimensions in array


data buffer object loosely wrapping the array data (only works for single-segment
arrays)

size total number of elements


itemsize size (in bytes) of each element
nbytes total number of bytes used
base object this array is using for its data buffer, or None if it owns its own memory

dtype data-type object for this array


Methods on ndarray (Use as array.method(args)
Method Parameter Results
mean (axis=0 or axis=1) Mean of row(1) or column(0)
var (axis=0 or axis=1) Variance of row or column
sqrt (axis=0 or axis=1) Square root of a row or Column
std (axis=0 or axis=1) Standard Deviation of row or column
min (axis=0 or axis=1) Minimum value of a row or column
max (axis=0 or axis=1) Maximum value of a row or column
argmin (axis=0 or axis=1) Index value of Minimum value
argmax (axis=0 or axis=1) Index value of Maximum Value
exp (axis=0 or axis=1) Exponential Value of the entire row or column
log (axis=0 or axis=1) Natural Log of entire row or column
log10 (axis=0 or axis=1) Log base 10 of entire row or column
I/O Handling Using Numpy
(INPUT)
Converting list to array
>>>import numpy as np
>>>l=[[1,2,3],[45,78,90]]
>>>a=np.array(l)
Converting a String to array
>>>n=input()
>>>a=np.fromstring(string=n,dtype=int,count=-1,sep=“,”)
Converting file (csv) to an array
>>>a=np.genfromtxt(‘file.csv’,delimiter=“,”)
Converting file (txt) to an array
>>>a=np.loadtxt(‘file.txt’)
I/O Handling Using Numpy
(OUTPUT)
Printing Array as Strings
>>>import numpy as np
>>>np.array2string(a)
Taking Output as file
>>>x=np.arange(100).reshape(20,5)
>>>import os
>>>np.savetxt(‘x.txt’,x)
>>>np.savetxt(‘x1.csv’,x,delimiter=“,”)

You might also like