12/13/24, 7:46 PM AI With Python Practicals
Practical 1 : Write Python program to Implement
Data Preparation using techniques like data
cleaning on dataset
1) Installation packages , Loading Dataset , Locate Missing
Data ,Show data Frame
In [1]: # 1. Install Anaconda...
# 2. Launch Jupyter...
# 3. Install necessary Packages..
# 4. Take any dataset and load it and perform operations...
import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
Out[1]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [2]: data.head(10)
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 1/73
12/13/24, 7:46 PM AI With Python Practicals
Out[2]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
5 15728773 Male 27 58000 0
6 15598044 Female 27 84000 0
7 15694829 Female 32 150000 1
8 15600575 Male 25 33000 0
9 15727311 Female 35 65000 0
In [3]: data.tail()
Out[3]: User ID Gender Age EstimatedSalary Purchased
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [4]: data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User ID 400 non-null int64
1 Gender 400 non-null object
2 Age 400 non-null int64
3 EstimatedSalary 400 non-null int64
4 Purchased 400 non-null int64
dtypes: int64(4), object(1)
memory usage: 15.8+ KB
In [5]: data.describe()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 2/73
12/13/24, 7:46 PM AI With Python Practicals
Out[5]: User ID Age EstimatedSalary Purchased
count 4.000000e+02 400.000000 400.000000 400.000000
mean 1.569154e+07 37.655000 69742.500000 0.357500
std 7.165832e+04 10.482877 34096.960282 0.479864
min 1.556669e+07 18.000000 15000.000000 0.000000
25% 1.562676e+07 29.750000 43000.000000 0.000000
50% 1.569434e+07 37.000000 70000.000000 0.000000
75% 1.575036e+07 46.000000 88000.000000 1.000000
max 1.581524e+07 60.000000 150000.000000 1.000000
In [6]: data.iloc[:,:] #Will print all rows and columns
Out[6]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [7]: data.iloc[:,[1,2]] # Will print all rows ---> 1st & 2nd column
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 3/73
12/13/24, 7:46 PM AI With Python Practicals
Out[7]: Gender Age
0 Male 19
1 Male 35
2 Female 26
3 Female 27
4 Male 19
... ... ...
395 Female 46
396 Male 51
397 Female 50
398 Male 36
399 Female 49
400 rows × 2 columns
In [8]: data.iloc[399,2] # Will print value of 399th row and 2nd column
49
Out[8]:
2) Data Cleansing technique (Drop the data , input missing
data, check duplicate value, drop duplicate value )
In [9]: import pandas as pd
import numpy as np
data=pd.read_csv('feedback.csv')
data
Out[9]: Rating Review Title Review Cust Name Date Review ID
0 4 Works Well product work fine philip oct 10, 2021 #123
1 3 good NaN elena oct 5,2021 NaN
2 5 Buy this buy this olivia NaN NaN
3 5 Amazing Product it works john sept 5 2022 #111
4 1 Doesn't work doesn't work abc dec 10 ,2021 NaN
5 2 good Go to buy this pqr NaN NaN
6 7 abc NaN abcd april 15,2000 #12345
7 10 laptop not bad emmy august 12,2021 #987
In [10]: data.isnull()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 4/73
12/13/24, 7:46 PM AI With Python Practicals
Out[10]: Rating Review Title Review Cust Name Date Review ID
0 False False False False False False
1 False False True False False True
2 False False False False True True
3 False False False False False False
4 False False False False False True
5 False False False False True True
6 False False True False False False
7 False False False False False False
In [11]: data.isnull().sum()
Rating 0
Out[11]:
Review Title 0
Review 2
Cust Name 0
Date 2
Review ID 4
dtype: int64
In [12]: remove = ['Cust Name']
print(data.drop(remove,inplace=True, axis=1)) ## axis=1 --> indicates column
## axis = 0 --> indicates row
#If you got any error so restart & run all cells...
None
In [13]: data ##Cust Name column is deleted...
Out[13]: Rating Review Title Review Date Review ID
0 4 Works Well product work fine oct 10, 2021 #123
1 3 good NaN oct 5,2021 NaN
2 5 Buy this buy this NaN NaN
3 5 Amazing Product it works sept 5 2022 #111
4 1 Doesn't work doesn't work dec 10 ,2021 NaN
5 2 good Go to buy this NaN NaN
6 7 abc NaN april 15,2000 #12345
7 10 laptop not bad august 12,2021 #987
In [14]: remove = ['Rating']
print(data.drop(remove,inplace=True, axis=1))
None
In [15]: data ##Rating column is deleted
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 5/73
12/13/24, 7:46 PM AI With Python Practicals
Out[15]: Review Title Review Date Review ID
0 Works Well product work fine oct 10, 2021 #123
1 good NaN oct 5,2021 NaN
2 Buy this buy this NaN NaN
3 Amazing Product it works sept 5 2022 #111
4 Doesn't work doesn't work dec 10 ,2021 NaN
5 good Go to buy this NaN NaN
6 abc NaN april 15,2000 #12345
7 laptop not bad august 12,2021 #987
In [16]: print(data['Date'].describe())
count 6
unique 6
top oct 10, 2021
freq 1
Name: Date, dtype: object
In [17]: print(data.duplicated())
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
dtype: bool
In [18]: print(data.drop_duplicates())
Review Title Review Date Review ID
0 Works Well product work fine oct 10, 2021 #123
1 good NaN oct 5,2021 NaN
2 Buy this buy this NaN NaN
3 Amazing Product it works sept 5 2022 #111
4 Doesn't work doesn't work dec 10 ,2021 NaN
5 good Go to buy this NaN NaN
6 abc NaN april 15,2000 #12345
7 laptop not bad august 12,2021 #987
Practical 2 : Write Python program to Implement
Data Preparation using techniques like data
filtration on dataset
1) Installation packages, Loading Dataset, Show data frame.
In [19]: ## Loading Dataset & Show table data...
import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 6/73
12/13/24, 7:46 PM AI With Python Practicals
Out[19]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
2) Data Filtration technique (Select Single and Multiple
column by label, Selecting columns by data type ,selecting
single Or multiple row , etc) ----- Follow practical no 17. -------
-
In [20]: ### Follow Practical no 17....
Practical 3 : Write Python program to Implement
feature engineering technique like one hot
encoding, outlier management on dataset
1) Installation packages, Loading Dataset , Show data frame ,
detect outlier
In [21]: import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 7/73
12/13/24, 7:46 PM AI With Python Practicals
Out[21]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [22]: ### ------ Outlier Management ----- A data point which is totally different from al
2) one hot encoding (convert text-based values into numeric
values)
In [23]: from sklearn.preprocessing import LabelEncoder
encode=LabelEncoder()
df=data
df.Gender=encode.fit_transform(df.Gender) ## The gender will be converted to 0 & 1
## '0' as Female && '1' as Male
df
Out[23]: User ID Gender Age EstimatedSalary Purchased
0 15624510 1 19 19000 0
1 15810944 1 35 20000 0
2 15668575 0 26 43000 0
3 15603246 0 27 57000 0
4 15804002 1 19 76000 0
... ... ... ... ... ...
395 15691863 0 46 41000 1
396 15706071 1 51 23000 1
397 15654296 0 50 20000 1
398 15755018 1 36 33000 0
399 15594041 0 49 36000 1
400 rows × 5 columns
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 8/73
12/13/24, 7:46 PM AI With Python Practicals
In [24]: from sklearn.preprocessing import OneHotEncoder
## One hot encoading --> Convert categorical data into numeric data
# creating one hot encoder object
encode = OneHotEncoder()
enc_df=data
enc_df = pd.DataFrame(encode.fit_transform(enc_df[['Gender']]).toarray())
enc_df
Out[24]: 0 1
0 0.0 1.0
1 0.0 1.0
2 1.0 0.0
3 1.0 0.0
4 0.0 1.0
... ... ...
395 1.0 0.0
396 0.0 1.0
397 1.0 0.0
398 0.0 1.0
399 1.0 0.0
400 rows × 2 columns
In [25]: abc=df.join(enc_df)
abc
Out[25]: User ID Gender Age EstimatedSalary Purchased 0 1
0 15624510 1 19 19000 0 0.0 1.0
1 15810944 1 35 20000 0 0.0 1.0
2 15668575 0 26 43000 0 1.0 0.0
3 15603246 0 27 57000 0 1.0 0.0
4 15804002 1 19 76000 0 0.0 1.0
... ... ... ... ... ... ... ...
395 15691863 0 46 41000 1 1.0 0.0
396 15706071 1 51 23000 1 0.0 1.0
397 15654296 0 50 20000 1 1.0 0.0
398 15755018 1 36 33000 0 0.0 1.0
399 15594041 0 49 36000 1 1.0 0.0
400 rows × 7 columns
In [26]: final = abc.drop(['Gender'], axis='columns')
final
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 9/73
12/13/24, 7:46 PM AI With Python Practicals
Out[26]: User ID Age EstimatedSalary Purchased 0 1
0 15624510 19 19000 0 0.0 1.0
1 15810944 35 20000 0 0.0 1.0
2 15668575 26 43000 0 1.0 0.0
3 15603246 27 57000 0 1.0 0.0
4 15804002 19 76000 0 0.0 1.0
... ... ... ... ... ... ...
395 15691863 46 41000 1 1.0 0.0
396 15706071 51 23000 1 0.0 1.0
397 15654296 50 20000 1 1.0 0.0
398 15755018 36 33000 0 0.0 1.0
399 15594041 49 36000 1 1.0 0.0
400 rows × 6 columns
Practical 4 : Write Python program to Implement
logistic regression classifier on dataset
1) Installation packages , Loading Dataset , Show data frame .
In [27]: import pandas as pd
import numpy as np
data=pd.read_csv('User_data.csv')
data
Out[27]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 10/73
12/13/24, 7:46 PM AI With Python Practicals
2) Implement logistic regression classifier with score
In [28]: data.head()
Out[28]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
In [29]: data.tail(10)
Out[29]: User ID Gender Age EstimatedSalary Purchased
390 15807837 Male 48 33000 1
391 15592570 Male 47 23000 1
392 15748589 Female 45 45000 1
393 15635893 Male 60 42000 1
394 15757632 Female 39 59000 0
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [30]: x=data.iloc[:,[2,3]].values ## Independent varibale
In [31]: y=data.iloc[:,4].values ## dependent variable
In [32]: data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 11/73
12/13/24, 7:46 PM AI With Python Practicals
Out[32]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [33]: x
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 12/73
12/13/24, 7:46 PM AI With Python Practicals
array([[ 19, 19000],
Out[33]:
[ 35, 20000],
[ 26, 43000],
[ 27, 57000],
[ 19, 76000],
[ 27, 58000],
[ 27, 84000],
[ 32, 150000],
[ 25, 33000],
[ 35, 65000],
[ 26, 80000],
[ 26, 52000],
[ 20, 86000],
[ 32, 18000],
[ 18, 82000],
[ 29, 80000],
[ 47, 25000],
[ 45, 26000],
[ 46, 28000],
[ 48, 29000],
[ 45, 22000],
[ 47, 49000],
[ 48, 41000],
[ 45, 22000],
[ 46, 23000],
[ 47, 20000],
[ 49, 28000],
[ 47, 30000],
[ 29, 43000],
[ 31, 18000],
[ 31, 74000],
[ 27, 137000],
[ 21, 16000],
[ 28, 44000],
[ 27, 90000],
[ 35, 27000],
[ 33, 28000],
[ 30, 49000],
[ 26, 72000],
[ 27, 31000],
[ 27, 17000],
[ 33, 51000],
[ 35, 108000],
[ 30, 15000],
[ 28, 84000],
[ 23, 20000],
[ 25, 79000],
[ 27, 54000],
[ 30, 135000],
[ 31, 89000],
[ 24, 32000],
[ 18, 44000],
[ 29, 83000],
[ 35, 23000],
[ 27, 58000],
[ 24, 55000],
[ 23, 48000],
[ 28, 79000],
[ 22, 18000],
[ 32, 117000],
[ 27, 20000],
[ 25, 87000],
[ 23, 66000],
[ 32, 120000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 13/73
12/13/24, 7:46 PM AI With Python Practicals
[ 59, 83000],
[ 24, 58000],
[ 24, 19000],
[ 23, 82000],
[ 22, 63000],
[ 31, 68000],
[ 25, 80000],
[ 24, 27000],
[ 20, 23000],
[ 33, 113000],
[ 32, 18000],
[ 34, 112000],
[ 18, 52000],
[ 22, 27000],
[ 28, 87000],
[ 26, 17000],
[ 30, 80000],
[ 39, 42000],
[ 20, 49000],
[ 35, 88000],
[ 30, 62000],
[ 31, 118000],
[ 24, 55000],
[ 28, 85000],
[ 26, 81000],
[ 35, 50000],
[ 22, 81000],
[ 30, 116000],
[ 26, 15000],
[ 29, 28000],
[ 29, 83000],
[ 35, 44000],
[ 35, 25000],
[ 28, 123000],
[ 35, 73000],
[ 28, 37000],
[ 27, 88000],
[ 28, 59000],
[ 32, 86000],
[ 33, 149000],
[ 19, 21000],
[ 21, 72000],
[ 26, 35000],
[ 27, 89000],
[ 26, 86000],
[ 38, 80000],
[ 39, 71000],
[ 37, 71000],
[ 38, 61000],
[ 37, 55000],
[ 42, 80000],
[ 40, 57000],
[ 35, 75000],
[ 36, 52000],
[ 40, 59000],
[ 41, 59000],
[ 36, 75000],
[ 37, 72000],
[ 40, 75000],
[ 35, 53000],
[ 41, 51000],
[ 39, 61000],
[ 42, 65000],
[ 26, 32000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 14/73
12/13/24, 7:46 PM AI With Python Practicals
[ 30, 17000],
[ 26, 84000],
[ 31, 58000],
[ 33, 31000],
[ 30, 87000],
[ 21, 68000],
[ 28, 55000],
[ 23, 63000],
[ 20, 82000],
[ 30, 107000],
[ 28, 59000],
[ 19, 25000],
[ 19, 85000],
[ 18, 68000],
[ 35, 59000],
[ 30, 89000],
[ 34, 25000],
[ 24, 89000],
[ 27, 96000],
[ 41, 30000],
[ 29, 61000],
[ 20, 74000],
[ 26, 15000],
[ 41, 45000],
[ 31, 76000],
[ 36, 50000],
[ 40, 47000],
[ 31, 15000],
[ 46, 59000],
[ 29, 75000],
[ 26, 30000],
[ 32, 135000],
[ 32, 100000],
[ 25, 90000],
[ 37, 33000],
[ 35, 38000],
[ 33, 69000],
[ 18, 86000],
[ 22, 55000],
[ 35, 71000],
[ 29, 148000],
[ 29, 47000],
[ 21, 88000],
[ 34, 115000],
[ 26, 118000],
[ 34, 43000],
[ 34, 72000],
[ 23, 28000],
[ 35, 47000],
[ 25, 22000],
[ 24, 23000],
[ 31, 34000],
[ 26, 16000],
[ 31, 71000],
[ 32, 117000],
[ 33, 43000],
[ 33, 60000],
[ 31, 66000],
[ 20, 82000],
[ 33, 41000],
[ 35, 72000],
[ 28, 32000],
[ 24, 84000],
[ 19, 26000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 15/73
12/13/24, 7:46 PM AI With Python Practicals
[ 29, 43000],
[ 19, 70000],
[ 28, 89000],
[ 34, 43000],
[ 30, 79000],
[ 20, 36000],
[ 26, 80000],
[ 35, 22000],
[ 35, 39000],
[ 49, 74000],
[ 39, 134000],
[ 41, 71000],
[ 58, 101000],
[ 47, 47000],
[ 55, 130000],
[ 52, 114000],
[ 40, 142000],
[ 46, 22000],
[ 48, 96000],
[ 52, 150000],
[ 59, 42000],
[ 35, 58000],
[ 47, 43000],
[ 60, 108000],
[ 49, 65000],
[ 40, 78000],
[ 46, 96000],
[ 59, 143000],
[ 41, 80000],
[ 35, 91000],
[ 37, 144000],
[ 60, 102000],
[ 35, 60000],
[ 37, 53000],
[ 36, 126000],
[ 56, 133000],
[ 40, 72000],
[ 42, 80000],
[ 35, 147000],
[ 39, 42000],
[ 40, 107000],
[ 49, 86000],
[ 38, 112000],
[ 46, 79000],
[ 40, 57000],
[ 37, 80000],
[ 46, 82000],
[ 53, 143000],
[ 42, 149000],
[ 38, 59000],
[ 50, 88000],
[ 56, 104000],
[ 41, 72000],
[ 51, 146000],
[ 35, 50000],
[ 57, 122000],
[ 41, 52000],
[ 35, 97000],
[ 44, 39000],
[ 37, 52000],
[ 48, 134000],
[ 37, 146000],
[ 50, 44000],
[ 52, 90000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 16/73
12/13/24, 7:46 PM AI With Python Practicals
[ 41, 72000],
[ 40, 57000],
[ 58, 95000],
[ 45, 131000],
[ 35, 77000],
[ 36, 144000],
[ 55, 125000],
[ 35, 72000],
[ 48, 90000],
[ 42, 108000],
[ 40, 75000],
[ 37, 74000],
[ 47, 144000],
[ 40, 61000],
[ 43, 133000],
[ 59, 76000],
[ 60, 42000],
[ 39, 106000],
[ 57, 26000],
[ 57, 74000],
[ 38, 71000],
[ 49, 88000],
[ 52, 38000],
[ 50, 36000],
[ 59, 88000],
[ 35, 61000],
[ 37, 70000],
[ 52, 21000],
[ 48, 141000],
[ 37, 93000],
[ 37, 62000],
[ 48, 138000],
[ 41, 79000],
[ 37, 78000],
[ 39, 134000],
[ 49, 89000],
[ 55, 39000],
[ 37, 77000],
[ 35, 57000],
[ 36, 63000],
[ 42, 73000],
[ 43, 112000],
[ 45, 79000],
[ 46, 117000],
[ 58, 38000],
[ 48, 74000],
[ 37, 137000],
[ 37, 79000],
[ 40, 60000],
[ 42, 54000],
[ 51, 134000],
[ 47, 113000],
[ 36, 125000],
[ 38, 50000],
[ 42, 70000],
[ 39, 96000],
[ 38, 50000],
[ 49, 141000],
[ 39, 79000],
[ 39, 75000],
[ 54, 104000],
[ 35, 55000],
[ 45, 32000],
[ 36, 60000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 17/73
12/13/24, 7:46 PM AI With Python Practicals
[ 52, 138000],
[ 53, 82000],
[ 41, 52000],
[ 48, 30000],
[ 48, 131000],
[ 41, 60000],
[ 41, 72000],
[ 42, 75000],
[ 36, 118000],
[ 47, 107000],
[ 38, 51000],
[ 48, 119000],
[ 42, 65000],
[ 40, 65000],
[ 57, 60000],
[ 36, 54000],
[ 58, 144000],
[ 35, 79000],
[ 38, 55000],
[ 39, 122000],
[ 53, 104000],
[ 35, 75000],
[ 38, 65000],
[ 47, 51000],
[ 47, 105000],
[ 41, 63000],
[ 53, 72000],
[ 54, 108000],
[ 39, 77000],
[ 38, 61000],
[ 38, 113000],
[ 37, 75000],
[ 42, 90000],
[ 37, 57000],
[ 36, 99000],
[ 60, 34000],
[ 54, 70000],
[ 41, 72000],
[ 40, 71000],
[ 42, 54000],
[ 43, 129000],
[ 53, 34000],
[ 47, 50000],
[ 42, 79000],
[ 42, 104000],
[ 59, 29000],
[ 58, 47000],
[ 46, 88000],
[ 38, 71000],
[ 54, 26000],
[ 60, 46000],
[ 60, 83000],
[ 39, 73000],
[ 59, 130000],
[ 37, 80000],
[ 46, 32000],
[ 46, 74000],
[ 42, 53000],
[ 41, 87000],
[ 58, 23000],
[ 42, 64000],
[ 48, 33000],
[ 44, 139000],
[ 49, 28000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 18/73
12/13/24, 7:46 PM AI With Python Practicals
[ 57, 33000],
[ 56, 60000],
[ 49, 39000],
[ 39, 71000],
[ 47, 34000],
[ 48, 35000],
[ 48, 33000],
[ 47, 23000],
[ 45, 45000],
[ 60, 42000],
[ 39, 59000],
[ 46, 41000],
[ 51, 23000],
[ 50, 20000],
[ 36, 33000],
[ 49, 36000]], dtype=int64)
In [34]: y
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
Out[34]:
1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1], dtype=int64)
In [35]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=20)
In [36]: from sklearn.linear_model import LogisticRegression
cs = LogisticRegression()
cs.fit(x_train, y_train)
Out[36]: ▾ LogisticRegression
LogisticRegression()
In [37]: y_pred=cs.predict(x_test)
y_pred ## predicted output
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Out[37]:
dtype=int64)
In [38]: y_test ## Testing data
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
Out[38]:
dtype=int64)
In [39]: from sklearn.metrics import accuracy_score
print("Accuracy :",accuracy_score(y_test,y_pred)*100)
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 19/73
12/13/24, 7:46 PM AI With Python Practicals
Accuracy : 90.0
Practical 5 : Write Python program to Implement
Naïve Bayes classifier on dataset
1) Installation packages , Loading breast_cancer dataset from
sklearn , Show data frame .
In [40]: import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
data=load_breast_cancer()
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 20/73
12/13/24, 7:46 PM AI With Python Practicals
{'data': array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,
Out[40]:
1.189e-01],
[2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
8.902e-02],
[1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
8.758e-02],
...,
[1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
7.820e-02],
[2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
1.240e-01],
[7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
7.039e-02]]),
'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1]),
'frame': None,
'target_names': array(['malignant', 'benign'], dtype='<U9'),
'DESCR': '.. _breast_cancer_dataset:\n\nBreast cancer wisconsin (diagnostic) data
set\n--------------------------------------------\n\n**Data Set Characteristics:**
\n\n :Number of Instances: 569\n\n :Number of Attributes: 30 numeric, predic
tive attributes and the class\n\n :Attribute Information:\n - radius (me
an of distances from center to points on the perimeter)\n - texture (standa
rd deviation of gray-scale values)\n - perimeter\n - area\n -
smoothness (local variation in radius lengths)\n - compactness (perimeter^2
/ area - 1.0)\n - concavity (severity of concave portions of the contour)\n
- concave points (number of concave portions of the contour)\n - symmetry\n
- fractal dimension ("coastline approximation" - 1)\n\n The mean, standard
error, and "worst" or largest (mean of the three\n worst/largest values) of
these features were computed for each image,\n resulting in 30 features. F
or instance, field 0 is Mean Radius, field\n 10 is Radius SE, field 20 is W
orst Radius.\n\n - class:\n - WDBC-Malignant\n
- WDBC-Benign\n\n :Summary Statistics:\n\n =================================
==== ====== ======\n Min Max\n ===
================================== ====== ======\n radius (mean):
6.981 28.11\n texture (mean): 9.71 39.28\n perimete
r (mean): 43.79 188.5\n area (mean):
143.5 2501.0\n smoothness (mean): 0.053 0.163\n compact
ness (mean): 0.019 0.345\n concavity (mean):
0.0 0.427\n concave points (mean): 0.0 0.201\n symmetry
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 21/73
12/13/24, 7:46 PM AI With Python Practicals
(mean): 0.106 0.304\n fractal dimension (mean):
0.05 0.097\n radius (standard error): 0.112 2.873\n texture
(standard error): 0.36 4.885\n perimeter (standard error):
0.757 21.98\n area (standard error): 6.802 542.2\n smoothne
ss (standard error): 0.002 0.031\n compactness (standard error):
0.002 0.135\n concavity (standard error): 0.0 0.396\n concave
points (standard error): 0.0 0.053\n symmetry (standard error):
0.008 0.079\n fractal dimension (standard error): 0.001 0.03\n radius (w
orst): 7.93 36.04\n texture (worst):
12.02 49.54\n perimeter (worst): 50.41 251.2\n area (wo
rst): 185.2 4254.0\n smoothness (worst):
0.071 0.223\n compactness (worst): 0.027 1.058\n concavit
y (worst): 0.0 1.252\n concave points (worst):
0.0 0.291\n symmetry (worst): 0.156 0.664\n fractal
dimension (worst): 0.055 0.208\n ==================================
=== ====== ======\n\n :Missing Attribute Values: None\n\n :Class Distributio
n: 212 - Malignant, 357 - Benign\n\n :Creator: Dr. William H. Wolberg, W. Nick
Street, Olvi L. Mangasarian\n\n :Donor: Nick Street\n\n :Date: November, 199
5\n\nThis is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.\nhttp
s://goo.gl/U2Uwz2\n\nFeatures are computed from a digitized image of a fine needle
\naspirate (FNA) of a breast mass. They describe\ncharacteristics of the cell nuc
lei present in the image.\n\nSeparating plane described above was obtained using\n
Multisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree\nConstruction Via
Linear Programming." Proceedings of the 4th\nMidwest Artificial Intelligence and C
ognitive Science Society,\npp. 97-101, 1992], a classification method which uses l
inear\nprogramming to construct a decision tree. Relevant features\nwere selected
using an exhaustive search in the space of 1-4\nfeatures and 1-3 separating plane
s.\n\nThe actual linear program used to obtain the separating plane\nin the 3-dime
nsional space is that described in:\n[K. P. Bennett and O. L. Mangasarian: "Robust
Linear\nProgramming Discrimination of Two Linearly Inseparable Sets",\nOptimizatio
n Methods and Software 1, 1992, 23-34].\n\nThis database is also available through
the UW CS ftp server:\n\nftp ftp.cs.wisc.edu\ncd math-prog/cpo-dataset/machine-lea
rn/WDBC/\n\n.. topic:: References\n\n - W.N. Street, W.H. Wolberg and O.L. Manga
sarian. Nuclear feature extraction \n for breast tumor diagnosis. IS&T/SPIE 19
93 International Symposium on \n Electronic Imaging: Science and Technology, v
olume 1905, pages 861-870,\n San Jose, CA, 1993.\n - O.L. Mangasarian, W.N.
Street and W.H. Wolberg. Breast cancer diagnosis and \n prognosis via linear p
rogramming. Operations Research, 43(4), pages 570-577, \n July-August 1995.\n
- W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques\n
to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) \n
163-171.',
'feature_names': array(['mean radius', 'mean texture', 'mean perimeter', 'mean ar
ea',
'mean smoothness', 'mean compactness', 'mean concavity',
'mean concave points', 'mean symmetry', 'mean fractal dimension',
'radius error', 'texture error', 'perimeter error', 'area error',
'smoothness error', 'compactness error', 'concavity error',
'concave points error', 'symmetry error',
'fractal dimension error', 'worst radius', 'worst texture',
'worst perimeter', 'worst area', 'worst smoothness',
'worst compactness', 'worst concavity', 'worst concave points',
'worst symmetry', 'worst fractal dimension'], dtype='<U23'),
'filename': 'breast_cancer.csv',
'data_module': 'sklearn.datasets.data'}
2) Implement Naïve Bayes classifier using GaussianNB model
and predict value.
In [41]: x=data.data ## Independent varibale
x
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 22/73
12/13/24, 7:46 PM AI With Python Practicals
array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,
Out[41]:
1.189e-01],
[2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
8.902e-02],
[1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
8.758e-02],
...,
[1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
7.820e-02],
[2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
1.240e-01],
[7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
7.039e-02]])
In [42]: y=data.target ## Dependent varibale
y
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
Out[42]:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1])
In [43]: data.target_names ## It will display taeget variable..that we want to predict
array(['malignant', 'benign'], dtype='<U9')
Out[43]:
In [44]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=30)
In [45]: from sklearn.naive_bayes import GaussianNB
cs=GaussianNB()
cs.fit(x_train,y_train)
Out[45]: ▾ GaussianNB
GaussianNB()
In [46]: y_pred=cs.predict(x_test)
y_pred ## Traget varibale
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 23/73
12/13/24, 7:46 PM AI With Python Practicals
array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
Out[46]:
1, 1, 0, 1, 1, 0, 1, 0])
In [47]: y_test ## Testing data... which will be used for prediction
array([0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0,
Out[47]:
1, 1, 0, 1, 1, 0, 1, 0])
In [48]: from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)
cm
array([[10, 0],
Out[48]:
[ 3, 17]], dtype=int64)
In [49]: from sklearn.metrics import accuracy_score
print("Accuracy :",accuracy_score(y_test,y_pred)*100)
Accuracy : 90.0
Practical 6 : Write Python program to use of
confusion matrixes to describe performance of
classifier on dataset
1) Installation packages , Loading dataset, Show data frame , Create confusion_matrix 2)
Describe accuracy_score, precision_score, recall_score, f1_score using confusion matrix
Practical 7 : Write Python program to implement
classifier using support vector machines.
1) Installation packages , Loading dataset, Show data frame , Create confusion_matrix 2)
Describe accuracy_score, precision_score, recall_score, f1_score using confusion matrix
In [50]: ### Practical 6 & 7 are Same almost...Use Support Vector Machine & Then calculate c
In [51]: import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 24/73
12/13/24, 7:46 PM AI With Python Practicals
Out[51]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [52]: x=data.iloc[:,[2,3]].values ## Independent variable
y=data.iloc[:,4].values ## dependent variable (target / Predict)
In [53]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=20,random_state=0)
In [54]: from sklearn.svm import SVC
cs=SVC()
cs.fit(x_train,y_train)
cs
Out[54]: ▾ SVC
SVC()
In [55]: y_pred=cs.predict(x_test)
y_pred ## Dependent variable
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
Out[55]:
dtype=int64)
In [56]: #### ----- Confusion Matrix - Precision - Recall - F1 Score - Accuracy ------
from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test,y_pred)
cm
array([[17, 1],
Out[56]:
[ 0, 2]], dtype=int64)
In [57]: ## Accuracy
from sklearn.metrics import accuracy_score
print("Accuracy :" , accuracy_score(y_pred,y_test)*100)
Accuracy : 95.0
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 25/73
12/13/24, 7:46 PM AI With Python Practicals
In [58]: ## Precision - Recall - F1 Score
from sklearn.metrics import precision_score,recall_score,f1_score
precision=precision_score(y_test,y_pred)*100
print("precision :",precision)
recall=recall_score(y_test,y_pred)*100
print("recall :",recall)
f1_score=f1_score(y_test,y_pred)*100
print("f1 Score :",f1_score)
precision : 66.66666666666666
recall : 100.0
f1 Score : 80.0
In [ ]:
Practical 8 : Write Python program to implement
classifier using support vector machines.
Follow Practical 6 & 7------ for SVM
1) Installation packages , Loading dataset, Show data frame, Create confusion_matrix 2)
Visualizing the train, test result in colormap & Show classification report
In [59]: ## 2. Visualizing the train, test result in colormap & Show classification rep
import seaborn as sns ## help to give color and some attractive properties to our
## we use heatmap methond of seaborn library to give color, and make our graph look
import matplotlib.pyplot as plt ## Used for plotting graph
plt.figure(figsize=(4,4)) ## We give size for our graph....
sns.heatmap(cm, annot=True, fmt='d', cmap='Greens')
## annot=True --- This adds the actual values to the heatmap cells.
## fmt='d' --- Formats the annotations as integers.
## cmap --- defines the color of our graph -- cmap='Greens' : so green colo
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 26/73
12/13/24, 7:46 PM AI With Python Practicals
In [ ]:
Practical 9 : Write Python program to Build a
decision tree classifier .
1) Installation packages , Loading dataset, Show data frame, Build a decision tree classifier 2)
Evaluate performance of a classifier by printing classification report.
In [60]: import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 27/73
12/13/24, 7:46 PM AI With Python Practicals
Out[60]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [61]: data.head()
Out[61]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
In [62]: data.tail(10)
Out[62]: User ID Gender Age EstimatedSalary Purchased
390 15807837 Male 48 33000 1
391 15592570 Male 47 23000 1
392 15748589 Female 45 45000 1
393 15635893 Male 60 42000 1
394 15757632 Female 39 59000 0
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [63]: data.info()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 28/73
12/13/24, 7:46 PM AI With Python Practicals
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User ID 400 non-null int64
1 Gender 400 non-null object
2 Age 400 non-null int64
3 EstimatedSalary 400 non-null int64
4 Purchased 400 non-null int64
dtypes: int64(4), object(1)
memory usage: 15.8+ KB
In [64]: x=data.iloc[:,[2,3]].values
x
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 29/73
12/13/24, 7:46 PM AI With Python Practicals
array([[ 19, 19000],
Out[64]:
[ 35, 20000],
[ 26, 43000],
[ 27, 57000],
[ 19, 76000],
[ 27, 58000],
[ 27, 84000],
[ 32, 150000],
[ 25, 33000],
[ 35, 65000],
[ 26, 80000],
[ 26, 52000],
[ 20, 86000],
[ 32, 18000],
[ 18, 82000],
[ 29, 80000],
[ 47, 25000],
[ 45, 26000],
[ 46, 28000],
[ 48, 29000],
[ 45, 22000],
[ 47, 49000],
[ 48, 41000],
[ 45, 22000],
[ 46, 23000],
[ 47, 20000],
[ 49, 28000],
[ 47, 30000],
[ 29, 43000],
[ 31, 18000],
[ 31, 74000],
[ 27, 137000],
[ 21, 16000],
[ 28, 44000],
[ 27, 90000],
[ 35, 27000],
[ 33, 28000],
[ 30, 49000],
[ 26, 72000],
[ 27, 31000],
[ 27, 17000],
[ 33, 51000],
[ 35, 108000],
[ 30, 15000],
[ 28, 84000],
[ 23, 20000],
[ 25, 79000],
[ 27, 54000],
[ 30, 135000],
[ 31, 89000],
[ 24, 32000],
[ 18, 44000],
[ 29, 83000],
[ 35, 23000],
[ 27, 58000],
[ 24, 55000],
[ 23, 48000],
[ 28, 79000],
[ 22, 18000],
[ 32, 117000],
[ 27, 20000],
[ 25, 87000],
[ 23, 66000],
[ 32, 120000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 30/73
12/13/24, 7:46 PM AI With Python Practicals
[ 59, 83000],
[ 24, 58000],
[ 24, 19000],
[ 23, 82000],
[ 22, 63000],
[ 31, 68000],
[ 25, 80000],
[ 24, 27000],
[ 20, 23000],
[ 33, 113000],
[ 32, 18000],
[ 34, 112000],
[ 18, 52000],
[ 22, 27000],
[ 28, 87000],
[ 26, 17000],
[ 30, 80000],
[ 39, 42000],
[ 20, 49000],
[ 35, 88000],
[ 30, 62000],
[ 31, 118000],
[ 24, 55000],
[ 28, 85000],
[ 26, 81000],
[ 35, 50000],
[ 22, 81000],
[ 30, 116000],
[ 26, 15000],
[ 29, 28000],
[ 29, 83000],
[ 35, 44000],
[ 35, 25000],
[ 28, 123000],
[ 35, 73000],
[ 28, 37000],
[ 27, 88000],
[ 28, 59000],
[ 32, 86000],
[ 33, 149000],
[ 19, 21000],
[ 21, 72000],
[ 26, 35000],
[ 27, 89000],
[ 26, 86000],
[ 38, 80000],
[ 39, 71000],
[ 37, 71000],
[ 38, 61000],
[ 37, 55000],
[ 42, 80000],
[ 40, 57000],
[ 35, 75000],
[ 36, 52000],
[ 40, 59000],
[ 41, 59000],
[ 36, 75000],
[ 37, 72000],
[ 40, 75000],
[ 35, 53000],
[ 41, 51000],
[ 39, 61000],
[ 42, 65000],
[ 26, 32000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 31/73
12/13/24, 7:46 PM AI With Python Practicals
[ 30, 17000],
[ 26, 84000],
[ 31, 58000],
[ 33, 31000],
[ 30, 87000],
[ 21, 68000],
[ 28, 55000],
[ 23, 63000],
[ 20, 82000],
[ 30, 107000],
[ 28, 59000],
[ 19, 25000],
[ 19, 85000],
[ 18, 68000],
[ 35, 59000],
[ 30, 89000],
[ 34, 25000],
[ 24, 89000],
[ 27, 96000],
[ 41, 30000],
[ 29, 61000],
[ 20, 74000],
[ 26, 15000],
[ 41, 45000],
[ 31, 76000],
[ 36, 50000],
[ 40, 47000],
[ 31, 15000],
[ 46, 59000],
[ 29, 75000],
[ 26, 30000],
[ 32, 135000],
[ 32, 100000],
[ 25, 90000],
[ 37, 33000],
[ 35, 38000],
[ 33, 69000],
[ 18, 86000],
[ 22, 55000],
[ 35, 71000],
[ 29, 148000],
[ 29, 47000],
[ 21, 88000],
[ 34, 115000],
[ 26, 118000],
[ 34, 43000],
[ 34, 72000],
[ 23, 28000],
[ 35, 47000],
[ 25, 22000],
[ 24, 23000],
[ 31, 34000],
[ 26, 16000],
[ 31, 71000],
[ 32, 117000],
[ 33, 43000],
[ 33, 60000],
[ 31, 66000],
[ 20, 82000],
[ 33, 41000],
[ 35, 72000],
[ 28, 32000],
[ 24, 84000],
[ 19, 26000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 32/73
12/13/24, 7:46 PM AI With Python Practicals
[ 29, 43000],
[ 19, 70000],
[ 28, 89000],
[ 34, 43000],
[ 30, 79000],
[ 20, 36000],
[ 26, 80000],
[ 35, 22000],
[ 35, 39000],
[ 49, 74000],
[ 39, 134000],
[ 41, 71000],
[ 58, 101000],
[ 47, 47000],
[ 55, 130000],
[ 52, 114000],
[ 40, 142000],
[ 46, 22000],
[ 48, 96000],
[ 52, 150000],
[ 59, 42000],
[ 35, 58000],
[ 47, 43000],
[ 60, 108000],
[ 49, 65000],
[ 40, 78000],
[ 46, 96000],
[ 59, 143000],
[ 41, 80000],
[ 35, 91000],
[ 37, 144000],
[ 60, 102000],
[ 35, 60000],
[ 37, 53000],
[ 36, 126000],
[ 56, 133000],
[ 40, 72000],
[ 42, 80000],
[ 35, 147000],
[ 39, 42000],
[ 40, 107000],
[ 49, 86000],
[ 38, 112000],
[ 46, 79000],
[ 40, 57000],
[ 37, 80000],
[ 46, 82000],
[ 53, 143000],
[ 42, 149000],
[ 38, 59000],
[ 50, 88000],
[ 56, 104000],
[ 41, 72000],
[ 51, 146000],
[ 35, 50000],
[ 57, 122000],
[ 41, 52000],
[ 35, 97000],
[ 44, 39000],
[ 37, 52000],
[ 48, 134000],
[ 37, 146000],
[ 50, 44000],
[ 52, 90000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 33/73
12/13/24, 7:46 PM AI With Python Practicals
[ 41, 72000],
[ 40, 57000],
[ 58, 95000],
[ 45, 131000],
[ 35, 77000],
[ 36, 144000],
[ 55, 125000],
[ 35, 72000],
[ 48, 90000],
[ 42, 108000],
[ 40, 75000],
[ 37, 74000],
[ 47, 144000],
[ 40, 61000],
[ 43, 133000],
[ 59, 76000],
[ 60, 42000],
[ 39, 106000],
[ 57, 26000],
[ 57, 74000],
[ 38, 71000],
[ 49, 88000],
[ 52, 38000],
[ 50, 36000],
[ 59, 88000],
[ 35, 61000],
[ 37, 70000],
[ 52, 21000],
[ 48, 141000],
[ 37, 93000],
[ 37, 62000],
[ 48, 138000],
[ 41, 79000],
[ 37, 78000],
[ 39, 134000],
[ 49, 89000],
[ 55, 39000],
[ 37, 77000],
[ 35, 57000],
[ 36, 63000],
[ 42, 73000],
[ 43, 112000],
[ 45, 79000],
[ 46, 117000],
[ 58, 38000],
[ 48, 74000],
[ 37, 137000],
[ 37, 79000],
[ 40, 60000],
[ 42, 54000],
[ 51, 134000],
[ 47, 113000],
[ 36, 125000],
[ 38, 50000],
[ 42, 70000],
[ 39, 96000],
[ 38, 50000],
[ 49, 141000],
[ 39, 79000],
[ 39, 75000],
[ 54, 104000],
[ 35, 55000],
[ 45, 32000],
[ 36, 60000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 34/73
12/13/24, 7:46 PM AI With Python Practicals
[ 52, 138000],
[ 53, 82000],
[ 41, 52000],
[ 48, 30000],
[ 48, 131000],
[ 41, 60000],
[ 41, 72000],
[ 42, 75000],
[ 36, 118000],
[ 47, 107000],
[ 38, 51000],
[ 48, 119000],
[ 42, 65000],
[ 40, 65000],
[ 57, 60000],
[ 36, 54000],
[ 58, 144000],
[ 35, 79000],
[ 38, 55000],
[ 39, 122000],
[ 53, 104000],
[ 35, 75000],
[ 38, 65000],
[ 47, 51000],
[ 47, 105000],
[ 41, 63000],
[ 53, 72000],
[ 54, 108000],
[ 39, 77000],
[ 38, 61000],
[ 38, 113000],
[ 37, 75000],
[ 42, 90000],
[ 37, 57000],
[ 36, 99000],
[ 60, 34000],
[ 54, 70000],
[ 41, 72000],
[ 40, 71000],
[ 42, 54000],
[ 43, 129000],
[ 53, 34000],
[ 47, 50000],
[ 42, 79000],
[ 42, 104000],
[ 59, 29000],
[ 58, 47000],
[ 46, 88000],
[ 38, 71000],
[ 54, 26000],
[ 60, 46000],
[ 60, 83000],
[ 39, 73000],
[ 59, 130000],
[ 37, 80000],
[ 46, 32000],
[ 46, 74000],
[ 42, 53000],
[ 41, 87000],
[ 58, 23000],
[ 42, 64000],
[ 48, 33000],
[ 44, 139000],
[ 49, 28000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 35/73
12/13/24, 7:46 PM AI With Python Practicals
[ 57, 33000],
[ 56, 60000],
[ 49, 39000],
[ 39, 71000],
[ 47, 34000],
[ 48, 35000],
[ 48, 33000],
[ 47, 23000],
[ 45, 45000],
[ 60, 42000],
[ 39, 59000],
[ 46, 41000],
[ 51, 23000],
[ 50, 20000],
[ 36, 33000],
[ 49, 36000]], dtype=int64)
In [65]: y=data.iloc[:,4].values
y
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
Out[65]:
1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1], dtype=int64)
In [66]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=20)
In [67]: from sklearn.tree import DecisionTreeClassifier
cs=DecisionTreeClassifier(criterion='entropy')
cs.fit(x_train,y_train)
cs
Out[67]: ▾ DecisionTreeClassifier
DecisionTreeClassifier(criterion='entropy')
In [68]: y_pred=cs.predict(x_test)
y_pred
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0],
Out[68]:
dtype=int64)
In [69]: y_test
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
Out[69]:
dtype=int64)
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 36/73
12/13/24, 7:46 PM AI With Python Practicals
In [70]: from sklearn.metrics import confusion_matrix,classification_report
cm=confusion_matrix(y_test,y_pred)
print("Confusion Matrix :" ,cm)
report=classification_report(y_test,y_pred)
print("Classification Report :",report)
Confusion Matrix : [[15 3]
[ 0 2]]
Classification Report : precision recall f1-score support
0 1.00 0.83 0.91 18
1 0.40 1.00 0.57 2
accuracy 0.85 20
macro avg 0.70 0.92 0.74 20
weighted avg 0.94 0.85 0.88 20
In [71]: from sklearn.metrics import accuracy_score
print("Accuracy :" , accuracy_score(y_test,y_pred)*100)
Accuracy : 85.0
Practical 10 : Write Python program to Build
random forest on dataset
1) Installation packages , Loading dataset, Show data frame, Fitting Decision Tree classifier to
the training set random forest 2) Visualizing the train/test set result & printing classification
report.
In [72]: # 1)Installation packages,Loading dataset, Show data frame, Fitting Decision Tree c
import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
Out[72]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 37/73
12/13/24, 7:46 PM AI With Python Practicals
In [73]: # 2) Visualizing the train/test set result & printing classification report.
data.head()
Out[73]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
In [74]: data.tail(7)
Out[74]: User ID Gender Age EstimatedSalary Purchased
393 15635893 Male 60 42000 1
394 15757632 Female 39 59000 0
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [75]: x=data.iloc[:,[2,3]].values
x ## Independent Variable
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 38/73
12/13/24, 7:46 PM AI With Python Practicals
array([[ 19, 19000],
Out[75]:
[ 35, 20000],
[ 26, 43000],
[ 27, 57000],
[ 19, 76000],
[ 27, 58000],
[ 27, 84000],
[ 32, 150000],
[ 25, 33000],
[ 35, 65000],
[ 26, 80000],
[ 26, 52000],
[ 20, 86000],
[ 32, 18000],
[ 18, 82000],
[ 29, 80000],
[ 47, 25000],
[ 45, 26000],
[ 46, 28000],
[ 48, 29000],
[ 45, 22000],
[ 47, 49000],
[ 48, 41000],
[ 45, 22000],
[ 46, 23000],
[ 47, 20000],
[ 49, 28000],
[ 47, 30000],
[ 29, 43000],
[ 31, 18000],
[ 31, 74000],
[ 27, 137000],
[ 21, 16000],
[ 28, 44000],
[ 27, 90000],
[ 35, 27000],
[ 33, 28000],
[ 30, 49000],
[ 26, 72000],
[ 27, 31000],
[ 27, 17000],
[ 33, 51000],
[ 35, 108000],
[ 30, 15000],
[ 28, 84000],
[ 23, 20000],
[ 25, 79000],
[ 27, 54000],
[ 30, 135000],
[ 31, 89000],
[ 24, 32000],
[ 18, 44000],
[ 29, 83000],
[ 35, 23000],
[ 27, 58000],
[ 24, 55000],
[ 23, 48000],
[ 28, 79000],
[ 22, 18000],
[ 32, 117000],
[ 27, 20000],
[ 25, 87000],
[ 23, 66000],
[ 32, 120000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 39/73
12/13/24, 7:46 PM AI With Python Practicals
[ 59, 83000],
[ 24, 58000],
[ 24, 19000],
[ 23, 82000],
[ 22, 63000],
[ 31, 68000],
[ 25, 80000],
[ 24, 27000],
[ 20, 23000],
[ 33, 113000],
[ 32, 18000],
[ 34, 112000],
[ 18, 52000],
[ 22, 27000],
[ 28, 87000],
[ 26, 17000],
[ 30, 80000],
[ 39, 42000],
[ 20, 49000],
[ 35, 88000],
[ 30, 62000],
[ 31, 118000],
[ 24, 55000],
[ 28, 85000],
[ 26, 81000],
[ 35, 50000],
[ 22, 81000],
[ 30, 116000],
[ 26, 15000],
[ 29, 28000],
[ 29, 83000],
[ 35, 44000],
[ 35, 25000],
[ 28, 123000],
[ 35, 73000],
[ 28, 37000],
[ 27, 88000],
[ 28, 59000],
[ 32, 86000],
[ 33, 149000],
[ 19, 21000],
[ 21, 72000],
[ 26, 35000],
[ 27, 89000],
[ 26, 86000],
[ 38, 80000],
[ 39, 71000],
[ 37, 71000],
[ 38, 61000],
[ 37, 55000],
[ 42, 80000],
[ 40, 57000],
[ 35, 75000],
[ 36, 52000],
[ 40, 59000],
[ 41, 59000],
[ 36, 75000],
[ 37, 72000],
[ 40, 75000],
[ 35, 53000],
[ 41, 51000],
[ 39, 61000],
[ 42, 65000],
[ 26, 32000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 40/73
12/13/24, 7:46 PM AI With Python Practicals
[ 30, 17000],
[ 26, 84000],
[ 31, 58000],
[ 33, 31000],
[ 30, 87000],
[ 21, 68000],
[ 28, 55000],
[ 23, 63000],
[ 20, 82000],
[ 30, 107000],
[ 28, 59000],
[ 19, 25000],
[ 19, 85000],
[ 18, 68000],
[ 35, 59000],
[ 30, 89000],
[ 34, 25000],
[ 24, 89000],
[ 27, 96000],
[ 41, 30000],
[ 29, 61000],
[ 20, 74000],
[ 26, 15000],
[ 41, 45000],
[ 31, 76000],
[ 36, 50000],
[ 40, 47000],
[ 31, 15000],
[ 46, 59000],
[ 29, 75000],
[ 26, 30000],
[ 32, 135000],
[ 32, 100000],
[ 25, 90000],
[ 37, 33000],
[ 35, 38000],
[ 33, 69000],
[ 18, 86000],
[ 22, 55000],
[ 35, 71000],
[ 29, 148000],
[ 29, 47000],
[ 21, 88000],
[ 34, 115000],
[ 26, 118000],
[ 34, 43000],
[ 34, 72000],
[ 23, 28000],
[ 35, 47000],
[ 25, 22000],
[ 24, 23000],
[ 31, 34000],
[ 26, 16000],
[ 31, 71000],
[ 32, 117000],
[ 33, 43000],
[ 33, 60000],
[ 31, 66000],
[ 20, 82000],
[ 33, 41000],
[ 35, 72000],
[ 28, 32000],
[ 24, 84000],
[ 19, 26000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 41/73
12/13/24, 7:46 PM AI With Python Practicals
[ 29, 43000],
[ 19, 70000],
[ 28, 89000],
[ 34, 43000],
[ 30, 79000],
[ 20, 36000],
[ 26, 80000],
[ 35, 22000],
[ 35, 39000],
[ 49, 74000],
[ 39, 134000],
[ 41, 71000],
[ 58, 101000],
[ 47, 47000],
[ 55, 130000],
[ 52, 114000],
[ 40, 142000],
[ 46, 22000],
[ 48, 96000],
[ 52, 150000],
[ 59, 42000],
[ 35, 58000],
[ 47, 43000],
[ 60, 108000],
[ 49, 65000],
[ 40, 78000],
[ 46, 96000],
[ 59, 143000],
[ 41, 80000],
[ 35, 91000],
[ 37, 144000],
[ 60, 102000],
[ 35, 60000],
[ 37, 53000],
[ 36, 126000],
[ 56, 133000],
[ 40, 72000],
[ 42, 80000],
[ 35, 147000],
[ 39, 42000],
[ 40, 107000],
[ 49, 86000],
[ 38, 112000],
[ 46, 79000],
[ 40, 57000],
[ 37, 80000],
[ 46, 82000],
[ 53, 143000],
[ 42, 149000],
[ 38, 59000],
[ 50, 88000],
[ 56, 104000],
[ 41, 72000],
[ 51, 146000],
[ 35, 50000],
[ 57, 122000],
[ 41, 52000],
[ 35, 97000],
[ 44, 39000],
[ 37, 52000],
[ 48, 134000],
[ 37, 146000],
[ 50, 44000],
[ 52, 90000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 42/73
12/13/24, 7:46 PM AI With Python Practicals
[ 41, 72000],
[ 40, 57000],
[ 58, 95000],
[ 45, 131000],
[ 35, 77000],
[ 36, 144000],
[ 55, 125000],
[ 35, 72000],
[ 48, 90000],
[ 42, 108000],
[ 40, 75000],
[ 37, 74000],
[ 47, 144000],
[ 40, 61000],
[ 43, 133000],
[ 59, 76000],
[ 60, 42000],
[ 39, 106000],
[ 57, 26000],
[ 57, 74000],
[ 38, 71000],
[ 49, 88000],
[ 52, 38000],
[ 50, 36000],
[ 59, 88000],
[ 35, 61000],
[ 37, 70000],
[ 52, 21000],
[ 48, 141000],
[ 37, 93000],
[ 37, 62000],
[ 48, 138000],
[ 41, 79000],
[ 37, 78000],
[ 39, 134000],
[ 49, 89000],
[ 55, 39000],
[ 37, 77000],
[ 35, 57000],
[ 36, 63000],
[ 42, 73000],
[ 43, 112000],
[ 45, 79000],
[ 46, 117000],
[ 58, 38000],
[ 48, 74000],
[ 37, 137000],
[ 37, 79000],
[ 40, 60000],
[ 42, 54000],
[ 51, 134000],
[ 47, 113000],
[ 36, 125000],
[ 38, 50000],
[ 42, 70000],
[ 39, 96000],
[ 38, 50000],
[ 49, 141000],
[ 39, 79000],
[ 39, 75000],
[ 54, 104000],
[ 35, 55000],
[ 45, 32000],
[ 36, 60000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 43/73
12/13/24, 7:46 PM AI With Python Practicals
[ 52, 138000],
[ 53, 82000],
[ 41, 52000],
[ 48, 30000],
[ 48, 131000],
[ 41, 60000],
[ 41, 72000],
[ 42, 75000],
[ 36, 118000],
[ 47, 107000],
[ 38, 51000],
[ 48, 119000],
[ 42, 65000],
[ 40, 65000],
[ 57, 60000],
[ 36, 54000],
[ 58, 144000],
[ 35, 79000],
[ 38, 55000],
[ 39, 122000],
[ 53, 104000],
[ 35, 75000],
[ 38, 65000],
[ 47, 51000],
[ 47, 105000],
[ 41, 63000],
[ 53, 72000],
[ 54, 108000],
[ 39, 77000],
[ 38, 61000],
[ 38, 113000],
[ 37, 75000],
[ 42, 90000],
[ 37, 57000],
[ 36, 99000],
[ 60, 34000],
[ 54, 70000],
[ 41, 72000],
[ 40, 71000],
[ 42, 54000],
[ 43, 129000],
[ 53, 34000],
[ 47, 50000],
[ 42, 79000],
[ 42, 104000],
[ 59, 29000],
[ 58, 47000],
[ 46, 88000],
[ 38, 71000],
[ 54, 26000],
[ 60, 46000],
[ 60, 83000],
[ 39, 73000],
[ 59, 130000],
[ 37, 80000],
[ 46, 32000],
[ 46, 74000],
[ 42, 53000],
[ 41, 87000],
[ 58, 23000],
[ 42, 64000],
[ 48, 33000],
[ 44, 139000],
[ 49, 28000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 44/73
12/13/24, 7:46 PM AI With Python Practicals
[ 57, 33000],
[ 56, 60000],
[ 49, 39000],
[ 39, 71000],
[ 47, 34000],
[ 48, 35000],
[ 48, 33000],
[ 47, 23000],
[ 45, 45000],
[ 60, 42000],
[ 39, 59000],
[ 46, 41000],
[ 51, 23000],
[ 50, 20000],
[ 36, 33000],
[ 49, 36000]], dtype=int64)
In [76]: y=data.iloc[:,4].values
y ## Dependent variable (Target/predict)
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
Out[76]:
1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1], dtype=int64)
In [77]: from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=25)
In [78]: from sklearn.ensemble import RandomForestClassifier
cs=RandomForestClassifier(n_estimators=10,criterion='entropy')
cs.fit(x_train,y_train)
cs
Out[78]: ▾ RandomForestClassifier
RandomForestClassifier(criterion='entropy', n_estimators=10)
In [79]: y_pred=cs.predict(x_test)
y_pred
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
Out[79]:
0, 1, 0], dtype=int64)
In [80]: y_test
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
Out[80]:
0, 1, 0], dtype=int64)
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 45/73
12/13/24, 7:46 PM AI With Python Practicals
In [81]: from sklearn.metrics import confusion_matrix,classification_report
cm=confusion_matrix(y_test,y_pred)
print("Confusion Matrix :",cm)
report=classification_report(y_test,y_pred)
print("Classifcation Report :",report)
Confusion Matrix : [[19 2]
[ 0 4]]
Classifcation Report : precision recall f1-score support
0 1.00 0.90 0.95 21
1 0.67 1.00 0.80 4
accuracy 0.92 25
macro avg 0.83 0.95 0.88 25
weighted avg 0.95 0.92 0.93 25
In [82]: from sklearn.metrics import accuracy_score
print("Accuracy :",accuracy_score(y_test,y_pred)*100)
Accuracy : 92.0
In [83]: ## 2. Visualizing the train, test result & Show classification report
import seaborn as sns ## help to give color and some attractive properties to our
## we use heatmap methond of seaborn library to give color, and make our graph look
import matplotlib.pyplot as plt ## Used for plotting graph
plt.figure(figsize=(5,5)) ## We give size for our graph....
sns.heatmap(cm, annot=True)
## annot=True --- This adds the actual values to the heatmap cells.
## fmt='d' --- Formats the annotations as integers.
## cmap --- defines the color of our graph -- cmap='Greens' : so green colo
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 46/73
12/13/24, 7:46 PM AI With Python Practicals
Practical 11 : Write Python program to implement K-
Means for clustering on dataset.
1) Installation packages , Loading dataset, Show data frame,
implement k-Means Clustering
In [84]: import pandas as pd
import numpy as np
data=pd.read_csv('Book1.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 47/73
12/13/24, 7:46 PM AI With Python Practicals
Out[84]: Name Roll_No Marks
0 A 40 88
1 B 32 67
2 C 67 55
3 D 30 59
4 E 12 60
5 F 10 73
6 G 45 75
7 H 44 72
8 I 20 40
9 J 39 80
10 K 57 78
11 L 18 35
12 M 82 43
13 N 59 52
14 O 3 61
15 P 2 75
16 Q 13 65
17 R 62 89
18 S 51 81
19 T 36 34
20 U 17 20
21 V 5 78
22 W 9 76
23 X 11 53
24 Y 21 70
25 Z 71 44
In [85]: data.head()
Out[85]: Name Roll_No Marks
0 A 40 88
1 B 32 67
2 C 67 55
3 D 30 59
4 E 12 60
In [86]: data.tail()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 48/73
12/13/24, 7:46 PM AI With Python Practicals
Out[86]: Name Roll_No Marks
21 V 5 78
22 W 9 76
23 X 11 53
24 Y 21 70
25 Z 71 44
In [87]: x=data.iloc[:,[0,1]].values
x ## Independent variable
array([['A', 40],
Out[87]:
['B', 32],
['C', 67],
['D', 30],
['E', 12],
['F', 10],
['G', 45],
['H', 44],
['I', 20],
['J', 39],
['K', 57],
['L', 18],
['M', 82],
['N', 59],
['O', 3],
['P', 2],
['Q', 13],
['R', 62],
['S', 51],
['T', 36],
['U', 17],
['V', 5],
['W', 9],
['X', 11],
['Y', 21],
['Z', 71]], dtype=object)
In [88]: y=data.iloc[:,2].values
y ## dependent variable
array([88, 67, 55, 59, 60, 73, 75, 72, 40, 80, 78, 35, 43, 52, 61, 75, 65,
Out[88]:
89, 81, 34, 20, 78, 76, 53, 70, 44], dtype=int64)
2) visualizing cluster & generate the centroids of our clusters.
In [89]: ### Plotting graph
import matplotlib.pyplot as plt
plt.scatter(data.Roll_No,data['Marks'])
plt.xlabel('Roll_No')
plt.ylabel('Marks')
Text(0, 0.5, 'Marks')
Out[89]:
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 49/73
12/13/24, 7:46 PM AI With Python Practicals
In [90]: from sklearn.cluster import KMeans
km=KMeans(n_clusters=3)
km
Out[90]: ▾ KMeans
KMeans(n_clusters=3)
In [91]: predicted = km.fit_predict(data[['Roll_No','Marks']])
predicted
C:\ProgramData\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1412: Future
Warning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set t
he value of `n_init` explicitly to suppress the warning
super()._check_params_vs_input(X, default_n_init=10)
C:\ProgramData\anaconda3\Lib\site-packages\sklearn\cluster\_kmeans.py:1436: UserWa
rning: KMeans is known to have a memory leak on Windows with MKL, when there are l
ess chunks than available threads. You can avoid it by setting the environment var
iable OMP_NUM_THREADS=1.
warnings.warn(
array([0, 2, 0, 2, 2, 2, 0, 0, 1, 0, 0, 1, 0, 0, 2, 2, 2, 0, 0, 1, 1, 2,
Out[91]:
2, 2, 2, 0])
In [92]: data['cluster']=predicted
## 3 Clusters we stored in three different variables.....
data1 = data[data.cluster==0]
data2 = data[data.cluster==1]
data3 = data[data.cluster==2]
plt.scatter(data1.Roll_No,data1['Marks'],color='green')
plt.scatter(data2.Roll_No,data2['Marks'],color='red')
plt.scatter(data3.Roll_No,data3['Marks'],color='blue')
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 50/73
12/13/24, 7:46 PM AI With Python Practicals
plt.xlabel('Roll_No')
plt.ylabel('Marks')
Text(0, 0.5, 'Marks')
Out[92]:
In [93]: data = data.drop(['cluster'], axis='columns')
data['cluster']=predicted
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 51/73
12/13/24, 7:46 PM AI With Python Practicals
Out[93]: Name Roll_No Marks cluster
0 A 40 88 0
1 B 32 67 2
2 C 67 55 0
3 D 30 59 2
4 E 12 60 2
5 F 10 73 2
6 G 45 75 0
7 H 44 72 0
8 I 20 40 1
9 J 39 80 0
10 K 57 78 0
11 L 18 35 1
12 M 82 43 0
13 N 59 52 0
14 O 3 61 2
15 P 2 75 2
16 Q 13 65 2
17 R 62 89 0
18 S 51 81 0
19 T 36 34 1
20 U 17 20 1
21 V 5 78 2
22 W 9 76 2
23 X 11 53 2
24 Y 21 70 2
25 Z 71 44 0
In [94]: km.cluster_centers_
array([[56.09090909, 68.81818182],
Out[94]:
[22.75 , 32.25 ],
[13.45454545, 67. ]])
In [95]: data['cluster']=predicted
## 3 Clusters we stored in three different variables.....
data1 = data[data.cluster==0]
data2 = data[data.cluster==1]
data3 = data[data.cluster==2]
plt.scatter(data1.Roll_No,data1['Marks'],color='green')
plt.scatter(data2.Roll_No,data2['Marks'],color='red')
plt.scatter(data3.Roll_No,data3['Marks'],color='blue')
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 52/73
12/13/24, 7:46 PM AI With Python Practicals
plt.scatter(km.cluster_centers_[:,0],km.cluster_centers_[:,1],color='black',marker=
## We find the centroid & marked it as a black color with * symbol....
plt.xlabel('Roll_No')
plt.ylabel('Marks')
Text(0, 0.5, 'Marks')
Out[95]:
Practical 12 : Write Python program to implement
K-NN classifier (KNeighborsClassifier) on dataset
1) Installation packages , Loading dataset, Show data frame, Fitting K-NN classifier. 2)
Visualizing the train/test set result & printing classification report.
In [96]: import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 53/73
12/13/24, 7:46 PM AI With Python Practicals
Out[96]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [97]: data.head()
Out[97]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
In [98]: data.tail()
Out[98]: User ID Gender Age EstimatedSalary Purchased
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [99]: x=data.iloc[:,[2,3]].values
x ## Independent variable
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 54/73
12/13/24, 7:46 PM AI With Python Practicals
array([[ 19, 19000],
Out[99]:
[ 35, 20000],
[ 26, 43000],
[ 27, 57000],
[ 19, 76000],
[ 27, 58000],
[ 27, 84000],
[ 32, 150000],
[ 25, 33000],
[ 35, 65000],
[ 26, 80000],
[ 26, 52000],
[ 20, 86000],
[ 32, 18000],
[ 18, 82000],
[ 29, 80000],
[ 47, 25000],
[ 45, 26000],
[ 46, 28000],
[ 48, 29000],
[ 45, 22000],
[ 47, 49000],
[ 48, 41000],
[ 45, 22000],
[ 46, 23000],
[ 47, 20000],
[ 49, 28000],
[ 47, 30000],
[ 29, 43000],
[ 31, 18000],
[ 31, 74000],
[ 27, 137000],
[ 21, 16000],
[ 28, 44000],
[ 27, 90000],
[ 35, 27000],
[ 33, 28000],
[ 30, 49000],
[ 26, 72000],
[ 27, 31000],
[ 27, 17000],
[ 33, 51000],
[ 35, 108000],
[ 30, 15000],
[ 28, 84000],
[ 23, 20000],
[ 25, 79000],
[ 27, 54000],
[ 30, 135000],
[ 31, 89000],
[ 24, 32000],
[ 18, 44000],
[ 29, 83000],
[ 35, 23000],
[ 27, 58000],
[ 24, 55000],
[ 23, 48000],
[ 28, 79000],
[ 22, 18000],
[ 32, 117000],
[ 27, 20000],
[ 25, 87000],
[ 23, 66000],
[ 32, 120000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 55/73
12/13/24, 7:46 PM AI With Python Practicals
[ 59, 83000],
[ 24, 58000],
[ 24, 19000],
[ 23, 82000],
[ 22, 63000],
[ 31, 68000],
[ 25, 80000],
[ 24, 27000],
[ 20, 23000],
[ 33, 113000],
[ 32, 18000],
[ 34, 112000],
[ 18, 52000],
[ 22, 27000],
[ 28, 87000],
[ 26, 17000],
[ 30, 80000],
[ 39, 42000],
[ 20, 49000],
[ 35, 88000],
[ 30, 62000],
[ 31, 118000],
[ 24, 55000],
[ 28, 85000],
[ 26, 81000],
[ 35, 50000],
[ 22, 81000],
[ 30, 116000],
[ 26, 15000],
[ 29, 28000],
[ 29, 83000],
[ 35, 44000],
[ 35, 25000],
[ 28, 123000],
[ 35, 73000],
[ 28, 37000],
[ 27, 88000],
[ 28, 59000],
[ 32, 86000],
[ 33, 149000],
[ 19, 21000],
[ 21, 72000],
[ 26, 35000],
[ 27, 89000],
[ 26, 86000],
[ 38, 80000],
[ 39, 71000],
[ 37, 71000],
[ 38, 61000],
[ 37, 55000],
[ 42, 80000],
[ 40, 57000],
[ 35, 75000],
[ 36, 52000],
[ 40, 59000],
[ 41, 59000],
[ 36, 75000],
[ 37, 72000],
[ 40, 75000],
[ 35, 53000],
[ 41, 51000],
[ 39, 61000],
[ 42, 65000],
[ 26, 32000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 56/73
12/13/24, 7:46 PM AI With Python Practicals
[ 30, 17000],
[ 26, 84000],
[ 31, 58000],
[ 33, 31000],
[ 30, 87000],
[ 21, 68000],
[ 28, 55000],
[ 23, 63000],
[ 20, 82000],
[ 30, 107000],
[ 28, 59000],
[ 19, 25000],
[ 19, 85000],
[ 18, 68000],
[ 35, 59000],
[ 30, 89000],
[ 34, 25000],
[ 24, 89000],
[ 27, 96000],
[ 41, 30000],
[ 29, 61000],
[ 20, 74000],
[ 26, 15000],
[ 41, 45000],
[ 31, 76000],
[ 36, 50000],
[ 40, 47000],
[ 31, 15000],
[ 46, 59000],
[ 29, 75000],
[ 26, 30000],
[ 32, 135000],
[ 32, 100000],
[ 25, 90000],
[ 37, 33000],
[ 35, 38000],
[ 33, 69000],
[ 18, 86000],
[ 22, 55000],
[ 35, 71000],
[ 29, 148000],
[ 29, 47000],
[ 21, 88000],
[ 34, 115000],
[ 26, 118000],
[ 34, 43000],
[ 34, 72000],
[ 23, 28000],
[ 35, 47000],
[ 25, 22000],
[ 24, 23000],
[ 31, 34000],
[ 26, 16000],
[ 31, 71000],
[ 32, 117000],
[ 33, 43000],
[ 33, 60000],
[ 31, 66000],
[ 20, 82000],
[ 33, 41000],
[ 35, 72000],
[ 28, 32000],
[ 24, 84000],
[ 19, 26000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 57/73
12/13/24, 7:46 PM AI With Python Practicals
[ 29, 43000],
[ 19, 70000],
[ 28, 89000],
[ 34, 43000],
[ 30, 79000],
[ 20, 36000],
[ 26, 80000],
[ 35, 22000],
[ 35, 39000],
[ 49, 74000],
[ 39, 134000],
[ 41, 71000],
[ 58, 101000],
[ 47, 47000],
[ 55, 130000],
[ 52, 114000],
[ 40, 142000],
[ 46, 22000],
[ 48, 96000],
[ 52, 150000],
[ 59, 42000],
[ 35, 58000],
[ 47, 43000],
[ 60, 108000],
[ 49, 65000],
[ 40, 78000],
[ 46, 96000],
[ 59, 143000],
[ 41, 80000],
[ 35, 91000],
[ 37, 144000],
[ 60, 102000],
[ 35, 60000],
[ 37, 53000],
[ 36, 126000],
[ 56, 133000],
[ 40, 72000],
[ 42, 80000],
[ 35, 147000],
[ 39, 42000],
[ 40, 107000],
[ 49, 86000],
[ 38, 112000],
[ 46, 79000],
[ 40, 57000],
[ 37, 80000],
[ 46, 82000],
[ 53, 143000],
[ 42, 149000],
[ 38, 59000],
[ 50, 88000],
[ 56, 104000],
[ 41, 72000],
[ 51, 146000],
[ 35, 50000],
[ 57, 122000],
[ 41, 52000],
[ 35, 97000],
[ 44, 39000],
[ 37, 52000],
[ 48, 134000],
[ 37, 146000],
[ 50, 44000],
[ 52, 90000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 58/73
12/13/24, 7:46 PM AI With Python Practicals
[ 41, 72000],
[ 40, 57000],
[ 58, 95000],
[ 45, 131000],
[ 35, 77000],
[ 36, 144000],
[ 55, 125000],
[ 35, 72000],
[ 48, 90000],
[ 42, 108000],
[ 40, 75000],
[ 37, 74000],
[ 47, 144000],
[ 40, 61000],
[ 43, 133000],
[ 59, 76000],
[ 60, 42000],
[ 39, 106000],
[ 57, 26000],
[ 57, 74000],
[ 38, 71000],
[ 49, 88000],
[ 52, 38000],
[ 50, 36000],
[ 59, 88000],
[ 35, 61000],
[ 37, 70000],
[ 52, 21000],
[ 48, 141000],
[ 37, 93000],
[ 37, 62000],
[ 48, 138000],
[ 41, 79000],
[ 37, 78000],
[ 39, 134000],
[ 49, 89000],
[ 55, 39000],
[ 37, 77000],
[ 35, 57000],
[ 36, 63000],
[ 42, 73000],
[ 43, 112000],
[ 45, 79000],
[ 46, 117000],
[ 58, 38000],
[ 48, 74000],
[ 37, 137000],
[ 37, 79000],
[ 40, 60000],
[ 42, 54000],
[ 51, 134000],
[ 47, 113000],
[ 36, 125000],
[ 38, 50000],
[ 42, 70000],
[ 39, 96000],
[ 38, 50000],
[ 49, 141000],
[ 39, 79000],
[ 39, 75000],
[ 54, 104000],
[ 35, 55000],
[ 45, 32000],
[ 36, 60000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 59/73
12/13/24, 7:46 PM AI With Python Practicals
[ 52, 138000],
[ 53, 82000],
[ 41, 52000],
[ 48, 30000],
[ 48, 131000],
[ 41, 60000],
[ 41, 72000],
[ 42, 75000],
[ 36, 118000],
[ 47, 107000],
[ 38, 51000],
[ 48, 119000],
[ 42, 65000],
[ 40, 65000],
[ 57, 60000],
[ 36, 54000],
[ 58, 144000],
[ 35, 79000],
[ 38, 55000],
[ 39, 122000],
[ 53, 104000],
[ 35, 75000],
[ 38, 65000],
[ 47, 51000],
[ 47, 105000],
[ 41, 63000],
[ 53, 72000],
[ 54, 108000],
[ 39, 77000],
[ 38, 61000],
[ 38, 113000],
[ 37, 75000],
[ 42, 90000],
[ 37, 57000],
[ 36, 99000],
[ 60, 34000],
[ 54, 70000],
[ 41, 72000],
[ 40, 71000],
[ 42, 54000],
[ 43, 129000],
[ 53, 34000],
[ 47, 50000],
[ 42, 79000],
[ 42, 104000],
[ 59, 29000],
[ 58, 47000],
[ 46, 88000],
[ 38, 71000],
[ 54, 26000],
[ 60, 46000],
[ 60, 83000],
[ 39, 73000],
[ 59, 130000],
[ 37, 80000],
[ 46, 32000],
[ 46, 74000],
[ 42, 53000],
[ 41, 87000],
[ 58, 23000],
[ 42, 64000],
[ 48, 33000],
[ 44, 139000],
[ 49, 28000],
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 60/73
12/13/24, 7:46 PM AI With Python Practicals
[ 57, 33000],
[ 56, 60000],
[ 49, 39000],
[ 39, 71000],
[ 47, 34000],
[ 48, 35000],
[ 48, 33000],
[ 47, 23000],
[ 45, 45000],
[ 60, 42000],
[ 39, 59000],
[ 46, 41000],
[ 51, 23000],
[ 50, 20000],
[ 36, 33000],
[ 49, 36000]], dtype=int64)
In [100… y=data.iloc[:,4]
y ## Dependent variable
0 0
Out[100]:
1 0
2 0
3 0
4 0
..
395 1
396 1
397 1
398 0
399 1
Name: Purchased, Length: 400, dtype: int64
In [101… from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=25)
In [102… from sklearn.neighbors import KNeighborsClassifier
cs=KNeighborsClassifier(n_neighbors=2)
cs.fit(x_train,y_train)
cs
Out[102]: ▾ KNeighborsClassifier
KNeighborsClassifier(n_neighbors=2)
In [103… y_pred=cs.predict(x_test)
y_pred
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1,
Out[103]:
0, 1, 0], dtype=int64)
In [104… y_test
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 61/73
12/13/24, 7:46 PM AI With Python Practicals
132 0
Out[104]:
309 0
341 0
196 0
246 0
60 0
155 0
261 1
141 0
214 0
37 0
134 0
113 0
348 0
12 0
59 0
293 0
140 0
206 1
199 0
176 0
268 1
124 0
344 1
175 0
Name: Purchased, dtype: int64
In [105… from sklearn.metrics import confusion_matrix,accuracy_score
cm=confusion_matrix(y_test,y_pred)
print("Confusion Matrix :", cm)
print("Accuracy Score :",accuracy_score(y_test,y_pred)*100)
Confusion Matrix : [[19 2]
[ 0 4]]
Accuracy Score : 92.0
Practical 13 : Write Python program to visualizing
audio signals
Practical 14 : Write Python program to transform
audio signals to the frequency domain.
1) Installation packages , read audio file, Normalize the signal, Apply Fourier transform 2)
Data Aggregation function (sum , min, max, std, mean,describe ,count)
Practical 15 : Write Python program to generate
audio signal.
1) Installation packages , read audio file, Specify audio parameters, Generate the audio
signal 2) Data Aggregation function (sum , min, max, std, mean,describe ,count)
1) Installation packages , read audio file, Normalize the signal.
OR
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 62/73
12/13/24, 7:46 PM AI With Python Practicals
Installation packages , read audio file, Normalize the signal,
Apply Fourier transform OR
Installation packages , read audio file, Specify audio
parameters, Generate the audio signal
In [ ]:
2) Data Aggregation function (sum , min, max, std,
mean,describe ,count) OR
Write Python program to Implement Data Preparation using
techniques like data Aggregation on dataset
In [106… import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
Out[106]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [107… data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 400 entries, 0 to 399
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User ID 400 non-null int64
1 Gender 400 non-null object
2 Age 400 non-null int64
3 EstimatedSalary 400 non-null int64
4 Purchased 400 non-null int64
dtypes: int64(4), object(1)
memory usage: 15.8+ KB
In [108… data.describe()
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 63/73
12/13/24, 7:46 PM AI With Python Practicals
Out[108]: User ID Age EstimatedSalary Purchased
count 4.000000e+02 400.000000 400.000000 400.000000
mean 1.569154e+07 37.655000 69742.500000 0.357500
std 7.165832e+04 10.482877 34096.960282 0.479864
min 1.556669e+07 18.000000 15000.000000 0.000000
25% 1.562676e+07 29.750000 43000.000000 0.000000
50% 1.569434e+07 37.000000 70000.000000 0.000000
75% 1.575036e+07 46.000000 88000.000000 1.000000
max 1.581524e+07 60.000000 150000.000000 1.000000
In [109… data.head(15)
Out[109]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
5 15728773 Male 27 58000 0
6 15598044 Female 27 84000 0
7 15694829 Female 32 150000 1
8 15600575 Male 25 33000 0
9 15727311 Female 35 65000 0
10 15570769 Female 26 80000 0
11 15606274 Female 26 52000 0
12 15746139 Male 20 86000 0
13 15704987 Male 32 18000 0
14 15628972 Male 18 82000 0
In [110… data.tail(5)
Out[110]: User ID Gender Age EstimatedSalary Purchased
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
In [111… data.iloc[:,:]
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 64/73
12/13/24, 7:46 PM AI With Python Practicals
Out[111]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [112… data.iloc[:,[1,2,3]]
Out[112]: Gender Age EstimatedSalary
0 Male 19 19000
1 Male 35 20000
2 Female 26 43000
3 Female 27 57000
4 Male 19 76000
... ... ... ...
395 Female 46 41000
396 Male 51 23000
397 Female 50 20000
398 Male 36 33000
399 Female 49 36000
400 rows × 3 columns
In [113… data.groupby('Gender')
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002099B05E650>
Out[113]:
In [114… group=data.groupby('Gender')
In [115… print(group.sum())
User ID Age EstimatedSalary Purchased
Gender
Female 3201435452 7836 14639000 77
Male 3075180451 7226 13258000 66
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 65/73
12/13/24, 7:46 PM AI With Python Practicals
In [116… print(group.min())
User ID Age EstimatedSalary Purchased
Gender
Female 15566689 18 15000 0
Male 15570932 18 15000 0
In [117… print(group.max())
User ID Age EstimatedSalary Purchased
Gender
Female 15815236 60 150000 1
Male 15814816 60 150000 1
In [118… print(group.count())
User ID Age EstimatedSalary Purchased
Gender
Female 204 204 204 204
Male 196 196 196 196
In [119… print(group.std())
User ID Age EstimatedSalary Purchased
Gender
Female 73025.769592 10.859019 35595.236189 0.485942
Male 70346.177439 10.043636 32421.819181 0.473804
In [120… print(group.mean())
User ID Age EstimatedSalary Purchased
Gender
Female 1.569331e+07 38.411765 71759.803922 0.377451
Male 1.568970e+07 36.867347 67642.857143 0.336735
Practical 16 : Write Python program to installation
of NLTK and tokenizing text data
1) Installation package (NLTK) , Define input text, 2) Divide the input text into sentence
tokens and word tokens
------ This practicle is running by creating a seperate file -----
In [121… pip install nltk
Requirement already satisfied: nltk in c:\programdata\anaconda3\lib\site-packages
(3.8.1)
Requirement already satisfied: click in c:\programdata\anaconda3\lib\site-packages
(from nltk) (8.0.4)
Requirement already satisfied: joblib in c:\programdata\anaconda3\lib\site-package
s (from nltk) (1.2.0)
Requirement already satisfied: regex>=2021.8.3 in c:\programdata\anaconda3\lib\sit
e-packages (from nltk) (2022.7.9)
Requirement already satisfied: tqdm in c:\programdata\anaconda3\lib\site-packages
(from nltk) (4.65.0)
Requirement already satisfied: colorama in c:\programdata\anaconda3\lib\site-packa
ges (from click->nltk) (0.4.6)
Note: you may need to restart the kernel to use updated packages.
WARNING: There was an error checking the latest version of pip.
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 66/73
12/13/24, 7:46 PM AI With Python Practicals
In [122… ## NLTK is a --> Natural Langugae Toolkit...
## nltk is a library used for tokanization of words or sentences....
## Tokenization -- >> Divide the words or sentences into small easy undersatndabl
In [123… import nltk
nltk.download('punkt')
[nltk_data] Downloading package punkt to
[nltk_data] C:\Users\DELL\AppData\Roaming\nltk_data...
[nltk_data] Package punkt is already up-to-date!
True
Out[123]:
In [124… from nltk.tokenize import word_tokenize,sent_tokenize,WordPunctTokenizer
In [125… input_text="Hello! I am Kumud MCA 2nd year student. Welcome to Shirpur"
In [126… ##print("\n Sentence Tokenize")
##print(sent_tokenize(input_text))
In [127… print("\n Word Tokenizer")
print(word_tokenize(input_text))
Word Tokenizer
['Hello', '!', 'I', 'am', 'Kumud', 'MCA', '2nd', 'year', 'student', '.', 'Welcom
e', 'to', 'Shirpur']
In [128… print("\n Sentence Tokenizer")
print(sent_tokenize(input_text))
Sentence Tokenizer
['Hello!', 'I am Kumud MCA 2nd year student.', 'Welcome to Shirpur']
Practical 17 : Write Python program to Implement
Data Preparation using techniques like data
filtration on dataset
1) Installation packages , Loading Dataset, Show data frame 2) Data Filtration technique
(Select Single and Multiple column by label , Selecting columns by data type ,selecting single
Or multiple row , etc)
In [129… ## 1) Installation packages , Loading Dataset, Show data frame
import pandas as pd
import numpy as np
data=pd.read_csv('User_Data.csv')
data
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 67/73
12/13/24, 7:46 PM AI With Python Practicals
Out[129]: User ID Gender Age EstimatedSalary Purchased
0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
... ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
400 rows × 5 columns
In [130… #### -------2) Data Filtration technique
###### (Select Single and Multiple column by label , Selecting columns by data typ
# Step 4: Data Filtration Techniques
# 4.1. Selecting a single column by label (e.g., 'Age')
print("\nSelecting 'Age' column:")
age_column = data['Age']
print(age_column)
Selecting 'Age' column:
0 19
1 35
2 26
3 27
4 19
..
395 46
396 51
397 50
398 36
399 49
Name: Age, Length: 400, dtype: int64
In [131… # 4.2. Selecting multiple columns by label (e.g., 'Name' and 'Age')
print("\nSelecting 'Gender' and 'Age' columns:")
selected_columns = data[['Gender', 'Age']]
print(selected_columns)
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 68/73
12/13/24, 7:46 PM AI With Python Practicals
Selecting 'Gender' and 'Age' columns:
Gender Age
0 Male 19
1 Male 35
2 Female 26
3 Female 27
4 Male 19
.. ... ...
395 Female 46
396 Male 51
397 Female 50
398 Male 36
399 Female 49
[400 rows x 2 columns]
In [132… # 4.3. Selecting columns by data type (e.g., numeric columns)
print("\nSelecting all numeric columns:")
numeric_columns = data.select_dtypes(include='number')
print(numeric_columns)
Selecting all numeric columns:
User ID Age EstimatedSalary Purchased
0 15624510 19 19000 0
1 15810944 35 20000 0
2 15668575 26 43000 0
3 15603246 27 57000 0
4 15804002 19 76000 0
.. ... ... ... ...
395 15691863 46 41000 1
396 15706071 51 23000 1
397 15654296 50 20000 1
398 15755018 36 33000 0
399 15594041 49 36000 1
[400 rows x 4 columns]
In [133… # 4.4. Selecting rows based on conditions (e.g., 'Age' > 30)
print("\nSelecting rows where 'Age' > 30:")
age_above_30 = data[data['Age'] > 30]
print(age_above_30)
Selecting rows where 'Age' > 30:
User ID Gender Age EstimatedSalary Purchased
1 15810944 Male 35 20000 0
7 15694829 Female 32 150000 1
9 15727311 Female 35 65000 0
13 15704987 Male 32 18000 0
16 15733883 Male 47 25000 1
.. ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1
[289 rows x 5 columns]
In [134… # 4.5. Selecting rows with multiple conditions (e.g., 'Age' > 30 and 'Salary' > 500
print("\nSelecting rows where 'Age' > 30 AND 'EstimatedSalary' > 50000:")
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 69/73
12/13/24, 7:46 PM AI With Python Practicals
filtered_data = data[(data['Age'] > 30) & (data['EstimatedSalary'] > 50000)]
print(filtered_data)
Selecting rows where 'Age' > 30 AND 'EstimatedSalary' > 50000:
User ID Gender Age EstimatedSalary Purchased
7 15694829 Female 32 150000 1
9 15727311 Female 35 65000 0
30 15581198 Male 31 74000 0
41 15591915 Female 33 51000 0
42 15772798 Male 35 108000 0
.. ... ... ... ... ...
380 15683758 Male 42 64000 0
382 15715622 Female 44 139000 1
385 15775335 Male 56 60000 1
387 15627220 Male 39 71000 0
394 15757632 Female 39 59000 0
[206 rows x 5 columns]
In [135… # 4.6. Selecting a specific row by index (e.g., index 1)
print("\nSelecting row at index 1:")
specific_row = data.iloc[1]
print(specific_row)
Selecting row at index 1:
User ID 15810944
Gender Male
Age 35
EstimatedSalary 20000
Purchased 0
Name: 1, dtype: object
In [136… # 4.7. Slicing the DataFrame: Select the first 5 rows and the first 3 columns
print("\nSelecting first 5 rows and first 3 columns:")
subset = data.iloc[:5, :3]
print(subset)
Selecting first 5 rows and first 3 columns:
User ID Gender Age
0 15624510 Male 19
1 15810944 Male 35
2 15668575 Female 26
3 15603246 Female 27
4 15804002 Male 19
In [137… ## finding any particular record
subset=data.iloc[4,2]
subset
19
Out[137]:
In [138… subset=data.iloc[398,3]
subset
33000
Out[138]:
Practical 18 : Write Python program to Implement
Data Preparation using techniques like Handling
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 70/73
12/13/24, 7:46 PM AI With Python Practicals
missing values, Feature Scaling on dataset
1) Installation packages , Loading Dataset , Show data frame , Handling missing values 2)
Feature Scaling( using min max scaler , standard scaler )
In [139… ## 1) Installation packages , Loading Dataset , Show data frame ,Data Preparation
import pandas as pd
data={'Roll Num':[1,2,3,4,5,6,7,8,9,10],
'Student_Name':['Kumud','Nikita','Prajakta','Harshada',
'Mayuri','Kumud','Ragini','Ragini','Kajal','Kajal'],
'City':['Parola','Nandurbar','Jalgaon',
'Mumbai','Pune','Parola','Jalgaon','Nashik','Shirpur','Delhi'],
'Class':['MCA','MCA','MCA',
'BCA','10th','12th','BCA','BBA','BMS','BCOM']}
df=pd.DataFrame(data)
df
Out[139]: Roll Num Student_Name City Class
0 1 Kumud Parola MCA
1 2 Nikita Nandurbar MCA
2 3 Prajakta Jalgaon MCA
3 4 Harshada Mumbai BCA
4 5 Mayuri Pune 10th
5 6 Kumud Parola 12th
6 7 Ragini Jalgaon BCA
7 8 Ragini Nashik BBA
8 9 Kajal Shirpur BMS
9 10 Kajal Delhi BCOM
In [140… print(df.groupby('Student_Name'))
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002099C30CD10>
In [141… print(type(df.groupby('Student_Name')))
<class 'pandas.core.groupby.generic.DataFrameGroupBy'>
In [142… group_data=df.groupby('Student_Name') ##group data is a variable
In [143… print(group_data.sum())
Roll Num City Class
Student_Name
Harshada 4 Mumbai BCA
Kajal 19 ShirpurDelhi BMSBCOM
Kumud 7 ParolaParola MCA12th
Mayuri 5 Pune 10th
Nikita 2 Nandurbar MCA
Prajakta 3 Jalgaon MCA
Ragini 15 JalgaonNashik BCABBA
In [144… print(group_data.min())
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 71/73
12/13/24, 7:46 PM AI With Python Practicals
Roll Num City Class
Student_Name
Harshada 4 Mumbai BCA
Kajal 9 Delhi BCOM
Kumud 1 Parola 12th
Mayuri 5 Pune 10th
Nikita 2 Nandurbar MCA
Prajakta 3 Jalgaon MCA
Ragini 7 Jalgaon BBA
In [145… print(group_data.max())
Roll Num City Class
Student_Name
Harshada 4 Mumbai BCA
Kajal 10 Shirpur BMS
Kumud 6 Parola MCA
Mayuri 5 Pune 10th
Nikita 2 Nandurbar MCA
Prajakta 3 Jalgaon MCA
Ragini 8 Nashik BCA
In [146… print(group_data.count())
Roll Num City Class
Student_Name
Harshada 1 1 1
Kajal 2 2 2
Kumud 2 2 2
Mayuri 1 1 1
Nikita 1 1 1
Prajakta 1 1 1
Ragini 2 2 2
In [147… print(group_data.describe())
Roll Num
count mean std min 25% 50% 75% max
Student_Name
Harshada 1.0 4.0 NaN 4.0 4.00 4.0 4.00 4.0
Kajal 2.0 9.5 0.707107 9.0 9.25 9.5 9.75 10.0
Kumud 2.0 3.5 3.535534 1.0 2.25 3.5 4.75 6.0
Mayuri 1.0 5.0 NaN 5.0 5.00 5.0 5.00 5.0
Nikita 1.0 2.0 NaN 2.0 2.00 2.0 2.00 2.0
Prajakta 1.0 3.0 NaN 3.0 3.00 3.0 3.00 3.0
Ragini 2.0 7.5 0.707107 7.0 7.25 7.5 7.75 8.0
In [148… print(group_data.describe().transpose()['Kajal']) ## Transpose convert rows into
## and columns into rows...
Roll Num count 2.000000
mean 9.500000
std 0.707107
min 9.000000
25% 9.250000
50% 9.500000
75% 9.750000
max 10.000000
Name: Kajal, dtype: float64
In [149… #### Handling missing values
In [150… ##### 1. Drop rows or columns that have a missing value
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 72/73
12/13/24, 7:46 PM AI With Python Practicals
In [151… ## 2) Feature Scaling( using min max scaler , standard scaler )
In [ ]:
In [ ]:
Practical 19 : Write Python program to Implement
feature selection using technique univariate
selection, correlation heatmaps on dataset
1) Installation packages , Loading Dataset , Show data frame , Univariate selection using Chi2
2) Feature Selection( Show features score , plot correlation matrix with heatmaps ) on given
dataset
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
file:///C:/Users/saibaba/Downloads/AI With Python Practicals.html 73/73