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 …
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 # …
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 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 Dictionary get() Method Returns the value for key if exists Usage The get() method returns the value for key if key is in the dictionary. You can also specify the default parameter that will be returned if the specified key is not found. If default is not specified, it returns None. Therefore, this method never raises a KeyError. It’s an easy way of getting the …
Python Dictionary copy() Method Copies the dictionary shallowly Usage The copy() method returns the Shallow copy of the specified dictionary. Syntax dictionary.copy() Example D = {‘name’: ‘Bob’, ‘age’: 25} X = D.copy() print(X) # Prints {‘age’: 25, ‘name’: ‘Bob’} copy() vs Assignment statement Assignment statement does not copy objects. For example, old_Dict = {‘name’: ‘Bob’, ‘age’: 25} new_Dict = old_Dict …
Python Set update() Method Updates the set by adding items from all the specified sets Usage The update() method updates the original set by adding items from all the specified sets, with no duplicates. You can specify as many sets as you want, just separate each set with a comma. If you don’t want to update the original set, use union() method. Syntax …
Python Tuple count() Method Python Set symmetric_difference_update() Method Updates the set by keeping only elements found in either set, but not in both Usage The symmetric_difference_update() method updates the set by keeping only elements found in either set, but not in both. If you don’t want to update the original set, use symmetric_difference() method. The symmetric difference is actually the union of the …
Python Set symmetric_difference() Method Returns a new set with items from all the sets, except common items Usage The symmetric_difference() method returns a new set containing all items from both the sets, except common items. If you want to modify the original set instead of returning a new one, use symmetric_difference_update() method. The symmetric difference is actually the union of the two sets, minus …