0% found this document useful (0 votes)
4 views20 pages

UfpdsMAR2025 - 1.ipynb - Colab 16apr2025

The document provides a comprehensive guide on basic Python programming concepts, including printing data, creating variables, arithmetic operations, and using loops. It includes practical examples such as calculating employee salaries, checking even/odd numbers, and simulating an ATM machine. Additionally, it covers relational and logical operators, and demonstrates how to display student details and grades based on marks.

Uploaded by

yeror58532
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)
4 views20 pages

UfpdsMAR2025 - 1.ipynb - Colab 16apr2025

The document provides a comprehensive guide on basic Python programming concepts, including printing data, creating variables, arithmetic operations, and using loops. It includes practical examples such as calculating employee salaries, checking even/odd numbers, and simulating an ATM machine. Additionally, it covers relational and logical operators, and demonstrates how to display student details and grades based on marks.

Uploaded by

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

4/16/25, 8:12 AM ufpdsMAR2025_1.

ipynb - Colab

How to print data in Python

1 print('Navrachana\t\t\t\tUniversity')
2 print("Vadodara")
3 print("BTech, BCA, BBA BSc-Bio, Chem")

Navrachana University
Vadodara
BTech, BCA, BBA BSc-Bio, Chem

Creating Variables and Performing Arithmetic Caculation

1 a=300
2 print("value of a is ",a)
3 b=500
4 print("Value of b is ",b)
5 sum=a+b
6 print("Addition of ",a," and ",b," is ",sum)

value of a is 300
Value of b is 500
Addition of 300 and 500 is 800

Demo of Arithmetic Operators

Double-click (or enter) to edit

1 x=10
2 y=3
3 sub=x-y
4 mul=x*y
5 div=x/y #Floating point division
6 di=x//y #Integer division omit decimal values
7 rem=x%y
8 rai=x**y
9 print("Subtraction is",sub)
10 print("Product is",mul)
11 print("Division is ",div)
12 print("Integer Division is ",di)
13 print("Remainder is ",rem)
14 print("raised to ",rai)
15

Subtraction is 7
Product is 30
Division is 3.3333333333333335
Integer Division is 3
Remainder is 1
raised to 1000

Double-click (or enter) to edit

1 a=float(input("Enter first number:"))


2 b=int(input("Enter second number"))
3 sum=a+b
4 print("Addition of ",a," and ",b," is ",sum)

Enter first number:7.50


Enter second number8
Addition of 7.5 and 8 is 15.5

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 1/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab

Write a Python Program to input Employee Name, age, basic salary from keyboard. Calculate DA as 45% of basic salary, HRA as
57% of basic salary and PF as 9% of basic salary.Also calculate net salary as basic salary+DA+HRA-PF. Display all the data in proper
salary slip format as below /////////////////

Enter Employee Id : 2355

Enter Name : Jatin Shah

Enter Basic Salary : 59000

keyboard_arrow_down ABC Products Pvt. Ltd

Empoyee Id : 2355

Name : Jatin Shah

Basic Salary :50000

Dearness Allowance : 60000

House Rent Allowance : 54000

Provident Fund : 10000

Net Salary = 154000

1 empid=input("Enter Employee ID:")


2 empname=input("Enter Employee Name:")
3 age=int(input("Enter age:"))
4 exp=int(input("Enter experience in years:"))
5 basic_salary=float(input("Enter Basic Salary:"))
6 DA=basic_salary*(58/100)
7 HRA=basic_salary*(32/100)
8 PF=basic_salary*(9.5/100)
9 netsal=basic_salary+DA+HRA-PF
10 print("~~~~ XYZ Organization Pvt. Ltd ~~~~")
11 print("Empid :",empid)
12 print("Name :",empname)
13 print("Age :",age)
14 print("Experience :",exp)
15 print("Basic Salary :",basic_salary)
16 print("House Rent Allowance :",HRA)
17 print("Dearness Allowance :",DA)
18 print("Provident Fund :",PF)
19 print("Net Salary :",netsal)

