DAY 1-Numpy - 18.12.24-Data Science
DAY 1-Numpy - 18.12.24-Data Science
/usr/bin/env python
# coding: utf-8
# In[1]:
# In[105]:
# In[2]:
# In[4]:
#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]:
# 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]:
# In[111]:
# In[112]:
# In[ ]:
# In[114]:
# In[16]:
# In[20]:
# 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[ ]:
# In[119]:
# In[ ]:
Shape that returns a tuple with each index having the number of corresponding
elements.
# In[95]:
print(arr.shape)
arr1=np.array([[1,2,1],[3,4,4],[5,6,4]])
print(arr1.shape)
# In[46]:
s=np.array([[1,2,3],[3,4,4]])
size=s.size
print(size)
# In[120]:
# In[53]:
# In[54]:
# In[55]:
# In[56]:
# In[57]:
# In[58]:
# In[104]:
import numpy as np
# Creating two 2D arrays
# In[122]:
#Splitting Arrays -
#numpy.array_split() divides an array into multiple sub-
arrays.
# In[72]:
#searching
#numpy.where:() It returns the indices of elements in an input array where the
given condition is satisfied
# In[ ]:
Slicing arrays
Slicing in python means taking elements from one given index to another given
index.
# In[81]:
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.
# In[82]:
for x in arr:
print(x)
# In[84]:
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]:
# 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[ ]: