# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np

x = [1,2,3.5,4.8]

type(x)

2*x

x

a = np.array(x,dtype=np.float32)
print(a)

type(a)

print(a*2)

a.dtype

x = np.arange(1,10,2)
print(x)

print(x[0])
print(x[-1])
print(x[0:3:2])

x = np.arange(3.2)
y = np.arange(0.1,1.2,0.1)
print(x)
print(y)


v1 = np.arange(5)
print ('Creating a numpy array via arange (stop=5) : %s\n'%v1)
v2 = np.arange(2,5)
print ('Creating a numpy array via arange (start=2,stop=5) : %s\n'%v2)
v3 = np.arange(0,-10,-2)
print ('Creating a numpy array via arange (start=0,stop=-10,step=-2) : %s\n'%v3)

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

# +
b = np.array([[1,1],[2,2], [3,3]])

print(b.ndim,b.size)
print(b)
# -

b.shape

np.shape(b)

a = np.arange(12).reshape(3,4)
a

a.shape

a.flatten()

a.reshape(2,3,-1).shape

a.reshape(-1).shape

for cell in a.flatten():
    print(cell)

a = np.linspace(0, 4, 9)
a

x = np.zeros((2, 3,10,7,4))
x;

x.shape

np.ones((3, 2))

np.eye(5)

np.diag([1,2,3,4,-1])

np.array([1, 2, 3] * 3)

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

x = np.diag([1,2,3,4,-1])
x

x[1:4,1:4]

np.sin(x).astype(np.float16)

mat = np.random.normal(2,2.5,(40,80))
mat.shape

print ('Overall mean of matrix is \n%s\n'%np.mean(mat))

print ('Row mean of matrix is \n%s\n'%np.mean(mat, axis=0).shape)

print ('Column mean of matrix is \n%s\n'%np.mean(mat, axis=1))

print ('Overall varience of matrix is %s\n'%np.var(mat))

print ('Overall standard deviation of matrix is %s\n'%np.std(mat))

print ('Overall sum of matrix is %s\n'%np.sum(mat))

print ('Overall min of matrix is %s\n'%np.min(mat))

print ('Overall max of matrix is %s\n'%np.max(mat))

marks = np.array([30,31,32,40,90,95,97,98,99,100])
print ('Marks = %s\n'%marks)
print ('Overall 30 percent quantile of marks is %s\n'%(str(np.percentile(marks,30))))

np.sum(np.zeros((10,15,20)),axis=(1,2)).shape

# +
# dir(np)
# -

len(dir(np))

x = np.arange(10)+3
x

x>5

x[x>5]

np.where(x>5)

x

x[np.array([0,8])]

# # Combining Data

data1 = np.random.randint(5,15,size=(5,3))

data2 = np.random.randint(5,15,size=(5,3))

np.hstack([data1,data2]).shape

np.vstack([data1, data2]).shape

np.concatenate([data1, data2], axis=1).shape

np.stack([data1, data2], axis=2).shape

# # Save and load data

x = np.linspace(0,5,100)
y = np.sin(x)

y.shape

np.savetxt('x.txt',x)

np.savetxt('data.txt',np.c_[x,y])

x = np.loadtxt('data.txt')
x.shape

# +
# # !cat x.txt
# -

x = np.random.normal(0,1,(1000,1000))

np.savetxt('x.txt',x,)

np.save('xdata',x)

x = np.load('xdata.npy')

x.shape

x = np.random.normal(0,1,(100,100))
y = np.random.normal(0,1,(100,100))
z = np.random.normal(0,1,(100,100))

np.savez('dataset',d1=x,d2=y,d3=z)

data = np.load('dataset.npz')

data.files

x = data['d1']
y = data['d2']
z = data['d3']



x = np.diag([1,2])
y = np.ones((2,2))

x

y

x*y

np.dot(x,y)

x = np.random.normal(0,1,[5,5])
np.linalg.inv(x)
np.linalg.eigvals(x).astype(np.complex64)

# +
x = np.array([1,1,2])
pdf, ebin = np.histogram(x,2)
cbin = 0.5*(ebin[1:]+ebin[:-1])

pdf,cbin

StarcoderdataJupyterScriptsDedupFiltered