Enter Employee ID:5645


Enter Employee Name:Amit Shukla
Enter age:23
Enter experience in years:8
Enter Basic Salary:42000
~~~~ XYZ Organization Pvt. Ltd ~~~~
Empid : 5645
Name : Amit Shukla
Age : 23
Experience : 8
Basic Salary : 42000.0
House Rent Allowance : 13440.0
Dearness Allowance : 24360.0
Provident Fund : 3990.0
Net Salary : 75810.0

Relational Operators in Python are

, <, >=, <=, != (not equal to), == (equal to) used to writing a condition Logical Operator and, or are logical operators used to check
multiple conditions simultaneously

1 a=6
2 if(a>=0):
https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 2/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
3 print("Postive")
4 else:
5 print("Negative")

Postive

Program to check whether number is Odd or even

1 x=int(input("Enter a number:"))
2 rem=x%2
3 #print(rem)
4 if(rem==0):
5 print("EVEN")
6 else:
7 print("ODD")

Enter a number:17
ODD

Write a program to imitate ATM Machine to withdraw money

1 print("Bank of ABC")
2 print("~~~~~~~~~~~")
3 balance=60000
4 print("Your balance is", balance)
5 print("1. Deposit \n2. Withdraw")
6 x=int(input("Enter your choice:"))
7 if(x==1):
8 dep=int(input("Enter Deposit amount:"))
9 balance=balance+dep
10 print("Now your balance is", balance)
11 elif(x==2):
12 withd=int(input("Enter Withdraw amount:"))
13 if(withd>(balance-5000)):
14 print("Insufficient Balance")
15 else:
16 balance=balance-withd
17
18 print("Now your balance is", balance)
19

Bank of ABC
~~~~~~~~~~~
Your balance is 60000
1. Deposit
2. Withdraw
Enter your choice:2
Enter Withdraw amount:40000
Now your balance is 20000

Write a program to display Student details. Input Student Name, roll no, marks of any 3 subjects out of 100. Display total,
Percentage, Status (pass/fail) and grades. If students scores more than 40 marks in each subject then the status is pass, otherwise
fail. Calculate grade if only student is pass. Grades must be

>=90% - A+

>=80% - A

>=70% - B+

>=60% - B

>=50% - C

>=40% - D

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 3/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
<40% - F

1 rollno=input("Enter your Roll no:")


2 sname=input("Enter your Name :")
3 maths=int(input("Enter marks of Mathematics (out of 100) :"))
4 science=int(input("Enter marks of Science (out of 100) :"))
5 english=int(input("Enter marks of English (out of 100) :"))
6 total=maths+science+english
7 percentage=total/3
8 status=""
9 grade=""
10 if(maths>=40 and science>=40 and english>=40):
11 status="Pass"
12 if(percentage>=90):
13 grade="A+"
14 elif(percentage>=80):
15 grade="A"
16 elif(percentage>=70):
17 grade="B+"
18 elif(percentage>=60):
19 grade="B"
20 elif(percentage>=50):
21 grade="C"
22 elif(percentage>=40):
23 grade="D"
24 else:
25 grade="F"
26
27 else:
28 status="Fail"
29
30 print("Total is :",total)
31 print("Percentage is :",percentage)
32 print("Status is : ",status)
33 print("Grade is " ,grade)
34
35

Enter your Roll no:565


Enter your Name :jkjkjk
Enter marks of Mathematics (out of 100) :45
Enter marks of Science (out of 100) :59
Enter marks of English (out of 100) :89
Total is : 193
Percentage is : 64.33333333333333
Status is : Pass
Grade is B

LOOPS in Python Loops enable programmers to repeat lines of code based on some condition There are two types of loop in
Python

1. While loop
2. for loop

while loop syntax initialize variable while(condition): few statements upgrade variable

Write a program to display Navrachana University 5 times without using while loop

