OPIIF_Clustering


In [1]:
from IPython.display import HTML

input_form = """
<a id="admin_link" target="_blank" href="#">Ajenti Administration Interface</a>
<p>User: root<br> Password: admin</p>
"""

javascript = """
<script type="text/Javascript">
document.getElementById('admin_link').href = "https://" + window.location.hostname + ":8000"
</script>
"""

HTML(input_form + javascript)


Out[1]:
Ajenti Administration Interface

User: root
Password: admin

Importar Librerias


In [28]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from matplotlib.finance import candlestick, quotes_historical_yahoo, date2num
from sklearn.cluster import KMeans
from sklearn.preprocessing import normalize

from datetime import datetime, timedelta

pd.options.display.max_columns=50

Funciones a utilizar

Descargar precios Yahoo Finance


In [51]:
def download_data(symbol, days_delta=60):
    finish_date = datetime.today()
    start_date = finish_date - timedelta(days=days_delta)

    stocks_raw = quotes_historical_yahoo(symbol, start_date, finish_date)
    stocks_df = pd.DataFrame(stocks_raw, columns=["n_date", "open", "close", "high", "low", "volume"])
    return stocks_df

Configuracion de fecha


In [52]:
def process_date(stocks_df):
    stocks_df["n_date"] = stocks_df["n_date"].astype(np.int32)
    stocks_df["date"] = stocks_df["n_date"].apply(datetime.fromordinal)
    return stocks_df

Calculo de estadisticas


In [53]:
def calculate_stats(stocks_df):
    stocks_df["average"] = (stocks_df["close"] + stocks_df["high"] + stocks_df["low"]) / 3.0
    stocks_df["change_amount"] = stocks_df["close"] - stocks_df["open"]
    stocks_df["change_per"] = stocks_df["change_amount"] / stocks_df["average"]
    stocks_df["range"] = (stocks_df["high"] - stocks_df["low"]) / stocks_df["average"]
    stocks_df["change_1_amount"] = pd.Series(0.0)
    stocks_df["change_1_amount"][1:] = stocks_df["average"][1:].values - stocks_df["average"][:-1].values
    stocks_df["change_1_per"] = stocks_df["change_1_amount"] / stocks_df["average"]
    return stocks_df

Ajuste de datos para Clustering


In [54]:
def pivot_data(stocks_df, values="change_1_per"):
    clustering_data = stocks_df.pivot(index="Ticker", columns="n_date", values=values)
    return clustering_data

Clustering


In [55]:
def cluster_data(data, n_clusters=8, normalize_data=False):
    if normalize_data:
        data = normalize(data.values, norm='l2', axis=1, copy=True)
    cluster_model = KMeans(n_clusters=n_clusters)
    prediction = cluster_model.fit_predict(data)
    return prediction, cluster_model, data

Visualizar Clusters


In [56]:
def visualize_clusters(data_df, values="change_1_per", n_clusters=8, normalize_data=False):
    data = pivot_data(data_df, values)
    prediction, model, c_data = cluster_data(data, n_clusters=n_clusters, normalize_data=normalize_data)
    c_data = pd.DataFrame(c_data, index=data.index,columns=data.columns)
    data["Cluster"] = prediction
    c_data["Cluster"] = prediction
    plt.figure
    for cluster in np.unique(prediction):
        plt.plot(model.cluster_centers_[cluster], "o-", alpha=0.5, linewidth=2)
    plt.show()
    for cluster in np.unique(prediction):
        temp_cluster_data = c_data[c_data["Cluster"]==cluster]
        print "Cluster: %s" % cluster
        print "Members: %s" % ["%s: %s"% (symbol, stock_dict[symbol]) for symbol in list(temp_cluster_data.index)]
        plt.figure()
        plt.title("Cluster#: %s" % cluster)
        plt.plot(model.cluster_centers_[cluster], "o--", alpha=0.5, linewidth=2)
        for symbol in temp_cluster_data.index:
            plt.plot(np.ravel(temp_cluster_data.loc[[symbol]].drop("Cluster", 1).values),
                     alpha=0.2, linewidth=2)
            
        plt.grid()
        plt.show();
    return prediction, model, c_data

Medicion de desempeno Cluster


In [57]:
def measure_error(prediction, model, c_data):
    error_score = []
    for counter in range(len(c_data)):
        true_val = c_data.drop("Cluster",1).values[counter]
        center_val = model.cluster_centers_[c_data["Cluster"][counter]]

        error_score.append(np.average(np.abs(true_val - center_val)) / np.average(center_val))
    
    cluster_counts = c_data["Cluster"].value_counts()
    
    return np.average(error_score), len(cluster_counts[cluster_counts==1])

In [58]:
stock_dict={"ALFAA.MX": "ALFA.A",
            "ALPEKA.MX": "ALPEK.A",
            "ALSEA.MX": "ALSEA",
            "AMXL.MX": "AMX.L",
            "ASURB.MX": "ASUR.B",
            "BIMBOA.MX": "BIMBO.A",
            "BOLSAA.MX": "BOLSA.A",
            "CEMEXCPO.MX": "CEMEX.CPO",
            "COMERCIUBC.MX": "COMERCI.UBC",
            "ELEKTRA.MX": "ELEKTRA",
            "GAPB.MX": "GAP.B",
            "GENTERA.MX": "GENTERA",
            "GFINBURO.MX": "GFINBUR.O",
            "GFNORTEO.MX": "GFNORTE.O",
            "GFREGIOO.MX": "GFREGIO.O",
            "GMEXICOB.MX": "GMEXICO.B",
            "GRUMAB.MX": "GRUMA.B",
            "GSANBORB-1.MX": "GSANBOR.B-1",
            "ICA.MX": "ICA",
            "ICHB.MX": "ICH.B",
            "IENOVA.MX": "IENOVA",
            "KIMBERA.MX": "KIMBER.A",
            "KOFL.MX": "KOFL",
            "LABB.MX": "LAB.B",
            "LALAB.MX": "LALA.B",
            "LIVEPOLC-1.MX": "LIVEPOL.C-1",
            "MEXCHEM.MX": "MEXCHEM",
            "OHLMEX.MX": "OHLMEX",
            "PINFRA.MX": "PINFRA",
            "SANMEXB.MX": "SANMEX.B",
            "TLEVISACPO.MX": "TLEVISA.CPO",
            "WALMEX.MX": "WALMEX",
           }
symbols = stock_dict.keys()
names = stock_dict.values()

stocks_data = pd.DataFrame(symbols, columns=["Ticker"])
stocks_data["NAIC"] = names
stocks_data


Out[58]:
Ticker NAIC
0 ICHB.MX ICH.B
1 GMEXICOB.MX GMEXICO.B
2 MEXCHEM.MX MEXCHEM
3 ELEKTRA.MX ELEKTRA
4 GFINBURO.MX GFINBUR.O
5 IENOVA.MX IENOVA
6 PINFRA.MX PINFRA
7 ALPEKA.MX ALPEK.A
8 WALMEX.MX WALMEX
9 ALFAA.MX ALFA.A
10 LABB.MX LAB.B
11 GFNORTEO.MX GFNORTE.O
12 OHLMEX.MX OHLMEX
13 AMXL.MX AMX.L
14 ALSEA.MX ALSEA
15 TLEVISACPO.MX TLEVISA.CPO
16 GENTERA.MX GENTERA
17 BOLSAA.MX BOLSA.A
18 SANMEXB.MX SANMEX.B
19 LIVEPOLC-1.MX LIVEPOL.C-1
20 GFREGIOO.MX GFREGIO.O
21 COMERCIUBC.MX COMERCI.UBC
22 ICA.MX ICA
23 GSANBORB-1.MX GSANBOR.B-1
24 KIMBERA.MX KIMBER.A
25 KOFL.MX KOFL
26 GAPB.MX GAP.B
27 BIMBOA.MX BIMBO.A
28 LALAB.MX LALA.B
29 GRUMAB.MX GRUMA.B
30 CEMEXCPO.MX CEMEX.CPO
31 ASURB.MX ASUR.B

