Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python

The document provides a comprehensive overview of Python programming, covering various topics such as data types, list operations, functions, file I/O, and libraries like NumPy, Pandas, and Matplotlib. It also touches on machine learning concepts, including supervised and unsupervised learning, and introduces Scikit-learn for implementing algorithms like logistic regression. Additionally, it discusses object-oriented programming principles and provides examples of code snippets for practical understanding.

Uploaded by

pallavisingh2002
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python

The document provides a comprehensive overview of Python programming, covering various topics such as data types, list operations, functions, file I/O, and libraries like NumPy, Pandas, and Matplotlib. It also touches on machine learning concepts, including supervised and unsupervised learning, and introduces Scikit-learn for implementing algorithms like logistic regression. Additionally, it discusses object-oriented programming principles and provides examples of code snippets for practical understanding.

Uploaded by

pallavisingh2002
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

1) Import random //To create random number

2) # for single line comment


3) ‘’’ ‘’’ for multi line comment
4) Data types of Python(main) - Numbers Strings , List, Tuples, Dictionaries(Map)
5) string_name = ‘’’ ‘’’ print(string_name) // To print multi line string
6) Different print different line

7) List = array [ ]
8) List_name.append(‘ ‘) // to add elemts to list
9) List_name.insert(3 , ‘ ‘ ) // to insert at index 3
10) List_name.remove ( ‘ ‘)
11) Print( List_name + [ ‘pillow’ , ‘ ‘ , ‘ ‘ ])// to print multiple elemts with lists
12) Print( min (List_name) ) // to print max,min
13) Tuples ( ) // similar like list but no changes
14) List_name = List_name2( Tup)
15) Dictionaries
16)

17)
18) To scan – input() // take as string
19) Number = int( input ( ) ) // type casted to int
20) loops
21) for i in range (staring , last ) :
print(i)
22)

23) Function
24) String

25) File IO
TO READ FILE

26) OOPS
_ MEAN private
More study yourself

Data Science
1.
2.
3. Numpy
4. Takes less time, space
5. Arr = np.arrange(10000) //To range 0 to 10000
6. %time ///for finding time
7. np.array_name =( [ ] , dtype = ‘ ‘ ) // to declare numpy
8. import numpy as np , from numpy import * // difference is 2nd 1 import all function
(function may same from other modules too like max)
9. array_name.shape // (column ,row)
10. np.zeros( (r,c) ) //to create blank array of 0
11. np.ones(6/0.dtype // array of 1 1 1 1 1 1 dtype(‘float64’)
12. np.empty( ( r,c) ) // garbage value but mostly like np.zeros
13. np doesn’t do matrix element - array_name*array*name
14. It do element wise operation like 1/array_name
Slicing
1. The concept is array declaration is view not copy

2.
3. Arr2 = arr1.copy() // to copy

More on numpy function

1. Axis 2 types – Axis 0 –> column, Axis 1 – >row


2. Arr.sum(axis = 0)
3. Dot product only for m*n and n*m
4. Arr.transpose() //To transpose
5. Dir(np) // to know all the function
6. Np.sort(array_name,axis = 0,kind = ‘mergesort’)
7. B = b.reshape(r,c) // to convert array of p*q to r*c
8. Np.argsort( array_name) // sort and out is given as index number
9.

Pandas
1. We can read csv using pandas
2. To make data frame
3. Df = pd.DataFrame() / to make empty data frame
4. Dataframe_name.head(3) // to show top 3 data from data frame similarly .tail
5. Data slice In pandas
6. Df1.iloc[ 0:3 , 4 :6 ] //to access r rows ,c column
7. Print( df [‘tyle_name’] ) //to print the column
8. Astype // to change the data type
9. Object data type will store all data type individually and category will create a list and create
a map so that I will not store every time
WRITE a CSV
1. File_name.to_csv( ‘ file name .csv ‘ , ) // to create write csv
2. For more information go to pandas.Dataframe.to_csv
3. Data frame merging
4. Df3 = pd.merge(df1,df2,right_on = “y” ,left_one = “b”) // to merge two csv
5.
6. Json = Java script object notation
7. Helps in storing data
8. Rules of json
1) 1 no ' ' single notation
2) 2 no comment is written