1 print("Navrachana University\n"*5)

Show hidden output

Write a program to display Navrachana University 5 times using while loop

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 4/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab

1 a=1
2 while(a<=5):
3 print("Navrachana University")
4 a=a+1

Navrachana University
Navrachana University
Navrachana University
Navrachana University
Navrachana University

1 #program to print 1 2 3 4 5
2 a=1
3 while(a<=100):
4 print(a)
5 a=a+1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 5/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
1 #program to print 100 to 1
2 a=100
3 while(a>=1):
4 print(a)
5 a=a-1

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43 

1 #program to print odd numbers between 1 to 100


2 a=1
3 while(a<=100):
4 print(a)
5 a=a+2

1
3
5
7
9
11

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 6/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99

1 #program to print even numbers between 1 to 100


2 a=2
3 while(a<=100):
4 print(a)
5 a=a+2

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 7/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

1 # Write a program to print following series


2 # 2 4 8 16 32 64 128 256 512 1024
3 a=2
4 while(a<=1024):
5 print(a)
6 a=a*2
7

2
4
8
16
32
64
128
256
512
1024

1 a=1024
2 while(a>=2):
3 print(a)
4 a=a//2

1024
512
256
128
64
32
16
8
4
2

Double-click (or enter) to edit

1 num=int(input("Enter a number"))
2 ans=""
3 a=1
4 while(a<=10):
5 ans=num*a
6 print(num," X ",a," = ",ans)
7 a=a+1
8

Enter a number9
9 X 1 = 9

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 8/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
9 X 2 = 18
9 X 3 = 27
9 X 4 = 36
9 X 5 = 45
9 X 6 = 54
9 X 7 = 63
9 X 8 = 72
9 X 9 = 81
9 X 10 = 90

1 #program to print odd numbers between 1 to 100


2 a=1
3 while(a<=100):
4 rem=a%2
5 if(rem==1):
6 print(a)
7 a=a+1

1 #program to print Navrachana Uni 5 times using for loop and 1 to 5


2 for a in range(1,6):
3 print(a)
4

1
2
3
4
5

1 #program to print 5 to 1
2 for a in range(5,0,-1):
3 print(a)

5
4
3
2
1

1 #program to print 5 to 1
2 for a in range(1,101,2):
3 print(a)

1 a=[8,12,17,78,90,34] #list
2 print(a)
3 i=0
4 while(i<=5):
5 print(a[i])
6 i=i+1
7

[8, 12, 17, 78, 90, 34]


8
12
17
78
90
34

1 a=[8,12,17,78,90,34] #list
2 print(a)
3 for i in a:
4 print(i)

{17, 34, 8, 90, 12, 78}


17
34

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 9/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
8
90
12
78

1 #
2 a=[56,34,56,78] #list variable - allows duplicate values, stores data in natural ordering
3 b={56,34,56,78} #set variable, does not allows duplicates, used for searhing as it searche
4 c=(56,34,56,78) #tuple - it is like list but it is read-only collection
5 print("List is ",a)
6 print("Set is ",b)
7 #print(44 in b)
8 a.append(100)
9 b.add(100)
10 print(a)
11 print(b)
12 a.insert(2,200)
13 print(a)
14 a.pop(0)
15 print(a)
16 b.remove(56)
17 print(b)
18
19
20
21

List is [56, 34, 56, 78]


Set is {56, 34, 78}
[56, 34, 56, 78, 100]
{56, 34, 100, 78}
[56, 34, 200, 56, 78, 100]
[34, 200, 56, 78, 100]
{34, 100, 78}
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-b0aacb22ef2a> in <cell line: 0>()
16 b.remove(56)
17 print(b)
---> 18 c.pop(0)
19
20

AttributeError: 'tuple' object has no attribute 'pop'

 

1 a=[12,45,66,88,99,100,200]
2 print(a[1:-6:])
3

[]

