Python Tutorials

Learn Python By Example – Generating Random Numbers With NumPy

Hits: 6 Generating Random Numbers With NumPy Import Numpy import numpy as np Generate A Random Number From The Normal Distribution np.random.normal() 0.5661104974399703 Generate Four Random Numbers From The Normal Distribution np.random.normal(size=4) array([-1.03175853, 1.2867365 , -0.23560103, -1.05225393]) Generate Four Random Numbers From The Uniform Distribution np.random.uniform(size=4) array([ 0.00193123, 0.51932356, 0.87656884, 0.33684494]) Generate Four Random Integers …

Learn Python By Example – Formatting Numbers

Hits: 7 Formatting Numbers Create A Long Number annual_revenue = 9282904.9282872782 Format Number /* Format rounded to two decimal places */ format(annual_revenue, ‘0.2f’) ‘9282904.93’ /* Format with commas and rounded to one decimal place */ format(annual_revenue, ‘0,.1f’) ‘9,282,904.9’ /* Format as scientific notation */ format(annual_revenue, ‘e’) ‘9.282905e+06’ /* Format as scientific notation rounded to two …

Learn Python By Example – Flatten Lists Of Lists

Hits: 1 Flatten Lists Of Lists Create A List Of Lists /* Create a list containing three lists of names */ list_of_lists = [[‘Amy’,’Betty’,’Cathryn’,’Dana’], [‘Elizabeth’,’Fay’,’Gora’], [‘Heidi’,’Jane’,’Kayley’]] Flatten The Lists Of Lists Into A Single List /* For each element in list_of_lists, take each element in the list */ flattened_list = [i for row in list_of_lists …

Learn Python By Example – Exiting A Loop

Hits: 3 Exiting A Loop Create A List /* Create a list: */ armies = [‘Red Army’, ‘Blue Army’, ‘Green Army’] Breaking Out Of A For Loop for army in armies: print(army) if army == ‘Blue Army’: print(‘Blue Army Found! Stopping.’) break Red Army Blue Army Blue Army Found! Stopping. Notice that the loop stopped …

learn Python By Example – Dictionary Basics

Hits: 6 Dictionary Basics Basics Not sequences, but mappings. That is, stored by key, not relative position. Dictionaries are mutable.   Build a dictionary via brackets unef_org = {‘name’ : ‘UNEF’, ‘staff’ : 32, ‘url’ : ‘http://unef.org’} View the variable unef_org {‘name’: ‘UNEF’, ‘staff’: 32, ‘url’: ‘http://unef.org’} Build a dict via keys who_org = {} …

Learn Python By Example – Data Structure Basics

Hits: 2 Data Structure Basics Lists “A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list.” – A Byte Of Python Lists are mutable. /* Create a list of countries, then print the results */ allies = [‘USA’,’UK’,’France’,’New Zealand’, ‘Australia’,’Canada’,’Poland’]; allies …

Machine Learning for Beginners in Python: Support Vector Classifier

Hits: 4 Support Vector Classifier There is a balance between SVC maximizing the margin of the hyperplane and minimizing the misclassification. In SVC, the later is controlled with the hyperparameter C, the penalty imposed on errors. C is a parameter of the SVC learner and is the penalty for misclassifying a data point. When C is …

Machine Learning for Beginners in Python: How to Handle Imbalanced Classes In Logistic Regression

Hits: 3 Handling Imbalanced Classes In Logistic Regression Preliminaries /* Load libraries */ from sklearn.linear_model import LogisticRegression from sklearn import datasets from sklearn.preprocessing import StandardScaler import numpy as np Load Iris Flower Dataset /* Load data */ iris = datasets.load_iris() X = iris.data y = iris.target Make Classes Imbalanced /* Make class highly imbalanced by …

Machine Learning for Beginners in Python: Apply Operations To Elements

Hits: 29 Apply Operations To Elements Preliminaries import numpy as np Create Matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Create Vectorized Function add_100 = lambda i: i + 100 vectorized_add_100 = np.vectorize(add_100) Apply Function To Elements vectorized_add_100(matrix) array([[101, 102, 103], [104, 105, 106], [107, 108, 109]])   Python Example for …

Machine Learning for Beginners in Python: How to Create A Matrix

Hits: 3 Create A Matrix Preliminaries import numpy as np Create Matrix matrix = np.array([[1, 4], [2, 5]]) Note NumPy’s mat data structure is less flexible for this purposes and should be avoided.   Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning & Data Science Recipes Portfolio Projects for Aspiring Data Scientists: Tabular …

Python Built-in Methods – Python List count() Method

Hits: 32 Python List count() Method Counts the number of occurrences of an item Usage Use count() method to find the number of times the specified item appears in the list. Syntax list.count(item) Parameter Condition Description item Required Any item (of type string, list, set, etc.) you want to search for. Examples # Count number of occurrences of ‘red’ L …

Python Built-in Methods – Python List copy() Method

Hits: 19 Python List copy() Method Copies the list shallowly Usage The copy() method returns the Shallow copy of the specified list. Syntax list.copy() Basic Example # Create a copy of list ‘L’ L = [‘red’, ‘green’, ‘blue’] X = L.copy() print(X) # Prints [‘red’, ‘green’, ‘blue’] copy() vs Assignment statement Assignment statement does not copy objects. For example, old_List …

Python Built-in Methods – Python List clear() Method

Hits: 4 Python List clear() Method Removes all items from the list Usage Use clear() method to remove all items from the list. This method does not return anything; it modifies the list in place. Syntax list.clear() Basic Example L = [‘red’, ‘green’, ‘blue’] L.clear() print(L) # Prints [] Please note that clear() is not same as assigning an empty …

Python Built-in Methods – Python List append() Method

Hits: 0 Python List append() Method Appends an item to a list Usage The append() method adds a single item to the end of the list. This method does not return anything; it modifies the list in place. Syntax list.append(item) Parameter Condition Description item Required An item you want to append to the list Examples # Append ‘yellow’ L = …

Python Built-in Methods – Python zip() Function

Hits: 5 Python zip() Function Combines multiple iterables together Usage The zip() function combines items from each of the specified iterables. The return value is a list of tuples where the items of each passed iterable at same index are paired together. Syntax zip(iterables) Parameter Condition Description iterables Optional One or more iterables (list, tuple, dictionary etc.) to be joined together …