KEMBAR78
Numpy Semi 1 | PDF | Computer Programming | Arithmetic
0% found this document useful (0 votes)
12 views15 pages

Numpy Semi 1

Uploaded by

onlinetonistark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Numpy Semi 1

Uploaded by

onlinetonistark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

NUMPY ARRAYS

Data manipulation in Python is nearly synonymous


with NumPy array manipulation: even newer tools like
Pandas are built around the NumPy array
Attributes of arrays:
Determining the size, shape, memory consumption,
and data types of arrays
Indexing of arrays:
Getting and setting the value of individual array
elements
Slicing of arrays:
Getting and setting smaller subarrays within a larger
array
Reshaping of arrays:
Changing the shape of a given array Joining and
splitting of arrays Combining multiple arrays into one,
and splitting one array into many.
//Attributes Of Array:
import numpy as np
np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-
dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-
dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-
dimensional array
Each array has attributes ndim (the number of
dimensions), shape (the size of each dimension), and
size (the total size of the array).
print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
x3 ndim: 3 x3
x3 shape: (3, 4, 5)
x3 size: 60

//Array Indexing:
Indexing is classified two types
*normal Indexing
*fancy Indexing
In a one-dimensional array, you can access the i th
value (counting from zero) by specifying the desired
index in square brackets, just as with Python lists:
In: x1=[5,0,3,3,7,9]
In: x1[0]
Out: 5
In: x1[4]
Out: 7
To index from the end of the array, you can use
negative indices
In:x1[-1]
Out:9
In:x1[-3]
Out:3
Ex:
x2=([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
In: x2[0, 0]
Out: 3
In:x2[2,2]
Out:7
//Array Slicing: Accessing Subarrays
x[start :stop :step]
x=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In: x[:5] # first five elements
Out: 0, 1, 2, 3, 4
In: x[5:] # elements after index 5
Out: 5, 6, 7, 8, 9
In: x[2:9:2]
Out: 2,4,6,8

//Creating copies of arrays


Despite the nice features of array views, it is
sometimes useful to instead explicitly copy the data
within an array or a subarray.
This can be most easily done with the copy() method
x=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
m=x[2:9:2].copy()
print(m)
OUT: 2,4,6,8
In:
X=np.array([3,4,5,6])
print(X.copy())
Out:
[3,4,5,6]

Copy function may seem to be bit useless in case of


smaller programs but when we code for larger
number of lines copy() plays a major role in displaying
the same array..
//Reshaping of Arrays
grid = np. arange(1, 10).reshape((3, 3))
print(grid)
Out: [[1 2 3]
[4 5 6]
[7 8 9]]
the size of the initial array must match the size of the
reshaped array
x=[1,2,3]
x. reshape((3,1))
Out
([[1],
[2],
[3]])

// Array Concatenation and Splitting


x = np .array([1, 2, 3])
y = np .array([3, 2, 1])
np .concatenate([x, y])
Out: ([1, 2, 3, 3, 2, 1])
We can even use “np.hstack” , ”np. vstack” to
concatenate two arrays.
np.hstack([x,y])
// Exploring NumPy’s UFunc
Some of the operations may be slowly executed in
numpy to overcome this we use UNIVERSAL
FUNCTIONS(U Funcs)

Array arithmetic
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print(“x/2=”,x/2)

Out:
x = [0 1 2 3]
x + 5 = [5 6 7 8]
x - 5 = [-5 -4 -3 -2]
x * 2 = [0 2 4 6] x / 2 = [ 0. 0.5 1. 1.5]
//Absolute value
x = np.array([-2, -1, 0, 1, 2])
abs(x) or
np.absolute(x) or
np.abs(x)

Out: array([2, 1, 0, 1, 2]

// Exponents and logarithms


x = [1, 2, 3]
print("x =", x)
print("e^x =", np.exp(x))
print("2^x =", np.exp2(x))
print("3^x =", np.power(3, x))
Out:
x = [1, 2, 3]
e^x = [ 2.71828183 7.3890561 20.08553692]
2^x = [ 2. 4. 8.]
3^x = [ 3 9 27]
x = [1, 2, 4, 10]
Out:
print("x =", x)
print("ln(x) =", np.log(x))
print("log2(x) =", np.log2(x))
print("log10(x) =", np.log10(x))
x = [1, 2, 4, 10]
ln(x) = [ 0. 0.69314718 1.38629436 2.30258509]
log2(x) = [ 0. 1. 2. 3.32192809]
log10(x) = [ 0. 0.30103 0.60205999 1. ]

//Specialized Functions
Another excellent source for more specialized and
obscure ufuncs is the submodule scipy.special. If you
want to compute some obscure mathematical
function on your data, chances are it is implemented
in scipy.special. There are far too many functions to
list them all, but the following snippet shows a couple
that might come up in a statistics context
In: from scipy import special
x = [1, 5, 10]
print("gamma(x) =", special.gamma(x))
print("ln|gamma(x)| =", special.gammaln(x))
print("beta(x, 2) =", special.beta(x, 2))
Out:
gamma(x) = [ 1.00000000e+00 2.40000000e+01
3.62880000e+05]
ln|gamma(x)| = [ 0. 3.17805383 12.80182748]
beta(x, 2) = [ 0.5 0.03333333 0.00909091]

// Aggregates
if we’d like to reduce an array with a particular
operation, we can use the “reduce” method of any
ufunc.
x = np.arange(1, 6)
np.add.reduce(x)
Out: 15

np.add.accumulate(x)
Out: array([ 1, 3, 6, 10, 15]
// Exploring Fancy Indexing
Fancy indexing is conceptually simple: it means
passing an array of indices to access multiple array
elements at once. For example, consider the
following array:
import numpy as np
rand = np.random.RandomState(42)
x = rand.randint(100, size=10)
print(x)
Out:
[51 92 14 71 60 20 82 86 74 74]

[x[3], x[7], x[2]]


Out: [71, 86, 14]

ind = [3, 7, 4]
x[ind]
Out: array([71, 86, 60]

Why it is RANDOMSTATE(42)??

The reason is little quirky. The number simply has been


made popular by a comic science fiction called “The
Hitchhiker’s Guide To Galaxy” authored by Douglas
Adams.
In this novel, a supercomputer called DEEP THOUGHT
takes 7 and half million years to compute the answer
to the ultimate question of life, the Universe and
everything.
The answer turns out to be 42>

//Computation on Arrays: Broadcasting


Broadcasting is simply a set of rules for applying
binary ufuncs (addition, subtraction, multiplication,
etc.) on arrays of different sizes.
import numpy as np
In: a = np.array([0, 1, 2])
b = np.array([5, 5, 5])
a+b
Out: array([5, 6, 7])
In: a + 5
Out: array([5, 6, 7])
We may think that it creates an duplicate array [5,5,5]
but broadcasting doesn’t perform certain operations
like this.
//Comparisons, Masks, and Boolean Logic
Masking comes up when you want to extract, modify,
count, or otherwise manipulate values in an array
based on some criterion: for example, you might wish
to count all values greater than a certain value, or
perhaps remove all out‐ liers that are above some
threshold. In NumPy, Boolean masking is often the
most efficient way to accomplish these types of tasks.

x = np.array([1, 2, 3, 4, 5])
In: x < 3 # less than
Out: array([ True, True, False, False, False],
dtype=bool)
In: x > 3 # greater than
Out: array([False, False, False, True, True], dtype=bool)
In: x <= 3 # less than or equal
Out: array([ True, True, True, False, False], dtype=bool)
In: x >= 3 # greater than or equal
Out: array([False, False, True, True, True], dtype=bool)
In: x != 3 # not equal
Out: array([ True, True, False, True, True], dtype=bool)
In: x == 3 # equal
Out: array([False, False, True, False, False],
dtype=bool)

In:x=[[5 0 3 3]
[7 9 3 5]
[2 4 7 6]]
np.count_nonzero(x < 6)
Out:8

In: np.any(x > 8)


Out[18]: True
In[19]:
np.any(x < 0)
Out[19]: False
In[20]: np.all(x < 10)
Out[20]: True
In[21]: np.all(x == 6)
Out[21]: False
np.all() and np.any() can be used along particular
axes as well.

//Combined Indexing
In:[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
X[2, [2, 0, 1]]
Out: array([10, 8,9])

You might also like