1 stu={"rollno" : 240023,
2 "name" : "Jigar Shah",
3 "age" : 21
4 }
5 print(stu)
6 print(stu["rollno"])
7 print(stu["name"])
8 print(stu["age"])
9

{'rollno': 240023, 'name': 'Jigar Shah', 'age': 21}


240023
Jigar Shah
21

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 10/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
1 stu={"rollno" : 240023,
2 "name" : "Jigar Shah",
3 "age" : 21
4 }
5
6 for k in stu.keys():
7 print(k,"----",stu[k])
8
9 for v in stu.values():
10 print(v)
11
12 for k,v in stu.items():
13 print(k,"----",v)
14
15

1 from google.colab import drive


2 import pandas as pd #pip install pandas
3
4 drive.mount("/content/drive")#loads google drive in google collab
5 filepath=r"/content/drive/My Drive/Colab Notebooks/mynew.xlsx"
6 stu={"rollno" : 240023,
7 "name" : "Jigar Shah",
8 "age" : 21
9 }
10 print(stu)
11 print(stu["rollno"])
12 print(stu["name"])
13 print(stu["age"])
14 #below line converts python dictionary to dataframe
15 df=pd.DataFrame(stu,index=[1])
16 #below line saves python dataframe to excel
17 df.to_excel(filepath,index=False)
18

Draw a line chart

A 2D line plot in matplotlib is a graphical representation of data points connected by straight lines on a two-dimensional coordinate
system. It is a basic type of plot used to visualize the relationship between two continuous variables.

1 import pandas as pd #to read data from Excel or any tabular structure
2 import numpy as np #to modify pandas data at lower level
3 import matplotlib.pyplot as plt #to draw charts #pip install matplotlib
4 import seaborn as sns #to draw charts #pip install seaborn
5
6 year=[2015,2016,2017,2018,2019,2020]
7 price=[48000, 54000, 57000, 49000, 47000, 45000]
8 plt.plot(year, price)#by default always draws line chart
9 plt.grid()

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 11/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab

 

Reading data from Excel file and plots

1 from google.colab import drive


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import seaborn as sns
6 #from pandasql import sqldf
7 drive.mount("/content/drive")#loads google drive in google collab
8 filepath=r"/content/drive/My Drive/Colab Notebooks/dsdata.xlsx"
9 df = pd.read_excel(filepath,"cricket1")
10 print(df)
11 #plt.plot(df['Year'],df['Kohli'], label="Kohli",color="#6d192a",linewidth=8) #label is used
12 #df[["Year","Kohli"]].plot(kind="line", xlabel=df["Year"])
13 #df[["Year","Rohit","Kohli"]].plot(kind="line",x="Year")
14 #plt.plot(df['Year'],df['Rohit'], label="Rohit",color="#FC00D6")
15 #plt.title("Kohli Vs Rohit")
16 # Add axis labels
17 #plt.xlabel("Years")
18 #plt.ylabel("Runs Scored")
19 #plt.grid()
20 plt.legend()
21 #plt.show()
22 sns.lineplot(x = "Year", y = "Rohit", data=df,color="green")
23 sns.lineplot(x = "Year", y = "Kohli", data=df,color="red")
24 plt.show()
25 #plt.ylabel("Runs Scored")
26

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 12/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo
Year Rohit Kohli
0 2008 404 165
1 2009 362 246
2 2010 404 307
3 2011 372 557
4 2012 433 364
5 2013 538 639
6 2014 390 359
7 2015 482 505
8 2016 489 973
9 2017 333 308
<ipython-input-11-f847abf446b1>:20: UserWarning: No artists with labels found to put in legend. Note that artists who
plt.legend()
<ipython-input-11-f847abf446b1>:24: UserWarning: No artists with labels found to put in legend. Note that artists who
plt.legend()

Double-click (or enter) to edit

Draw a Scatter plot

Scatter Plot

A scatter plot in matplotlib is a type of plot used to visualize the relationship between two continuous variables. It displays
individual data points as markers on a two-dimensional coordinate system, with one variable represented on the x-axis and the
other variable represented on the y-axis.

