# ---
# 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
# ---

# numpy ? -- multidimensional container for generic data
# it is nothing but matrices in mathematics
# numpy is the core library for scientific computing in python
# provides a high-performance multi-dimensional array object, and tools for working with these arrays.
import numpy as np

a=np.array([1,2,3]) # single dimensional array
print(a)
print(type(a))
print()
b=np.array([(4,5,6),(7,8,9)]) # two dimensional array
print(b)
print(type(b))

# how numpy looks and list looks
l1=[i for i in range(0,10)])
a1=np.arange(10)
print(f"List: {l1}")
print(f"numpy array: {a1}")

# +
# numpy VS lists---numpy takes less memory ,it is fast and convinient
# -

import sys
# list
S=[i for i in range(0,1000)]  # range function returns 1000 values from 0 to 999 in the form of list
# size of list = size of one element * length
print(f"memory for list: {sys.getsizeof(1)*len(S)}")
# numpy array
D=np.arange(1000) # arange is similar to range but returns values in the form of array
# size of numpy is
print(f"memory for numpy: {D.size*D.itemsize}")

# +
import time

SIZE=100000

l1=[i for i in range(SIZE)] # list-1
l2=[i for i in range(SIZE)] # list-2

a1=np.arange(SIZE) # array-1
a2=np.arange(SIZE) # array-2

start=time.time()

result=[(x+y) for (x,y) in zip(l1,l2)]
# time for addition of lists

print((time.time()-start)*1000) # to convert to milliseconds

#time for adding arrays
start=time.time()
result=a1+a2

print((time.time()-start)*1000) # to convert to milliseconds

# i.e., numpy is fast in computing


# +
# numpy operations
# find the dimension of the array
# find the byte size of the array
# find the data type of the elements
# -

a=np.array([(1,2,3),(3,4,5)])
print(a.ndim) # ndim--returns the dimension of the array
print(a.itemsize) # itemsize--returns the byte size of the each element
print(a.dtype)  #  prints the data type of elements

# +
a=np.array([1,2,3,4])
print(a.size) # size-- returns the number of elemnts in complete array
print(a.shape) # shape--returns the rows and column number

b=np.array([(1,2,3,4,1),(4,5,6,9,3),(7,8,9,4,8)])
print(b.size)
print(b.shape)
# -

# reshape -- 
a=np.array([(1,2,3,4),(5,6,7,8),(3,4,5,6),(4,5,6,7)])
print(a)
print(a.shape)
print()
a=a.reshape(2,8)
print(a.shape)
print(a)
print()
a=a.reshape(8,2)
print(a)
print(a.shape)

# slicing
a=np.array([(1,2),(3,4),(5,6),(7,8)])
print(a[3,1]) # print element from 4 th row and 2 nd column
print(a[1:,0]) # prints elements from all rows from first row and first column
print(a[0:2,1]) # prints elements from row 1 to 2 excludes 3 and second column

# line spacing 
a=np.linspace(1,5,10)# prints 10 values from 1 to 5 with equal space ie. difference between any two numbers is same
print(a)
a=np.linspace(1,3,5)
print(a)

# finding max.and min. and sum element
a=np.array([(1,2,3),(4,5,6),(7,8,9)])
print(a.max())
print(a.min())
print(a.sum())

# rows are called as axis-1 and columns are called axis-0 in numpy
a=np.array([(1,2,3,5),(4,5,6,9)])
print(a.sum(axis=0))
print(a.sum(axis=1))

# standard deviation of numpy
# standard deviation -- finding the mean and then finding how much each element has deviated from that mean
a=np.array([(1,2,3,4),(5,6,7,8)])
print(a.std())


# printing the square root of each element in the numpy array
a=np.array([(1,2,3,4),(5,6,7,8)])
print(np.sqrt(a))

a=np.array([(1,2,3),(3,4,5)])
b=np.array([(5,6,9),(7,8,1)])
print(a+b)
print()
print(a/b)
print()
print(a-b)
print()
print(a*b)

# vertical stacking of arrays --placing one array on the top of other
# horizontal stocking--placinf arrays side by side
print("vertical stocking....")
print(np.vstack((a,b)))
print("horizontal stocking....")
print(np.hstack((a,b)))


# converting the array to single column
print(a.ravel())

c=np.vstack((a,b))
c=c.ravel()
print(c)

# +
# ploting sine graph and cosine graph
import matplotlib.pyplot as plt

x=np.arange(0, 3*np.pi, 0.1)
y=np.sin(x)
plt.subplot(211)
plt.plot(x,y,label="sine",color="g")

z=np.cos(x)
plt.subplot(212)
plt.plot(x,z,label="cosine",color="m")

plt.legend()
plt.show()



# +
x=np.arange(0, 3*np.pi, 0.1)
y=np.sin(x)
graph1=plt
graph1.plot(x,y,color="c")
graph1.show()

z=np.cos(x)
graph2=plt
graph2.plot(x,z,color="g")
graph2.show()

print(type(graph1))
# -

# performing exponential function and logarithmic function using numpy
a=np.array([(1,2,3),(4,6,10)])
print(np.exp(a)) # calculates exponential values of a
print(np.log(a)) # calculates ln value
print(np.log10(a)) # calculates log base 10 value
print(np.log2(a)) # calculates log of base 2 value



StarcoderdataJupyterScriptsDedupFiltered