Sunday, May 9, 2021

Numpy Tutorials

 1.Array Create

import numpy as np

a = np.array([12345])

print(a)

Output

[12345]

2.2D Array

import numpy as np

a = np.array([[123], [456]])

print(a)

Output

[[1 2 3]
 [4 5 6]]
3.Array Indexing 1 dimenstional Array
import numpy as np

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

print(a[0])
Output

1

4.Array Indexing 2D Array

import numpy as np

arr = np.array([[1,2,3], [6,7,8]])

print(a[01])

Output

2

5.Array Slicing 1D array

import numpy as np

a= np.array([12345])

print(a[1:2])

Output

1

6.Array Slicing 1D array Negative Index


import numpy as np

a= np.array([12345])

print(a[-1])

Output

5

7.Array Slicing 2D Array

import numpy as np

a = np.array([[12345], [678910]])

print(a[02:3])

Output

3

8.Dtype

import numpy as np

a = np.array([1234])

print(a.dtype)

Output 

int64

9.Dtype String

import numpy as np

a = np.array(['cat''dog''cherry'])

print(a.dtype)

Output

.
<U6

10.Copy 

import numpy as np

a = np.array([1, 2, 3, 4, 5])
x = a.copy()
a[0] = 7

print(a)
print(x)

Output
[7  2  3  4  5]
[1 2 3 4 5]
11.View

a = np.array([1, 2, 3, 4, 5])
x = a.view()
a[0] = 7

print(a)
print(x)

Output
[7  2  3  4  5]
[7  2 3 4 5]
12.Array Shape Find

import numpy as np

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

print(a.shape)

Output

(2,3)

13.Array reshape

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

b = a.reshape(2,6)

print(b)

Output
[[ 1  2  3  4  5  6] 
 [ 7 8 9 10 11 12]]

14. Array For each

import numpy as np

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

for x in a:
  print(x)

Output
1
2
3

15.Array Joining

import numpy as np

a1 = np.array([123])

a2 = np.array([456])

a = np.concatenate((a1, a2))

print(a)

Output
[1 2 3 4 5 6]

16.Array Split

import numpy as np

a = np.array([123456])

a1 = np.array_split(a, 3)

print(a1)
Output
[array([1, 2]), array([3, 4]), array([5, 6])]
17.Array Search
import numpy as np

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

x = np.where(a == 1)

print(x)
Output
(array([0]),)
18.Array Sort

import numpy as np a = np.array([3, 2, 0, 1]) print(np.sort(a))

Output
[0,1,2,3]

19.Data Distribution in Numpy


from numpy import random x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(20)) print(x)

Output

[7 7 5 7 7 7 5 7 7 7 7 5 3 7 5 7 7 7 5 7]


20.Normal Distribution

from numpy import random

x = random.normal(size=(1,2))

print(x)

Output
[[ 0.9638252  -0.66766091]][[ 0.9638252  -0.66766091]]

21.Binomial Distribution

from numpy import random x = random.binomial(n=5, p=0.5, size=5) print(x)

Output

[3,3,2,2,2]

22.Poisson Distribution
from numpy import random

x = random.poisson(lam=2, size=5)

print(x)

Output

[6 1 4 4 2 1 0 1 2 2]
23.chisquare
from numpy import random

x = random.chisquare(df=2, size=(2, 3))

print(x)

Output
[[0.66396936 0.60936515 4.82225736]
 [0.1746385  0.25990412 4.1739717 ]]
















No comments:

Post a Comment

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...