Matplotlib

1) Inline make map/graph here only


2) Matplotlib styles

MACHINE
LEARNING
1) Feature = x ; label = y
2) Datasets – Finding data and instructing machine on what to do and how to behave
3) Uci ml repository – can take data sets frm here ex -iris(type of flower)
4) Supervised – Regression(numerical) , Classification (type)
5) Unsupervised – Association and Clustering

SCIKIT learn
1. Is a library donated by goggle
2.

Linear Regression

Loss function

 Mini Batch and Stochastic Gradient Descent


Logistic Regression


 Log of odd -> odd = y = mx+c
 Cost function
 U can take data sets frm uci
 # Train a logistic regression classifier to predict
whether a flower is iris virginica or not
 from sklearn import datasets
 from sklearn.linear_model import LogisticRegression
 import numpy as np
 import matplotlib.pyplot as plt
 iris = datasets.load_iris()
 # print(list(iris.keys()))
 # print(iris['data'].shape)
 # print(iris['target'])
 # print(iris['DESCR'])
 X = iris["data"][:, 3:]
 y = (iris["target"] == 2).astype(np.int)

 # Train a logistic regression classifier
 clf = LogisticRegression()
 clf.fit(X,y)
 example = clf.predict(([[2.6]])) // result
 print(example)

 # Using matplotlib to plot the visualization
 X_new = np.linspace(0,3,1000).reshape(-1,1)
 y_prob = clf.predict_proba(X_new)
 print(y_prob)
 plt.plot(X_new, y_prob[:,1], "g-", label="virginica")
 plt.show()


 # print(y)
 # print(iris["data"])
 # print(X)



 Epoc



 t = time.localtime()
 formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", t)


 Imputer – filling missing cells


pipeline used for automization

 To add new column in csv

Gradient Descent and Cost Function


 Reduce cost
 Cost function – 1) mse

Python Basics
 To check the Python version of the editor, you can find it by importing
the sys module:

 Variables do not need to be declared with any particular type, and can
even change type after they have been set.
 type()
From Python's perspective, lists are defined as objects with the data type
'list':

<class 'list'>




 If you have a collection of values in a list, tuple etc. Python allows you
to extract the values into variables. This is called unpacking.

 The best way to output multiple variables in the print() function is to


separate them with commas(not ‘+’ operator ), which even support
different data types:

 Variables that are created outside of a function are known as global


variables.
 Global variables can be used by everyone, both inside of functions and
outside.
 List =[] tuple= () dict = { : , : } set {}

 Float can also be scientific numbers with an "e" to indicate the power
of 10.
 Note: You cannot convert complex numbers into another number type.
 You can use quotes inside a string, as long as they don't match the
quotes surrounding the string:
print("He is called 'Johnny'")
 Square brackets can be used to access elements of the string.
 for x in "banana":
print(x)
//output
b
a
n
a
n
a

 txt = "The best things in life are free!"


print("free" in txt)

Check if "expensive" is NOT present in the following text:

txt = "The best things in life are free!"


print("expensive" not in txt)
 Get the characters from the start to position 5 (not included):
 The strip() method removes any whitespace from the beginning or
the end:

 To specify a string as an f-string, simply put an f in front of the


string literal, and add curly brackets {} as placeholders for variables
and other operations.

 A modifier is included by adding a colon : followed by a legal


formatting type, like .2f which means fixed point number with 2
decimals:
 Example
Display the price with 2 decimals:
price = 59
txt = f"The price is {price:.2f} dollars"
print(txt)

 To insert characters that are illegal in a string, use an escape


character.
 An escape character is a backslash \ followed by the character you
want to insert.
 \b Backspace
#This example erases one character (backspace):
txt = "Hello \bWorld!"
print(txt) //HelloWorld!



 All string methods return new values. They do not change the original
string.


 One more value, or object in this case, evaluates to False, and that is
if you have an object that is made from a class with
a __len__ function that returns 0 or False:


 Python also has many built-in functions that return a boolean value,
like the isinstance() function, which can be used to determine if an
object is of a certain data type:


 Insert/append

 thislist.insert(2, "watermelon")
 To add an item to the end of the list, use the append() method:

 To append elements from another list to the current list, use


