Cartesian Product Preliminaries /* import pandas as pd */ import pandas as pd Create Data /* Create two lists */ i = [1,2,3,4,5] j = [1,2,3,4,5] Calculate Cartesian Product (Method 1) /* List every single x in i with every single y (i.e. Cartesian product) */ [(x, y) for x in i for y in …
Basic Operations With NumPy Array /* Import modules */ import numpy as np /* Create an array */ civilian_deaths = np.array([4352, 233, 3245, 256, 2394]) civilian_deaths array([4352, 233, 3245, 256, 2394]) /* Mean value of the array */ civilian_deaths.mean() 2096.0 /* Total amount of deaths */ civilian_deaths.sum() 10480 /* Smallest value in the array */ …
Assignment Operators Create some variables a = 2 b = 1 c = 0 d = 3 Assigns values from right side to left side c = a + b c 3 Add right to the left and assign the result to left (c = a + c) c += a c 5 Subtract right …
Arithmetic Basics Create some simulated variables x = 6 y = 9 x plus y x + y 15 x minus y x – y -3 x times y x * y 54 the remainder of x divided by y x % y 6 x divided by y x / y 0.6666666666666666 x divided by …
Applying Functions To List Items Create a list of regiment names regimentNames = [‘Night Riflemen’, ‘Jungle Scouts’, ‘The Dragoons’, ‘Midnight Revengence’, ‘Wily Warriors’] Using A For Loop Create a for loop goes through the list and capitalizes each /* create a variable for the for loop results */ regimentNamesCapitalized_f = [] /* for every item …
Apply Operations Over Items In A List Method 1: map() /* Create a list of casualties from battles */ battleDeaths = [482, 93, 392, 920, 813, 199, 374, 237, 244] /* Create a function that updates all battle deaths by adding 100 */ def updated(x): return x + 100 /* Create a list that applies …
Append Using The Operator Create A List /* Create a list of three names */ names = [‘chris’, ‘nguyen’, ‘jack’] Create A Value To Append To The List /* Create a string list variable containing a name */ to_append = [‘sarah’] Append Value To List /* Create a new list with all the original names …
All Combinations For A List Of Objects Preliminary /* Import combinations with replacements from itertools */ from itertools import combinations_with_replacement Create a list of objects /* Create a list of objects to combine */ list_of_objects = [‘warplanes’, ‘armor’, ‘infantry’] Find all combinations (with replacement) for the list /* Create an empty list object to hold …
Add Padding Around String Create Some Text text = ‘Chapter 1’ Add Padding Around Text /* Add Spaces Of Padding To The Left */ format(text, ‘>20’) ‘ Chapter 1’ /* Add Spaces Of Padding To The Right */ format(text, ‘<20’) ‘Chapter 1 ‘ /* Add Spaces Of Padding On Each Side */ format(text, ‘^20’) ‘ …
K-Nearest Neighbors Classification Preliminaries import pandas as pd from sklearn import neighbors import numpy as np %matplotlib inline import seaborn Create Dataset Here we create three variables, test_1 and test_2 are our independent variables, ‘outcome’ is our dependent variable. We will use this data to train our learner. training_data = pd.DataFrame() training_data[‘test_1’] = [0.3051,0.4949,0.6974,0.3769,0.2231,0.341,0.4436,0.5897,0.6308,0.5] training_data[‘test_2’] = [0.5846,0.2654,0.2615,0.4538,0.4615,0.8308,0.4962,0.3269,0.5346,0.6731] training_data[‘outcome’] = …