Question: I am new at Python and running the code below. I get an index error (shown at the buttom of the code below) and cant
I am new at Python and running the code below. I get an index error (shown at the buttom of the code below) and cant seem to figure out what i am doing wrong: Any ideas as how to fix will be appreciated.
===========================================================
import numpy as np import matplotlib.pyplot as plt from numpy.linalg import norm,inv import csv import numpy
# Load CSV (using python) Xtest = 'X_test.csv' Ytest = 'Y_test.csv' Xtrain = 'X_train.csv' Ytrain = 'Y_train.csv'
Xtraindata = numpy.genfromtxt(Xtrain,delimiter=',') Xtestdata = numpy.genfromtxt(Xtest,delimiter=',') Ytraindata = numpy.genfromtxt(Ytrain,delimiter=',') Ytestdata = numpy.genfromtxt(Ytest,delimiter=',') print(Xtraindata.shape) print(Xtestdata.shape) print(Ytestdata.shape) print(Ytraindata.shape)
def KN(b): rows = Xtraindata.shape[0] print (rows) matrix = numpy.zeros((350,350)) print(matrix.shape) for i in range(rows): for j in range(rows): val = numpy.exp((-1/b)*(norm(Xtraindata[i]-Xtraindata[j])**2)) matrix[i][j]=val return matrix
#calculating K(X, Dn) def K(b): rows = Xtraindata.shape[0] print (rows) matrix = numpy.zeros((350,350)) print(matrix.shape) for i in range(rows): for j in range(rows): val = numpy.exp((-1/b)*(norm(Xtraindata[i]-Xtestdata[j])**2)) matrix[i][j]=val return matrix
# Loping KN over b and sigma squared.
for b in [5,7,9,11,13,15]: for sigma in [.1,.2,.3,.4,.5,.6,.7,.8,.9,1]: kxdn=K(b) kn =KN(b) ypredict = MU(kxdn,sigma,kn)
350 (350, 350)
--------------------------------------------------------------------------- IndexError Traceback (most recent call last)in () 3 for b in [5,7,9,11,13,15]: 4 for sigma in [.1,.2,.3,.4,.5,.6,.7,.8,.9,1]: ----> 5 kxdn=K(b) 6 kn =KN(b) 7 ypredict = MU(kxdn,sigma,kn) in K(b) 7 for i in range(rows): 8 for j in range(rows): ----> 9 val = numpy.exp((-1/b)*(norm(Xtraindata[i]-Xtestdata[j])**2)) 10 matrix[i][j]=val 11 return matrix IndexError: index 42 is out of bounds for axis 0 with size 42
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