32 rows × 2 columns


In [59]:
temp_list = []
for symbol in stocks_data["Ticker"]:
    temp_data = download_data(symbol)
    process_date(temp_data)
    calculate_stats(temp_data)
    temp_data["Ticker"] = symbol
    temp_list.append(temp_data)

stocks_df = pd.concat(temp_list)
stocks_df


Out[59]:
n_date open close high low volume date average change_amount change_per range change_1_amount change_1_per Ticker
0 735955 54.900000 54.54000 55.350000 54.260000 354900 2015-12-23 54.716667 -0.360000 -0.006579 0.019921 0.000000 0.000000 ICHB.MX
1 735956 55.000000 55.05000 55.540000 54.880000 61800 2015-12-24 55.156667 0.050000 0.000907 0.011966 0.440000 0.007977 ICHB.MX
2 735957 55.050000 55.05000 55.050000 55.050000 0 2015-12-25 55.050000 0.000000 0.000000 0.000000 -0.106667 -0.001938 ICHB.MX
3 735960 55.540000 54.92000 55.590000 54.670000 57400 2015-12-28 55.060000 -0.620000 -0.011260 0.016709 0.010000 0.000182 ICHB.MX
4 735961 54.910000 56.09000 56.670000 54.910000 147400 2015-12-29 55.890000 1.180000 0.021113 0.031490 0.830000 0.014851 ICHB.MX
5 735962 56.000000 55.40000 57.800000 55.090000 251900 2015-12-30 56.096667 -0.600000 -0.010696 0.048309 0.206667 0.003684 ICHB.MX
6 735963 55.590000 55.67000 56.410000 54.900000 86700 2015-12-31 55.660000 0.080000 0.001437 0.027129 -0.436667 -0.007845 ICHB.MX
7 735964 55.670000 55.67000 55.670000 55.670000 0 2016-01-01 55.670000 0.000000 0.000000 0.000000 0.010000 0.000180 ICHB.MX
8 735967 55.670000 54.93000 55.670000 54.690000 170800 2016-01-04 55.096667 -0.740000 -0.013431 0.017787 -0.573333 -0.010406 ICHB.MX
9 735968 55.300000 54.70000 55.450000 54.270000 734000 2016-01-05 54.806667 -0.600000 -0.010948 0.021530 -0.290000 -0.005291 ICHB.MX
10 735969 54.500000 53.97000 55.000000 53.600000 1124900 2016-01-06 54.190000 -0.530000 -0.009780 0.025835 -0.616667 -0.011380 ICHB.MX
11 735970 54.000000 52.58000 54.000000 51.800000 1041200 2016-01-07 52.793333 -1.420000 -0.026897 0.041672 -1.396667 -0.026455 ICHB.MX
12 735971 52.900000 52.57000 54.000000 52.070000 905500 2016-01-08 52.880000 -0.330000 -0.006241 0.036498 0.086667 0.001639 ICHB.MX
13 735974 52.950000 52.71000 53.000000 51.910000 778800 2016-01-11 52.540000 -0.240000 -0.004568 0.020746 -0.340000 -0.006471 ICHB.MX
14 735975 53.100000 52.83000 53.100000 51.500000 5042700 2016-01-12 52.476667 -0.270000 -0.005145 0.030490 -0.063333 -0.001207 ICHB.MX
15 735976 53.000000 52.70000 54.240000 52.610000 856500 2016-01-13 53.183333 -0.300000 -0.005641 0.030649 0.706667 0.013287 ICHB.MX
16 735977 52.700000 52.94000 53.000000 52.010000 757000 2016-01-14 52.650000 0.240000 0.004558 0.018803 -0.533333 -0.010130 ICHB.MX
17 735978 52.400000 54.90000 56.340000 52.090000 791200 2016-01-15 54.443333 2.500000 0.045919 0.078063 1.793333 0.032939 ICHB.MX
18 735981 54.900000 54.86000 55.000000 52.220000 303800 2016-01-18 54.026667 -0.040000 -0.000740 0.051456 -0.416667 -0.007712 ICHB.MX
19 735982 55.150000 54.72000 55.150000 52.200000 1245900 2016-01-19 54.023333 -0.430000 -0.007960 0.054606 -0.003333 -0.000062 ICHB.MX
20 735983 54.740000 53.87000 54.740000 52.610000 842300 2016-01-20 53.740000 -0.870000 -0.016189 0.039635 -0.283333 -0.005272 ICHB.MX
21 735984 54.000000 54.49000 55.010000 53.590000 187000 2016-01-21 54.363333 0.490000 0.009013 0.026121 0.623333 0.011466 ICHB.MX
22 735985 54.800000 55.04000 55.290000 54.500000 164500 2016-01-22 54.943333 0.240000 0.004368 0.014378 0.580000 0.010556 ICHB.MX
23 735988 54.800000 54.87000 55.600000 53.590000 140100 2016-01-25 54.686667 0.070000 0.001280 0.036755 -0.256667 -0.004693 ICHB.MX
24 735989 54.800000 54.09000 55.130000 53.410000 124000 2016-01-26 54.210000 -0.710000 -0.013097 0.031728 -0.476667 -0.008793 ICHB.MX
25 735990 54.000000 52.32000 54.070000 52.010000 413200 2016-01-27 52.800000 -1.680000 -0.031818 0.039015 -1.410000 -0.026705 ICHB.MX
26 735991 53.000000 52.52000 53.670000 52.000000 260600 2016-01-28 52.730000 -0.480000 -0.009103 0.031671 -0.070000 -0.001328 ICHB.MX
27 735992 52.520000 53.07000 53.510000 52.520000 167100 2016-01-29 53.033333 0.550000 0.010371 0.018668 0.303333 0.005720 ICHB.MX
28 735995 53.070000 53.07000 53.070000 53.070000 0 2016-02-01 53.070000 0.000000 0.000000 0.000000 0.036667 0.000691 ICHB.MX
29 735996 53.250000 52.93000 53.500000 51.000000 792500 2016-02-02 52.476667 -0.320000 -0.006098 0.047640 -0.593333 -0.011307 ICHB.MX
30 735997 53.200000 53.16000 53.440000 52.810000 809400 2016-02-03 53.136667 -0.040000 -0.000753 0.011856 0.660000 0.012421 ICHB.MX
31 735998 53.350000 52.38000 54.420000 52.200000 733100 2016-02-04 53.000000 -0.970000 -0.018302 0.041887 -0.136667 -0.002579 ICHB.MX
32 735999 52.350000 52.53000 53.220000 51.950000 1034400 2016-02-05 52.566667 0.180000 0.003424 0.024160 -0.433333 -0.008244 ICHB.MX
33 736002 52.500000 53.79000 55.000000 51.900000 943600 2016-02-08 53.563333 1.290000 0.024084 0.057875 0.996667 0.018607 ICHB.MX
34 736003 53.350000 53.84000 54.270000 51.920000 904700 2016-02-09 53.343333 0.490000 0.009186 0.044054 -0.220000 -0.004124 ICHB.MX
35 736004 53.820000 52.33000 53.820000 51.950000 1012600 2016-02-10 52.700000 -1.490000 -0.028273 0.035484 -0.643333 -0.012207 ICHB.MX
36 736005 52.700000 52.36000 52.700000 51.350000 944600 2016-02-11 52.136667 -0.340000 -0.006521 0.025893 -0.563333 -0.010805 ICHB.MX
37 736006 52.340000 54.35000 54.500000 52.310000 921600 2016-02-12 53.720000 2.010000 0.037416 0.040767 1.583333 0.029474 ICHB.MX
38 736009 54.500000 56.07000 56.500000 54.450000 88100 2016-02-15 55.673333 1.570000 0.028200 0.036822 1.953333 0.035086 ICHB.MX
39 736010 56.050000 55.99000 56.580000 54.800000 942600 2016-02-16 55.790000 -0.060000 -0.001075 0.031905 0.116667 0.002091 ICHB.MX
40 736011 56.320000 55.78000 57.220000 54.640000 953300 2016-02-17 55.880000 -0.540000 -0.009664 0.046170 0.090000 0.001611 ICHB.MX
41 736012 55.740000 56.79000 57.400000 54.880000 949700 2016-02-18 56.356667 1.050000 0.018631 0.044715 0.476667 0.008458 ICHB.MX
42 736013 56.000000 57.09000 57.310000 55.700000 944600 2016-02-19 56.700000 1.090000 0.019224 0.028395 0.343333 0.006055 ICHB.MX
0 735955 36.861358 38.19742 39.064863 36.791564 8138400 2015-12-23 38.017949 1.336062 0.035143 0.059795 0.000000 0.000000 GMEXICOB.MX
1 735956 38.396836 37.81854 38.406806 37.668981 3779700 2015-12-24 37.964776 -0.578296 -0.015232 0.019434 -0.053173 -0.001401 GMEXICOB.MX
2 735957 37.818540 37.81854 37.818540 37.818540 0 2015-12-25 37.818540 0.000000 0.000000 0.000000 -0.146236 -0.003867 GMEXICOB.MX
3 735960 37.818544 37.27016 37.938191 37.180424 3007500 2015-12-28 37.462925 -0.548384 -0.014638 0.020227 -0.355615 -0.009492 GMEXICOB.MX
4 735961 37.389803 37.33995 37.938187 37.120596 3874400 2015-12-29 37.466244 -0.049853 -0.001331 0.021822 0.003319 0.000089 GMEXICOB.MX
5 735962 37.678956 36.94113 37.688926 36.861365 3470300 2015-12-30 37.163807 -0.737826 -0.019853 0.022268 -0.302437 -0.008138 GMEXICOB.MX
6 735963 36.741714 36.68189 37.280127 35.545240 3474700 2015-12-31 36.502419 -0.059824 -0.001639 0.047528 -0.661388 -0.018119 GMEXICOB.MX
7 735964 36.681890 36.68189 36.681890 36.681890 0 2016-01-01 36.681890 0.000000 0.000000 0.000000 0.179471 0.004893 GMEXICOB.MX
8 735967 36.013855 35.60506 36.661945 35.276030 4722000 2016-01-04 35.847678 -0.408795 -0.011404 0.038661 -0.834212 -0.023271 GMEXICOB.MX
9 735968 35.894212 36.34289 36.562244 35.305946 10231900 2016-01-05 36.070360 0.448678 0.012439 0.034829 0.222682 0.006174 GMEXICOB.MX
10 735969 35.744647 35.46547 35.844353 35.305940 8703800 2016-01-06 35.538588 -0.279177 -0.007856 0.015150 -0.531772 -0.014963 GMEXICOB.MX
11 735970 35.046709 34.90712 35.355798 34.408589 9761500 2016-01-07 34.890502 -0.139589 -0.004001 0.027148 -0.648086 -0.018575 GMEXICOB.MX
12 735971 34.907122 34.05962 35.505359 33.561089 12996200 2016-01-08 34.375356 -0.847502 -0.024654 0.056560 -0.515146 -0.014986 GMEXICOB.MX
13 735974 33.810355 34.05962 34.528239 33.401559 11500300 2016-01-11 33.996473 0.249265 0.007332 0.033141 -0.378883 -0.011145 GMEXICOB.MX
14 735975 34.069593 34.98689 35.405656 33.172238 10387600 2016-01-12 34.521595 0.917297 0.026572 0.064696 0.525122 0.015211 GMEXICOB.MX
15 735976 35.096568 35.13645 35.405657 34.298918 8816800 2016-01-13 34.947008 0.039882 0.001141 0.031669 0.425414 0.012173 GMEXICOB.MX
16 735977 35.276039 35.13645 35.375745 34.418566 5406000 2016-01-14 34.976920 -0.139589 -0.003991 0.027366 0.029912 0.000855 GMEXICOB.MX
... ... ... ... ... ... ... ... ... ... ... ... ... ...

