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’) ‘ …
Term Frequency Inverse Document Frequency Preliminaries import numpy as np from sklearn.feature_extraction.text import TfidfVectorizer import pandas as pd Create Text Data text_data = np.array([‘I love Brazil. Brazil!’, ‘Sweden is best’, ‘Germany beats both’]) Create Feature Matrix tfidf = TfidfVectorizer() feature_matrix = tfidf.fit_transform(text_data) feature_matrix.toarray() array([[ 0. , 0. , 0. , 0.89442719, 0. , 0. …
Imputing Missing Class Labels Preliminaries import numpy as np from sklearn.preprocessing import Imputer Create Feature Matrix With Missing Values X = np.array([[0, 2.10, 1.45], [1, 1.18, 1.33], [0, 1.22, 1.27], [0, -0.21, -1.19], [np.nan, 0.87, 1.31], [np.nan, -0.67, -0.22]]) Fill Missing Values’ Class With Most Frequent Class imputer = Imputer(strategy=’most_frequent’, axis=0) imputer.fit_transform(X) array([[ 0. , …
Saving Machine Learning Models In scikit there are two main ways to save a model for future use: a pickle string and a pickled model as a file. Preliminaries from sklearn.linear_model import LogisticRegression from sklearn import datasets import pickle from sklearn.externals import joblib Load Data # Load the iris data iris = datasets.load_iris() # Create …
Loading Features From Dictionaries from sklearn import datasets import numpy as np iris = datasets.load_iris() X = iris.data[:, [2, 3]] X array([[1.4, 0.2], [1.4, 0.2], [1.3, 0.2], [1.5, 0.2], [1.4, 0.2], [1.7, 0.4], [1.4, 0.3], [1.5, 0.2], [1.4, 0.2], [1.5, 0.1], [1.5, 0.2], [1.6, 0.2], [1.4, 0.1], [1.1, 0.1], [1.2, 0.2], [1.5, 0.4], [1.3, 0.4], …
Python Dictionary values() Method Returns a list of values from a dictionary Usage The values() method returns a list of values from a dictionary. Syntax dictionary.values() Examples # Print all values from the dictionary D = {‘name’: ‘Bob’, ‘age’: 25} L = D.values() print(L) # Prints dict_values([25, ‘Bob’]) values() method is generally used to iterate through all the values …
Python Dictionary update() Method Updates/Adds multiple items to the dictionary Usage The update() method updates the dictionary with the key:value pairs from element. If the key is already present in the dictionary, value gets updated. If the key is not present in the dictionary, a new key:value pair is added to the dictionary. element can be either another dictionary object or …
Python Dictionary setdefault() Method Returns the value for key if exists, else inserts it Usage The setdefault() method returns the value for key if key is in the dictionary. If not, it inserts key with a value of default and returns default. Syntax dictionary.setdefault(key,default) Parameter Condition Description key Required Any key you want to return value for default Optional A value to insert if …