the extend() method.

 Removal
 The pop() method removes the specified index.
 The remove() method removes the specified item.
 If you do not specify the index, the pop() method removes the last
item.
The del keyword also removes the specified index:

 The clear() method empties the list.


 List Comprehension offers the shortest syntax for looping through
lists: [print(x) for x in thislist]

 newlist =
[expression for item in iterable if condition == True]

 o if you want a case-insensitive sort function, use str.lower as a key


function:

thislist = ["banana", "Orange", "Kiwi", "cherry"]


thislist.sort(key = str.lower)
print(thislist)

What if you want to reverse the order of a list, regardless of the alphabet?
The reverse() method reverses the current sorting order of the elements.

 You cannot copy a list simply by typing list2 = list1,


because: list2 will only be a reference to list1, and changes made
in list1 will automatically also be made in list2.
mylist = thislist.copy()

 list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

for x in list2:
list1.append(x)

print(list1)

 list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

list1.extend(list2)
print(list1)

 To create a tuple with only one item, you have to add a comma
after the item, otherwise Python will not recognize it as a tuple.
 Convert the tuple into a list to be able to change it:

 imp

 Unpacking

A set is a collection which is unordered, unchangeable*, and unindexed.

* Note: Set items are unchangeable, but you can remove items and add new
items.

 To remove an item in a set, use the remove(), or the discard() method.

Tuples are unchangeable, meaning that you cannot change, add, or


remove items once the tuple is created.

But there are some workarounds.

 SET-You can also use the pop() method to remove an item, but this
method will remove a random item, so you cannot be sure what item
that gets removed.

The return value of the pop() method is the removed item.


 Note: The | operator only allows you to join sets with sets, and not
with other data types like you can with the union() method.


 Dictionary items are ordered, changeable, and do not
allow duplicates.

 x = thisdict.get("model")

 The keys() method will return a list of all the keys in the dictionary.

 Add In dic

Make a change in the original dictionary, and see that the items list
gets updated as well

 The update() method will update the dictionary with the items from a
given argument. If the item does not exist, the item will be
added.

The argument must be a dictionary, or an iterable object with


key:value pairs.

 Del,pop,clear

 When looping through a dictionary, the return value are


the keys of the dictionary, but there are methods to return
the values as well.

print all values in the dictionary, one by one:

for x in thisdict:
print(thisdict[x])

You can also use the values() method to return values of a dictionary:
for x in thisdict.values():
print(x)

 Loop through both keys and values, by using the items() method:

for x, y in thisdict.items():
print(x, y)

 Access Items in Nested


Dictionaries
To access items from a nested dictionary, you use the name of the
dictionaries, starting with the outer dictionary:

Example
Print the name of child 2:

print(myfamily["child2"]["name"])


 The range() function defaults to increment the sequence by 1,


however it is possible to specify the increment value by adding a third
parameter: range(2, 30, 3):

Function

 In Python a function is defined using the def keyword:


 The terms parameter and argument can be used for the same thing:
information that are passed into a function.

From a function's perspective:


A parameter is the variable listed inside the parentheses in the function
definition.

An argument is the value that is sent to the function when it is called.

If you do not know how many arguments that will be passed into your
function, add a * before the parameter name in the function definition.

This way the function will receive a tuple of arguments, and can access the
items accordingly:

 Default argument

 You can send any data types of argument to a function (string, number,
list, dictionary etc.), and it will be treated as the same data type inside
the function.

 There are two kind of arguments, namely, keyword and


positional. As the name suggests, the keyword argument is
identified by a function based on some key whereas the
positional argument is identified based on its position in the
function definition.
 def my_function(*, x):
print(x)

my_function(3) //used only to use keyword argument

You can combine the two argument types in the same function.

Any argument before the / , are positional-only, and any


argument after the *, are keyword-only.

Example
def my_function(a, b, /, *, c, d):
print(a + b + c + d)

my_function(5, 6, c = 7, d = 8)
 Lambda //act as small funtcion
x = lambda a : a + 10 //lambda arguments : expression
print(x(5))