1376 rows × 14 columns


In [60]:
clustering_data = pivot_data(stocks_df, values="change_amount")
clustering_data


Out[60]:
n_date 735955 735956 735957 735960 735961 735962 735963 735964 735967 735968 735969 735970 735971 735974 735975 735976 735977 735978 735981 735982 735983 735984 735985 735988 735989 735990 735991 735992 735995 735996 735997 735998 735999 736002 736003 736004 736005 736006 736009 736010 736011 736012 736013
Ticker
ALFAA.MX 0.390000 -0.270000 0 -0.050000 -0.320000 -0.420000 -0.020000 0 -0.250000 0.090000 -0.340000 -1.970000 -0.820000 0.520000 -0.480000 -0.620000 -0.410000 0.480000 -0.120000 0.470000 0.510000 0.100000 -0.050000 0.400000 0.120000 -0.050000 0.270000 1.420000 0 -1.620000 -0.030000 0.100000 -0.470000 -0.160000 1.260000 0.210000 -0.360000 -0.750000 0.420000 -0.260000 0.290000 -0.380000 -0.910000
ALPEKA.MX -0.350000 -0.020000 0 -0.110000 0.400000 -0.060000 -0.020000 0 0.070000 -0.480000 -1.120000 -0.230000 0.300000 -0.520000 -0.780000 0.480000 -0.140000 0.000000 -0.120000 0.170000 -0.810000 0.850000 0.510000 -0.350000 -0.340000 -0.320000 0.160000 0.470000 0 -0.220000 0.760000 0.120000 0.090000 -0.220000 0.620000 -0.040000 -0.070000 0.400000 -0.120000 -0.200000 0.070000 0.810000 0.220000
ALSEA.MX -0.190000 -0.190000 0 1.040000 0.120000 -0.700000 1.250000 0 0.430000 0.190000 -0.010000 -2.260000 -2.060000 1.080000 1.230000 0.010000 -0.480000 0.750000 0.580000 -0.450000 -0.010000 2.530000 0.650000 0.360000 0.110000 -1.120000 1.080000 1.700000 0 -1.240000 -0.270000 -0.920000 1.910000 2.040000 0.850000 0.310000 -0.540000 -3.010000 -0.060000 -0.750000 0.300000 -0.380000 -0.890000
AMXL.MX -0.030000 -0.110000 0 -0.180000 0.280000 0.050000 -0.220000 0 0.050000 -0.020000 -0.210000 -0.360000 -0.490000 0.190000 0.000000 0.150000 0.240000 -0.210000 0.040000 0.010000 0.290000 0.270000 0.270000 -0.220000 0.430000 0.280000 0.220000 0.230000 0 0.000000 0.160000 0.140000 -0.190000 -0.030000 -0.350000 -0.850000 0.070000 0.330000 0.340000 -0.210000 -0.020000 -0.210000 -0.270000
ASURB.MX 1.610000 -0.160000 0 -3.290000 -1.100000 -3.220000 -0.110000 0 -2.810000 -5.460000 -1.170000 -0.280000 -1.610000 -1.770000 1.850000 -5.290000 -1.470000 -0.680000 2.350000 0.040000 -0.070000 1.990000 1.540000 4.250000 1.820000 -5.050000 1.560000 6.720000 0 -3.440000 -1.210000 1.930000 -0.050000 -1.130000 -0.770000 3.300000 2.460000 -1.180000 -0.240000 2.240000 5.070000 -2.740000 -4.280000
BIMBOA.MX 0.500000 -0.490000 0 0.060000 -0.050000 -1.240000 0.540000 0 -0.030000 0.780000 0.860000 0.660000 0.160000 0.760000 0.090000 0.720000 -0.160000 -0.320000 -0.460000 -0.240000 0.640000 -1.830000 0.170000 -0.390000 0.940000 0.110000 1.030000 1.760000 0 1.410000 0.000000 0.190000 -1.340000 0.910000 0.940000 0.550000 -1.200000 0.010000 0.780000 -0.020000 -0.330000 0.730000 1.230000
BOLSAA.MX 0.000000 0.120000 0 -0.090000 -0.070000 0.010000 -0.190000 0 -0.330000 -0.020000 -0.050000 -0.460000 -0.380000 -0.100000 -0.210000 0.100000 0.670000 -0.190000 0.170000 0.120000 0.320000 -0.130000 -0.180000 0.410000 0.030000 0.230000 0.550000 0.700000 0 0.860000 -0.690000 -0.040000 -0.750000 -0.230000 -0.150000 -0.400000 -0.430000 0.640000 0.090000 0.580000 0.330000 0.660000 0.330000
CEMEXCPO.MX 0.570000 0.070000 0 -0.310000 -0.150000 -0.090000 0.230000 0 -0.170000 -0.120000 0.080000 -0.610000 -0.680000 0.040000 0.080000 -0.310000 0.180000 0.130000 -0.070000 -0.470000 0.050000 0.550000 0.000000 -0.580000 0.130000 0.000000 0.000000 0.340000 0 0.040000 0.370000 0.620000 -0.200000 -0.550000 0.010000 -0.160000 0.100000 0.170000 0.170000 -0.100000 0.680000 -0.400000 0.070000
COMERCIUBC.MX -0.520000 0.450000 0 -1.050000 -0.350000 -2.230000 0.010000 0 -0.690000 0.060000 0.000000 0.760000 0.170000 -0.310000 -0.050000 -0.340000 -0.100000 0.430000 -0.160000 0.020000 0.370000 -0.180000 0.750000 0.500000 -0.260000 -0.510000 0.210000 0.040000 0 -0.340000 -0.110000 0.030000 -0.500000 -0.020000 1.070000 0.050000 0.760000 -0.040000 0.060000 0.000000 0.570000 0.050000 -0.030000
ELEKTRA.MX -1.410000 -2.750000 0 -9.310000 -13.880000 -8.810000 3.040000 0 -8.570000 -12.690000 -14.620000 -2.900000 -4.550000 17.030000 -1.030000 -14.620000 -0.760000 20.180000 -2.930000 -17.860000 -9.130000 4.490000 4.070000 4.540000 7.540000 -0.950000 4.020000 1.120000 0 -0.250000 -2.050000 -4.670000 0.250000 -1.080000 8.140000 5.660000 -4.720000 -1.090000 5.450000 -5.390000 7.700000 -12.000000 -3.310000
GAPB.MX -0.700000 -9.200000 0 -1.530000 -0.470000 -1.070000 -1.000000 0 -3.230000 -0.660000 -1.270000 -4.680000 2.240000 3.640000 -5.840000 2.300000 -1.200000 -0.340000 0.000000 -0.950000 1.120000 7.850000 -1.770000 2.620000 2.240000 4.440000 -3.390000 0.840000 0 -4.300000 1.650000 1.740000 -0.830000 -1.400000 -0.390000 -0.500000 -0.420000 -0.470000 -0.690000 0.770000 -1.060000 0.950000 -2.410000
GENTERA.MX 0.080000 -0.440000 0 -0.270000 0.040000 0.080000 0.190000 0 -1.200000 0.370000 -0.110000 -1.300000 -0.460000 -0.590000 -0.630000 0.260000 0.920000 -0.860000 -0.400000 0.030000 1.120000 0.000000 0.170000 -1.180000 -0.220000 -0.110000 0.500000 1.630000 0 0.570000 0.090000 0.950000 0.220000 0.800000 -0.440000 0.560000 -0.090000 -0.200000 0.250000 -0.160000 1.650000 1.020000 -1.210000
GFINBURO.MX -0.170000 -0.060000 0 0.270000 -0.490000 -0.040000 0.310000 0 -0.650000 -0.530000 -0.320000 -0.580000 0.050000 0.920000 -0.390000 -0.230000 -0.180000 -0.510000 0.670000 -0.890000 0.410000 3.740000 -0.010000 -0.430000 0.120000 0.220000 0.160000 1.410000 0 0.120000 0.040000 1.030000 -0.910000 0.510000 -0.330000 0.530000 -0.100000 0.270000 0.330000 0.980000 0.780000 0.900000 -0.060000
GFNORTEO.MX 0.000000 -0.620000 0 0.890000 0.450000 -0.280000 1.380000 0 -1.920000 -0.090000 0.280000 -1.710000 -2.480000 0.700000 0.030000 -1.080000 -0.460000 -1.580000 0.170000 -0.240000 -0.670000 -2.170000 0.880000 1.820000 -0.120000 1.460000 0.150000 5.230000 0 -3.950000 1.050000 -1.010000 -0.900000 -1.020000 -1.050000 1.500000 -1.230000 -1.960000 1.910000 -1.070000 5.070000 -0.130000 0.020000
GFREGIOO.MX 0.110000 0.270000 0 -0.930000 0.920000 0.170000 -0.660000 0 2.110000 -1.160000 -1.180000 -1.070000 -0.210000 -0.150000 0.000000 -0.510000 -0.480000 -0.270000 1.110000 0.500000 -0.960000 -0.590000 -0.950000 0.480000 -1.260000 0.760000 -0.490000 2.960000 0 0.610000 -0.210000 -1.130000 -1.540000 0.560000 -0.110000 1.010000 -0.340000 0.150000 0.120000 1.150000 4.070000 -0.210000 1.820000
GMEXICOB.MX 1.336062 -0.578296 0 -0.548384 -0.049853 -0.737826 -0.059824 0 -0.408795 0.448678 -0.279177 -0.139589 -0.847502 0.249265 0.917297 0.039882 -0.139589 -0.239295 -0.468619 -0.528443 1.296180 -0.368913 -0.219354 -0.707914 1.186503 -0.807620 -0.329030 0.498531 0 -1.236356 0.119647 1.256298 -0.827561 -0.498531 0.159530 -0.680000 0.530000 1.580000 0.540000 0.450000 0.480000 -0.020000 0.140000
GRUMAB.MX -2.740000 -0.990000 0 0.350000 -1.920000 -0.530000 -0.250000 0 -1.570000 -2.820000 10.830000 1.660000 2.690000 3.390000 -3.240000 2.150000 2.030000 -1.640000 -2.950000 -0.590000 4.870000 2.640000 3.510000 1.060000 5.450000 1.590000 6.700000 10.260000 0 0.510000 -5.940000 5.110000 -0.570000 -0.590000 0.340000 2.150000 -5.520000 2.360000 2.470000 -4.270000 -2.340000 7.930000 1.760000
GSANBORB-1.MX 0.170000 0.140000 0 0.130000 -0.060000 -0.240000 0.270000 0 0.100000 -0.490000 -0.710000 0.500000 0.170000 0.510000 -0.910000 -0.780000 0.120000 -0.110000 -0.210000 0.570000 0.330000 -0.610000 -0.360000 -0.400000 -0.080000 -0.280000 0.020000 0.560000 0 -0.650000 0.480000 0.420000 0.950000 0.640000 -0.730000 0.400000 0.010000 0.490000 0.340000 -0.930000 0.250000 -0.370000 -0.100000
ICA.MX 0.000000 0.020000 0 -0.120000 -0.100000 -0.050000 0.010000 0 0.010000 -0.040000 -0.230000 -0.190000 -0.020000 0.000000 -0.100000 -0.110000 -0.190000 -0.090000 -0.120000 -0.300000 -0.360000 -0.070000 1.270000 -0.080000 1.100000 0.060000 -0.290000 0.380000 0 -0.330000 -0.230000 -0.230000 -0.450000 0.530000 0.010000 -0.030000 -0.070000 0.020000 0.010000 -0.030000 0.070000 -0.030000 0.160000
ICHB.MX -0.360000 0.050000 0 -0.620000 1.180000 -0.600000 0.080000 0 -0.740000 -0.600000 -0.530000 -1.420000 -0.330000 -0.240000 -0.270000 -0.300000 0.240000 2.500000 -0.040000 -0.430000 -0.870000 0.490000 0.240000 0.070000 -0.710000 -1.680000 -0.480000 0.550000 0 -0.320000 -0.040000 -0.970000 0.180000 1.290000 0.490000 -1.490000 -0.340000 2.010000 1.570000 -0.060000 -0.540000 1.050000 1.090000
IENOVA.MX 2.760000 -0.280000 0 -1.510000 0.200000 -0.140000 0.530000 0 1.480000 -1.590000 -0.310000 1.310000 -1.230000 -1.080000 0.190000 -1.920000 -0.390000 -0.210000 -0.270000 0.340000 -1.370000 -0.860000 -1.080000 0.130000 -0.090000 0.270000 0.680000 -0.330000 0 2.610000 1.580000 0.190000 -1.890000 2.150000 -0.520000 0.120000 0.920000 0.510000 0.380000 0.360000 -1.080000 -1.270000 -0.840000
KIMBERA.MX -0.190000 -0.160000 0 -0.110000 -0.030000 -0.250000 1.350000 0 0.150000 -0.400000 0.240000 0.160000 0.660000 -0.190000 0.870000 0.930000 0.190000 0.460000 -0.770000 0.480000 0.060000 -0.030000 1.160000 0.490000 -0.670000 0.030000 0.900000 1.560000 0 -1.030000 0.120000 0.350000 -0.150000 0.300000 -0.440000 -0.080000 -0.840000 -0.130000 0.240000 -0.860000 0.400000 -0.730000 0.210000
KOFL.MX 1.440000 -0.920000 0 -0.770000 -1.530000 -0.010000 1.400000 0 -0.710000 -1.540000 2.100000 1.680000 -1.030000 0.530000 -0.910000 -0.020000 1.000000 1.370000 -2.230000 1.520000 4.570000 1.800000 -0.630000 1.030000 -0.220000 -0.570000 0.070000 4.120000 0 -3.230000 2.520000 -1.110000 0.930000 1.820000 0.720000 1.810000 0.280000 2.600000 0.280000 2.950000 -1.810000 -1.770000 0.140000
LABB.MX -0.110000 0.200000 0 -0.080000 0.110000 0.560000 0.190000 0 0.720000 -0.630000 -0.030000 -0.160000 -0.220000 0.260000 0.180000 -0.170000 -0.110000 -0.060000 -0.090000 -0.230000 0.130000 0.970000 0.030000 -0.470000 -0.120000 -0.400000 -0.380000 0.020000 0 0.300000 -0.470000 -0.120000 -0.180000 0.180000 0.210000 -0.180000 -0.400000 -0.180000 -0.060000 -0.290000 0.160000 -0.430000 -0.050000
LALAB.MX -0.651524 0.315890 0 -0.217213 -0.957524 -0.671278 0.325800 0 0.029617 0.266570 0.069111 -0.424532 -0.473834 0.651519 -0.187576 -0.454111 0.661404 0.987198 0.078982 -0.098728 0.375125 -1.115668 0.098718 0.059232 0.878688 -0.760217 0.306032 1.243869 0 0.138213 0.069110 -0.493605 -1.767309 0.483713 0.019744 -0.167831 -0.661433 -0.177711 0.078985 -0.878742 -0.750368 -0.493657 0.473855
LIVEPOLC-1.MX 3.520000 -3.620000 0 -0.750000 -0.310000 -6.100000 3.310000 0 2.120000 0.610000 -4.010000 3.600000 -1.570000 -0.140000 -6.560000 -1.430000 -5.680000 -1.950000 -0.800000 2.630000 1.850000 0.520000 -2.830000 0.000000 6.070000 5.120000 -1.200000 6.650000 0 -4.720000 -2.580000 2.420000 -2.120000 0.500000 -1.500000 -2.130000 0.150000 -2.510000 2.880000 -3.870000 -2.770000 -2.130000 -1.150000
MEXCHEM.MX 0.542467 -0.197261 0 -0.325480 -0.177535 -1.045483 0.246576 0 0.355070 -0.029589 -0.138083 -1.311785 -0.581920 0.818633 -0.039452 0.039452 0.611509 -0.315617 -0.838359 -0.394522 -0.138083 0.512878 0.207124 -0.611509 -0.216987 0.098630 0.088767 1.390689 0 -0.591783 0.078904 0.453700 -0.364933 -1.696444 0.897537 -0.216987 0.700276 0.345206 0.118357 0.641098 -0.433974 -0.946852 -0.335344
OHLMEX.MX -0.780000 0.020000 0 0.010000 -0.690000 0.130000 -0.030000 0 -0.130000 -0.220000 -0.050000 -0.260000 -0.050000 -0.240000 -0.010000 -0.230000 0.000000 -0.580000 -0.040000 0.310000 0.040000 0.130000 0.160000 -0.210000 -0.090000 -0.700000 0.470000 0.170000 0 0.710000 1.010000 -0.010000 0.180000 -0.290000 -0.330000 0.150000 0.530000 -0.140000 0.430000 0.170000 0.300000 0.360000 -0.020000
PINFRA.MX -1.254005 -1.244055 0 1.761580 -3.911307 -3.294256 2.955877 0 1.920822 0.557337 -1.592392 -3.234539 -7.096074 -3.841634 -4.950000 -4.370000 0.630000 -3.000000 -1.860000 6.240000 -0.840000 19.320000 6.260000 2.440000 0.500000 -4.600000 0.880000 2.360000 0 1.010000 3.040000 5.010000 0.480000 -1.020000 -0.900000 -0.700000 -0.010000 0.350000 -0.870000 -2.520000 3.070000 -2.230000 -2.570000
SANMEXB.MX 0.310000 -0.120000 0 -0.220000 -0.210000 -0.200000 0.330000 0 -0.240000 0.130000 0.330000 -0.500000 -0.460000 -0.360000 -0.220000 -0.410000 -0.600000 0.090000 -0.200000 -0.300000 -0.850000 0.180000 0.410000 -0.400000 0.070000 0.120000 -0.060000 0.320000 0 -0.180000 -0.250000 0.190000 0.250000 -0.880000 -0.720000 0.030000 -0.380000 0.860000 0.130000 0.370000 0.800000 0.300000 -0.390000
TLEVISACPO.MX 0.000000 0.510000 0 -0.010000 0.230000 -1.180000 -1.030000 0 -0.900000 -1.110000 -1.190000 0.500000 -0.220000 2.130000 3.450000 0.220000 0.720000 -1.050000 -0.600000 -0.800000 -0.750000 -4.150000 1.560000 -0.930000 0.940000 -0.550000 -1.800000 1.890000 0 2.730000 -1.240000 -1.320000 -4.130000 -0.780000 -1.520000 1.160000 1.560000 -1.100000 1.230000 -0.370000 0.720000 -0.780000 -0.460000
WALMEX.MX -0.647854 0.039868 0 0.847193 -0.787391 -1.016632 0.289042 0 -0.119604 -0.189373 -0.269109 -1.325609 0.039868 1.634585 0.059802 -0.009967 0.518283 0.607986 -0.687722 1.096368 0.936896 -0.139538 -1.275774 -0.269109 0.578085 0.578085 -0.308976 1.066467 0 0.209307 -0.039868 0.049835 0.318943 -0.448514 -0.508316 0.009967 0.338877 -0.388712 0.338877 -0.936896 -1.594717 -0.060000 0.010000

32 rows × 43 columns


In [61]:
norm_data = normalize(clustering_data.values, axis=1)
norm_data = pd.DataFrame(norm_data)
for item in norm_data.values:
    plt.plot(item)
plt.show();

In [62]:
prediction, model, data = cluster_data(clustering_data, n_clusters=8, normalize_data=True)
print "Cluster Count: %s" % len(np.unique(prediction))
clustering_data["Cluster"] = prediction
clustering_data


Cluster Count: 8
Out[62]:
n_date 735955 735956 735957 735960 735961 735962 735963 735964 735967 735968 735969 735970 735971 735974 735975 735976 735977 735978 735981 735982 735983 735984 735985 735988 735989 735990 735991 735992 735995 735996 735997 735998 735999 736002 736003 736004 736005 736006 736009 736010 736011 736012 736013 Cluster
Ticker
ALFAA.MX 0.390000 -0.270000 0 -0.050000 -0.320000 -0.420000 -0.020000 0 -0.250000 0.090000 -0.340000 -1.970000 -0.820000 0.520000 -0.480000 -0.620000 -0.410000 0.480000 -0.120000 0.470000 0.510000 0.100000 -0.050000 0.400000 0.120000 -0.050000 0.270000 1.420000 0 -1.620000 -0.030000 0.100000 -0.470000 -0.160000 1.260000 0.210000 -0.360000 -0.750000 0.420000 -0.260000 0.290000 -0.380000 -0.910000 1
ALPEKA.MX -0.350000 -0.020000 0 -0.110000 0.400000 -0.060000 -0.020000 0 0.070000 -0.480000 -1.120000 -0.230000 0.300000 -0.520000 -0.780000 0.480000 -0.140000 0.000000 -0.120000 0.170000 -0.810000 0.850000 0.510000 -0.350000 -0.340000 -0.320000 0.160000 0.470000 0 -0.220000 0.760000 0.120000 0.090000 -0.220000 0.620000 -0.040000 -0.070000 0.400000 -0.120000 -0.200000 0.070000 0.810000 0.220000 3
ALSEA.MX -0.190000 -0.190000 0 1.040000 0.120000 -0.700000 1.250000 0 0.430000 0.190000 -0.010000 -2.260000 -2.060000 1.080000 1.230000 0.010000 -0.480000 0.750000 0.580000 -0.450000 -0.010000 2.530000 0.650000 0.360000 0.110000 -1.120000 1.080000 1.700000 0 -1.240000 -0.270000 -0.920000 1.910000 2.040000 0.850000 0.310000 -0.540000 -3.010000 -0.060000 -0.750000 0.300000 -0.380000 -0.890000 1
AMXL.MX -0.030000 -0.110000 0 -0.180000 0.280000 0.050000 -0.220000 0 0.050000 -0.020000 -0.210000 -0.360000 -0.490000 0.190000 0.000000 0.150000 0.240000 -0.210000 0.040000 0.010000 0.290000 0.270000 0.270000 -0.220000 0.430000 0.280000 0.220000 0.230000 0 0.000000 0.160000 0.140000 -0.190000 -0.030000 -0.350000 -0.850000 0.070000 0.330000 0.340000 -0.210000 -0.020000 -0.210000 -0.270000 0
ASURB.MX 1.610000 -0.160000 0 -3.290000 -1.100000 -3.220000 -0.110000 0 -2.810000 -5.460000 -1.170000 -0.280000 -1.610000 -1.770000 1.850000 -5.290000 -1.470000 -0.680000 2.350000 0.040000 -0.070000 1.990000 1.540000 4.250000 1.820000 -5.050000 1.560000 6.720000 0 -3.440000 -1.210000 1.930000 -0.050000 -1.130000 -0.770000 3.300000 2.460000 -1.180000 -0.240000 2.240000 5.070000 -2.740000 -4.280000 1
BIMBOA.MX 0.500000 -0.490000 0 0.060000 -0.050000 -1.240000 0.540000 0 -0.030000 0.780000 0.860000 0.660000 0.160000 0.760000 0.090000 0.720000 -0.160000 -0.320000 -0.460000 -0.240000 0.640000 -1.830000 0.170000 -0.390000 0.940000 0.110000 1.030000 1.760000 0 1.410000 0.000000 0.190000 -1.340000 0.910000 0.940000 0.550000 -1.200000 0.010000 0.780000 -0.020000 -0.330000 0.730000 1.230000 5
BOLSAA.MX 0.000000 0.120000 0 -0.090000 -0.070000 0.010000 -0.190000 0 -0.330000 -0.020000 -0.050000 -0.460000 -0.380000 -0.100000 -0.210000 0.100000 0.670000 -0.190000 0.170000 0.120000 0.320000 -0.130000 -0.180000 0.410000 0.030000 0.230000 0.550000 0.700000 0 0.860000 -0.690000 -0.040000 -0.750000 -0.230000 -0.150000 -0.400000 -0.430000 0.640000 0.090000 0.580000 0.330000 0.660000 0.330000 5
CEMEXCPO.MX 0.570000 0.070000 0 -0.310000 -0.150000 -0.090000 0.230000 0 -0.170000 -0.120000 0.080000 -0.610000 -0.680000 0.040000 0.080000 -0.310000 0.180000 0.130000 -0.070000 -0.470000 0.050000 0.550000 0.000000 -0.580000 0.130000 0.000000 0.000000 0.340000 0 0.040000 0.370000 0.620000 -0.200000 -0.550000 0.010000 -0.160000 0.100000 0.170000 0.170000 -0.100000 0.680000 -0.400000 0.070000 0
COMERCIUBC.MX -0.520000 0.450000 0 -1.050000 -0.350000 -2.230000 0.010000 0 -0.690000 0.060000 0.000000 0.760000 0.170000 -0.310000 -0.050000 -0.340000 -0.100000 0.430000 -0.160000 0.020000 0.370000 -0.180000 0.750000 0.500000 -0.260000 -0.510000 0.210000 0.040000 0 -0.340000 -0.110000 0.030000 -0.500000 -0.020000 1.070000 0.050000 0.760000 -0.040000 0.060000 0.000000 0.570000 0.050000 -0.030000 7
ELEKTRA.MX -1.410000 -2.750000 0 -9.310000 -13.880000 -8.810000 3.040000 0 -8.570000 -12.690000 -14.620000 -2.900000 -4.550000 17.030000 -1.030000 -14.620000 -0.760000 20.180000 -2.930000 -17.860000 -9.130000 4.490000 4.070000 4.540000 7.540000 -0.950000 4.020000 1.120000 0 -0.250000 -2.050000 -4.670000 0.250000 -1.080000 8.140000 5.660000 -4.720000 -1.090000 5.450000 -5.390000 7.700000 -12.000000 -3.310000 7
GAPB.MX -0.700000 -9.200000 0 -1.530000 -0.470000 -1.070000 -1.000000 0 -3.230000 -0.660000 -1.270000 -4.680000 2.240000 3.640000 -5.840000 2.300000 -1.200000 -0.340000 0.000000 -0.950000 1.120000 7.850000 -1.770000 2.620000 2.240000 4.440000 -3.390000 0.840000 0 -4.300000 1.650000 1.740000 -0.830000 -1.400000 -0.390000 -0.500000 -0.420000 -0.470000 -0.690000 0.770000 -1.060000 0.950000 -2.410000 2
GENTERA.MX 0.080000 -0.440000 0 -0.270000 0.040000 0.080000 0.190000 0 -1.200000 0.370000 -0.110000 -1.300000 -0.460000 -0.590000 -0.630000 0.260000 0.920000 -0.860000 -0.400000 0.030000 1.120000 0.000000 0.170000 -1.180000 -0.220000 -0.110000 0.500000 1.630000 0 0.570000 0.090000 0.950000 0.220000 0.800000 -0.440000 0.560000 -0.090000 -0.200000 0.250000 -0.160000 1.650000 1.020000 -1.210000 1
GFINBURO.MX -0.170000 -0.060000 0 0.270000 -0.490000 -0.040000 0.310000 0 -0.650000 -0.530000 -0.320000 -0.580000 0.050000 0.920000 -0.390000 -0.230000 -0.180000 -0.510000 0.670000 -0.890000 0.410000 3.740000 -0.010000 -0.430000 0.120000 0.220000 0.160000 1.410000 0 0.120000 0.040000 1.030000 -0.910000 0.510000 -0.330000 0.530000 -0.100000 0.270000 0.330000 0.980000 0.780000 0.900000 -0.060000 2
GFNORTEO.MX 0.000000 -0.620000 0 0.890000 0.450000 -0.280000 1.380000 0 -1.920000 -0.090000 0.280000 -1.710000 -2.480000 0.700000 0.030000 -1.080000 -0.460000 -1.580000 0.170000 -0.240000 -0.670000 -2.170000 0.880000 1.820000 -0.120000 1.460000 0.150000 5.230000 0 -3.950000 1.050000 -1.010000 -0.900000 -1.020000 -1.050000 1.500000 -1.230000 -1.960000 1.910000 -1.070000 5.070000 -0.130000 0.020000 1
GFREGIOO.MX 0.110000 0.270000 0 -0.930000 0.920000 0.170000 -0.660000 0 2.110000 -1.160000 -1.180000 -1.070000 -0.210000 -0.150000 0.000000 -0.510000 -0.480000 -0.270000 1.110000 0.500000 -0.960000 -0.590000 -0.950000 0.480000 -1.260000 0.760000 -0.490000 2.960000 0 0.610000 -0.210000 -1.130000 -1.540000 0.560000 -0.110000 1.010000 -0.340000 0.150000 0.120000 1.150000 4.070000 -0.210000 1.820000 4
GMEXICOB.MX 1.336062 -0.578296 0 -0.548384 -0.049853 -0.737826 -0.059824 0 -0.408795 0.448678 -0.279177 -0.139589 -0.847502 0.249265 0.917297 0.039882 -0.139589 -0.239295 -0.468619 -0.528443 1.296180 -0.368913 -0.219354 -0.707914 1.186503 -0.807620 -0.329030 0.498531 0 -1.236356 0.119647 1.256298 -0.827561 -0.498531 0.159530 -0.680000 0.530000 1.580000 0.540000 0.450000 0.480000 -0.020000 0.140000 0
GRUMAB.MX -2.740000 -0.990000 0 0.350000 -1.920000 -0.530000 -0.250000 0 -1.570000 -2.820000 10.830000 1.660000 2.690000 3.390000 -3.240000 2.150000 2.030000 -1.640000 -2.950000 -0.590000 4.870000 2.640000 3.510000 1.060000 5.450000 1.590000 6.700000 10.260000 0 0.510000 -5.940000 5.110000 -0.570000 -0.590000 0.340000 2.150000 -5.520000 2.360000 2.470000 -4.270000 -2.340000 7.930000 1.760000 5
GSANBORB-1.MX 0.170000 0.140000 0 0.130000 -0.060000 -0.240000 0.270000 0 0.100000 -0.490000 -0.710000 0.500000 0.170000 0.510000 -0.910000 -0.780000 0.120000 -0.110000 -0.210000 0.570000 0.330000 -0.610000 -0.360000 -0.400000 -0.080000 -0.280000 0.020000 0.560000 0 -0.650000 0.480000 0.420000 0.950000 0.640000 -0.730000 0.400000 0.010000 0.490000 0.340000 -0.930000 0.250000 -0.370000 -0.100000 6
ICA.MX 0.000000 0.020000 0 -0.120000 -0.100000 -0.050000 0.010000 0 0.010000 -0.040000 -0.230000 -0.190000 -0.020000 0.000000 -0.100000 -0.110000 -0.190000 -0.090000 -0.120000 -0.300000 -0.360000 -0.070000 1.270000 -0.080000 1.100000 0.060000 -0.290000 0.380000 0 -0.330000 -0.230000 -0.230000 -0.450000 0.530000 0.010000 -0.030000 -0.070000 0.020000 0.010000 -0.030000 0.070000 -0.030000 0.160000 7
ICHB.MX -0.360000 0.050000 0 -0.620000 1.180000 -0.600000 0.080000 0 -0.740000 -0.600000 -0.530000 -1.420000 -0.330000 -0.240000 -0.270000 -0.300000 0.240000 2.500000 -0.040000 -0.430000 -0.870000 0.490000 0.240000 0.070000 -0.710000 -1.680000 -0.480000 0.550000 0 -0.320000 -0.040000 -0.970000 0.180000 1.290000 0.490000 -1.490000 -0.340000 2.010000 1.570000 -0.060000 -0.540000 1.050000 1.090000 3
IENOVA.MX 2.760000 -0.280000 0 -1.510000 0.200000 -0.140000 0.530000 0 1.480000 -1.590000 -0.310000 1.310000 -1.230000 -1.080000 0.190000 -1.920000 -0.390000 -0.210000 -0.270000 0.340000 -1.370000 -0.860000 -1.080000 0.130000 -0.090000 0.270000 0.680000 -0.330000 0 2.610000 1.580000 0.190000 -1.890000 2.150000 -0.520000 0.120000 0.920000 0.510000 0.380000 0.360000 -1.080000 -1.270000 -0.840000 4
KIMBERA.MX -0.190000 -0.160000 0 -0.110000 -0.030000 -0.250000 1.350000 0 0.150000 -0.400000 0.240000 0.160000 0.660000 -0.190000 0.870000 0.930000 0.190000 0.460000 -0.770000 0.480000 0.060000 -0.030000 1.160000 0.490000 -0.670000 0.030000 0.900000 1.560000 0 -1.030000 0.120000 0.350000 -0.150000 0.300000 -0.440000 -0.080000 -0.840000 -0.130000 0.240000 -0.860000 0.400000 -0.730000 0.210000 1
KOFL.MX 1.440000 -0.920000 0 -0.770000 -1.530000 -0.010000 1.400000 0 -0.710000 -1.540000 2.100000 1.680000 -1.030000 0.530000 -0.910000 -0.020000 1.000000 1.370000 -2.230000 1.520000 4.570000 1.800000 -0.630000 1.030000 -0.220000 -0.570000 0.070000 4.120000 0 -3.230000 2.520000 -1.110000 0.930000 1.820000 0.720000 1.810000 0.280000 2.600000 0.280000 2.950000 -1.810000 -1.770000 0.140000 2
LABB.MX -0.110000 0.200000 0 -0.080000 0.110000 0.560000 0.190000 0 0.720000 -0.630000 -0.030000 -0.160000 -0.220000 0.260000 0.180000 -0.170000 -0.110000 -0.060000 -0.090000 -0.230000 0.130000 0.970000 0.030000 -0.470000 -0.120000 -0.400000 -0.380000 0.020000 0 0.300000 -0.470000 -0.120000 -0.180000 0.180000 0.210000 -0.180000 -0.400000 -0.180000 -0.060000 -0.290000 0.160000 -0.430000 -0.050000 2
LALAB.MX -0.651524 0.315890 0 -0.217213 -0.957524 -0.671278 0.325800 0 0.029617 0.266570 0.069111 -0.424532 -0.473834 0.651519 -0.187576 -0.454111 0.661404 0.987198 0.078982 -0.098728 0.375125 -1.115668 0.098718 0.059232 0.878688 -0.760217 0.306032 1.243869 0 0.138213 0.069110 -0.493605 -1.767309 0.483713 0.019744 -0.167831 -0.661433 -0.177711 0.078985 -0.878742 -0.750368 -0.493657 0.473855 6
LIVEPOLC-1.MX 3.520000 -3.620000 0 -0.750000 -0.310000 -6.100000 3.310000 0 2.120000 0.610000 -4.010000 3.600000 -1.570000 -0.140000 -6.560000 -1.430000 -5.680000 -1.950000 -0.800000 2.630000 1.850000 0.520000 -2.830000 0.000000 6.070000 5.120000 -1.200000 6.650000 0 -4.720000 -2.580000 2.420000 -2.120000 0.500000 -1.500000 -2.130000 0.150000 -2.510000 2.880000 -3.870000 -2.770000 -2.130000 -1.150000 6
MEXCHEM.MX 0.542467 -0.197261 0 -0.325480 -0.177535 -1.045483 0.246576 0 0.355070 -0.029589 -0.138083 -1.311785 -0.581920 0.818633 -0.039452 0.039452 0.611509 -0.315617 -0.838359 -0.394522 -0.138083 0.512878 0.207124 -0.611509 -0.216987 0.098630 0.088767 1.390689 0 -0.591783 0.078904 0.453700 -0.364933 -1.696444 0.897537 -0.216987 0.700276 0.345206 0.118357 0.641098 -0.433974 -0.946852 -0.335344 0
OHLMEX.MX -0.780000 0.020000 0 0.010000 -0.690000 0.130000 -0.030000 0 -0.130000 -0.220000 -0.050000 -0.260000 -0.050000 -0.240000 -0.010000 -0.230000 0.000000 -0.580000 -0.040000 0.310000 0.040000 0.130000 0.160000 -0.210000 -0.090000 -0.700000 0.470000 0.170000 0 0.710000 1.010000 -0.010000 0.180000 -0.290000 -0.330000 0.150000 0.530000 -0.140000 0.430000 0.170000 0.300000 0.360000 -0.020000 2
PINFRA.MX -1.254005 -1.244055 0 1.761580 -3.911307 -3.294256 2.955877 0 1.920822 0.557337 -1.592392 -3.234539 -7.096074 -3.841634 -4.950000 -4.370000 0.630000 -3.000000 -1.860000 6.240000 -0.840000 19.320000 6.260000 2.440000 0.500000 -4.600000 0.880000 2.360000 0 1.010000 3.040000 5.010000 0.480000 -1.020000 -0.900000 -0.700000 -0.010000 0.350000 -0.870000 -2.520000 3.070000 -2.230000 -2.570000 2
SANMEXB.MX 0.310000 -0.120000 0 -0.220000 -0.210000 -0.200000 0.330000 0 -0.240000 0.130000 0.330000 -0.500000 -0.460000 -0.360000 -0.220000 -0.410000 -0.600000 0.090000 -0.200000 -0.300000 -0.850000 0.180000 0.410000 -0.400000 0.070000 0.120000 -0.060000 0.320000 0 -0.180000 -0.250000 0.190000 0.250000 -0.880000 -0.720000 0.030000 -0.380000 0.860000 0.130000 0.370000 0.800000 0.300000 -0.390000 1
TLEVISACPO.MX 0.000000 0.510000 0 -0.010000 0.230000 -1.180000 -1.030000 0 -0.900000 -1.110000 -1.190000 0.500000 -0.220000 2.130000 3.450000 0.220000 0.720000 -1.050000 -0.600000 -0.800000 -0.750000 -4.150000 1.560000 -0.930000 0.940000 -0.550000 -1.800000 1.890000 0 2.730000 -1.240000 -1.320000 -4.130000 -0.780000 -1.520000 1.160000 1.560000 -1.100000 1.230000 -0.370000 0.720000 -0.780000 -0.460000 4
WALMEX.MX -0.647854 0.039868 0 0.847193 -0.787391 -1.016632 0.289042 0 -0.119604 -0.189373 -0.269109 -1.325609 0.039868 1.634585 0.059802 -0.009967 0.518283 0.607986 -0.687722 1.096368 0.936896 -0.139538 -1.275774 -0.269109 0.578085 0.578085 -0.308976 1.066467 0 0.209307 -0.039868 0.049835 0.318943 -0.448514 -0.508316 0.009967 0.338877 -0.388712 0.338877 -0.936896 -1.594717 -0.060000 0.010000 6

