Skip to content

Commit 3066aa1

Browse files
authored
Add files via upload
1 parent 0171f3b commit 3066aa1

5 files changed

Lines changed: 537 additions & 0 deletions

File tree

Polymorphism.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
#%% Polymorphism
3+
4+
# It provides a way to perform a single action in different forms
5+
# It is achieved through method overriding and interfaces
6+
7+
#%% Method Overriding
8+
# It allows a child class to provide a specific implementation of a method that is already defined in its parent class
9+
10+
## base Class 1
11+
class Animal:
12+
def speak(self):
13+
return "Sound of the animal"
14+
15+
# Derived class 1
16+
class Dog(Animal):
17+
def speak(self):
18+
return "Woof!"
19+
20+
#Derived dClass
21+
class Cat(Animal):
22+
def speak(self):
23+
return "Meow!"
24+
#object creation
25+
dog = Dog()
26+
cat = Cat()
27+
print(dog.speak())
28+
print(cat.speak())
29+
30+
#%% Function that demonstrtes polymorphism
31+
32+
def animal_speak(animal):
33+
print(animal.speak())
34+
35+
animal_speak(cat)
36+
animal_speak(dog)
37+
38+
#%% POLYMORPHISM with function and methods
39+
class Shape:
40+
def area(self):
41+
return "The area of the figure"
42+
43+
#Derived class 1
44+
45+
class Rectangle(Shape):
46+
def __init__(self, width, height):
47+
self.width = width
48+
self.height = height
49+
50+
def area(self):
51+
return self.width * self.height
52+
53+
#Derived class 2
54+
55+
class Circle(Shape):
56+
def __init__(self, radius):
57+
self.radius = radius
58+
59+
def area(self):
60+
return 3.14 * self.radius * self.radius
61+
62+
#Function that generates polymorphism
63+
def print_area(shape):
64+
print(f"the area is {shape.area()}")
65+
66+
rectangle = Rectangle(4,5)
67+
circle = Circle(3)
68+
69+
print_area(rectangle)
70+
print_area(circle)
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+

RAndom_Forset.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#%% RF
2+
3+
import numpy as np
4+
import pandas as pd
5+
import matplotlib.pyplot as plt
6+
7+
df = pd.read_csv("C:/Users/akash/titanic.csv")
8+
df.head()
9+
10+
#%%
11+
12+
NAs = pd.concat([df.isnull().sum()], axis = 1, keys = ['Train'])
13+
14+
NAs[NAs.sum(axis = 1)>0]
15+
16+
#%%
17+
df['Age'] = df['Age'].fillna(df['Age'].mean())
18+
df['Embarked'] = df['Embarked'].fillna(df['Embarked'].mode()[0])
19+
df['Cabin'] = df['Cabin'].fillna(df['Cabin'].mode()[0])
20+
21+
#%%
22+
23+
df['Pclass'] = df['Pclass'].apply(str)
24+
#%%
25+
for col in df.dtypes[df.dtypes == 'object'].index:
26+
for_dummy = df.pop(col)
27+
df = pd.concat([df, pd.get_dummies(for_dummy, prefix = col)], axis = 1)
28+
df.head()
29+
#%%
30+
31+
labels = df.pop("Survived")
32+
#%%
33+
from sklearn.ensemble import RandomForestClassifier
34+
from sklearn.model_selection import train_test_split
35+
x_train, x_test, y_train, y_test = train_test_split(df, labels, test_size = 0.25)
36+
rf = RandomForestClassifier()
37+
38+
#%%
39+
rf.fit(x_train, y_train)
40+
y_pred = rf.predict(x_test)
41+
42+
#%%
43+
44+
from sklearn.metrics import roc_curve, auc
45+
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
46+
roc_auc = auc(fpr, tpr)
47+
roc_auc
48+
#%%
49+
50+
n_estimators = [1, 2, 4, 8, 16, 32, 64, 100, 200]
51+
train_results = []
52+
test_results = []
53+
54+
for estimator in n_estimators:
55+
rf = RandomForestClassifier(n_estimators = estimator, n_jobs=-1)
56+
rf.fit(x_train, y_train)
57+
df_pred = rf.predict(x_train)
58+
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
59+
roc_auc = auc(fpr, tpr)
60+
train_results.append(roc_auc)
61+
y_pred = rf.predict(x_test)
62+
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
63+
roc_auc = auc(fpr, tpr)
64+
test_results.append(roc_auc)
65+
66+
from matplotlib.legend_handler import HandlerLine2D
67+
line1, = plt.plot(n_estimators, train_results, 'b', label = "Train AUC")
68+
line2, = plt.plot(n_estimators, test_results, 'r', label = "Test AUC")
69+
plt.legend(handler_map= {line1: HandlerLine2D(numpoints = 2)})
70+
plt.ylabel("AUC score")
71+
plt.xlabel('n_estimators')
72+
plt.show()
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+