Use lambda functions when an anonymous function is required for a short


period of time.

Note: The list's remove() method only removes the first occurrence of the
specified value.

 The self parameter is a reference to the current instance of the class,


and is used to access variables that belong to the class.

Python Inheritance
Inheritance allows us to define a class that inherits all the methods and
properties from another class.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived
class.

 When you add the __init__() function, the child class will no longer
inherit the parent's __init__() function.

 To keep the inheritance of the parent's __init__() function, add a call
to the parent's __init__() function:

 Python also has a super() function that will make the child class
inherit all the methods and properties from its parent:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)

The __iter__() method acts similar, you can do operations (initializing etc.),
but must always return the iterator object itself.

The __next__() method also allows you to do operations, and must return the
next item in the sequence.

Python JSON
 JSON is a syntax for storing and exchanging data.
 JSON is text, written with JavaScript object notation.

 If you have a JSON string, you can parse it by using


the json.loads() method.
 If you have a Python object, you can convert it into a JSON string by
using the json.dumps() method. # convert into JSON:

Python RegEx
 A RegEx, or Regular Expression, is a sequence of characters that forms a
search pattern.

 Python has a built-in package called re, which can be used to work with
Regular Expressions.
 Import the re module:

import re

#Check if the string starts with "The" and ends with "Spain":

txt = "The rain in Spain"


x = re.search("^The.*Spain$", txt)

if x:
print("YES! We have a match!")
else:
print("No match")

 Metacharacters are characters with a special meaning:

 A special sequence is a \ followed by one of the characters in the list


below, and has a special meaning:
 The findall() function returns a list containing all matches.

 import re

txt = "The rain in Spain"


x = re.search("\s", txt)

print("The first white-space character is located in


position:", x.start())
 The split() function returns a list where the string has been split at
each match:

You can control the number of occurrences by specifying


the maxsplit parameter:


.span() returns a tuple containing the start-, and end positions of the
match.
.string returns the string passed into the function
.group() returns the part of the string where there was a match

Python PIP
 PIP is a package manager for Python packages, or modules if you like.
 Use the list command to list all the packages installed on your
system:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-
32\Scripts>pip list

 The try block lets you test a block of code for errors.
 The except block lets you handle the error.
 The else block lets you execute code when there is no error.
 The finally block lets you execute code, regardless of the result of
the try- and except blocks.

 username = input("Enter username:")


print("Username is: " + username)

F-Strings
 F-string allows you to format selected parts of a string.
 To specify a string as an f-string, simply put an f in front of the string
literal, like this:

Return "Expensive" if the price is over 50, otherwise return "Cheap":


price = 49
txt = f"It is very {'Expensive' if price>50 else 'Cheap'}"

print(txt)

Python File Open


The open() function takes two parameters; filename, and mode.

There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not
exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

 Note: the "w" method will overwrite the entire file.

“a” will add/append

Create a New File


 To create a new file in Python, use the open() method, with one of the
following parameters:

"x" - Create - will create a file, returns an error if the file exist

"a" - Append - will create a file if the specified file does not exist

"w" - Write - will create a file if the specified file does not exist

 C:\Users\Your Name>pip install matplotlib


If this command fails, then use a python distribution that already
has Matplotlib installed, like Anaconda, Spyder etc.

 PLOTTING
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()


xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

 plt.plot(xpoints, ypoints, marker ='o')

 Marker Reference

You can choose any of these markers:

 This parameter is also called fmt, and is written with this syntax:

marker|line|color

Marker Size

You can use the keyword argument markersize or the shorter version, ms to
set the size of the markers:

 plt.plot(ypoints, marker = 'o', ms = 20)

 You can use the keyword argument markeredgecolor or the


shorter mec to set the color of the edge of the markers:

plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r')

 You can use the keyword argument markerfacecolor or the


shorter mfc to set the color inside the edge of the markers:
 You can use the keyword argument linestyle, or shorter ls, to change
the style of the plotted line:

plt.plot(x, y)

plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.title("Sports Watch Data")

 You can use the fontdict parameter in xlabel(), ylabel(),


and title() to set font properties for the title and labels.

font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}

plt.title("Sports Watch Data", fontdict = font1)


