#!
/usr/bin/env python
# coding: utf-8
# In[1]:
pip install numpy
# In[105]:
import numpy ## Import entire Numpy Package
# In[2]:
import numpy as np ## rename as np
# In[4]:
# Creating an array using Numpy
#import numpy
marks = numpy.array([10,20,30])
print(marks)
# In[106]:
import numpy as n
#The arange() method creates an array with evenly spaced elements as per the
intervals.
a= n.arange(10,100,2) #------------- arange(start,stop,step)
print(a)
# In[107]:
b= np.random.randint(4)
print(b)
# In[109]:
b=np.random.random(3) #--------------- size of random number required between 0 to
1
print(b)
# In[19]:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]]) #Create a 2-D array containing two arrays
with the values 1,2,3 and 4,5,6:
print(arr)
# In[110]:
# 2D Array
#Random value less than 5, so that 0,1,2,3,4 with 3 rows and 3 columns
a1= np.random.randint(3,size=(4,3))
print(a1)
# In[17]:
print(np.__version__) # np version
# In[9]:
# All the elements filled with 1
a1=np.ones((2,3))
print(a1)
# In[111]:
# All the elements filled with 0
a1=np.zeros((2,2))
print(a1)
# In[112]:
# All values to be same
c=np.full((2,3),'@')
print(c)
# In[ ]:
##### Arithemetic Operations on Array
# In[114]:
# Adding two Array
t1=np.array([50,60,70])
t2=np.array([50,50,-30])
t3=t1+t2
print("Addition of two term marks",t3)
# In[16]:
# difference two Array
t1=np.array([50,60,70])
t2=np.array([50,90,30])
t3=t1-t2
print("Difference between two term marks",t3)
# In[20]:
# Subtracting the array value with numeric value
total_stock=np.array([10,15,30,40,15])
item_sold=5
balance=total_stock-item_sold
print("Available Balance", balance)
# In[22]:
#multiplication
qty_sold=np.array([10,20,30])
price_per_unit=np.array([3,5,10])
total_cost=qty_sold*price_per_unit
print("Total Cost",total_cost)
# In[25]:
#Division
dist_traveled=np.array([300,100,200])
time_taken=np.array([2,3,5])
speed=dist_traveled/time_taken
print(speed)
# In[27]:
#exponent
arr=np.array([10,20,3])
print("exponent are",arr**2)
# In[ ]:
#### Array Functions
# In[118]:
# dtype Returns the data type of the array
#arr=np.array([[1,3,4],[2,3,4]]) #----------------Two dimension
arr=np.array([1,2,3])
data_type=arr.dtype
print(data_type)
# In[119]:
#ndim -----Dimension to indicate whether the array is 1D,2D..
dimension=arr.ndim
print(dimension)
# In[ ]:
The shape of an array is the number of elements in each dimension.
Shape that returns a tuple with each index having the number of corresponding
elements.
# In[95]:
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
arr1=np.array([[1,2,1],[3,4,4],[5,6,4]])
print(arr1.shape)
# In[46]:
## Size- Total number of element in the array
size=arr.size
print("total number of element",size)
s=np.array([[1,2,3],[3,4,4]])
size=s.size
print(size)
# In[120]:
## To find the maximum and minimum value of the array
a=np.array([-10,-3,-100])
max_value=np.max(a)
print("Maximum Value in the array",max_value)
min_value=np.min(a)
print("Minimum Value in the array",min_value)
# In[53]:
# To find max value in each row and column
x=np.array([[1,2,3],[-4,3,10]])
row=np.max(x,axis=1)
col=np.max(x,axis=0)
print("Row max= ", row)
print("column max= ",col)
# In[54]:
# To find min value in each row and column
x=np.array([[1,2,3],[-4,3,10]])
row=np.min(x,axis=1)
col=np.min(x,axis=0)
print("Row min= ", row)
print("column min= ",col)
# In[55]:
# To sort in Ascending Order
arr=np.array([10,4,2,40,23])
sort=np.sort(arr)
print("Sorted array",sort)
# In[56]:
# To sort in Descending Order
arr=np.array([10,4,2,40,23])
sort=np.sort(arr)[::-1]
print("Sorted array",sort)
# In[57]:
# Sum of array elements
arr=np.array([1,2,3])
sum_value=np.sum(arr)
print("Sum of array elements= ", sum_value)
# In[58]:
# Creating two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Joining along rows (vertical)
concatenated_array = np.concatenate((arr1, arr2))
print("Concatenated Array:")
print(concatenated_array)
# In[104]:
import numpy as np
# Creating two 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Joining along rows (vertical stacking, axis=0)
concatenated_axis0 = np.concatenate((arr1, arr2), axis=0)
# Joining along columns (horizontal stacking, axis=1)
concatenated_axis1 = np.concatenate((arr1, arr2.T), axis=1)
print("Concatenated Along Rows (Axis 0):")
print(concatenated_axis0)
print("Concatenated Along Columns (Axis 1):")
print(concatenated_axis1)
# In[122]:
#Splitting Arrays -
#numpy.array_split() divides an array into multiple sub-
arrays.
arr = np.array([1, 2, 3, 4, 5, 6,7])
# Splitting into three parts
split_array = np.array_split(arr, 3)
print(split_array)
# Accessing split arrays
print(split_array[1])
# In[72]:
#searching
#numpy.where:() It returns the indices of elements in an input array where the
given condition is satisfied
# creating the array
arr = np.array([10, 32, 30, 50, 20, 82, 91, 45])
print("arr = {}".format(arr))
# looking for value 30 in arr and storing its index in i
i = np.where(arr == 30)
print("i = {}".format(i[0]))
# In[ ]:
Slicing arrays
Slicing in python means taking elements from one given index to another given
index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1
# In[81]:
#Slice elements from index 1 to index 5 from the following array
arr = np.array([10, 20, 30, 40, 50, 60, 70])
print(arr[1:5]) #The result includes the start index, but excludes the end index
# In[ ]:
Iterating Arrays
Iterating means going through elements one by one.
As we deal with multi-dimensional arrays in numpy, we can do this using basic for
loop of python.
If we iterate on a 1-D array it will go through each element one by one.
# In[82]:
arr = np.array([1, 2, 3])
for x in arr:
print(x)
# In[84]:
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
for y in x:
print(y)
# In[ ]:
copying an array refers to the process of creating a new array that contains all
the elements of the original array.
This operation can be done using assignment operator (=)
# In[89]:
a = np.array([110, 220, 330, 440, 550])
b = a
print("Actual Array",a)
print("Copied array:",b)
# In[ ]:
identity array of dimension n x n, with its main diagonal set to one, and all
other elements 0.
# In[98]:
a = np.identity(3)
print("\nMatrix a : \n", a)
# In[ ]: