Beginner’s Guide to Python

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: Make Simulated Data For Clustering

Make Simulated Data For Clustering Preliminaries from sklearn.datasets import make_blobs import matplotlib.pyplot as plt Make Data /* Make the features (X) and output (y) with 200 samples, */ X, y = make_blobs(n_samples = 200, n_features = 2, centers = 3, cluster_std = 0.5, shuffle = True) View Data /* Create a scatterplot of the first …

Machine Learning for Beginners in Python: Loading scikit-learn’s Iris Dataset

Loading scikit-learn’s Iris Dataset Preliminaries # Load libraries from sklearn import datasets import matplotlib.pyplot as plt Load Iris Dataset The Iris flower dataset is one of the most famous databases for classification. It contains three classes (i.e. three species of flowers) with 50 observations per class. # Load digits dataset iris = datasets.load_iris() # Create feature matrix …

Machine Learning for Beginners in Python: Loading scikit-learn’s Digits Dataset

Loading scikit-learn’s Digits Dataset Preliminaries # Load libraries from sklearn import datasets import matplotlib.pyplot as plt Load Digits Dataset Digits is a dataset of handwritten digits. Each feature is the intensity of one pixel of an 8 x 8 image. # Load digits dataset digits = datasets.load_digits() # Create feature matrix X = digits.data # …

Machine Learning for Beginners in Python: Loading scikit-learn’s Boston Housing Dataset

Loading scikit-learn’s Boston Housing Dataset Preliminaries # Load libraries from sklearn import datasets import matplotlib.pyplot as plt Load Boston Housing Dataset The Boston housing dataset is a famous dataset from the 1970s. It contains 506 observations on housing prices around Boston. It is often used in regression examples and contains 15 features. # Load digits dataset boston …

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 …

Python Built-in Methods – Python Dictionary popitem() Method

Python Dictionary popitem() Method Removes a key-value pair from a dictionary Usage The popitem() method removes and returns the last inserted key:value pair from the dictionary. Pairs are returned in Last In First Out (LIFO) order. In versions before 3.7, popitem() would remove and return a random item. Syntax dictionary.popitem() Examples # Remove the last inserted item from the dictionary D = {‘name’: ‘Bob’, …