plt.xlabel("Average Pulse", fontdict = font2)
plt.ylabel("Calorie Burnage", fontdict = font2)

 You can use the loc parameter in title() to position the title.

Legal values are: 'left', 'right', and 'center'. Default value is 'center'.

plt.title("Sports Watch Data", loc = 'left')

 plt.grid()

 plt.grid(axis = 'x') // to specify which grid lines to display.

 With the subplot() function you can draw multiple plots in one
figure:

The subplot() function takes three arguments that describes the layout
of the figure.
The layout is organized in rows and columns, which are represented by
the first and second argument.
The third argument represents the index of the current plot.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is
the first plot.

 plt.scatter(x, y, color = '#88c999')

NUMPY
 n array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

 NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(d.ndim) // output 3

Access 2-D Arrays


To access elements from 2-D arrays we can use comma separated integers
representing the dimension and the index of the element.

Think of 2-D arrays like a table with rows and columns, where the dimension
represents the row and the index represents the column.

 We can also define the step, like this: [start:end:step].

 If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

The result includes the start index, but excludes the end index.
 Focus on , :

 The best way to change the data type of an existing array, is to make a copy of the array with
the astype() method.

 I and int

 The Difference Between Copy and View


 The main difference between a copy and a view of an array is that the copy is a new array,
and the view is just a view of the original array.

 Check if Array Owns its Data


As mentioned above, copies owns the data, and views does not own the data, but how can
we check this?
Every NumPy array has the attribute base that returns None if the array owns the data.
Otherwise, the base attribute refers to the original object.
x = arr.copy()
y = arr.view()

print(x.base)//none
print(y.base)//original array

 The shape of an array is the number of elements in each dimension.


The shape of an array is the number of elements in each dimension.

 Iterating //look at horizontal and vertical

If we iterate on a n-D array it will go through n-1th dimension one by one.

 nditer()

b'1'
b'2'
b'3'

 …………………………………………………………

…………………………………………………………


 We pass a sequence of arrays that we want to join to the concatenate() function, along with
the axis. If axis is not explicitly passed, it is taken as 0.

 Stack and concatenate

 We use array_split() for splitting arrays, we pass it the array we want to split and the number
of splits.

 Searching Arrays
To search an array, use the where() method.

ExampleGet your own Python Server


Find the indexes where the value is 4:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.where(arr == 4)

 import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 1)

print(x)
 There is a method called searchsorted() which performs a binary search in the array, and
returns the index where the specified value would be inserted to maintain the search order.

The searchsorted() method is assumed to be used on sorted arrays.//act as where()


+binarysearh

 Filtering
import numpy as np

arr = np.array([41, 42, 43, 44])

# Create an empty list


filter_arr = []

# go through each element in arr


for element in arr:
# if the element is higher than 42, set the value to True, otherwise False:
if element > 42:
filter_arr.append(True)
else:
filter_arr.append(False)

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

or
import numpy as np
arr = np.array([41, 42, 43, 44])
filter_arr = arr > 42
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
 The random module's rand() method returns a random float between 0 and 1.
 The randint() method takes a size parameter where you can specify the shape of an array.
x=random.randint(100, size=(5))

 The choice() method takes an array as a parameter and randomly


returns one of the values.

x = random.choice([3, 5, 7, 9])

 x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0],


size=(100))

The sum of all probability numbers should be 1.

 Shuffle means changing arrangement of elements in-place. i.e.


in the array itself.

The shuffle() method makes changes to the original array.

The permutation() method returns a re-arranged array (and leaves the


original array un-changed).

 Seaborn is a library that uses Matplotlib underneath to plot


graphs. It will be used to visualize random distributions.

 Binomial Distribution is a Discrete Distribution.(same as normal


distribution)
 Poisson Distribution is a Discrete Distribution.

It estimates how many times an event can happen in a specified time.


e.g. If someone eats twice a day what is the probability he will eat
thrice?

It has two parameters:

lam - rate or known number of occurrences e.g. 2 for above problem.

size - The shape of the returned array.

Binomial distribution only has two possible outcomes, whereas poisson


distribution can have unlimited possible outcomes.

