Let's see how to tackle class imbalance:
- Let's import the libraries:
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.svm import SVCimport utilities
- Let's load the data (data_multivar_imbalance.txt):
input_file = 'data_multivar_imbalance.txt' X, y = utilities.load_data(input_file)
- 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', ...