0% found this document useful (0 votes)
16 views15 pages

ML Labs

The document provides an overview of various operations using NumPy and Pandas, including statistical calculations (mean, median, standard deviation) and array manipulations (filtering, broadcasting). It also covers the creation and manipulation of Pandas Series, including indexing and handling missing values. The document illustrates these concepts through code snippets and their outputs.

Uploaded by

donotsmile9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

ML Labs

The document provides an overview of various operations using NumPy and Pandas, including statistical calculations (mean, median, standard deviation) and array manipulations (filtering, broadcasting). It also covers the creation and manipulation of Pandas Series, including indexing and handling missing values. The document illustrates these concepts through code snippets and their outputs.

Uploaded by

donotsmile9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1/18/25, 10:18 AM LAB 1_4 ML

In [3]: ## Mean, Median, Minimum, Maximum, Std dev and Variance

In [5]: import numpy as np

In [20]: arr = np.array([120,165,150,150,140])


print('mean = ',np.mean(arr))
print('median = ',np.median(arr))
print('minimum = ',np.min(arr))
print('maximum = ',np.max(arr))
print('index of minimum = ',np.argmin(arr))
print('index of maximum = ',np.argmax(arr))
print('sorted array= ',np.sort(arr), '\ndescending order',np.sort(arr)[::-1])
print('stdev',np.std(arr))
print('variance',np.var(arr))

mean = 145.0
median = 150.0
minimum = 120
maximum = 165
index of minimum = 0
index of maximum = 1
sorted array= [120 140 150 150 165]
descending order [165 150 150 140 120]
stdev 14.832396974191326
variance 220.0

In [21]: ##Query/filtering an array

In [35]: std_name = np.array(['Anil','Rohit','Dinesh','Mohit','Khushi','Deepti'])