Bivariate Analysis

Numerical Vs Numerical

Used to find Correlation

1 from google.colab import drive


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt #used to draw charts
5 import seaborn as sns #used to draw charts
6 #from pandasql import sqldf
7 drive.mount("/content/drive")
8 filepath=r"/content/drive/My Drive/Colab Notebooks/dsdata.xlsx"
9 df = pd.read_excel(filepath,"cricket1")
10 #print(df)
11 plt.scatter(df['Year'],df['Kohli'], label="Kohli",color="#ADAA19",linewidth=3) #label is us
12 #plt.scatter(df['Year'],df['Rohit'], label="Rohit",color="#FC00D6")
13 plt.title("Kohli Vs Rohit")

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 13/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
14 # Add axis labels
15 plt.xlabel("Years")
16 plt.ylabel("Runs Scored")
17 plt.grid()
18 plt.legend()
19 plt.show()
20 sns.scatterplot(x = "Year", y = "Kohli", data=df)
21 plt.ylabel("Runs Scored")
22 plt.show()

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo

Bar Chart Bivariate Analysis Numerical Vs Categorical Aggregate Analysis of Groups

1 from google.colab import drive


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import seaborn as sns
6 #from pandasql import sqldf
7 drive.mount("/content/drive")

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 14/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
8 filepath=r"/content/drive/My Drive/Colab Notebooks/dsdata.xlsx"
9 df = pd.read_excel(filepath,"cricket2")
10 #print(df)
11 df.plot(kind="bar")
12 plt.title("IPL Indian Batsman")
13 # Add axis labels
14 plt.xlabel("Years")
15 plt.ylabel("Runs Scored")
16 plt.grid()
17 plt.legend()
18 plt.show()

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo

from google.colab import drive import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns

keyboard_arrow_down from pandasql import sqldf


