How to do it...

Let's see how to tackle class imbalance:

  1. Let's import the libraries:
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.svm import SVCimport utilities
  1. Let's load the data (data_multivar_imbalance.txt):
input_file = 'data_multivar_imbalance.txt' 
X, y = utilities.load_data(input_file) 
  1. Let's visualize the data. The code for visualization is exactly the same as it was in the previous recipe. You can also find it in the file named svm_imbalance.py, already provided to you:
# Separate the data into classes based on 'y'class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])# Plot the input dataplt.figure()plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', ...

Get Python Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.