Skip to content

Instantly share code, notes, and snippets.

@IevaZarina
Last active September 13, 2016 11:46
Show Gist options
  • Save IevaZarina/c6241b170b75029098b55c7825b35793 to your computer and use it in GitHub Desktop.
Save IevaZarina/c6241b170b75029098b55c7825b35793 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.decomposition import TruncatedSVD
# Enable saving plot to png file without UI
plt.switch_backend('agg')
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Visualize result using SVD
svd = TruncatedSVD(n_components=2)
X_reduced = svd.fit_transform(X)
# Get min and max sizes for coordinate axis
x_min, x_max = X_reduced[:, 0].min() - .5, X_reduced[:, 0].max() + .5
y_min, y_max = X_reduced[:, 1].min() - .5, X_reduced[:, 1].max() + .5
# Initialize scatter plot with x and y axis values
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, s=25)
plt.axis([float(x_min), float(x_max), float(y_min), float(y_max)])
plt.savefig("iris_plot.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment