Python
Python
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
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
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
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)
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.
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
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:
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:
newlist =
[expression for item in iterable if condition == True]
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.
for x in list2:
list1.append(x)
print(list1)
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
* Note: Set items are unchangeable, but you can remove items and add new
items.
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.
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.
Del,pop,clear
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)
Example
Print the name of child 2:
print(myfamily["child2"]["name"])
Function
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.
You can combine the two argument types in the same function.
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))
Note: The list's remove() method only removes the first occurrence of the
specified value.
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.
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":
if x:
print("YES! We have a match!")
else:
print("No match")
import re
.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.
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:
print(txt)
"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
"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
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()
Marker Reference
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.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.title("Sports Watch Data")
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
Legal values are: 'left', 'right', and 'center'. Default value is 'center'.
plt.grid()
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.
NUMPY
n array that has 2-D arrays (matrices) as its elements is called 3-D array.
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
Think of 2-D arrays like a table with rows and columns, where the dimension
represents the row and the index represents the column.
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
print(x.base)//none
print(y.base)//original array
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.
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.
import numpy as np
x = np.where(arr == 4)
import numpy as np
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.
Filtering
import numpy as np
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))
x = random.choice([3, 5, 7, 9])
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.
scale - (standard deviation) decides how flat the distribution will be default
1.0).
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.
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.
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))
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
Merge the dummy variable with real one and then drop 1 dummy variable bcz of trap -multi
co linear
TO DECODE
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.
LINK list