HOW TO WRITE A CLASS OBJECT IN PYTHON

The one way to get started with  the object oriented programming in python is to get started with the " creating a class object and using it".

things u need to get started:

1. Latest version of python3 u can get that from here 

2. An ide or u can use python default ide which it comes with!

that's all u need to get started.

here we are gonna make code for math function called standard scaler(for scaling data). you make one for anything u want I'm just a math guy!! 

so the math formula for standard scaler is :


hence we start with a class object called Scale , the class object doesn't need any input variable for it's name we do it in functions inside of it.

The first function inside of class object should be a __init__()" function it must be..!

next, an __init__() function should always starts with a "self" variable and other as follows by as other variables u may like...

 class Scale:
       def _init_(self,data):
          self.data = data

it doesn't need to return any data necessarily....unless u want to get an error!

then the rest of the "def" functions are followed by the above formula, and at last we need to call the class with any suitable name u like to initiate the class use..

[NOTE: the self variable doesn't need to given..if u get an error about no self variable then u done something wrong in initialisation]

Ex:

scaled = Scale()

code:

import numpy as np

#create a class obj called Scale

class Scale:
	def __init__(self):
		print("doesn't prints any\n")

#formula of scaling

	def standard_scaler(data):
		X = np.array(data)
		N = X.shape[0]
		print("sum of series:",N,end='\n')
		U = np.average(X,axis=0)
		print("avg of input:",U)
		sig = np.square(X-U)
		sig1 = np.sum(sig,axis=0)
		sig = sig1/N
		sig  = np.sqrt(sig)
		print("the sig value:",sig)
		Z = (X-U)/sig
		print(Z)
		return Z

#data to check

data = [[0, 0],
 [1, 0],
 [0, 1],
 [1, 1]]

scaled = Scale()
print(Scale.standard_scaler(data))

       
 



Comments

Popular posts from this blog

HACK AND CLONE CAR KEY FOB, MY EPIC FAILURE...!!

HACK HC-05(BLUETOOTH MODULE) FRIMWARE INTO HID FRIMWARE

secrets of arduino atmega328p adc