drive.mount("/content/drive") filepath=r"/content/drive/My Drive/Colab Notebooks/dsdata.xlsx" df = pd.read_excel(filepath,"product
info")

plotting a pie chart

plt.pie(df["Price"], labels=df["Object"])
df.plot(kind="pie",y="Price",labels=df["Product"])

Treating Missing Data

1 from google.colab import drive


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import seaborn as sns
6 #from pandasql import sqldf
7 drive.mount("/content/drive")
8 filepath=r"/content/drive/My Drive/Colab Notebooks/dsdata.xlsx"
9 df = pd.read_excel(filepath,"product info")
10 print(df)

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 15/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
11 ## plotting a pie chart
12 #plt.pie(df["Price"], labels=df["Object"])
13
14 df.plot(kind="pie",y="Price",labels=df["Product"])

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo
Product Price
0 Bulb 45
1 Lamp 38
2 Table 120
3 Pen 20
4 Notebook 60
<Axes: ylabel='Price'>

1 from google.colab import drive


2 import pandas as pd
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import seaborn as sns
6 #from pandasql import sqldf
7 drive.mount("/content/drive")
8 filepath=r"/content/drive/My Drive/Colab Notebooks/Data2.csv"
9 df=pd.read_csv(filepath)
10 print(df)
11 #print(df.isnull())
12 #print(df.notnull())
13 #df=df.dropna(axis="rows")
14 #print(df)
15 #df=df.dropna(axis="columns")
16 #print(df)
17 #df=df.dropna(axis='columns', how='all') #This version removes columns who have all columns
18 #df=df.fillna(method='ffill')
19 #print(df)
20 #df=df.fillna(method='bfill')
21 #print(df)
22 #mean_age=df["Age"].mean()
23 #df=df.fillna({"Age" :mean_age})
24 #print("~~~~~~~~~~~~")
25 #print(df)
26 #mean_salary=df["Salary"].mean()
27 #df["Salary"].fillna(mean_salary,inplace=True) #OR
28 #df=df.fillna({"Salary" :mean_salary})
29 #print(df)
30
31 #yesnocount=df["Purchased"].value_counts()

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 16/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
32 #print(yesnocount)
33 # print("no. of yes is ",yesnocount["No"])
34 #if (yesnocount["Yes"]>yesnocount["No"]):
35 # df=df.fillna({"Purchased":"Yes"})
36 #else:
37 # df=df.fillna({"Purchased":"No"})
38 #print(df)
39

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo
Country Age Salary Purchased
0 France 44.0 72000.0 No
1 Spain NaN NaN Yes
2 Germany 34.0 56000.0 No
3 Spain 38.0 61000.0 NaN
4 Germany NaN 47000.0 Yes
5 France 35.0 NaN Yes
6 Spain 56.0 80000.0 No
7 France 41.0 79000.0 NaN
8 Germany NaN NaN Yes
9 France 37.0 67000.0 Yes
Country Age Salary Purchased
0 France 44.0 72000.0 No
1 Spain 34.0 56000.0 Yes
2 Germany 34.0 56000.0 No
3 Spain 38.0 61000.0 Yes
4 Germany 35.0 47000.0 Yes
5 France 35.0 80000.0 Yes
6 Spain 56.0 80000.0 No
7 France 41.0 79000.0 Yes
8 Germany 37.0 67000.0 Yes
9 France 37.0 67000.0 Yes
Purchased
Yes 5
No 3
Name: count, dtype: int64
<ipython-input-35-0fdff979299c>:20: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a fu
df=df.fillna(method='bfill')
 

Outlier Detection and Removal

1 from scipy.stats import zscore


2 from google.colab import drive
3 import pandas as pd
4 import numpy as np
5 import matplotlib.pyplot as plt
6 import seaborn as sns
7 #from pandasql import sqldf
8 drive.mount("/content/drive")
9 filepath=r"/content/drive/My Drive/Colab Notebooks/Data_salary.csv"
10 df=pd.read_csv(filepath)
11 print(df)
12 df["z_score_salary"] = zscore(df.Salary )
13 df= df[(df.z_score_salary>-1.2) & (df.z_score_salary<1.2)]
14 print(df)

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remo
Experience Salary
0 1.0 10000
1 1.3 12000
2 1.4 12500
3 1.8 13800
4 2.0 14000
5 2.3 13990
6 2.6 14000
7 2.9 15000
8 3.0 15675
9 3.1 15899
10 3.2 17900
11 3.6 16900
12 3.8 18700

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 17/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
13 3.9 20400
14 4.0 27900
15 4.1 30300
16 4.5 32900
17 4.8 34800
18 4.9 37900
19 5.0 40000
20 5.2 41000
21 5.5 45800
22 5.8 46800
23 5.9 39000
24 6.0 48900
25 6.1 50000
26 6.2 51000
27 6.3 51800
28 6.6 59000
Experience Salary z_score_salary
1 1.3 12000 -1.137602
2 1.4 12500 -1.104602
3 1.8 13800 -1.018804
4 2.0 14000 -1.005604
5 2.3 13990 -1.006264
6 2.6 14000 -1.005604
7 2.9 15000 -0.939605
8 3.0 15675 -0.895056
9 3.1 15899 -0.880272
10 3.2 17900 -0.748208
11 3.6 16900 -0.814207
12 3.8 18700 -0.695409
13 3.9 20400 -0.583211
14 4.0 27900 -0.088220
15 4.1 30300 0.070177
16 4.5 32900 0.241774
17 4.8 34800 0.367172
18 4.9 37900 0.571768
19 5.0 40000 0.710366
20 5.2 41000 0.776365
21 5.5 45800 1.093159
22 5.8 46800 1.159158
23 5.9 39000 0.644367

1 #Predicting with Linear Regression


2 import numpy as np
3 import matplotlib.pyplot as plt
4 import pandas as pd
5 from sklearn.model_selection import train_test_split
6 from sklearn.linear_model import LinearRegression
7 from sklearn import neighbors
8 from sklearn.metrics import accuracy_score
9 from sklearn.metrics import mean_squared_error
10 from scipy.stats import zscore
11 from google.colab import drive 
12 import pandas as pd
13 import numpy as np
14 import matplotlib.pyplot as plt
15 import seaborn as sns
16 #from pandasql import sqldf
17 drive.mount("/content/drive")
18 filepath=r"/content/drive/My Drive/Colab Notebooks/Data_salary.csv"
19 df=pd.read_csv(filepath)
20 print(df)
21 x= df.iloc[:, 0:1].values#extracting first column (Experience)
22 y= df.iloc[:, 1].values#extracting column no. 1 (Salary)
23
24 # Splitting the dataset into training and test set.
25 x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.3, random_state=0)
26 #Fitting the Simple Linear Regression model to the training dataset
27 #lr=neighbors.KNeighborsRegressor(n_neighbors = 5)
28 lr=LinearRegression() #here object of Linear Regression is created and its constructor is i
29 lr.fit(x_train, y_train) #Training model, here lr is a model which is trained
https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 18/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
30
31 # ############################################
32 print("predicted ",lr.predict([[7]]))
33 print("score is ",lr.score(x_test,y_test))