physics_marks = np.array([65,82,91,55,96,95])
x = np.where(physics_marks>75)
y = np.where((physics_marks>75) & ((physics_marks<92) | (physics_marks%2 == 1)))
z = np.where((physics_marks>75) & ((physics_marks<93) | (physics_marks%2==1)),"P
print(x)
print(std_name[x])
print(std_name[y])
print(z)

(array([1, 2, 4, 5]),)
['Rohit' 'Dinesh' 'Khushi' 'Deepti']
['Rohit' 'Dinesh' 'Deepti']
['Fail' 'Pass' 'Pass' 'Fail' 'Fail' 'Pass']

In [42]: arr = np.array([1,2,3,4,5,6,7,8,9])


# Define conditions
condition1 = arr % 2 == 0 #Select even numbers
condition2 = arr>5
# Modidf array elements using where()
filtered_arr = np.where(condition1 & condition2, arr*2,arr)
print(filtered_arr)

[ 1 2 3 4 5 12 7 16 9]

np.select to handle mutiple arrays and mutiple


conditions

file:///C:/Users/231b078/Downloads/LAB 1_4 ML.html 1/2


1/18/25, 10:18 AM LAB 1_4 ML

In [50]: arr1 = np.array([1,2,3,4,5])


arr2 = arr1 *2
conditions = [arr1 >2 ,arr1 <5]
print('conditions',conditions,'\n')
choices = [arr1,arr2]
print('choices',choices,'\n')
result =np.select(conditions,choices,default =0)
print('result',result)

conditions [array([False, False, True, True, True]), array([ True, True, Tru
e, True, False])]

choices [array([1, 2, 3, 4, 5]), array([ 2, 4, 6, 8, 10])]

result [2 4 3 4 5]

In [ ]:

file:///C:/Users/231b078/Downloads/LAB 1_4 ML.html 2/2


1/18/25, 10:35 AM LAB 1_5 ML

In [2]: #Vector Operations

In [3]: import numpy as np

In [4]: marks = np.array([65,82,91,55,90,85])


bonus_marks = np.array([1,3,5,0,2,1])
final_marks = marks + bonus_marks
final_marks

Out[4]: array([66, 85, 96, 55, 92, 86])

In [8]: #Broadcast

In [9]: arr = np.array([5,10,15])


brr = np.array([6])
crr = arr * brr # broadcast * operation
crr

Out[9]: array([30, 60, 90])

In [10]: arr = np.array([5,10,15])


brr = arr + 10
print(brr)
crr = arr * 10
print(crr)

[15 20 25]
[ 50 100 150]

In [11]: #Linspace
# Returns the given number of evenly spaced vaues between the given intervals. T

In [14]: ##np.linspace(start,stop,num=50,endpoint=Ture,restep=False,dtype=None,axis=0)

In [20]: arr = np.linspace(0,10)


print(arr)
print('Length of arr: ',len(arr))
## Number of values = 3
brr = np.linspace(0,10,3)
print(brr)
print('Length of brr',len(brr))

[ 0. 0.20408163 0.40816327 0.6122449 0.81632653 1.02040816


1.2244898 1.42857143 1.63265306 1.83673469 2.04081633 2.24489796
2.44897959 2.65306122 2.85714286 3.06122449 3.26530612 3.46938776
3.67346939 3.87755102 4.08163265 4.28571429 4.48979592 4.69387755
4.89795918 5.10204082 5.30612245 5.51020408 5.71428571 5.91836735
6.12244898 6.32653061 6.53061224 6.73469388 6.93877551 7.14285714
7.34693878 7.55102041 7.75510204 7.95918367 8.16326531 8.36734694
8.57142857 8.7755102 8.97959184 9.18367347 9.3877551 9.59183673
9.79591837 10. ]
Length of arr: 50
[ 0. 5. 10.]
Length of brr 3

In [18]: arr = np.linspace(0,10,2)


print(arr)

file:///C:/Users/231b078/Downloads/LAB 1_5 ML.html 1/2


1/18/25, 10:35 AM LAB 1_5 ML

[ 0. 10.]

In [ ]:

file:///C:/Users/231b078/Downloads/LAB 1_5 ML.html 2/2


1/25/25, 10:33 AM LAB 1_1

Understanding Pandas
Collection of one col -> Series

Collection of series -> DataFrame

In [41]: import pandas as pd


import numpy as np

Series

is a 1-D labeled array capabale of holding values. The axis labels are collectively
referred to as the index.

In [42]: # Create a series


ser = pd.Series([1,2,3,4,5])
ser

Out[42]: 0 1
1 2
2 3
3 4
4 5
dtype: int64

In [43]: ser.index

Out[43]: RangeIndex(start=0, stop=5, step=1)

In [44]: ser[1]

Out[44]: np.int64(2)

In [46]: # Creae a sieries with custom index


ser = pd.Series([1,2,3,4,5],index=['a','b','c','d','e',])
ser

Out[46]: a 1
b 2
c 3
d 4
e 5
dtype: int64

In [47]: ser.index

Out[47]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

In [48]: ser['b']

Out[48]: np.int64(2)

In [49]: # Creating series from dictinory


dict_1 = {"monday":142.75,"tuesday":115.02,"wednesday":114.5,"Thursday":116}

file:///C:/Users/231b078/Downloads/LAB 1_1.html 1/3


1/25/25, 10:33 AM LAB 1_1

ser = pd.Series(dict_1)
ser

Out[49]: monday 142.75


tuesday 115.02
wednesday 114.50
Thursday 116.00
dtype: float64

In [50]: ser['wednesday']

Out[50]: np.float64(114.5)

In [51]: # Create series from numpy array


# Create series from numpy array
arr = np.array([12,13,10,11,14,15,9])
ser = pd.Series(arr)
ser

Out[51]: 0 12
1 13
2 10
3 11
4 14
5 15
6 9
dtype: int64

In [54]: print('ser.count() = ',ser.count())


print('ser.mean() = ',ser.mean())
print('ser.medidan() = ',ser.median())
print('ser.max() = ',ser.max())

ser.count() = 7
ser.mean() = 12.0
ser.medidan() = 12.0
ser.max() = 15

In [59]: # stats of the data


ser.describe()

Out[59]: count 7.000000


mean 12.000000
std 2.160247
min 9.000000
25% 10.500000
50% 12.000000
75% 13.500000
max 15.000000
dtype: float64

In [80]: print('ser.argmin() = ',ser.argmin())


print('ser.argmax() = ',ser.argmax())
print('ser.argsort() =')
print(ser.argsort())
newser = ser.argsort()
newser

file:///C:/Users/231b078/Downloads/LAB 1_1.html 2/3


1/25/25, 10:33 AM LAB 1_1

ser.argmin() = 2
ser.argmax() = 1
ser.argsort() =
a 2
b 0
c 3
d -1
e -1
f -1
g 1
dtype: int64
C:\Users\231b078\AppData\Local\Temp\ipykernel_7980\1997554770.py:4: FutureWarnin
g: The behavior of Series.argsort in the presence of NA values is deprecated. In
a future version, NA values will be ordered last instead of set to -1.
print(ser.argsort())
C:\Users\231b078\AppData\Local\Temp\ipykernel_7980\1997554770.py:5: FutureWarnin
g: The behavior of Series.argsort in the presence of NA values is deprecated. In
a future version, NA values will be ordered last instead of set to -1.
newser = ser.argsort()
Out[80]: a 2
b 0
c 3
d -1
e -1
f -1
g 1
dtype: int64

In [81]: d = {'a':10,'b':21,'c':5,'d':24,'g':17}
ser = pd.Series(d,index=['a','b','c','d','e','f','g'])

In [84]: print(ser.isna()) #isna -> isnull


print('-----------------')
print(ser.isna().sum())
print('-----------------')
print(ser.isnull().sum())

a False
b False
c False
d False
e True
f True
g False
dtype: bool
-----------------
2
-----------------
2

In [ ]:

file:///C:/Users/231b078/Downloads/LAB 1_1.html 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy