Tag Archives: python for business analysts

Machine Learning for Beginners in Python: How to do Cross Validation Pipeline

Cross Validation Pipeline The code below does a lot in only a few lines. To help explain things, here are the steps that code is doing: Split the raw data into three folds. Select one for testing and two for training. Preprocess the data by scaling the training features. Train a support vector classifier on …

Machine Learning for Beginners in Python: How to Convert A Dictionary Into A Matrix

Converting A Dictionary Into A Matrix Preliminaries from sklearn.feature_extraction import DictVectorizer Create Dictionary data_dict = [{‘Red’: 2, ‘Blue’: 4}, {‘Red’: 4, ‘Blue’: 3}, {‘Red’: 1, ‘Yellow’: 2}, {‘Red’: 2, ‘Yellow’: 2}] Feature Matrix From Dictionary dictvectorizer = DictVectorizer(sparse=False) features = dictvectorizer.fit_transform(data_dict) features array([[ 4., 2., 0.], [ 3., 4., 0.], [ 0., 1., 2.], [ …

Machine Learning for Beginners in Python: How to Create A Sparse Matrix

Create A Sparse Matrix Preliminaries import numpy as np from scipy import sparse Create Dense Matrix matrix = np.array([[0, 0], [0, 1], [3, 0]]) Convert To Sparse Matrix matrix_sparse = sparse.csr_matrix(matrix) Note: There are many types of sparse matrices. In the example above we use CSR but the type we use should reflect our use …

Machine Learning for Beginners in Python: How to Get The Diagonal Of A Matrix

Getting The Diagonal Of A Matrix Preliminaries import numpy as np Create Matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Get The Diagonal matrix.diagonal() array([1, 5, 9]) Calculate The Trace matrix.diagonal().sum() 15   Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning & Data Science Recipes Portfolio Projects for …

Machine Learning for Beginners in Python: How to Select Elements in An Array

Selecting Elements In An Array Preliminaries # Load library import numpy as np Create Vector # Create row vector vector = np.array([1, 2, 3, 4, 5, 6]) Select Element # Select second element vector[1] 2 Create Matrix # Create matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Select Element # Select …

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

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 Set symmetric_difference() Method

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 …

Python Built-in Methods – Python abs() Function

Python Set difference() Method Returns a new set with items in the set that are not in other sets Usage The difference() method returns a new set of items that are in the original set but not in any of the specified sets. You can specify as many sets as you want, just separate each set with a comma. If you …