# !pip install Quandl
# special IPython command to prepare the notebook for matplotlib
%matplotlib inline
import numpy as np
import numpy as numpy #For neural network code.
import pandas as pd
import math
import scipy
import random
import Quandl
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import string
import calendar
import datetime
from pandas.tseries.offsets import *
import operator
from itertools import combinations, permutations
# set some nicer defaults for matplotlib
from matplotlib import rcParams
import scipy.stats as stats
#colorbrewer2 Dark2 qualitative color table
dark2_colors = [(0.10588235294117647, 0.6196078431372549, 0.4666666666666667),
(0.8509803921568627, 0.37254901960784315, 0.00784313725490196),
(0.4588235294117647, 0.4392156862745098, 0.7019607843137254),
(0.9058823529411765, 0.1607843137254902, 0.5411764705882353),
(0.4, 0.6509803921568628, 0.11764705882352941),
(0.9019607843137255, 0.6705882352941176, 0.00784313725490196),
(0.6509803921568628, 0.4627450980392157, 0.11372549019607843)]
#http://colorbrewer2.org/
brewer_rg = ['#E41A1C', '#4DAF4A']
rcParams['figure.figsize'] = (10, 6)
rcParams['figure.dpi'] = 150
rcParams['axes.color_cycle'] = dark2_colors
rcParams['lines.linewidth'] = 2
rcParams['axes.facecolor'] = 'white'
rcParams['font.size'] = 14
rcParams['patch.edgecolor'] = 'white'
rcParams['patch.facecolor'] = dark2_colors[0]
rcParams['font.family'] = 'StixGeneral'
def remove_border(ax=None, axes=None, top=False, right=False, left=True, bottom=True):
"""
Minimize chartjunk by stripping out unnecesasry plot borders and axis ticks
The top/right/left/bottom keywords toggle whether the corresponding plot border is drawn
"""
if ax == None:
ax = axes or plt.gca()
ax.spines['top'].set_visible(top)
ax.spines['right'].set_visible(right)
ax.spines['left'].set_visible(left)
ax.spines['bottom'].set_visible(bottom)
#turn off all ticks
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_ticks_position('none')
#remove grid
ax.grid(False)
#now re-enable visibles
if top:
ax.xaxis.tick_top()
if bottom:
ax.xaxis.tick_bottom()
if left:
ax.yaxis.tick_left()
if right:
ax.yaxis.tick_right()
pd.set_option('display.width', 500)
pd.set_option('display.max_columns', 100)
authtoken='Cx1CtXeu61zjTzpehmNV'
'''
this is a list of all the Quandl datasets we will pull and renaming their columns for convenience
'''
data = {'gold_price':'BUNDESBANK/BBK01_WT5511.1',
'usd_to_pound':'QUANDL/USDGBP.1',
'cpi':'FRED/CPIAUCSL.1',
'unemployment':'FRED/UNRATE.1',
'gas_price':'BTS_MM/RETAILGAS.1',
'volatility':'YAHOO/INDEX_VIX.6',
'house_sales':'FRED/HSN1F.1',
'usd_to_euro':'QUANDL/USDEUR.1',
's_and_p_500':'YAHOO/INDEX_GSPC.6'}
'''
takes a dictionary of Quandl codes and returns a list of dataframes
the column name of each dataframe is its repective key from the dictionary
'''
def get_quandl_data(quandl_codes):
list_of_data = []
for number, code in enumerate(quandl_codes.values()):
data = Quandl.get([code], authtoken=authtoken)
data.columns = [quandl_codes.keys()[number]]
list_of_data.append(data)
return list_of_data
'''
Collapser: takes an individual dataset and adds rows for every business day during the timeframe of that dataset.
Then it performs a linear interpolation to fill in all those missing values, then drops all values except for Mondays.
'''
def collapser(df):
df = df.asfreq(BDay())
df = df.apply(pd.Series.interpolate)
df = df.asfreq(Week(weekday=1))
return df
'''
Merger: takes the list of dataframes, runs collapser on all of them and finds the latest start date and earliest end date. Then it runs
through all the dataframes again and truncates at those date, finally concatenating all the data into one dataframe
'''
def merger(list_of_data):
begin_date=datetime.datetime(1900,1,1,12,13,14)
end_date=datetime.datetime(2100,1,1,12,13,14)
for i in range(len(list_of_data)):
list_of_data[i] = collapser(list_of_data[i])
if (list_of_data[i].index[1]>begin_date):
begin_date=list_of_data[i].index[0]
if (list_of_data[i].index[-1]<end_date):
end_date=list_of_data[i].index[-1]
for i in range(len(list_of_data)):
list_of_data[i] = list_of_data[i].truncate(before=begin_date, after=end_date)
data=pd.concat(list_of_data, axis=1)
return data, (begin_date, end_date), len(list(data.columns))
#pulls all of our data off Quandl
# Run the commands to get data:
list_of_data = get_quandl_data(data)
Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'FRED.CPIAUCSL.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'FRED.HSN1F.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'BTS_MM.RETAILGAS.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'BUNDESBANK.BBK01_WT5511.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'QUANDL.USDGBP.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'QUANDL.USDEUR.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'FRED.UNRATE.1'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'YAHOO.INDEX_GSPC.6'] Token Cx1CtXeu61zjTzpehmNV activated and saved for later use. Returning Dataframe for [u'YAHOO.INDEX_VIX.6']
'''
cleans and merges our dataset
dataset: the dataframe with data.
dates: tuple of the start and end date for which we have data.
num_col: number of columns in the dataframe
'''
dataset, dates, num_col = merger(list_of_data)
'''
Input: dataframe and an array of x number of column names that need percent difference calculations
Output: data frame with x new columns, with extra '_pdiff' as column name, representing the percentage difference.
The first date is ommited becuase there is no percentage difference information on that date.
'''
def percentageDifference(df, columns):
diff_columns=[]
df_columns=list(df.columns)
for column in columns:
if column in df_columns:
diff_columns.append(column)
df[column+"_pdiff"]= float('nan')
else:
continue
for i in range(1, len(df)):
for ncolumn in diff_columns:
df[ncolumn+"_pdiff"].ix[i] = (df[ncolumn].ix[i] - df[ncolumn].ix[i-1]) / df[ncolumn].ix[i-1]
return df[1:]
def norm(df, cols):
for col in cols:
df[col+'_norm'] = df[col] / df[col].max()
return df
dataset = norm(dataset, ['cpi', 'house_sales', 's_and_p_500'])
dataset.head()
c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning) c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning) c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning)
cpi | house_sales | gas_price | gold_price | usd_to_pound | usd_to_euro | unemployment | s_and_p_500 | volatility | s_and_p_500_pdiff | s_and_p_500_pdiff_b_0.00-100.00 | s_and_p_500_pdiff_b_0.00-100.00_adv | cpi_pdiff | house_sales_pdiff | gas_price_pdiff | gold_price_pdiff | usd_to_pound_pdiff | usd_to_euro_pdiff | unemployment_pdiff | volatility_pdiff | s_and_p_500_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_pdiff | s_and_p_500_pdiff_b_0.00-100.00_adv_pdiff | cpi_pdiff_pdiff | house_sales_pdiff_pdiff | gas_price_pdiff_pdiff | gold_price_pdiff_pdiff | usd_to_pound_pdiff_pdiff | usd_to_euro_pdiff_pdiff | unemployment_pdiff_pdiff | volatility_pdiff_pdiff | s_and_p_500_pdiff_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_adv_pdiff_pdiff | cpi_pdiff_pdiff_pdiff | house_sales_pdiff_pdiff_pdiff | gas_price_pdiff_pdiff_pdiff | gold_price_pdiff_pdiff_pdiff | usd_to_pound_pdiff_pdiff_pdiff | usd_to_euro_pdiff_pdiff_pdiff | unemployment_pdiff_pdiff_pdiff | volatility_pdiff_pdiff_pdiff | s_and_p_500_pdiff_pdiff_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_pdiff_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_adv_pdiff_pdiff_pdiff | cpi_pdiff_pdiff_pdiff_pdiff | house_sales_pdiff_pdiff_pdiff_pdiff | gas_price_pdiff_pdiff_pdiff_pdiff | gold_price_pdiff_pdiff_pdiff_pdiff | usd_to_pound_pdiff_pdiff_pdiff_pdiff | usd_to_euro_pdiff_pdiff_pdiff_pdiff | unemployment_pdiff_pdiff_pdiff_pdiff | volatility_pdiff_pdiff_pdiff_pdiff | s_and_p_500_pdiff_pdiff_pdiff_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_pdiff_pdiff_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_adv_pdiff_pdiff_pdiff_pdiff | cpi_norm | house_sales_norm | s_and_p_500_norm | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1999-10-12 | 168.200000 | 869.000000 | 1.2464 | 323.50 | 0.604775 | 0.938612 | 4.100000 | 1313.04 | 22.84 | 0.008983 | 1 | 0 | 0.000425 | -0.002460 | -0.005902 | -0.008429 | 0.000548 | 0.004423 | 0.000000 | -0.078661 | NaN | 0 | -1.000000 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 0.726255 | 0.631431 | 0.838923 |
1999-10-19 | 168.271429 | 866.857143 | 1.2362 | 309.00 | 0.598787 | 0.922720 | 4.100000 | 1261.32 | 26.56 | -0.039390 | 0 | 1 | 0.000425 | -0.002466 | -0.008184 | -0.044822 | -0.009901 | -0.016931 | 0.000000 | 0.162872 | -5.384905 | -1 | inf | -0.000425 | 0.002466 | 0.386562 | 4.317550 | -19.080733 | -4.828217 | NaN | -3.070564 | NaN | -inf | -inf | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 0.726564 | 0.629874 | 0.805878 |
1999-10-26 | 168.342857 | 864.714286 | 1.2356 | 297.95 | 0.602823 | 0.938916 | 4.100000 | 1281.91 | 24.26 | 0.016324 | 1 | 1 | 0.000424 | -0.002472 | -0.000485 | -0.035761 | 0.006740 | 0.017552 | 0.000000 | -0.086596 | -1.414429 | 0 | 0.000000 | -0.000424 | 0.002472 | -0.940691 | -0.202171 | -1.680755 | -2.036682 | NaN | -1.531683 | -0.737334 | -1.000000 | NaN | -0.000424 | 0.002472 | -3.433481 | -1.046825 | -0.911914 | -0.578171 | NaN | -0.501172 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 0.726872 | 0.628317 | 0.819033 |
1999-11-02 | 168.418182 | 863.454545 | 1.2306 | 292.05 | 0.608459 | 0.949836 | 4.095455 | 1347.74 | 23.10 | 0.051353 | 1 | 1 | 0.000447 | -0.001457 | -0.004047 | -0.019802 | 0.009349 | 0.011630 | -0.001109 | -0.047815 | 2.145830 | 0 | 0.000000 | 0.054098 | -0.410664 | 7.337380 | -0.446261 | 0.387083 | -0.337390 | -inf | -0.447837 | -2.517099 | NaN | NaN | -128.444084 | -167.127432 | -8.799989 | 1.207351 | -1.230303 | -0.834343 | NaN | -0.707618 | 2.413783 | NaN | NaN | 302588.240014 | -67609.616594 | 1.562993 | -2.153346 | 0.349144 | 0.443073 | NaN | 0.411926 | NaN | NaN | NaN | 0.727197 | 0.627402 | 0.861093 |
1999-11-09 | 168.509091 | 865.727273 | 1.2366 | 291.25 | 0.616662 | 0.961091 | 4.072727 | 1365.28 | 22.70 | 0.013014 | 1 | 1 | 0.000540 | 0.002632 | 0.004876 | -0.002739 | 0.013482 | 0.011849 | -0.005549 | -0.017316 | -0.746571 | 0 | 0.000000 | 0.206357 | -2.806756 | -2.204876 | -0.861668 | 0.441983 | 0.018828 | 4.005549 | -0.637856 | -1.347917 | NaN | NaN | 2.814499 | 5.834671 | -1.300499 | 0.930858 | 0.141831 | -1.055806 | NaN | 0.424305 | -0.464496 | NaN | NaN | -1.021912 | -1.034912 | -0.852216 | -0.229008 | -1.115282 | 0.265433 | NaN | -1.599625 | -1.192435 | NaN | NaN | 0.727590 | 0.629053 | 0.872300 |
#dataset = percentageDifference(dataset, dataset.keys())
#THIS IS USED LATER
dataset.head()
c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning) c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning)
cpi | house_sales | gas_price | gold_price | usd_to_pound | usd_to_euro | unemployment | s_and_p_500 | volatility | s_and_p_500_pdiff | s_and_p_500_pdiff_b_0.00-100.00 | s_and_p_500_pdiff_b_0.00-100.00_adv | cpi_pdiff | house_sales_pdiff | gas_price_pdiff | gold_price_pdiff | usd_to_pound_pdiff | usd_to_euro_pdiff | unemployment_pdiff | volatility_pdiff | s_and_p_500_pdiff_pdiff | s_and_p_500_pdiff_b_0.00-100.00_pdiff | s_and_p_500_pdiff_b_0.00-100.00_adv_pdiff | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1999-09-21 | 167.990909 | 855.272727 | 1.2668 | 255.20 | 0.615034 | 0.959907 | 4.136364 | 1307.58 | 25.65 | -0.021485 | 0 | 0 | 0.000406 | 0.012375 | 0.010530 | -0.005843 | -0.010857 | -0.005147 | -0.005464 | 0.079091 | NaN | 0 | NaN |
1999-09-28 | 168.059091 | 865.727273 | 1.2606 | 288.25 | 0.607130 | 0.955362 | 4.113636 | 1282.20 | 26.06 | -0.019410 | 0 | 1 | 0.000406 | 0.012224 | -0.004894 | 0.129506 | -0.012851 | -0.004735 | -0.005495 | 0.015984 | -0.096578 | 0 | inf |
1999-10-05 | 168.128571 | 871.142857 | 1.2538 | 326.25 | 0.604444 | 0.934479 | 4.100000 | 1301.35 | 24.79 | 0.014935 | 1 | 1 | 0.000413 | 0.006256 | -0.005394 | 0.131830 | -0.004424 | -0.021859 | -0.003315 | -0.048734 | -1.769466 | 0 | 0.000000 |
1999-10-12 | 168.200000 | 869.000000 | 1.2464 | 323.50 | 0.604775 | 0.938612 | 4.100000 | 1313.04 | 22.84 | 0.008983 | 1 | 0 | 0.000425 | -0.002460 | -0.005902 | -0.008429 | 0.000548 | 0.004423 | 0.000000 | -0.078661 | -0.398539 | 0 | -1.000000 |
1999-10-19 | 168.271429 | 866.857143 | 1.2362 | 309.00 | 0.598787 | 0.922720 | 4.100000 | 1261.32 | 26.56 | -0.039390 | 0 | 1 | 0.000425 | -0.002466 | -0.008184 | -0.044822 | -0.009901 | -0.016931 | 0.000000 | 0.162872 | -5.384905 | -1 | inf |
'''
biner: takes in a dataframe, an array of column names, and a array of bins
Outpouts a new datatframe with indicators (0 or 1) of if the element is in the bin. Also returns an array of new column names.
'''
def biner(df, cols, bins):
add_cols = []
for col in cols:
for i in range(len(bins)-1):
title = '%s_b_%.2f-%.2f' % (col, bins[i], bins[i+1])
df[title] = 0
df[title][(df[col] >= bins[i]) & (df[col] < bins[i+1])] = 1
add_cols.append(title)
return df, add_cols
mean = dataset['s_and_p_500_pdiff'].mean()
std = dataset['s_and_p_500_pdiff'].std()
b = [-100., mean-std, mean+std, 100.]
dataset, add_cols = biner(dataset, ['s_and_p_500_pdiff'], [0.000001, 100.])
dataset.head()
c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning) c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning)
cpi | house_sales | gas_price | gold_price | usd_to_pound | usd_to_euro | unemployment | s_and_p_500 | volatility | s_and_p_500_pdiff | s_and_p_500_pdiff_b_0.00-100.00 | |
---|---|---|---|---|---|---|---|---|---|---|---|
1999-09-14 | 167.922727 | 844.818182 | 1.2536 | 256.70 | 0.621785 | 0.964873 | 4.159091 | 1336.29 | 23.77 | -0.010485 | 0 |
1999-09-21 | 167.990909 | 855.272727 | 1.2668 | 255.20 | 0.615034 | 0.959907 | 4.136364 | 1307.58 | 25.65 | -0.021485 | 0 |
1999-09-28 | 168.059091 | 865.727273 | 1.2606 | 288.25 | 0.607130 | 0.955362 | 4.113636 | 1282.20 | 26.06 | -0.019410 | 0 |
1999-10-05 | 168.128571 | 871.142857 | 1.2538 | 326.25 | 0.604444 | 0.934479 | 4.100000 | 1301.35 | 24.79 | 0.014935 | 1 |
1999-10-12 | 168.200000 | 869.000000 | 1.2464 | 323.50 | 0.604775 | 0.938612 | 4.100000 | 1313.04 | 22.84 | 0.008983 | 1 |
'''
advance_cols takes in a dataframe and an array of columns names and shifts all values up by one.
That is, the _adv column contains the value of the NEXT week.
'''
def advance_cols(df, cols):
for col in cols:
title = '%s_adv' % col
df[title] = float('nan')
for i in range(len(df)-1):
df[title][i] = df[col][i+1]
return df[:-1]
dataset = advance_cols(dataset, add_cols)
def clean_column_name(col):
return ' '.join(col.split('_'))
#NOTE THIS IS FOR NEURAL NETWORK USE AND SCALING!!!!!!!
#dataset['house_sales']=dataset['house_sales']/1000
#dataset['gold_price']=dataset['gold_price']/1000
#dataset['s_and_p_500']=dataset['gold_price']/1000
#dataset['s_and_p_500']=dataset['gold_price']/1000
dataset.head()
c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning) c:\Anaconda\lib\site-packages\pandas\core\config.py:570: DeprecationWarning: height has been deprecated. warnings.warn(d.msg, DeprecationWarning)
cpi | house_sales | gas_price | gold_price | usd_to_pound | usd_to_euro | unemployment | s_and_p_500 | volatility | s_and_p_500_pdiff | s_and_p_500_pdiff_b_0.00-100.00 | s_and_p_500_pdiff_b_0.00-100.00_adv | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1999-09-14 | 167.922727 | 844.818182 | 1.2536 | 256.70 | 0.621785 | 0.964873 | 4.159091 | 1336.29 | 23.77 | -0.010485 | 0 | 0 |
1999-09-21 | 167.990909 | 855.272727 | 1.2668 | 255.20 | 0.615034 | 0.959907 | 4.136364 | 1307.58 | 25.65 | -0.021485 | 0 | 0 |
1999-09-28 | 168.059091 | 865.727273 | 1.2606 | 288.25 | 0.607130 | 0.955362 | 4.113636 | 1282.20 | 26.06 | -0.019410 | 0 | 1 |
1999-10-05 | 168.128571 | 871.142857 | 1.2538 | 326.25 | 0.604444 | 0.934479 | 4.100000 | 1301.35 | 24.79 | 0.014935 | 1 | 1 |
1999-10-12 | 168.200000 | 869.000000 | 1.2464 | 323.50 | 0.604775 | 0.938612 | 4.100000 | 1313.04 | 22.84 | 0.008983 | 1 | 0 |
'''
takes a dataframe, a list of columns, and a target column and plots each column of the dataframe of them agaist the target column
overplots a liner fit
returns a sorted list column names by correlation coefficient and dict of the slopes of the liner regression line
'''
def scatter_plot(df, cols, target):
xaxis = target
yaxis = cols
ff = 5 * (len(yaxis)-1)
fig, axes=plt.subplots(nrows=len(yaxis), figsize=(5,ff))
Rs = []
slope = {}
for i, y in enumerate(yaxis):
axes[i].scatter(df[xaxis], df[y])
m, b, r, p, se = scipy.stats.linregress(df[xaxis], df[y])
lab = 'r^2 = %.2f \np = %.2f' % (r*r, p)
axes[i].plot(df[xaxis], np.array(df[xaxis]) * m + b, label=lab)
axes[i].set_xlabel(clean_column_name(xaxis))
axes[i].set_ylabel(clean_column_name(y))
axes[i].legend(frameon=False)
# axes[i].set_title(lab)
remove_border(axes[i])
Rs.append((y, (r*r)))
slope[y] = m
fig.tight_layout()
return sorted(Rs, key=operator.itemgetter(1), reverse=True), slope
r, slope = scatter_plot(dataset, data.keys(), 's_and_p_500')
# print 'correlation coefficient'
# for i in r:
# print '-' * 25
# print clean_column_name(i[0])
# print 's and p 500'
# print i[1]
'''
checks for colinerity between all variables
'''
def colin(df, pdiff=False):
cols = list(df.columns)
if pdiff == False:
cols = [c for c in list(df.columns) if c[-6:] != '_pdiff']
cols = combinations(cols, 2)
Rs = []
for col in cols:
m, b, r, p, se = scipy.stats.linregress(df[col[0]], df[col[1]])
Rs.append((col, (r*r)))
return sorted(Rs, key=operator.itemgetter(1), reverse=True)
print_num = 5
print 'top %d most collinear' % print_num
for i in colin(dataset)[:print_num]:
print '-' * 25
print clean_column_name(i[0][0])
print clean_column_name(i[0][1])
print i[1]
top 5 most collinear ------------------------- cpi gold price 0.856955415098 ------------------------- cpi gas price 0.824425129361 ------------------------- gas price gold price 0.71187244673 ------------------------- house sales gold price 0.684428266483 ------------------------- cpi usd to euro 0.68253891878
inc = dataset['s_and_p_500_pdiff'][dataset['s_and_p_500_pdiff'] > 0]
dec = dataset['s_and_p_500_pdiff'][dataset['s_and_p_500_pdiff'] <= 0]
mean = dataset['s_and_p_500_pdiff'].mean()
std = dataset['s_and_p_500_pdiff'].std()
in_one_std = dataset['s_and_p_500_pdiff'][(dataset['s_and_p_500_pdiff'] >= (mean - std)) & (dataset['s_and_p_500_pdiff'] <= (mean + std))]
out_one_std = dataset['s_and_p_500_pdiff'][(dataset['s_and_p_500_pdiff'] <= (mean - std)) | (dataset['s_and_p_500_pdiff'] >= (mean + std))]
int_inc = pd.concat([inc, out_one_std], axis=1, join='inner').ix[:,0]
int_dec = pd.concat([dec, out_one_std], axis=1, join='inner').ix[:,0]
extreme = dataset.sort_index(by=['s_and_p_500_pdiff'], ascending=False).s_and_p_500_pdiff
print_num = 5
extreme = pd.concat([extreme[:print_num], extreme[-print_num:]])
print 'the s&p 500 increased %d weeks (%.2f%%) and decreased %d weeks (%.2f%%) between %s and %s' % (len(inc), (float(len(inc))/len(dataset))*100, len(dec), (float(len(dec))/len(dataset))*100, dates[0].strftime('%B %d, %Y'), dates[1].strftime('%B %d, %Y'))
print 'over the whole period the s & p 500 incresaed by %.2f%%' % ((dataset['s_and_p_500'][-1] - dataset['s_and_p_500'][0])/dataset['s_and_p_500'][0]*100)
print 'the average weekly increase was %.2f%% with a standard deviation of %.2f%%' % ((inc.mean() * 100), inc.std()*100)
print 'the average weekly decrease was %.2f%% with a standard deviation of %.2f%%' % (abs(dec.mean() * 100), dec.std()*100)
print 'the average weekly change over the whole period was %.2f%% with a standard deviation of %.2f%%' % (mean*100, std*100)
print 'the weekly change was outside 1 standard deviation of the mean %d times' % len(out_one_std)
print 'of those %d of them were increses and had an average change of %.2f%% and a standard deviation of %.2f%%' % (len(int_inc), int_inc.mean()*100, int_inc.std()*100)
print 'and %d of them were decreases and had an average change of %.2f%% and a standard deviation of %.2f%%' % (len(int_dec), int_dec.mean()*100, int_dec.std()*100)
print
print 'the %d most extreme increses/decreces were' % print_num
for i in range(len(extreme)):
print '-' * 25
print extreme.index[i].strftime('%B %d, %Y')
print '%.2f%%' % (extreme[i]*100)
the s&p 500 increased 370 weeks (54.25%) and decreased 312 weeks (45.75%) between September 07, 1999 and October 09, 2012 over the whole period the s & p 500 incresaed by 8.19% the average weekly increase was 1.80% with a standard deviation of 1.67% the average weekly decrease was 2.03% with a standard deviation of 1.91% the average weekly change over the whole period was 0.04% with a standard deviation of 2.61% the weekly change was outside 1 standard deviation of the mean 166 times of those 77 of them were increses and had an average change of 4.30% and a standard deviation of 1.88% and 89 of them were decreases and had an average change of -4.30% and a standard deviation of 2.02% the 5 most extreme increses/decreces were ------------------------- July 30, 2002 13.17% ------------------------- October 15, 2002 10.36% ------------------------- March 21, 2000 9.91% ------------------------- March 18, 2003 8.21% ------------------------- March 17, 2009 8.13% ------------------------- January 20, 2009 -7.64% ------------------------- March 03, 2009 -9.93% ------------------------- November 11, 2008 -10.62% ------------------------- July 23, 2002 -11.46% ------------------------- October 07, 2008 -14.59%
plt.figure()
ax = dataset['s_and_p_500_pdiff'].hist(bins=np.sqrt(len(dataset)))
plt.xlabel('weekly percent change of the s&p 500')
plt.ylabel('frequency')
plt.title('weekly percent change of the s&p 500')
plt.vlines((mean+std), 0, 100, label=('+/-1 stadard deviation'))
plt.vlines((mean-std), 0, 100)
plt.legend(frameon=False)
remove_border(ax)
# mean = dataset['s_and_p_500_pdiff'].mean()
# std = dataset['s_and_p_500_pdiff'].std()
# rng = np.arange(dataset['s_and_p_500_pdiff'].min(), dataset['s_and_p_500_pdiff'].max(), 0.001)
# plt.plot(rng, stats.norm.pdf(rng, mean, std))
# one_std = len(dataset[(dataset['s_and_p_500_pdiff'] >= (mean - std)) & (dataset['s_and_p_500_pdiff'] <= (mean + std))])/float(len(dataset))
def filled_plt(df, col, begin_date=None, end_date=None):
if begin_date == None:
begin_date = df.index[0]
if end_date == None:
end_date = df.index[-1]
temp = df.truncate(before=begin_date, after=end_date)
for i in range(len(temp)-1):
x = temp.index[i:i+2]
y = temp[col][i:i+2]
c = brewer_rg[1]
if y[0] >= y[1]:
c = brewer_rg[0]
plt.fill_between(x, y, color=c)
ymin = min([0, temp[col].min()])
plt.ylim(ymin, temp[col].max())
plt.xticks(rotation='vertical')
plt.xlabel('date')
plt.ylabel('value')
plt.title('%s between %s and %s' % (clean_column_name(col), begin_date.strftime('%B %d, %Y'), end_date.strftime('%B %d, %Y')))
remove_border()
filled_plt(dataset, 's_and_p_500', begin_date=datetime.datetime(2007,6,7,0,0,0), end_date=datetime.datetime(2010,1,1,0,0,0))
def agreement_plt(df, target, var, dct, begin_date=None, end_date=None):
if begin_date == None:
begin_date = df.index[0]
if end_date == None:
end_date = df.index[-1]
temp = df.truncate(before=begin_date, after=end_date)
agree = 0.
total = 0.
title = ''
for v in var:
title += ' %s,' % clean_column_name(v)
for i in range(len(temp)-1):
x = temp.index[i:i+2]
y = temp[target][i:i+2]
c = dark2_colors[0]
t = []
for v in var:
if (dct[v] > 0):
if ((y[0] >= y[1]) & (temp[v][i] >= temp[v][i+1])) | ((y[0] <= y[1]) & (temp[v][i] <= temp[v][i+1])):
t.append(0)
else:
t.append(1)
else:
if ((y[0] >= y[1]) & (temp[v][i] <= temp[v][i+1])) | ((y[0] <= y[1]) & (temp[v][i] >= temp[v][i+1])):
t.append(0)
else:
t.append(1)
if(sum(t) == 0):
plt.fill_between(x, y, color=c)
agree += 1
total += 1
else:
plt.plot(x, y, color=dark2_colors[1])
total += 1
ymin = min([0, temp[target].min()])
plt.ylim(ymin, temp[target].max())
plt.xticks(rotation='vertical')
plt.xlabel('date')
plt.ylabel('value')
plt.title('trends of the%s and %s between %s and %s' % (title, clean_column_name(target), begin_date.strftime('%B %d, %Y'), end_date.strftime('%B %d, %Y')))
remove_border()
return (agree, total)
agree = agreement_plt(dataset, 's_and_p_500', ['volatility', 'gold_price'], slope, begin_date=datetime.datetime(2007,6,7,0,0,0), end_date=datetime.datetime(2010,1,1,0,0,0))
print 'they are in agreement %.2f%% of the time' % (agree[0]/agree[1]*100)
they are in agreement 45.86% of the time
#for neural network, data must be in an array without the index
'''
dataframe_to_array input:
-A dataframe with 'x' rows
-An array of string 'input column' names (length m)
-An array of string 'output column names (length n)
output:
dataframe_to_array outputs
-An array with x rows and (m+n) columns. The last columns are the output column values.
'''
datakeys = [x+"_pdiff" for x in data.keys()]
print datakeys
def dataframe_to_array(df, input_cols, output_cols):
return df[input_cols+output_cols].values
dataset = percentageDifference(dataset, dataset.keys())
data_array = dataframe_to_array(dataset, datakeys, ['s_and_p_500_pdiff_b_0.00-100.00_adv'])
#print data_array
print data_array
['cpi_pdiff', 'house_sales_pdiff', 'gas_price_pdiff', 'gold_price_pdiff', 'usd_to_pound_pdiff', 'usd_to_euro_pdiff', 'unemployment_pdiff', 's_and_p_500_pdiff', 'volatility_pdiff'] [[ 4.24844932e-04 -2.45982289e-03 -5.90205774e-03 ..., 8.98297921e-03 -7.86607503e-02 0.00000000e+00] [ 4.24664515e-04 -2.46588854e-03 -8.18356868e-03 ..., -3.93895083e-02 1.62872154e-01 1.00000000e+00] [ 4.24484252e-04 -2.47198418e-03 -4.85358356e-04 ..., 1.63241683e-02 -8.65963855e-02 1.00000000e+00] ..., [ 8.03527592e-04 -2.84432084e-03 3.73715353e-03 ..., 1.79692514e-02 -1.35892748e-01 0.00000000e+00] [ 8.02882454e-04 -2.85243408e-03 -1.18936808e-02 ..., -1.21494943e-02 8.81523272e-02 1.00000000e+00] [ 5.38107527e-04 1.63344048e-03 -2.19803224e-03 ..., 2.88570259e-03 1.81464679e-02 0.00000000e+00]]
from sklearn.cross_validation import train_test_split
from sklearn.naive_bayes import MultinomialNB
data_array_in=data_array[:,:-1]
data_array_out=data_array[:,-1:]
print data_array_in
#Converts the x by 1 by 1 array into a x by 1 array
data_array_out=data_array_out.reshape(data_array_out.shape[0])
xtrain, xtest, ytrain, ytest = train_test_split(data_array_in, data_array_out)
clf = MultinomialNB().fit(xtrain, ytrain)
clf.score(xtest, ytest)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-109-ae9def099cda> in <module>() 8 data_array_out=data_array_out.reshape(data_array_out.shape[0]) 9 xtrain, xtest, ytrain, ytest = train_test_split(data_array_in, data_array_out) ---> 10 clf = MultinomialNB().fit(xtrain, ytrain) 11 clf.score(xtest, ytest) c:\Anaconda\lib\site-packages\sklearn\naive_bayes.pyc in fit(self, X, y, sample_weight, class_prior) 338 self.feature_count_ = np.zeros((n_effective_classes, n_features), 339 dtype=np.float64) --> 340 self._count(X, Y) 341 self._update_feature_log_prob() 342 self._update_class_log_prior(class_prior=class_prior) c:\Anaconda\lib\site-packages\sklearn\naive_bayes.pyc in _count(self, X, Y) 440 """Count and smooth feature occurrences.""" 441 if np.any((X.data if issparse(X) else X) < 0): --> 442 raise ValueError("Input X must be non-negative") 443 self.feature_count_ += safe_sparse_dot(Y.T, X) 444 self.class_count_ += Y.sum(axis=0) ValueError: Input X must be non-negative
[[ 0.00042484 -0.00245982 -0.00590206 ..., 0. 0.00898298 -0.07866075] [ 0.00042466 -0.00246589 -0.00818357 ..., 0. -0.03938951 0.16287215] [ 0.00042448 -0.00247198 -0.00048536 ..., 0. 0.01632417 -0.08659639] ..., [ 0.00080353 -0.00284432 0.00373715 ..., -0.00291971 0.01796925 -0.13589275] [ 0.00080288 -0.00285243 -0.01189368 ..., -0.00292826 -0.01214949 0.08815233] [ 0.00053811 0.00163344 -0.00219803 ..., -0.00289855 0.0028857 0.01814647]]
#Neural Network Class
class Neural_Net:
#constructor initializes a new neural network with randomly selected weights and pre-specified height, and number of neurons per layer
def __init__(self,non,height):
#list to store the number of neurons in each layer of the network
self.num_of_neurons = non
#height of the network
self.L = height
#list to store number of weights in each layer of the network, indexed by layer, output neuron, input neuron
self.weights = numpy.zeros(shape=(10,10,10))
#delta_matrix: stores the gradient that is used in backpropagation
self.deltas = numpy.zeros(shape=(10,10))
#matrix that stores thresholded signals
self.signals = numpy.zeros(shape=(10,10))
#(tunable) learning_rate used in backpropagation
self.learning_rate = .01
#initialize weights to be between -2 and 2
for i in range(1,self.L+1):
for j in range(1,self.num_of_neurons[i]+1):
for k in range(self.num_of_neurons[i-1]+1):
self.weights[i][j][k] = (random.random()-.5)*2 #changed to adjust
#forward_pass computes the output of the neural network given an input
def forward_pass(self,x):
#(for convenience, we index neurons starting at 1 instead of zero)
self.signals[0][0] = -1
for i in range(1,self.num_of_neurons[0]+1):
self.signals[0][i] = x[i-1]
for i in range(1,self.L+1):
self.signals[i][0] = -1
for j in range(1,self.num_of_neurons[i]+1):
self.signals[i][j] = self.compute_signal(i,j)
return self.signals[self.L][1]
#tune_weights performs the backpropagation algorithm given a training example as input
def tune_weights(self,y):
self.deltas[self.L][1] = 2*(self.signals[self.L][1]-y)*(1-math.pow(self.signals[self.L][1],2))
for i in range(self.L-1,0,-1):
for j in range(1,self.num_of_neurons[i]+1):
self.deltas[i][j] = self.compute_delta(i,j)
for i in range(1,self.L+1):
for j in range(1,self.num_of_neurons[i]+1):
for k in range(self.num_of_neurons[i-1]+1):
self.weights[i][j][k] = self.weights[i][j][k]-self.learning_rate*self.signals[i-1][k]*self.deltas[i][j]
#compute_signal: computes the delta for a given neuron at a given level
def compute_signal(self,level,neuron):
s = 0
for i in range(self.num_of_neurons[level-1]+1):
s += self.weights[level][neuron][i]*self.signals[level-1][i]
return self.g(s)
#compute_delta: computes the signal s for a given neuron at a given level
def compute_delta(self,level,neuron):
s = 0
for j in range(1,self.num_of_neurons[level+1]+1):
s += self.weights[level+1][j][neuron]*self.deltas[level+1][j]
return (1-math.pow(self.signals[level][neuron],2))*s
#soft threshold function
def g(self,s):
#print s
return (math.exp(s)-math.exp(-s))/(math.exp(s)+math.exp(-s))
#Arbitrarily split the dataset into two halves for trianing/testing.
#s=int(len(data_array)/2)
#split=np.split(data_array,[s])
#print split
training=data_array[0:400]
testing=data_array[400:600]
training_rows=len(training)
training_columns=len(training[0])
testing_rows=len(testing)
testing_columns=len(testing[0])
#print training[0][9]
#print testing
#specify the number of neurons in each layer
num_of_neurons = [8,6,1]
#initialize a new neural network
network = Neural_Net(num_of_neurons,2)
#print network.weights
#store the training error and test error during each epoch
training_error = 0
test_error = 0
#store the training and test error for all epochs
train = numpy.zeros(shape = (1000))
test = numpy.zeros(shape = (1000))
#NOTES: EDITS MADE TO MAKE THE TRAINING FOR A DYNAMICALLY SIZED ARRAY.
'''
for epoch in range(1000):
training_error = 0
test_error = 0
#compute the test errors
for j in range(testing_rows):
test_error = test_error+math.pow(network.forward_pass(testing[j]) - testing[j][testing_columns-1], 2)
#compute the training errors, SEQUENTIALLY. In other words, we perform backpropagation for *every* example
#instead of all at once.
for i in range(training_rows):
training_error = training_error+math.pow(network.forward_pass(training[i])- training[i][training_columns-1], 2)
network.tune_weights(training[i][training_columns-1])
training_error = training_error/training_rows
test_error = test_error/testing_rows
train[epoch] = training_error
test[epoch] = test_error
'''
'\nfor epoch in range(1000):\n\ttraining_error = 0\n\ttest_error = 0\n\t#compute the test errors\n\tfor j in range(testing_rows):\n\t\ttest_error = test_error+math.pow(network.forward_pass(testing[j]) - testing[j][testing_columns-1], 2)\n\t#compute the training errors, SEQUENTIALLY. In other words, we perform backpropagation for *every* example\n\t#instead of all at once. \n\tfor i in range(training_rows):\n\t\ttraining_error = training_error+math.pow(network.forward_pass(training[i])- training[i][training_columns-1], 2)\n\t\tnetwork.tune_weights(training[i][training_columns-1])\t \n\ttraining_error = training_error/training_rows\n\ttest_error = test_error/testing_rows\n\ttrain[epoch] = training_error\n\ttest[epoch] = test_error\n'
for epoch in range(1000):
#compute the test errors
#compute the training errors, SEQUENTIALLY. In other words, we perform backpropagation for *every* example
#instead of all at once.
for i in range(training_rows):
network.forward_pass(training[i])
network.tune_weights(training[i][training_columns-1])
print network.weights
for j in range(testing_rows):
#print j
print np.array_str(testing[j])+ "==> " + np.array_str(network.forward_pass(testing[j]))
actual_results = []
nn_results=[]
for j in range(testing_rows):
actual_results.append(testing[j][testing_columns-1])
nn_results.append(network.forward_pass(testing[j]))
[[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [-0.03268989 0.94762176 -0.68406897 -1.36793 -1.30550739 -0.62603609 0.93452282 -1.01129081 -0.33885553 0. ] [ 0.00659289 0.28888343 -0.9675944 0.59054946 0.78545779 0.07077915 -0.45790114 0.85382152 -0.24602015 0. ] [ 0.02913704 0.32131391 0.02392506 1.99862013 1.86834524 -0.43166085 0.01242633 -0.23973267 0.99423859 0. ] [-0.016706 -0.65480787 -0.97097906 -0.61570291 0.59401388 0.30882397 0.647452 0.12249066 -0.94607753 0. ] [-0.0069197 -0.93671203 1.00371593 -0.54338136 -1.27678908 -0.87632541 0.63187123 -0.57320878 0.7891356 0. ] [ 0.0265672 0.88514303 -0.56198176 1.30197835 2.09542888 0.22348466 -0.46706785 0.01361728 0.83844873 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [-0.69037498 0.46226385 -0.24373801 -0.47025962 0.02202817 0.32831706 -0.49069944 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] [[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]]] [ 2.42932842e-04 -1.38598095e-02 -2.48981151e-02 -2.75584686e-02 8.33578114e-03 7.27359342e-03 0.00000000e+00 -2.47885300e-02 2.23037417e-01 1.00000000e+00]==> 0.715644857669 [ 0.00024287 -0.0140546 -0.01926468 0.00352328 -0.00552109 -0.00270673 0. 0.02726055 -0.22915417 0. ]==> 0.633628505548 [ 2.42814867e-04 -1.42549513e-02 -8.72286589e-03 -8.47198901e-03 -9.86448212e-03 -5.00617567e-03 0.00000000e+00 -2.66088544e-02 4.70038911e-01 1.00000000e+00]==> 0.662467314387 [ 0.00024276 -0.01446109 -0.00470209 0.01185436 -0.00545713 -0.00702398 0. 0.02142154 -0.19057702 0. ]==> 0.594315450852 [ 2.42697006e-04 -1.46732853e-02 1.05284471e-02 5.93381514e-03 -1.08535514e-03 -4.88255297e-03 0.00000000e+00 -9.67295573e-03 1.49117070e-01 1.00000000e+00]==> 0.595860773457 [ 0.00024264 -0.0148918 0.01208843 0.00302503 -0.0113563 -0.01104339 0. 0.02599131 -0.11041548 0. ]==> 0.581254895121 [ 0.00024258 -0.01511691 -0.02943117 0.02955591 -0.01053223 -0.00343061 0. -0.02473909 0.18682022 0. ]==> 0.624295318972 [ 2.42520429e-04 -1.53489435e-02 -2.48844166e-02 -2.34346393e-02 1.62267007e-02 1.29623076e-02 0.00000000e+00 -3.69083545e-02 2.67924528e-01 1.00000000e+00]==> 0.714162886214 [ 0.00073074 0.000597 -0.01526984 0.00374953 -0.00688779 -0.01070555 0.00202224 0.01473266 -0.08333333 0. ]==> 0.631569150789 [ 8.52192642e-04 4.64052504e-03 -1.79848474e-02 -2.83899888e-03 9.13463498e-03 8.75065530e-03 2.52270434e-03 -3.39741723e-02 2.83858998e-01 1.00000000e+00]==> 0.674986470326 [ 0.00085147 0.00461909 0.00144207 -0.0177568 0.01871988 0.01433665 0.00251636 0.01442651 -0.08778902 0. ]==> 0.653917081436 [ 8.50742649e-04 4.59785215e-03 -6.98394413e-03 1.75438596e-02 -1.60662634e-02 -1.35016705e-02 2.51004016e-03 -1.01995688e-02 4.15841584e-02 1.00000000e+00]==> 0.605481661664 [ 0.00085002 0.00457681 0.01522622 0.00974513 -0.00114445 0.002687 0.00250376 0.03983635 -0.1338403 0. ]==> 0.565716276539 [ 8.49297581e-04 4.55595691e-03 4.07084702e-03 4.56570156e-02 -5.65216777e-03 -1.08554900e-02 2.49750250e-03 -1.20382431e-02 1.09306409e-01 1.00000000e+00]==> 0.526403470452 [ 0.00084858 0.00453529 -0.00704175 0.01909833 0.01247827 -0.00654426 0.00249128 0.03281708 -0.19469727 0. ]==> 0.577109340886 [ 8.47857415e-04 4.51481828e-03 5.44412607e-03 1.39333984e-02 -9.22682974e-03 -1.59031799e-02 2.48508946e-03 -1.69103423e-03 -8.59950860e-02 1.00000000e+00]==> 0.584503383849 [ 0.00101963 -0.00156611 -0.00812197 0.01106225 -0.0112198 -0.01119725 0.00198314 0.01939086 -0.00591398 1. ]==> 0.604452893565 [ 0.00170787 -0.02584911 -0.0057463 -0.00944614 0.00261384 0.00991424 0. 0.01197442 -0.12817739 0. ]==> 0.644836414525 [ 0.00170496 -0.02653502 0.00209507 0.04521131 0.00150955 -0.0052451 0. -0.01700795 0.24193548 0. ]==> 0.525171465319 [ 0.00170206 -0.02725832 0.02112321 -0.00557926 -0.00450756 -0.00604607 0. -0.01231045 0.01948052 1. ]==> 0.598626333153 [ 0.00169916 -0.02802216 0.02379271 0.0349835 -0.00448909 -0.00904505 0. 0.00752177 0.03233709 0. ]==> 0.493405344703 [ 0.00112224 -0.01303822 0.04565202 0.04279337 -0.01480024 -0.00619456 0.00445324 -0.00702146 0.01518747 0. ]==> 0.449513609084 [ 0.00073871 -0.00254351 0.025061 -0.01730781 0.00260981 -0.00940338 0.00738916 -0.02579805 0.12669472 0. ]==> 0.621604124624 [ 0.00073816 -0.00255 -0.00321688 -0.01711476 0.0136414 -0.00191258 0.00733496 -0.02791938 0.03236515 0. ]==> 0.665336722364 [ 7.37618847e-04 -2.55651729e-03 -2.83999225e-03 4.25504971e-02 -5.74769271e-03 -1.18038776e-02 7.28155340e-03 -7.96693756e-03 5.62700965e-02 1.00000000e+00]==> 0.538066508253 [ 7.37075167e-04 -2.56306983e-03 -1.32694673e-02 -4.00850288e-02 2.98130285e-03 1.27875872e-02 7.22891566e-03 2.41977833e-02 -9.47488584e-02 1.00000000e+00]==> 0.706623437045 [ 0.00073653 -0.00256966 -0.0161375 0.02087947 0.01163657 -0.00119391 0.00717703 0.01015867 -0.00840689 0. ]==> 0.598738705778 [ 7.35990207e-04 -2.57627618e-03 -1.73356448e-03 -1.30151844e-02 8.69266190e-03 1.78053642e-02 7.12589074e-03 -1.53419281e-02 -4.02713014e-02 1.00000000e+00]==> 0.663328962124 [ 7.35448924e-04 -2.58293052e-03 6.67913438e-05 2.27106227e-02 1.78326115e-02 1.61969690e-03 7.07547170e-03 2.89179233e-02 -1.77120141e-01 0.00000000e+00]==> 0.558191615856 [ 0.00073491 -0.00258962 0.02324184 0.02983013 -0.0058982 -0.02192694 0.00702576 -0.0262482 0.22571122 0. ]==> 0.521441491429 [ 0.00052561 -0.01178836 0.01194439 0.04128782 0.01072799 -0.00107084 -0.00434783 -0.04635194 0.11364134 0. ]==> 0.539119257334 [ 0.00052534 -0.01192899 -0.01386739 0.03607214 0.0071129 -0.00654531 -0.00436681 -0.00664657 -0.08218639 0. ]==> 0.572859291896 [ 5.25062578e-04 -1.20730062e-02 -1.59591863e-02 -4.72506217e-02 3.21991805e-03 1.82934692e-02 -4.38596491e-03 -5.10156052e-02 3.28620394e-01 1.00000000e+00]==> 0.749655671472 [ 0.00052479 -0.01222054 -0.0105683 0.07598608 -0.01497806 -0.0099169 -0.00440529 0.0395269 -0.11899387 0. ]==> 0.457262034065 [ 5.95780719e-04 -1.19662740e-02 -9.40480989e-04 -4.07008086e-02 6.80019978e-03 -7.31700863e-03 -1.68563000e-03 -1.88357924e-02 3.36749634e-02 1.00000000e+00]==> 0.697800727568 [ 0.00070227 -0.01149564 0.0006724 0.03427929 0.01115409 0.01985726 0.00242718 0.00914233 -0.06763456 0. ]==> 0.556517392849 [ 7.01772908e-04 -1.16293304e-02 2.79532321e-02 -4.07497963e-03 -5.19818317e-03 -1.00068687e-02 2.42130751e-03 -5.93093427e-05 -2.81048234e-02 1.00000000e+00]==> 0.580177571441 [ 0.00070128 -0.01176616 0.02510132 0.01909438 -0.0049083 -0.01043366 0.00241546 0.02410326 -0.14419695 0. ]==> 0.522456244079 [ 0.00070079 -0.01190625 0.01217957 0.0511242 -0.00991026 -0.0241735 0.00240964 -0.03948483 0.1652968 0. ]==> 0.499731080504 [ 7.00298556e-04 -1.20497210e-02 1.95930196e-02 -1.27323657e-03 -1.45185768e-02 -1.13790511e-02 2.40384615e-03 -4.59770115e-03 3.29153605e-02 1.00000000e+00]==> 0.59263463253 [ 6.99808481e-04 -1.21966876e-02 1.30375680e-02 2.57521673e-02 6.24855124e-04 -2.33888117e-02 2.39808153e-03 7.64017719e-03 -2.16236722e-02 1.00000000e+00]==> 0.52707176484 [ 6.99319092e-04 -1.23472836e-02 -4.20860018e-03 -7.46706438e-02 1.67900488e-02 2.07787263e-02 2.39234450e-03 1.67200204e-02 -2.71423032e-03 1.00000000e+00]==> 0.74814505961 [ 0.00069883 -0.01250164 0.01016783 -0.03615752 -0.0052203 -0.0243107 0.00238663 0.01270519 -0.11819596 0. ]==> 0.654821621662 [ 0.00134488 -0.01356852 0.01364298 0.02675585 0.00087229 0.0050708 0.01818182 -0.00338642 -0.01410935 0. ]==> 0.540302530669 [ 0.00134308 -0.01375516 0.02075731 0.0116721 0.00881681 -0.00395602 0.01785714 -0.0227822 0.01878354 1. ]==> 0.561440460574 [ 0.00134128 -0.013947 0.03346226 -0.01180574 -0.01063721 -0.00387547 0.01754386 0.03110691 -0.08384548 1. ]==> 0.568704947583 [ 0.00133948 -0.01414427 0.02211511 -0.03774097 0.00366227 0.0132811 0.01724138 0.01090164 -0.03018687 1. ]==> 0.65266651679 [ 0.00177024 -0.00945497 0.0082663 -0.00902935 0.00721638 0.01185948 0.01198266 0.01964139 -0.10029644 0. ]==> 0.618450421753 [ 0.00205496 -0.00625261 0.02778695 -0.00113895 0.00925314 -0.00120202 0.00856898 -0.01073146 -0.01263042 1. ]==> 0.578855814727 [ 0.00205075 -0.00629195 0.02259222 0.03420753 -0.0006038 -0.00640561 0.00849618 0.00738397 -0.02224694 0. ]==> 0.501110615913 [ 0.00204655 -0.00633179 0.03261609 0.01846748 -0.01258373 -0.01243765 0.0084246 -0.01984576 0.11717861 0. ]==> 0.532810048767 [ 0.00204237 -0.00637213 0.01110322 -0.03058187 0.00351869 0.01410296 0.00835422 -0.00555816 0.0305499 0. ]==> 0.668924419237 [ 0.00203821 -0.006413 0.01479216 -0.01228364 0.00075573 -0.01403897 0.008285 -0.01394404 0.14525692 0. ]==> 0.618439598629 [ 0.00203406 -0.00645439 0.00835063 -0.00169587 0.00966525 0.02296571 0.00821693 -0.0055284 -0.08843831 0. ]==> 0.62427738831 [ 0.00202994 -0.00649632 0.00019601 0.00622877 -0.00918802 -0.01043439 0.00814996 -0.02712206 0.06105064 0. ]==> 0.617143722458 [ 0.00202582 -0.0065388 0.00406643 0.04614519 -0.01148263 -0.01319064 0.00808407 -0.02235427 0.05486173 0. ]==> 0.523788630785 [-0.00032358 -0.01914137 0.00365961 -0.00026896 0.00832222 0.00641911 0.01124438 -0.00872435 -0.02114165 0. ]==> 0.616229187029 [ -3.23686419e-04 -1.95149150e-02 -2.57669308e-03 5.64971751e-02 -5.52674092e-03 -1.44343866e-02 1.11193477e-02 -4.61568658e-02 2.32829374e-01 1.00000000e+00]==> 0.517154545754 [-0.00032379 -0.01990333 -0.01486645 -0.00891266 -0.0041159 0.003488 0.01099707 0.05110667 -0.25788367 0. ]==> 0.636260484573 [ -3.23896100e-04 -2.03075138e-02 -2.52832616e-02 -4.54779034e-02 3.08271007e-03 8.89108069e-03 1.08774474e-02 -1.08065779e-02 4.01322002e-02 1.00000000e+00]==> 0.734056040054 [ -1.12979154e-04 -1.28695047e-02 -1.88324873e-02 -4.81830417e-02 9.33714436e-03 9.16165152e-03 6.45624103e-03 1.71627612e-02 -4.03994553e-02 1.00000000e+00]==> 0.722877893338 [ 0.00020358 -0.00109517 -0.01826271 -0.08512443 0.02788443 0.03914383 0. 0.00366571 0.00141911 0. ]==> 0.787092490014 [ 2.03535244e-04 -1.09637101e-03 -1.74430860e-02 -2.19474498e-02 2.79682982e-02 1.91051872e-02 0.00000000e+00 -1.77575819e-02 5.19603212e-03 1.00000000e+00]==> 0.699277919549 [ 2.03493826e-04 -1.09757436e-03 -1.20675784e-02 2.37041719e-02 8.67470059e-03 -4.38562949e-03 0.00000000e+00 3.80519306e-03 -3.71240602e-02 1.00000000e+00]==> 0.592468194482 [-0.00022814 -0.00507437 -0.00282302 -0.00895338 0.02201462 0.00821459 0.00298063 0.00477385 0.07320644 0. ]==> 0.641390371663 [-0.00195495 -0.0210837 0.00321211 -0.00342679 0.02244925 0.02650738 0.01485884 -0.04153947 0.15825375 0. ]==> 0.643423723198 [-0.00195878 -0.0215378 0.03424323 -0.02500781 -0.01402373 0.00107191 0.01464129 -0.00890969 0.18963486 0. ]==> 0.617482752404 [-0.00196263 -0.02201189 -0.02906916 0.14459763 -0.02250748 -0.019124 0.01443001 -0.02091299 0.17887789 0. ]==> 0.338293145247 [-0.00196649 -0.02250731 -0.02658885 0.00504202 0.00575414 0.00178354 0.01422475 -0.01839727 0.10274356 0. ]==> 0.652725384422 [-0.0027925 -0.00837488 -0.05135465 -0.01700111 0.03691573 0.06229396 0.01428618 -0.14586406 0.36278243 1. ]==> 0.780998054379 [-0.00300643 -0.004752 -0.09182419 -0.03657499 0.02784839 0.01063493 0.01414927 0.00178674 0.02701192 0. ]==> 0.786198572642 [-0.00301549 -0.00477469 -0.0777162 -0.08063567 -0.0095209 0.00753478 0.01395187 -0.04304566 -0.03664067 0. ]==> 0.833881500835 [-0.00302461 -0.0047976 -0.08999441 -0.04609475 0.10141582 0.0682905 0.01375989 -0.01522433 0.26077951 1. ]==> 0.805922120562 [-0.00303379 -0.00482073 -0.09213759 -0.0147651 -0.02418202 -0.01629361 0.01357313 0.06936662 -0.28718638 0. ]==> 0.739360917787 [-0.00304302 -0.00484408 -0.07239513 0.01055858 0.02413728 -0.00238955 0.01339136 -0.10618941 0.28724073 0. ]==> 0.733764997734 [-0.00305231 -0.00486766 -0.07184537 -0.00707786 0.06243771 0.01455847 0.0132144 -0.04430725 0.10091146 0. ]==> 0.736708505078 [-0.00306165 -0.00489147 -0.07868369 0.08961303 -0.01053974 -0.00292465 0.01304206 -0.00201369 -0.09964518 0. ]==> 0.572762349064 [-0.00234318 -0.00864222 -0.04648683 -0.03738318 -0.01862121 -0.00195295 0.01330796 -0.01000711 0.03415435 1. ]==> 0.748734914175 [ 5.69641443e-04 -2.37543453e-02 -5.45678184e-02 -1.94174757e-03 3.16343821e-02 -8.75986784e-03 1.48456057e-02 4.69598615e-02 -6.46236901e-02 1.00000000e+00]==> 0.663519841404 [ 0.00056932 -0.02433234 -0.01963335 0.08106355 -0.01638828 -0.05004085 0.01462844 0.02758054 -0.11101681 0. ]==> 0.422669895519 [ 5.68993198e-04 -2.49391727e-02 -7.72107612e-03 1.25974805e-02 8.00855367e-03 -3.56930399e-02 1.44175317e-02 -5.47756193e-02 -1.40347527e-01 1.00000000e+00]==> 0.60101754889 [ 5.68669629e-04 -2.55770430e-02 -1.08206687e-02 3.08056872e-02 1.89826085e-02 -1.24176183e-02 1.42126208e-02 3.18365077e-02 -7.52998667e-02 1.00000000e+00]==> 0.536790075745 [ 0.00038329 -0.01036132 0.04719764 -0.02988506 0.00547086 0.0228439 0.01285734 0.04947004 -0.0737449 0. ]==> 0.589915013524 [ 0.00025982 0.00023248 0.05434272 -0.03376777 -0.03425472 0.02907776 0.01193317 -0.06730502 0.1221473 0. ]==> 0.657871319655 [ 2.59753313e-04 2.32428412e-04 2.70510965e-02 2.39117106e-02 2.33298001e-02 1.20852209e-02 1.17924528e-02 -7.63601326e-02 3.09221169e-01 1.00000000e+00]==> 0.565743167764 [ 2.59685859e-04 2.32374402e-04 1.95100802e-03 7.30538922e-02 6.78582306e-02 2.11118408e-02 1.16550117e-02 5.02843943e-02 -2.54192410e-01 0.00000000e+00]==> 0.419826945883 [ 0.00025962 0.00023232 0.02704457 0.00669643 -0.04201227 0.01623601 0.01152074 -0.00851356 0.0191716 0. ]==> 0.592272460256 [ 0.00025955 0.00023227 0.01832736 -0.00665188 -0.02962048 -0.01358594 0.01138952 -0.01353591 0.08383651 0. ]==> 0.612570008717 [ 0.00025948 0.00023221 0.0100331 0.07393973 0.03774116 0.01216078 0.01126126 -0.04592824 0.04263981 0. ]==> 0.462619131248 [ 0.00025942 0.00023216 -0.01996928 0.02857885 -0.0131012 -0.00175809 0.01113586 -0.02031248 -0.06514591 0. ]==> 0.610544733432 [ 2.59349112e-04 2.32104726e-04 1.11807732e-02 -6.56731498e-02 1.78468058e-02 1.65433324e-02 1.10132159e-02 -9.93481129e-02 1.19586722e-01 1.00000000e+00]==> 0.753075537996 [ 2.59281868e-04 2.32050866e-04 -3.10013434e-04 -1.43281968e-02 1.38371504e-02 -2.44546907e-03 1.08932462e-02 3.34180633e-02 -1.28804241e-01 1.00000000e+00]==> 0.628921351619 [ 2.59214658e-04 2.31997030e-04 -7.44262973e-03 9.32528799e-03 -1.24253916e-03 -2.26501111e-02 1.07758621e-02 8.13229572e-02 -8.04597701e-02 1.00000000e+00]==> 0.562576030421 [ 0.00025915 0.00023194 0.03041033 0.00951087 -0.03175506 -0.04907836 0.01066098 0.03598417 0.05220588 0. ]==> 0.509681286456 [ 2.59080343e-04 2.31889435e-04 3.30503335e-02 -1.10363392e-02 1.76316610e-02 2.81453393e-02 1.05485232e-02 -1.02342083e-02 2.81854181e-02 1.00000000e+00]==> 0.607694914072 [ 3.43367053e-04 2.10886160e-02 -2.15243127e-03 -4.27327164e-02 -4.04267842e-02 -1.88919528e-02 1.01853609e-02 2.21589983e-02 -8.49569551e-02 1.00000000e+00]==> 0.697130249453 [ 3.64330408e-04 2.57595773e-02 6.27512501e-03 1.79129940e-02 1.03498207e-02 2.12189494e-02 1.00200401e-02 3.18190178e-02 -6.73434018e-02 1.00000000e+00]==> 0.579791789564 [ 3.64197719e-04 2.51126851e-02 2.14362272e-03 -6.98324022e-03 -4.49792302e-04 1.62474764e-02 9.92063492e-03 1.01960784e-02 -1.40695514e-02 1.00000000e+00]==> 0.644661560535 [ 3.64065128e-04 2.44974874e-02 -1.06951872e-03 9.28270042e-03 4.66612158e-03 -1.36877838e-02 9.82318271e-03 5.97590815e-03 2.18093700e-02 1.00000000e+00]==> 0.600895914122 [ 0.00102024 0.01871478 0.02705859 0.00668896 -0.01969171 -0.00854836 0.00685566 0.05687825 -0.12094862 1. ]==> 0.543979575872 [ 0.00200266 0.01071879 0.0679492 0.01882614 -0.01699944 -0.02398623 0.00253036 0.0050343 -0.04676259 0. ]==> 0.453531997186 [ 1.99865835e-03 1.06051154e-02 3.56730855e-02 1.63043478e-03 -1.35643578e-03 1.02152434e-02 2.52397779e-03 -2.42197391e-04 -9.43396226e-02 1.00000000e+00]==> 0.574186376286 [ 0.00199467 0.01049383 0.0508097 0.02224634 -0.04393794 -0.03692297 0.00251762 0.00242256 0.06319444 1. ]==> 0.480886668808 [ 0.00157751 0.01040706 0.037182 0.03343949 -0.02253235 -0.01145312 0.00200904 0.03779948 -0.03233181 0. ]==> 0.472646117334 [ -7.51270123e-05 1.03878116e-02 3.52201258e-02 -2.15716487e-02 1.99739583e-02 1.64297571e-02 0.00000000e+00 -2.44511718e-03 -4.58994263e-02 0.00000000e+00]==> 0.621304894144 [ -7.51326568e-05 1.02810144e-02 1.60236938e-02 -1.65354331e-02 -2.63088186e-02 4.04104588e-05 0.00000000e+00 -3.23207029e-02 1.55995755e-01 0.00000000e+00]==> 0.657787152172 [ -7.51383021e-05 1.01763908e-02 2.01808805e-03 -1.76140913e-02 -3.99714841e-03 2.21273157e-03 0.00000000e+00 -1.84984155e-02 -6.42594859e-02 1.00000000e+00]==> 0.668891588356 [ -7.51439483e-05 1.00738751e-02 -1.68581232e-02 2.25482206e-02 -3.98028124e-03 -1.05470537e-02 0.00000000e+00 2.70584292e-02 -1.38325703e-01 0.00000000e+00]==> 0.59535409962 [ 4.76088563e-04 -3.54609929e-03 -1.54779970e-02 -1.62061637e-02 1.55486208e-02 6.70678843e-03 2.87081340e-03 -4.16503503e-02 1.70777989e-01 1.00000000e+00]==> 0.69159137118 [ 6.13605972e-04 -6.95062278e-03 -3.09032059e-02 -4.32082096e-03 4.03585912e-03 1.10127854e-03 3.57824427e-03 2.81602216e-02 -1.88978930e-01 1.00000000e+00]==> 0.664227699465 [ 6.13229690e-04 -6.99927208e-03 -1.74950298e-02 2.82072145e-02 -1.25222773e-02 -1.52573306e-02 3.56548609e-03 5.38064117e-02 -4.59632294e-02 1.00000000e+00]==> 0.56379771165 [ 6.12853870e-04 -7.04860720e-03 1.73209227e-02 7.64969665e-03 -2.80397906e-03 -4.46561308e-03 3.55281857e-03 2.62314316e-02 4.77586929e-02 1.00000000e+00]==> 0.564730473062 [ 0.00061248 -0.00709864 0.02434561 -0.00157068 -0.01943789 -0.00469898 0.00354024 0.02657153 -0.00479808 0. ]==> 0.577188407938 [ 0.0006121 -0.00714939 0.02718447 -0.00786576 0.00856757 0.00771189 0.00352775 -0.01123651 0.04419446 0. ]==> 0.602358950822 [ 6.11729169e-04 -7.20087563e-03 -3.70510397e-03 -8.19238901e-03 1.18487907e-02 2.28437733e-03 3.51535036e-03 -4.70659225e-03 7.31050404e-03 1.00000000e+00]==> 0.643803735531 [ 0.00061136 -0.0072531 -0.00387067 0.00985878 -0.00291847 -0.0122348 0.00350304 0.03873008 -0.04812834 0. ]==> 0.584938752424 [ 6.10981658e-04 -7.30609621e-03 -6.47619048e-03 2.37467018e-03 1.44767414e-02 1.39217517e-03 3.49080754e-03 -2.91439689e-02 1.69743178e-01 1.00000000e+00]==> 0.637914362494 [ 6.33769603e-04 5.88789449e-03 -8.51226994e-03 5.76467491e-02 -8.09167750e-03 -1.01264374e-03 4.63821892e-03 2.74037113e-02 -1.21097770e-01 1.00000000e+00]==> 0.508829577469 [ 6.33368193e-04 5.85343011e-03 -5.33683966e-03 -6.96864111e-03 -1.39522993e-02 -1.72696191e-02 4.61680517e-03 2.65655019e-02 -8.58704137e-02 1.00000000e+00]==> 0.629744700591 [ 0.00063297 0.00581937 -0.01189736 0.01829574 0.02406317 -0.00810472 0.00459559 0.01807853 -0.01451751 0. ]==> 0.58873743895 [ 0.00063257 0.0057857 -0.01912332 -0.02362786 0.02105043 0.0021988 0.00457457 -0.01031111 0.09142114 0. ]==> 0.694401771601 [ 5.37510464e-04 -5.46880853e-03 -8.26379974e-03 2.87370809e-02 -3.56508222e-03 3.16711709e-03 1.12254840e-03 -5.55340794e-03 2.02461294e-02 1.00000000e+00]==> 0.587192290609 [ 4.74151240e-04 -1.30208333e-02 1.36720330e-02 4.33717226e-02 7.94685085e-03 -8.70289342e-03 -1.16360251e-03 1.75117567e-02 -1.05447471e-01 1.00000000e+00]==> 0.489977424769 [ 0.00047393 -0.01319261 0.03511572 -0.0004697 -0.03143709 -0.01157358 -0.00116496 0.01665129 -0.09090909 0. ]==> 0.561144136398 [ 0.0004737 -0.01336898 0.03238242 -0.0218515 0.00192147 -0.00571066 -0.00116632 -0.02534233 0.18803828 0. ]==> 0.621854205126 [ 4.73477740e-04 -1.35501355e-02 3.88349515e-03 1.65745856e-02 -6.75953347e-03 1.71854029e-02 -1.16767865e-03 -1.69266793e-02 1.60289972e-01 1.00000000e+00]==> 0.605525602928 [ 4.73253665e-04 -1.37362637e-02 -1.10846600e-02 3.94612476e-02 -1.60065543e-02 -1.14299614e-02 -1.16904372e-03 4.55323749e-02 -2.07219715e-01 1.00000000e+00]==> 0.536442483625 [ 0.00047303 -0.01392758 -0.0103814 0.02909752 -0.00162686 -0.0018537 -0.00117041 0.015837 -0.01882662 0. ]==> 0.574063498442 [ 4.72806150e-04 -1.41242938e-02 2.28050171e-03 3.40181135e-02 1.00962832e-02 1.83622213e-03 -1.17178345e-03 -4.20599467e-03 -8.65684962e-02 1.00000000e+00]==> 0.550153824614 [ 0.00047258 -0.01432665 -0.00265453 0.01986755 0.00259439 -0.0066991 -0.00117316 0.00290327 0.07083537 0. ]==> 0.581698867905 [ 1.13013675e-04 -4.32312253e-03 -1.14068441e-03 -2.45077503e-02 3.66038010e-03 1.11598899e-02 -2.19587176e-03 -1.52589146e-02 8.07481752e-02 1.00000000e+00]==> 0.683220227303 [ 1.13000904e-04 -4.34189307e-03 -1.14198706e-02 -4.23019111e-02 1.15560554e-02 1.46107006e-02 -2.20070423e-03 1.46436617e-02 -9.24440692e-02 1.00000000e+00]==> 0.713172062249 [ 1.12988136e-04 -4.36082731e-03 -1.69426261e-03 -1.86098655e-02 7.69513357e-03 2.13428867e-02 -2.20555801e-03 9.10707355e-03 -9.11627907e-02 1.00000000e+00]==> 0.668762966613 [ 1.12975371e-04 -4.37992742e-03 1.00285428e-02 7.99634453e-03 1.02931308e-02 -3.36866298e-03 -2.21043324e-03 7.31650597e-03 2.40532242e-02 1.00000000e+00]==> 0.586108595508 [ 1.43476319e-05 -5.11743159e-03 2.42877874e-02 2.01722575e-02 -1.06640652e-02 2.69713839e-03 -1.32919805e-03 9.16355887e-03 -3.29835082e-02 0.00000000e+00]==> 0.545293608568 [ -1.33572918e-04 -6.22665006e-03 2.47557975e-02 2.44390136e-02 3.73448671e-03 -9.06903652e-03 0.00000000e+00 -2.63963679e-04 -5.68475452e-02 1.00000000e+00]==> 0.525545503619 [-0.00013359 -0.00626566 -0.00596667 -0.01626545 -0.01317964 0.00638544 0. 0.01233036 -0.03671233 0. ]==> 0.665598161507 [ -1.33608611e-04 -6.30517024e-03 -1.31761950e-02 -3.81393298e-02 9.69617774e-03 1.61045900e-02 0.00000000e+00 -5.04768611e-02 3.96473265e-01 1.00000000e+00]==> 0.731455798195 [ -1.01380865e-04 1.58629442e-03 -1.37230176e-02 2.13156085e-02 1.15717656e-02 1.93613568e-02 5.10204082e-04 1.02090334e-02 -1.25050916e-01 0.00000000e+00]==> 0.611627952776 [ 2.76043306e-05 3.32594235e-02 -6.01684717e-03 -4.12926391e-02 2.10027932e-02 1.52831760e-02 2.54971953e-03 -2.97284559e-02 2.10428305e-01 1.00000000e+00]==> 0.725769246103 [ 2.76035686e-05 3.21888412e-02 -9.76089588e-03 4.42415730e-02 -3.35294164e-03 4.28350720e-03 2.54323499e-03 2.27459552e-02 -1.44230769e-01 0.00000000e+00]==> 0.555932490037 [ 2.76028067e-05 3.11850312e-02 1.79567510e-02 -2.91414481e-03 1.29912885e-02 2.68193965e-04 2.53678336e-03 -2.46604620e-04 -3.95505618e-02 1.00000000e+00]==> 0.608941492486 [ 2.28016891e-06 2.89855072e-02 1.77901216e-02 3.59712230e-03 2.48755757e-02 1.44404823e-03 2.02429150e-03 2.16608807e-02 -1.08095461e-01 1.00000000e+00]==> 0.582270851604 [ -9.90071087e-05 2.32848705e-02 1.71841581e-02 3.58422939e-03 -1.70328426e-03 -3.76732296e-03 0.00000000e+00 1.97977305e-02 -5.98111228e-02 1.00000000e+00]==> 0.588652789708 [ -9.90169121e-05 2.27550228e-02 1.29785383e-02 -6.02678571e-03 5.44528846e-05 -6.15391751e-03 0.00000000e+00 1.66688588e-02 -1.28348214e-02 1.00000000e+00]==> 0.613795905263 [ -9.90267174e-05 2.22487519e-02 7.37241429e-03 -1.12283854e-02 6.84253991e-03 1.46969601e-02 0.00000000e+00 1.26869405e-02 -7.57490107e-02 0.00000000e+00]==> 0.644214021392 [ -9.90365247e-05 2.17645185e-02 -3.97896831e-03 8.17624347e-03 5.00238853e-03 5.95217965e-03 0.00000000e+00 -7.66498889e-04 4.77064220e-02 1.00000000e+00]==> 0.626680841602 [ -2.42066727e-05 -1.09875505e-02 1.02725068e-02 1.28407299e-02 -1.95048818e-02 -3.32291141e-03 -3.52360817e-03 1.37819939e-02 -5.25394046e-02 1.00000000e+00]==> 0.580931936705 [ 2.56870565e-05 -3.28744029e-02 9.17949442e-03 2.24644128e-02 -9.77951500e-03 -5.15817226e-03 -5.89344649e-03 6.60815174e-03 -1.84842884e-03 1.00000000e+00]==> 0.555599726728 [ 2.56863967e-05 -3.39918652e-02 -2.09907641e-04 -5.87339569e-03 6.33363508e-03 6.83546467e-03 -5.92838511e-03 8.24354798e-03 -2.90123457e-02 0.00000000e+00]==> 0.629434271355 [ 2.56857369e-05 -3.51879699e-02 3.49919519e-04 8.53391685e-03 -8.22749372e-03 8.55771575e-03 -5.96374046e-03 -1.94338826e-02 4.50095359e-01 0.00000000e+00]==> 0.618782124418 [ 2.56850772e-05 -3.64713217e-02 1.42017630e-02 2.77717509e-02 1.08748635e-02 8.33017240e-03 -5.99952004e-03 -8.54094331e-03 4.51556335e-02 0.00000000e+00]==> 0.544002436646 [ 2.56844175e-05 -3.78518279e-02 -8.96737256e-04 2.08993034e-02 2.57609097e-02 3.04227636e-02 -6.03573153e-03 -1.51755283e-02 1.87919463e-01 0.00000000e+00]==> 0.596857923246 [ 2.56837578e-05 -3.93409549e-02 -1.67080917e-02 4.96277916e-03 2.85012555e-02 4.19151362e-02 -6.07238280e-03 -3.02736656e-02 1.84675141e-01 0.00000000e+00]==> 0.662971791831 [ 2.56830982e-05 -4.09520476e-02 -2.59794973e-02 -2.09876543e-02 9.18459591e-04 -1.12407099e-02 -6.10948192e-03 -4.17291221e-02 3.15946349e-02 0.00000000e+00]==> 0.701511551295 [ 2.56824386e-05 -4.27007299e-02 -1.69405998e-02 2.54308533e-02 -2.36846288e-03 1.52278753e-02 -6.14703713e-03 -3.09116133e-03 2.68708466e-02 0.00000000e+00]==> 0.606039964919 [ 3.77517287e-04 -1.63934426e-02 -2.63987681e-03 2.31604837e-02 1.55423678e-03 2.83974637e-02 2.41779497e-03 -8.13478907e-03 -5.17726505e-02 1.00000000e+00]==> 0.600807392032 [ 0.00037737 -0.01666667 -0.0039703 -0.02003205 -0.01329826 -0.01899528 0.00241196 0.05012241 -0.23234421 0. ]==> 0.634795057722 [ 0.00037723 -0.01694915 0.01343471 0.01001635 -0.0116585 -0.01660372 0.00240616 -0.01786179 0.04561268 0. ]==> 0.580550793893 [ 0.00037709 -0.01724138 0.00182096 0.00060716 -0.01572754 0.00328663 0.00240038 -0.04936502 0.26173752 0. ]==> 0.645029014329 [ 3.47815518e-04 1.11642743e-03 -9.59720808e-03 -2.04288026e-02 -6.08868598e-03 -1.57606709e-02 9.57854406e-04 -1.26579847e-02 -1.31262819e-01 1.00000000e+00]==> 0.675349838401 [ 0.00032828 0.0135415 -0.0020555 -0.00351022 0.00824075 -0.00456028 0. 0.06544365 -0.17166948 0. ]==> 0.60578240001 [ 3.28171904e-04 1.33605784e-02 3.16316022e-03 -2.07210941e-02 -1.57694129e-02 -2.60389557e-02 0.00000000e+00 -1.08276882e-02 -2.56514658e-02 1.00000000e+00]==> 0.657914434252 [ 3.28064243e-04 1.31844269e-02 6.89301166e-03 2.11595430e-03 -1.18393454e-02 5.71943209e-04 0.00000000e+00 2.80208218e-02 -3.09235270e-02 1.00000000e+00]==> 0.607150973616 [ 3.27956652e-04 1.30128598e-02 -5.82623261e-04 0.00000000e+00 -1.99305245e-02 -1.22768175e-02 0.00000000e+00 5.94340300e-03 -2.41483398e-02 1.00000000e+00]==> 0.627634292125 [ 0.00032785 0.0128457 0.01122204 0.01076858 -0.00904954 -0.01293612 0. 0.00053549 -0.01148917 0. ]==> 0.586200642086 [ 0.00032774 0.01268278 -0.01390791 0.02465009 0.01933883 0.03691205 0. -0.02544021 0.08761734 0. ]==> 0.633363696363 [ 0.00032763 0.01252394 -0.01359252 -0.00672783 0.00478319 0.00741333 0. -0.03722518 0.12864776 0. ]==> 0.680477650595 [ 3.27526993e-04 1.23690338e-02 -6.51948437e-03 1.27257800e-02 1.46869123e-03 -2.03683833e-03 0.00000000e+00 -2.41474707e-03 -5.13474144e-02 1.00000000e+00]==> 0.615965118761 [ 6.29333041e-04 -1.25053903e-02 2.90827740e-03 1.11471423e-02 5.99806014e-03 -1.27320455e-02 0.00000000e+00 4.05115645e-02 -8.63723608e-02 1.00000000e+00]==> 0.566616640479 [ 7.04368081e-04 -1.89228530e-02 1.17480854e-02 4.81058328e-03 1.37294829e-03 8.73840840e-03 0.00000000e+00 2.67987984e-02 -9.41176471e-02 1.00000000e+00]==> 0.585051262242 [ 7.03872296e-04 -1.92878338e-02 -1.54332329e-03 2.03470975e-02 -1.30590175e-02 -2.22061802e-02 0.00000000e+00 1.66622068e-02 3.66419295e-02 1.00000000e+00]==> 0.56457897581 [ 7.03377208e-04 -1.96671710e-02 -5.74120418e-03 8.01564027e-03 -1.39654384e-02 -3.03821452e-02 0.00000000e+00 6.94870940e-03 1.11856823e-02 1.00000000e+00]==> 0.596607127825 [ 6.28387225e-04 -1.33303939e-02 1.76932188e-02 2.85104732e-02 7.42433980e-04 -1.92619206e-02 3.00751880e-03 1.13705672e-02 -3.71681416e-02 1.00000000e+00]==> 0.5124321972 [ 0.00051632 -0.00327708 0.02640576 0.01338865 -0.00718946 -0.01361748 0.00749625 0.00777084 -0.13005515 0. ]==> 0.540227485837 [ 5.16052940e-04 -3.28785139e-03 3.04748405e-03 1.80498697e-02 -4.93921578e-05 -9.75159891e-04 7.44047619e-03 -3.30834267e-03 8.98045431e-02 1.00000000e+00]==> 0.582740028204 [ 5.15786767e-04 -3.29869701e-03 -5.58185544e-03 -2.46755621e-02 1.36520294e-02 -3.30901885e-03 7.38552437e-03 1.69311262e-02 -1.98739699e-02 1.00000000e+00]==> 0.662499337623 [ 6.18990100e-04 3.51270441e-03 1.06579508e-03 1.79910045e-02 -2.03044169e-02 3.22486772e-03 3.53239136e-03 6.68837084e-03 6.67655786e-02 1.00000000e+00]==> 0.5946721512 [ 0.00103223 0.03069416 0.01866705 0.04289396 -0.00698436 -0.00276545 -0.0116225 0.01661402 -0.11543811 0. ]==> 0.513715193055 [ 0.00103116 0.02978009 0.00641026 -0.03742277 0.00338505 0.02317643 -0.01175917 -0.02889402 0.18343816 1. ]==> 0.716801040032 [ 0.0010301 0.02891888 -0.00581556 -0.00165047 0.00722715 -0.00164012 -0.0118991 0.00202828 -0.08635961 0. ]==> 0.648286768408 [ 1.02904147e-03 2.81060824e-02 1.53203343e-03 1.02865540e-02 2.57562192e-02 3.61939783e-02 -1.20423892e-02 -1.52448062e-04 1.41056714e-01 1.00000000e+00]==> 0.632828277597 [ 7.99496999e-04 -1.02341231e-02 2.98984842e-02 3.70909091e-02 -9.69934070e-03 -1.12618594e-02 -5.36323745e-03 3.65931134e-02 -2.35768904e-01 1.00000000e+00]==> 0.470837889863 [ 7.41782289e-04 -1.98300283e-02 6.07615447e-03 -1.47265077e-02 -3.75688382e-03 8.68156934e-03 -3.67647059e-03 1.45781410e-02 -2.11228460e-02 1.00000000e+00]==> 0.640494192256 [ 7.41232455e-04 -2.02312139e-02 5.23419675e-03 -1.35231317e-02 1.75029227e-02 6.49785922e-03 -3.69003690e-03 1.04784993e-02 -6.36002271e-02 1.00000000e+00]==> 0.633718212537 [ 7.40683437e-04 -2.06489676e-02 1.98931909e-02 7.09475709e-03 6.55840536e-03 2.40994302e-03 -3.70370370e-03 3.11653117e-03 6.24620982e-02 1.00000000e+00]==> 0.573579170291 [ 7.40135231e-04 -2.10843373e-02 5.95627700e-03 6.92537313e-03 -7.93480625e-03 -1.61620273e-02 -3.71747212e-03 9.28876211e-03 -7.99086758e-03 1.00000000e+00]==> 0.586885124999 [ 7.39587836e-04 -2.15384615e-02 5.92100982e-03 -1.74315190e-02 4.47803663e-04 3.37732603e-02 -3.73134328e-03 3.36954810e-03 -2.81933257e-02 1.00000000e+00]==> 0.66284995425 [ 0.00073904 -0.02201258 0.00426908 -0.00887038 -0.02090835 -0.03238716 -0.00374532 0.01611638 -0.06039076 0. ]==> 0.611417008821 [ 7.38495471e-04 -2.25080386e-02 9.66121345e-04 -3.12328767e-02 -6.71456282e-03 -1.97062081e-02 -3.75939850e-03 -2.96520517e-03 1.08380592e-01 1.00000000e+00]==> 0.668610624987 [ 7.37950498e-04 -2.30263158e-02 -3.21729618e-04 8.67269985e-03 6.01204326e-03 -1.34333234e-03 -3.77358491e-03 1.27093047e-02 2.27401933e-03 1.00000000e+00]==> 0.596552805851 [ 1.34965842e-03 2.77777778e-02 8.49639547e-03 1.23364486e-02 -1.42222632e-02 -8.42245279e-04 -2.77777778e-03 1.29857218e-02 -1.03233125e-01 1.00000000e+00]==> 0.596794449588 [ 0.00134784 0.02702703 0.00516977 0.01347858 0.00604139 0.00920976 -0.00278552 0.00259707 0.03542062 0. ]==> 0.604556664052 [ 0.00134603 0.02631579 0.02476348 0.01621425 -0.01371452 -0.01239108 -0.0027933 -0.00946529 0.27061698 0. ]==> 0.564135479013 [ 0.00134422 0.02564103 0.05657104 0.01434206 0.00551369 -0.0063677 -0.00280112 -0.00692544 0.01009615 1. ]==> 0.506651936804 [ 0.0007532 0.00724638 0.03489327 0.01449275 -0.00664314 -0.01259941 0.0024426 0.01185765 -0.0566397 0. ]==> 0.527086094791 [ 7.52633292e-04 7.19424460e-03 1.03700346e-02 -1.95121951e-02 1.03134522e-02 8.43890096e-04 2.43664717e-03 -3.02234797e-02 2.27043391e-01 1.00000000e+00]==> 0.658320818807 [ 7.52067261e-04 7.14285714e-03 7.85193494e-04 1.31485430e-02 -9.41958430e-03 -1.16275018e-02 2.43072436e-03 9.28331266e-03 -1.68996711e-01 1.00000000e+00]==> 0.592297147856 [ 7.51502081e-04 7.09219858e-03 1.25532392e-02 -8.06734479e-03 1.45033728e-02 2.15319101e-03 2.42483026e-03 1.98412392e-02 -1.01434933e-01 1.00000000e+00]==> 0.611400034541 [ 0.00069811 0.00286931 0.02540403 0.01449788 -0.00690059 -0.01114243 0.00196892 0.00999667 -0.05011013 0. ]==> 0.545071919808
def scorer(actual_res, nn_res):
correct=0
for j in range(len(results)):
if (actual_res[j]==0.0 and nn_res[j]<=.5) or (actual_res[j]==1.0 and nn_res[j]>.5):
correct+=1
return float(correct)/len(actual_res)
def average_scorer(actual_res, nn_res):
correct=0
average=float(sum(nn_res))/len(nn_res)
#print average
for j in range(len(results)):
if (actual_res[j]==0.0 and nn_res[j]<=average) or (actual_res[j]==1.0 and nn_res[j]>average):
correct+=1
return float(correct)/len(actual_res)
print scorer(actual_results, nn_results)
print average_scorer(actual_results, nn_results)
0.55 0.505
fig, ax = plt.subplots()
ax.plot(numpy.arange(1000), test, lw=2, label = 'test')
ax.plot(numpy.arange(1000), train, lw=2, label = 'train')
ax.legend(loc=0)
ax.set_xlabel('Epoch')
ax.set_ylabel('MSE')
<matplotlib.text.Text at 0x107c5e990>
nn_prep = []
for i in gsp.iterrows():
i = i[1]
nn_prep.append([[i['gold_pdiff'], i['fx_euro_pdiff']], [i['1_or_0']]])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-28-8fd44eb1f46d> in <module>() 1 nn_prep = [] ----> 2 for i in gsp.iterrows(): 3 i = i[1] 4 nn_prep.append([[i['gold_pdiff'], i['fx_euro_pdiff']], [i['1_or_0']]]) NameError: name 'gsp' is not defined
gsp_train = nn_prep[:-50]
gsp_test = nn_prep[-50:]
assert len(gsp) == len(gsp_test) + len(gsp_train)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-29-badf69662615> in <module>() 1 gsp_train = nn_prep[:-50] 2 gsp_test = nn_prep[-50:] ----> 3 assert len(gsp) == len(gsp_test) + len(gsp_train) NameError: name 'gsp' is not defined
# Back-Propagation Neural Networks
#
# Written in Python. See http://www.python.org/
# Placed in the public domain.
# Neil Schemenauer <nas@arctrix.com>
random.seed(0)
# calculate a random number where: a <= rand < b
def rand(a, b):
return (b-a)*random.random() + a
# Make a matrix (we could use NumPy to speed this up)
def makeMatrix(I, J, fill=0.0):
m = []
for i in range(I):
m.append([fill]*J)
return m
# our sigmoid function, tanh is a little nicer than the standard 1/(1+e^-x)
def sigmoid(x):
return math.tanh(x)
# derivative of our sigmoid function, in terms of the output (i.e. y)
def dsigmoid(y):
return 1.0 - y**2
class NN:
def __init__(self, ni, nh, no):
# number of input, hidden, and output nodes
self.ni = ni + 1 # +1 for bias node
self.nh = nh
self.no = no
# activations for nodes
self.ai = [1.0]*self.ni
self.ah = [1.0]*self.nh
self.ao = [1.0]*self.no
# create weights
self.wi = makeMatrix(self.ni, self.nh)
self.wo = makeMatrix(self.nh, self.no)
# set them to random vaules
for i in range(self.ni):
for j in range(self.nh):
self.wi[i][j] = rand(-0.2, 0.2)
for j in range(self.nh):
for k in range(self.no):
self.wo[j][k] = rand(-2.0, 2.0)
# last change in weights for momentum
self.ci = makeMatrix(self.ni, self.nh)
self.co = makeMatrix(self.nh, self.no)
def update(self, inputs):
if len(inputs) != self.ni-1:
raise ValueError, 'wrong number of inputs'
# input activations
for i in range(self.ni-1):
#self.ai[i] = sigmoid(inputs[i])
self.ai[i] = inputs[i]
# hidden activations
for j in range(self.nh):
summ = 0.0
for i in range(self.ni):
summ = summ + self.ai[i] * self.wi[i][j]
self.ah[j] = sigmoid(summ)
# output activations
for k in range(self.no):
summ = 0.0
for j in range(self.nh):
summ = summ + self.ah[j] * self.wo[j][k]
self.ao[k] = sigmoid(summ)
return self.ao[:]
def backPropagate(self, targets, N, M):
if len(targets) != self.no:
raise ValueError, 'wrong number of target values'
# calculate error terms for output
output_deltas = [0.0] * self.no
for k in range(self.no):
error = targets[k]-self.ao[k]
output_deltas[k] = dsigmoid(self.ao[k]) * error
# calculate error terms for hidden
hidden_deltas = [0.0] * self.nh
for j in range(self.nh):
error = 0.0
for k in range(self.no):
error = error + output_deltas[k]*self.wo[j][k]
hidden_deltas[j] = dsigmoid(self.ah[j]) * error
# update output weights
for j in range(self.nh):
for k in range(self.no):
change = output_deltas[k]*self.ah[j]
self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k]
self.co[j][k] = change
#print N*change, M*self.co[j][k]
# update input weights
for i in range(self.ni):
for j in range(self.nh):
change = hidden_deltas[j]*self.ai[i]
self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j]
self.ci[i][j] = change
# calculate error
error = 0.0
for k in range(len(targets)):
error = error + 0.5*(targets[k]-self.ao[k])**2
return error
def test(self, patterns):
for p in patterns:
print p[0], '->', self.update(p[0])
def weights(self):
print 'Input weights:'
for i in range(self.ni):
print self.wi[i]
print
print 'Output weights:'
for j in range(self.nh):
print self.wo[j]
def train(self, patterns, iterations=1000, N=0.5, M=0.1):
# N: learning rate
# M: momentum factor
for i in xrange(iterations):
error = 0.0
for p in patterns:
inputs = p[0]
targets = p[1]
self.update(inputs)
error = error + self.backPropagate(targets, N, M)
if i % 100 == 0:
print 'error %-14f' % error
def demo():
# create a network with two input, two hidden, and one output nodes
n = NN(2, 2, 1)
# train it with some patterns
n.train(gsp_train)
# test it
n.test(gsp_test)
demo()
error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000 error 0.000000