<jupyter_start><jupyter_text>NumPy is a python library in Python is that provides a multidimensional array object, various derived objects you can perform various mathematical operations. It can be logical,  sorting, shape manipulation etc.<jupyter_code>#importing numpy package 
import numpy as np<jupyter_output><empty_output><jupyter_text>creating simple array and converting to numpy array<jupyter_code># for 1D array
my_array = [12,34,43,14,51,66]
my_array

# converting the array to numpy array 
np.array(my_array)
# similarly for a 2D array 
my_2D_array = [[12,34],[43,14],[51,66]]
my_2D_array 
np.array(my_2D_array )<jupyter_output><empty_output><jupyter_text>built-in methods to generate numpy arrays<jupyter_code>np.arange(0,10) # returns values 0 to 9.

np.arange(0,10,2) # specify start, stop & step values 
np.linspace(0,10,10) #returns evenly spaced numbers over a specified interval
#specify start, stop & number of values 
np.zeros(10) #generate array of zeroes 

np.ones(10) #generate arrays of ones
np.zeros((8,9)) #generate 2D array of zeroes 
#similarly for ones
np.eye(10) #generate 2D indentity matrix or a numpy array with ones in the diagonal
np.random.rand(10) # generate random array
# every time you run, it changes it's value 
np.random.rand(4,5)  # generate random 2D array
np.random.randn(4) # generates a set of numbers from the Standard Normal distribution

#what is SND ? 
#It's a  is a normal distribution with a mean of zero and standard deviation of 1. ~ Bell shaped graph
# It  allows us to make comparisons across the infinitely many normal distributions that can possibly exist
# similarly for 2D arrays
# some other properties 
np.random.randint(1,20) #generates a random integer from 1 to 19 
#it changes every time you run the cell 
np.random.randint(1,20,10) # generates 10 random integers from 1 to 19<jupyter_output><empty_output><jupyter_text>Different methods in numpy arrays<jupyter_code>array_ = np.random.randint(0,100,20) # array storing my random integer numpy array 
array_
array_.shape # would give the dimension or shape of the array 
#similarly with 2D array
array_.reshape(4,5) # to change the dimension 
array_.reshape(4,6) # not possible as 4*6 != 10 
array_.reshape(4,5).T # this would transpose the array<jupyter_output><empty_output><jupyter_text>numpy array operations and mathematical functions<jupyter_code>array = np.arange(1,10)
array
#if you want to multiply all array elements with itself 
array*array
# similarly divide/add/subtract
# if you have a 0 element in the array and you divide the array with itself then it would give 'nan' result
array**4 # gives fourth power of all elements 
# similarly to multiply every element with a number 
array*5
### Some Mathematical functions that we can perform are : 
np.sqrt(array) #square root

np.max(array) # for maximum element
#similarly min()

np.argmax(array) # for index of max element 
#similarly argmin()



np.log(array)# to find log of elements

np.sin(array)# to find sin() of the array
# similarly exp, var , man , std

array = np.random.randn(3,3) # consider a matrix with normalised values 
# we can round off the values of this matrix using the functions
array

np.round(array,decimals=3) # to round off upto 3 decimal places<jupyter_output><empty_output><jupyter_text>indexing in numpy arrays<jupyter_code>array = np.arange(1,10)
array

array[4] # to show element in index 4 
array[[0,2,5,8]] # to show elements from multiple indexes
array[0:5] # to show elements from the range 
array[[0,4,7]] # to show elements from multiple indexes
array[3:7]= 8383 # to replace the elements from the index range 
array 
# You can perform the similar operation in a 2D array 
# In a 2d array the elements would be of the form array[i][j]
array2D = np.arange(1,21).reshape(4,5)
array2D
# in this array 
array2D[:,(2,4)] # means select all rows and cols of index 2 & 4<jupyter_output><empty_output><jupyter_text>selection in numpy arrays<jupyter_code>array = np.arange(1,10)
array
array <5 #returns a boolean value 

array[array <5]
#copy a numpy array
#when you slice/make any index value changes/reshape then it affects the original array
# you can copy the array if you don't want the original array to be changed
#consider reshape the array
array = np.arange(1,10)
array[3]= 1000
array
array_copy = array.copy()
array_copy<jupyter_output><empty_output><jupyter_text>i/p & o/p in numpy<jupyter_code>cd Desktop
array_to_save = np.arange(10)
array_to_save
# to save array in binary format in your pc
np.save('array_saved_binary',array_to_save)
#the array would be saved in a file by the name array_saved.npy



#to load that saved array
np.load('array_saved_binary.npy')
#to save the array as a text file
array_to_save = np.arange(10)
np.savetxt('array_saved_text.txt',array_to_save,delimiter=',') #delimiter is used to separate values.
# we can also save the file in a zip format using the .savez() function.
# It's left for your own research & learning ! 


###### -----end--------- ######<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup