Tag Archives: python

learn Python By Example – How to Find All Combinations For A List Of Objects

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 …

Machine Learning for Beginners in Python: How to Find Term Frequency Inverse Document Frequency

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. …

Machine Learning for Beginners in Python: How to Impute Missing Class Labels

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. , …

Machine Learning for Beginners in Python: How to save Machine Learning Models

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 …

Machine Learning for Beginners in Python: Loading Features From Dictionaries

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 Built-in Methods – Python Dictionary values() Method

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 Built-in Methods – Python Dictionary update() Method

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 Built-in Methods – Python Dictionary setdefault() Method

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 …