0% found this document useful (0 votes)
3 views3 pages

ADS Lab 2 - Riya

The document is a Jupyter notebook that demonstrates data manipulation and preprocessing using Python's pandas and scikit-learn libraries. It includes examples of handling missing values with SimpleImputer, encoding categorical variables with LabelEncoder and OneHotEncoder, and scaling features using StandardScaler. The notebook processes student and country datasets, showcasing various data transformation techniques.

Uploaded by

rupadhyay7405
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)
3 views3 pages

ADS Lab 2 - Riya

The document is a Jupyter notebook that demonstrates data manipulation and preprocessing using Python's pandas and scikit-learn libraries. It includes examples of handling missing values with SimpleImputer, encoding categorical variables with LabelEncoder and OneHotEncoder, and scaling features using StandardScaler. The notebook processes student and country datasets, showcasing various data transformation techniques.

Uploaded by

rupadhyay7405
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

1/20/26, 1:52 PM ADS Lab 2.

ipynb - Colab

Riya Upadhyay Div -c Moodle – 22102092

1 import pandas as pd
2 import numpy as np
3 students = [[89, 'F', 'very good'], [67,'None', 'good'],[[Link], None, 'Very Good'],[92,'M','excellent'],[67,'None', 'v
4 dfstud = [Link](students)

1 [Link] = ['marks','gender','result']

1 dfstud

marks gender result

0 89.0 F very good

1 67.0 None good

2 NaN None Very Good

3 92.0 M excellent

4 67.0 None very good

5 NaN M excellent

1 [Link]().[Link]()

np.int64(3)

1 X=[Link][:,0:2].values
2 Y=[Link][:,2].values
3 type(X)

[Link]

1 Start coding or ge nerate with AI.

1 from [Link] import SimpleImputer


2 imputer = SimpleImputer(missing_values=[Link], strategy='mean')
3 X[:,0:1]=imputer.fit_transform(X[:,0:1])
4 print(X)

[[89.0 'F']
[67.0 'None']
[78.75 None]
[92.0 'M']
[67.0 'None']
[78.75 'M']]

1 imputer = SimpleImputer(missing_values=[Link], strategy='mean')


2 [Link] = imputer.fit_transform([Link](-1,1))[:,0]
3 dfstud

marks gender result

0 89.00 F very good

1 67.00 None good

2 78.75 None Very Good

3 92.00 M excellent

4 67.00 None very good

5 78.75 M excellent

1 dfstud = [Link](students)
2 [Link] = ['marks','gender','result']
3 dfstud

[Link] 1/3
1/20/26, 1:52 PM ADS Lab [Link] - Colab

marks gender result

0 89.0 F very good

1 67.0 None good

2 NaN None Very Good

3 92.0 M excellent

4 67.0 None very good

5 NaN M excellent

1 from [Link] import SimpleImputer


2 imputer = SimpleImputer(missing_values=[Link], strategy='mean')

1 Start coding or ge nerate with AI.

1 Start coding or ge nerate with AI.

1 Start coding or ge nerate with AI.

1 ds=pd.read_csv('/content/[Link]')
2 ds

Country Age Salary Purchased

0 France 44.0 72000.0 No

1 Spain 27.0 48000.0 Yes

2 Germany 30.0 54000.0 No

3 Spain 38.0 61000.0 No

4 Germany 40.0 NaN Yes

5 France 35.0 58000.0 Yes

6 Spain NaN 52000.0 No

7 France 48.0 79000.0 Yes

8 Germany 50.0 83000.0 No

9 France 37.0 67000.0 Yes

1 X=[Link][:,:-1].values
2 Y=[Link][:,3].values

1 from [Link] import SimpleImputer


2 imputer = SimpleImputer(missing_values=[Link], strategy='mean')
3 X[:,1:3]=imputer.fit_transform(X[:,1:3])

1 print(X)

[['France' 44.0 72000.0]


['Spain' 27.0 48000.0]
['Germany' 30.0 54000.0]
['Spain' 38.0 61000.0]
['Germany' 40.0 63777.77777777778]
['France' 35.0 58000.0]
['Spain' 38.77777777777778 52000.0]
['France' 48.0 79000.0]
['Germany' 50.0 83000.0]
['France' 37.0 67000.0]]

1 from [Link] import LabelEncoder


2 labelencoder_X = LabelEncoder()
3 X[:, 0] = labelencoder_X.fit_transform(X[:, 0])

1 print(X)

[[0 44.0 72000.0]


[2 27.0 48000.0]
[1 30.0 54000.0]
[2 38.0 61000.0]
[1 40.0 63777.77777777778]
[0 35.0 58000.0]
[2 38.77777777777778 52000.0]

[Link] 2/3
1/20/26, 1:52 PM ADS Lab [Link] - Colab
[0 48.0 79000.0]
[1 50.0 83000.0]
[0 37.0 67000.0]]

1 from [Link] import OneHotEncoder


2 from [Link] import ColumnTransformer
3 ct = ColumnTransformer([("Country", OneHotEncoder(), [0])], remainder = 'passthrough')

1 X = [Link](ct.fit_transform(X), dtype = str)

1 print(X)

[['0.0' '1.0' '0.0' '0.0' '44.0' '72000.0']


['1.0' '0.0' '0.0' '1.0' '27.0' '48000.0']
['1.0' '0.0' '1.0' '0.0' '30.0' '54000.0']
['1.0' '0.0' '0.0' '1.0' '38.0' '61000.0']
['1.0' '0.0' '1.0' '0.0' '40.0' '63777.77777777778']
['0.0' '1.0' '0.0' '0.0' '35.0' '58000.0']
['1.0' '0.0' '0.0' '1.0' '38.77777777777778' '52000.0']
['0.0' '1.0' '0.0' '0.0' '48.0' '79000.0']
['1.0' '0.0' '1.0' '0.0' '50.0' '83000.0']
['0.0' '1.0' '0.0' '0.0' '37.0' '67000.0']]

Use Label encoding when you want to show ordinality otherwise use one hot encoding

1 from [Link] import StandardScaler


2 sc_X = StandardScaler()
3 X = sc_X.fit_transform(X)
4 scaled_X_df = [Link](data=X[:, [4, 5]], columns=['Age','Salary'])
5 print(scaled_X_df)

Age Salary
0 7.588744e-01 7.494733e-01
1 -1.711504e+00 -1.438178e+00
2 -1.275555e+00 -8.912655e-01
3 -1.130238e-01 -2.532004e-01
4 1.776089e-01 3.357034e-16
5 -5.489729e-01 -5.266569e-01
6 2.386980e-16 -1.073570e+00
7 1.340140e+00 1.387538e+00
8 1.630773e+00 1.752147e+00
9 -2.583402e-01 2.937125e-01

1 Start coding or ge nerate with AI.

Double-click (or enter) to edit

[Link] 3/3

You might also like