32 rows × 44 columns


In [63]:
prediction, model, c_data = visualize_clusters(stocks_df, values="change_amount", n_clusters=8, normalize_data=True);


Cluster: 0
Members: ['AMXL.MX: AMX.L', 'CEMEXCPO.MX: CEMEX.CPO', 'GMEXICOB.MX: GMEXICO.B', 'MEXCHEM.MX: MEXCHEM', 'SANMEXB.MX: SANMEX.B']
Cluster: 1
Members: ['ALFAA.MX: ALFA.A', 'ALSEA.MX: ALSEA', 'ASURB.MX: ASUR.B', 'GENTERA.MX: GENTERA', 'GFNORTEO.MX: GFNORTE.O', 'GFREGIOO.MX: GFREGIO.O', 'KIMBERA.MX: KIMBER.A']
Cluster: 2
Members: ['GSANBORB-1.MX: GSANBOR.B-1', 'IENOVA.MX: IENOVA', 'LIVEPOLC-1.MX: LIVEPOL.C-1']
Cluster: 3
Members: ['GAPB.MX: GAP.B', 'GFINBURO.MX: GFINBUR.O', 'LABB.MX: LAB.B', 'PINFRA.MX: PINFRA']
Cluster: 4
Members: ['ALPEKA.MX: ALPEK.A', 'ICHB.MX: ICH.B', 'OHLMEX.MX: OHLMEX']
Cluster: 5
Members: ['BIMBOA.MX: BIMBO.A', 'COMERCIUBC.MX: COMERCI.UBC', 'ELEKTRA.MX: ELEKTRA', 'ICA.MX: ICA', 'LALAB.MX: LALA.B', 'TLEVISACPO.MX: TLEVISA.CPO', 'WALMEX.MX: WALMEX']
Cluster: 6
Members: ['BOLSAA.MX: BOLSA.A', 'GRUMAB.MX: GRUMA.B']
Cluster: 7
Members: ['KOFL.MX: KOFL']