TSA_smoothing_techniques.ipynb

Lines changed: 198 additions & 0 deletions
Large diffs are not rendered by default.

inheritence.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
#%% Inheritence
3+
4+
## parent Class
5+
class Car:
6+
def __init__(self, windows, doors, enginetype):
7+
self.windows = windows
8+
self.doors = doors
9+
self.enginetype = enginetype
10+
11+
def drive(self):
12+
print(f"The Person will drive the {self.enginetype} car")
13+
14+
#%%
15+
car1= Car(4,5, "petrol")
16+
car1.drive()
17+
18+
#%% Single Inheritence
19+
20+
class Tesla(Car):
21+
def __init__(self, windows, doors, enginetype, is_selfdriving):
22+
super().__init__(windows, doors, enginetype)
23+
self.is_selfdriving = is_selfdriving
24+
25+
def selfdriving(self):
26+
print(f"Tesla supports self driving: {self.is_selfdriving}")
27+
28+
# Assuming a definition for the Car class is present
29+
tesla1 = Tesla(4, 5, "electric", True)
30+
tesla1.selfdriving()
31+
32+
#%% Multiple Inheritence
33+
# When a class inherits more than one base class
34+
35+
class Animal:
36+
def __init__(self, name):
37+
self.name = name
38+
39+
def speak(self):
40+
print("Subclass must implement this method")
41+
42+
# Base class 2
43+
class Pet:
44+
def __init__(self, owner):
45+
self.owner = owner
46+
47+
# Derived class
48+
class Dog(Animal, Pet):
49+
def __init__(self, name, owner):
50+
Animal.__init__(self, name) # Fixed: added parentheses
51+
Pet.__init__(self, owner) # Fixed: added parentheses
52+
53+
def speak(self):
54+
return f"{self.name} says woof"
55+
56+
# Create an object
57+
dog = Dog("Buddy", "Krish")
58+
print(dog.speak())
59+
print(f"Owner: {dog.owner}")
60+
61+
62+
63+
64+
65+
66+
67+

oops_practice_1.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
3+
#%%
4+
class Car:
5+
pass
6+
7+
audi = Car()
8+
bmw = Car()
9+
10+
print(type(audi))
11+
print(type(bmw))
12+
13+
#%%
14+
15+
print(bmw)
16+
#%% Instance varibles and methods
17+
18+
class Dog:
19+
audi.windows = 4
20+
print(audi.windows)
21+
22+
#%%
23+
24+
tata = Car()
25+
tata.doors = 4
26+
x = dir(tata)
27+
28+
#%% Constructors
29+
30+
class Dog:
31+
## constructor
32+
def __init__(self, name, age):
33+
self.name = name
34+
self.age = age
35+
36+
## create objects
37+
dog1 = Dog("Buddy",3)
38+
print(dog1)
39+
print(dog1.name)
40+
print(dog1.age)
41+
42+
#%% Instance Methods
43+
44+
# Define a class with instance method
45+
class Dog:
46+
def __init__(self, name, age):
47+
self.name = name
48+
self.age = age
49+
50+
def bark(self):
51+
print(f"{self.name} says woof")
52+
53+
dog1 = Dog("Buddy", 3)
54+
dog1.bark()
55+
56+
dog2 = Dog("Lucy", 5)
57+
dog2.bark()
58+
59+
#%% Modelling a bank account
60+
61+
#define a clas for bank account
62+
63+
class BankAccount:
64+
def __init__(self, owner, balance=0):
65+
self.owner = owner
66+
self.balance = balance
67+
68+
def deposit(self, amount):
69+
self.balance+= amount
70+
print(f"{amount} is deposited. New balance is {self.balance}")
71+
72+
def get_balance(self):
73+
return self.balance
74+
75+
def withdraw(self, amount):
76+
if amount > self.balance:
77+
print("Insufficient Funds")
78+
else:
79+
self.balance-= amount
80+
print(f"{amount} is withdrawn. New balance is {self.balance}")
81+
82+
#create a bank account
83+
84+
account = BankAccount("Krish",5000)
85+
print(account.balance)
86+
87+
#Call instance methods
88+
89+
account.deposit(100)
90+
91+
account.withdraw(300)
92+
93+
#%%
94+
print(account.get_balance())
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+

0 commit comments

Comments
 (0)