# Python for Machine Learning

How can you apply inheritance in Python?

5 Likes

Why is learning programming important?

4 Likes

How do I create variables in python?

3 Likes

With inheritance one class can derive the properties of another class. Consider this example: You would have certain features similar to your father and your father would have certain features similar to your grandfather. This is what is known as inheritance. Or in other words, you can also say that, you have inherited some physical traits from your father and your father has inherited some physical traits from your grandfather.

Now, that we know what exactly is inheritance, let’s see how can implement inheritance in python:

class Vehicle:
def init(self,mileage, cost):
self.mileage = mileage
self.cost = cost
def show_details(self):
print(“I am a Vehicle”)
print("Mileage of Vehicle is ", self.mileage)
print("Cost of Vehicle is ", self.cost)

Code Explanation:
Here, we are creating our parent class called as ‘Vehicle’. This class has two methods:
init()
show_details()
With the help of init() method, we are just assigning the values for mileage and cost. Then, with the help of ‘show_details()’, we are printing out the mileage of the vehicle and the cost of the vehicle.

Now, that we have created the class, let’s go ahead and create an object for it:
v1 = Vehicle(500,500)
v1.show_details()

Code Explanation:
We are starting off by creating an instance of the Vehicle class. The Vehicle class takes in two parameters which are basically the values for mileage and cost. Once, we assign the values, we just go ahead and print out the details, by using the method v1.show_details(). Now, let’s go ahead and create the child class:
class Car(Vehicle):
def show_car(self):
print(“I am a car”)
c1 = Car(200,1200)
c1.show_details()
c1.show_car()

Code Explanation:
We are starting off by creating the child class ‘Car’. This child class inherits from the parent class “Vehicle”.
This child class has it’s own method ‘show_Car()’, which just prints out ‘I am car’. Once we create the child class, it’s time to instantiate the class. We use the command:
c1 = Car(200,1200), to create an object ‘c1’ of the class ‘Car’.

Now, with the help of c1, we are invoking the method, c1.show_details().
Even though, show_details in exclusively a part of the ‘Car’ class, we are able to invoke it because the ‘Car’ class inherits from the ‘Vehicle’ class and the show_details() method is part of the Vehicle class.

Finally, we are using c1.show_car() to go and print out ‘I am car’.

3 Likes

As someone from a non-technical background, the word ‘Programming’ would itself be enough to send shivers down your spine! So, you must be thinking ‘Why should I even put in effort to learn Programming’?

Well programmers are in high demand all over the world and the median salary of a computer programmer is 100,000k\$ per annum. And with the advent of artificial intelligence and the threat of many jobs becoming automated, you don’t have to be scared at all, because it will be you who will help in automating tasks as a computer programmer! Also there are still many systems for the next several decades that still require human intelligence, coupled with the ability to write code that makes this a very secure job option.

Now, that we know the importance of programming, let’s understand what exactly it is!
To answer that, let me ask a question! How do humans communicate with each other? We humans use a common language to speak with each other, isn’t it?

Similarly, if we have to speak with a computer, we need to learn a language which the computer can understand! And this is where a programming language comes in.
So, simply put, we solve real world problems, by speaking with the computer in a language it actually understands.

Let’s take the example of a calculator!
When, you feed in ‘1234 * 4321’ in the calculator. It immediately shows you, the result is ‘5,332,114’. How was the calculator able to do this multiplication?
This was possible because you wrote in a simple program to multiply two numbers which the calculator can understand.

Now, let’s look at some salary statistics for people who are skilled in programming!
According to LinkedIn, the average salary of a computer programmer in the United States is 100,000\$ per annum and Rs.8,00,000 per annum in India. Well, this should be a good enough motivation for you to start learning programming!

6 Likes

Programming languages deal with data. So, the question which arises over here is how do we actually store this data if we want to reuse it? Let’s you’re working with some data about employees of your organization and you’d have to store the names of employees, so how will you do that?

Well this is where variables come in. Variables are temporary storage spaces to store values.
Let’s say we create a variable called “employee_name”. Then we can go ahead and store the value “John” inside the variable. After some time, i can replace the value with “Sam”. Again after some time i’ll replace it with “Matt”.

Let’s see how can we create variables in python:
var1 = “Sam”
var1 = “Matt”
var1 = “John”
Since, variable is a temporary storage space, we can change the values which are stored in the variable “var1”.

1 Like

What are the basic math operators in Python?

1 Like

What are strings in Python?

1 Like

What is the best compiler for Python?

What does n do in Python?

What is the latest Python version?

Operators in Python as the name states help us to perform different types of operations on the data.

Arithmetic Operators:
Arithmetic operators help us to perform simple arithmetic operations such as addition, subtraction, multiplication and division.
a=10
b=20
a+b
a-b
a/b
a*b

1 Like

Compilers are programs that convert source code written in a high-level programming language to a lower-level programming language.

Here are the top 5 best compilers for Python:

• CPython: This compiler-cum-interpreter is the de-facto Python compiler as it belongs to the reference implementation of Python i.e. CPython

• Brython: Supports – Python 3 to 3.7

• Nuitka: Supports – Python 2.6, 2.7, 3.3 to 3.7

• PyJS: Supports – Up to Python 2.7

• Skulpt: Supports – Up to Python 3.3

1 Like

In Python strings, the backslash “” is a special character, also called the “escape” character. It is used in representing certain white space characters and “\n” is one of them. It is used to start a new line.

1 Like

Strings are sequence of characters enclosed within single quotes(‘ ’), double quotes(“ “) or triple quotes(‘’’ ‘’’) a = ‘Hello World’
b = “This is Sparta”
c = ‘’‘I am
going
to France
tomorrow
‘’’

Code Explanation:
Here we start off by creating a string variable called ‘my_string’ and assign the value “My name is John” to it. In a string, each individual character is at a particular index and the index value starts at 0.
So, if we have to extract the first character in the string ‘M’, then, we ‘d have to give this command:
my_string[0]
Since, indexing starts at 0, the index value of the first character ‘M’ will be 0.
We would have to give this command:
my_string[-1] to extract the last character from this string.

2 Likes

How can I read a pkl file for visualising it using pandas?

Pickle files (.pkl) are used to store the serialized form of Python objects. This means objects like list, set, tuple, dict, etc. are converted to a character stream before being stored on the disk. This allows you to continue working with the objects later on. These are particularly useful when you have trained your machine learning model and want to save them to make predictions later on.

So, if you serialized the files before saving them, you need to de-serialize them before you use them in your Python programs. This is done using the pickle.load() function in the pickle module. But when you open the pickle file with Python’s open() function, you need to provide the ‘rb’ parameter to read the binary file.

``````import pickle

with open('./Importing files/sample_pickle.pkl','rb') as file:

# pickle data
print(type(data))

df_pkl = pd.DataFrame(data)
df_pkl

``````

Sir,