In [64]:
measure_error(prediction, model, c_data)


Out[64]:
(-13.014854954903203, 1)

Busqueda del numero optimo de clusters


In [65]:
max_clusters = 30
feature = "average"
clustering_data = pivot_data(stocks_df, values=feature)
clustering_data["Cluster"] = pd.Series()
for normalize_data in [True, False]:
    fig = plt.figure(figsize=(10,6))
    plt.title("K-Means - Feature: %s Normalized: %s" % (feature, normalize_data))
    axes_1 = fig.add_subplot(111)
    axes_2 = axes_1.twinx()
    score_error_list = []
    failed_clusters_list = []
    
    for n_clusters in range(2,max_clusters):
        prediction, model, data = cluster_data(clustering_data.drop("Cluster",1), n_clusters=n_clusters,
                                               normalize_data=normalize_data)
        data = pd.DataFrame(data, index=clustering_data.index,columns=clustering_data.drop("Cluster",1).columns)
        data["Cluster"] = prediction
        score_error, failed_clusters =  measure_error(prediction, model, data)
        score_error_list.append(score_error)
        failed_clusters_list.append(failed_clusters)
    axes_1.plot(range(2,max_clusters), score_error_list, "ro-", label = "Average Error")
    axes_2.plot(range(2,max_clusters), failed_clusters_list, "bo-", label = "Failed Cluster")
    
    axes_1.grid()
    axes_1.legend(loc = "lower center")
    axes_2.legend(loc = "upper center")
    axes_1.set_ylabel("Average Error")
    axes_2.set_ylabel("Failed Cluster")
    axes_1.set_xlabel("Clusters")
    plt.show()

In [ ]: