ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
k_means.py
(572B)
1 from __future__ import division, print_function
2 from sklearn import datasets
3 import numpy as np
4
5 from mlfromscratch.unsupervised_learning import KMeans
6 from mlfromscratch.utils import Plot
7
8
9 def main():
10 # Load the dataset
11 X, y = datasets.make_blobs()
12
13 # Cluster the data using K-Means
14 clf = KMeans(k=3)
15 y_pred = clf.predict(X)
16
17 # Project the data onto the 2 primary principal components
18 p = Plot()
19 p.plot_in_2d(X, y_pred, title="K-Means Clustering")
20 p.plot_in_2d(X, y, title="Actual Clustering")
21
22
23
24 if __name__ == "__main__":
25 main()