But for very large n and near-zero p binomial distribution is near identical to
poisson distribution such that n * p is nearly equal to lam.
Difference Between Logistic and
Normal Distribution
Both distributions are near identical, but logistic distribution has more area
under the tails, meaning it represents more possibility of occurrence of an
event further away from mean.

For higher value of scale (standard deviation) the normal and logistic
distributions are near identical apart from the peak.

Relation Between Poisson and


Exponential Distribution
Poisson distribution deals with number of occurences of an event in a time
period whereas exponential distribution deals with the time between these
events.
Rayleigh distribution is used in signal processing.

It has two parameters:

scale - (standard deviation) decides how flat the distribution will be default
1.0).

size - The shape of the returned array.

Zipf's Law: In a collection, the nth common term is 1/n times of the
most common term. E.g. the 5th most common word in English occurs
nearly 1/5 times as often as the most common word.

ufuncs?
ufuncs stands for "Universal Functions" and they are NumPy functions that
operate on the ndarray object.

They also provide broadcasting and additional methods like


reduce,accumulate etc. that are very helpful for computation.
ufuncs also take additional arguments, like:

where boolean array or condition defining where the operations should


take place.

dtype defining the return type of elements.

out output array where the return value should be copied.

 ufuncs are used to implement vectorization in NumPy which is


way faster than iterating over elements.

What is Vectorization?
Converting iterative statements into a vector based operation is called
vectorization.


 z = np.add(x, y)
 newarr = np.add(arr1, arr2)
newarr = np.subtract(arr1, arr2)
newarr = np.multiply(arr1, arr2)
newarr = np.divide(arr1, arr2) //float
newarr = np.power(arr1, arr2)
newarr = np.mod(arr1, arr2)
newarr = np.remainder(arr1, arr2)
newarr = np.absolute(arr)
newarr = np.divmod(arr1, arr2)// ivmod() function return both
the quotient and the the mod.The return value is two arrays, the first
array contains the quotient and second array contains the mod.

 imp interesting//check function

 Check the type of a function to check if it is a ufunc or not.

A ufunc should return <class 'numpy.ufunc'>.

To test if the function is a ufunc in an if statement, use


the numpy.ufunc value (or np.ufunc if you use np as an alias for numpy):

Example
Use an if statement to check if the function is a ufunc or not:

import numpy as np

if type(np.add) == np.ufunc:
print('add is ufunc')
else:
print('add is not ufunc')
There are primarily five ways of rounding off decimals in NumPy:

1. Truncation//Remove the decimals, and return the float number closest to zero. Use the
trunc() and fix() functions.
2. Fix //
3. Rounding//The around() function increments preceding digit or decimal by 1 if >=5 else
do nothing.
4. Floor// The floor() function rounds off decimal to nearest lower integer.
5. Ceil// The ceil() function rounds off decimal to nearest upper integer.

 print(np.log2(arr))

 What is the difference between summation and addition?

Addition is done between two arguments whereas summation happens


over n elements.
 Imp most
 x = np.lcm(num1, num2)

The reduce() method will use the ufunc, in this case the lcm() function, on
each element, and reduce the array by one dimension.

 x = np.lcm.reduce(arr)
 x = np.gcd(num1, num2)

The reduce() method will use the ufunc, in this case the gcd() function, on
each element, and reduce the array by one dimension.

 To save model in ml

 To make string to numerical in featuring

 ONE hot Encoding


 Merge the dummy variable with real one and then drop 1 dummy variable bcz of trap -multi
co linear
 TO DECODE

Select high information gain in decision tree


 Label encoding

Extra
 re.sub(r'[^a-z0-9]', '', string):
re.sub(pattern, replacement, string): Replaces occurrences of the pattern in the string with
the replacement.
r'[^a-z0-9]': This regular expression pattern matches any character that is not a lowercase
letter (a-z) or a digit (0-9).
[^...]: A negated character class, matches any character not listed.
'': The replacement string, effectively removes the matched characters by replacing them
with an empty string.

 How memory Saved


Overhead of new memory and copying them when dynamic aray grow
Use agp arithmetic geometric progression

LINK list

 Python -Stack -> Deque

You might also like