1 # Predicting with KNN and Linear Regression


2
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import pandas as pd
6 from sklearn.model_selection import train_test_split
7 from sklearn.linear_model import LinearRegression
8 from sklearn import neighbors
9 from sklearn.metrics import accuracy_score
10 from sklearn.metrics import mean_squared_error
11 from scipy.stats import zscore
12 from google.colab import drive
13 import pandas as pd
14 import numpy as np
15 import matplotlib.pyplot as plt
16 import seaborn as sns
17 #from pandasql import sqldf
18
19 import pandas
20 import seaborn as sb
21 from sklearn import linear_model
22 from sklearn.preprocessing import StandardScaler
23 import pandas
24 from sklearn.neighbors import KNeighborsRegressor
25 drive.mount("/content/drive")
26 filepath=r"/content/drive/My Drive/Colab Notebooks/cars.csv"
27 df=pd.read_csv(filepath)
28 x = df.iloc[:, 2:4].values#independent variable denoted as upper X
29 y = df.iloc[:,4].values #dependent variable denoted as lower y
30 #regr = KNeighborsRegressor(n_neighbors=10)
31 regr=LinearRegression()
32 regr.fit(x, y)
33 #predict the CO2 emission of a car where the weight is 2300kg, and the volume is 1300cm3:
34 predictedCO2 = regr.predict([[3000, 1900]])
35 print(predictedCO2)
36

1 #Logistic Regression Classification Problem


2 from pandas import read_csv
3 from sklearn.model_selection import KFold
4 from sklearn.model_selection import cross_val_score
5 from sklearn.svm import SVC
6 from sklearn.svm import LinearSVC
7 from sklearn.linear_model import LogisticRegression
8 import seaborn as sns
9 from sklearn.model_selection import train_test_split
10 from sklearn.metrics import confusion_matrix
11 import numpy as np
12 from sklearn.feature_selection import SelectKBest
13 from sklearn.feature_selection import chi2
14 from sklearn.metrics import accuracy_score
15
16 drive.mount("/content/drive")
17 filepath=r"/content/drive/My Drive/Colab Notebooks/pima-indians-diabetes2.csv"
18 df = read_csv(filepath)
19 x = df.iloc[:, 0:8]
https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 19/20
4/16/25, 8:12 AM ufpdsMAR2025_1.ipynb - Colab
[ , ]
20 y = df.iloc[:, 8]
21
22 x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, test_size=0.20)
23

https://colab.research.google.com/drive/13Q7y3Nxsz6kICY24woquTpxwpwKTIY4L#scrollTo=Kpk4qXOQ3x9f&printMode=true 20/20

You might also like

pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy