# Value error array with 0 features in linear regression scikit

My input and output data are written in an 6xn row-column excel file,thatI read them using pandas

using this code :

from sklearn import linear_model
import os
import pandas as pd
import numpy as np
import openpyxl as pyx
import numpy as np
import matplotlib.pyplot as plt

path = r"E:\"
os.chdir( path )

le= 20;lg=len(data1)
x_train=[];x_t=[];y_train=[];y_t=[];x_test=[];x_ts=[];y_test=[];y_ts=[];
for i in range(le):
x_t = data1.iloc[i,:]
x_train.append(x_t)
y_t = data2.iloc[i,:]
y_train.append(y_t)
if i > le :
x_ts = data1.iloc[lg-i,:]
x_test.append(x_ts)
y_ts = data2.iloc[lg-i,:]
y_test.append(y_ts)

ols = linear_model.LinearRegression()
model = ols.fit(x_train, y_train)

print (model.predict(x_test)[0:5])


I get this error :

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "E:/Scikit-Learn.py", line 46, in print (model.predict(x_test)[0:5])

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 268, in predict return self._decision_function(X)

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 251, in _decision_function X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 424, in check_array context))

ValueError: Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.

• You should spend more time reading your error messages and do some more debugging work by printing out the values of your variables for instance, because this is an error you should be able to catch by yourself. – Valentin Calomme Jul 12 '18 at 12:05
• I actually did it, that's a pity I didn't delete the question before your negative vote – FabioSpaghetti Jul 12 '18 at 12:06

The error says that the array you feed into predict has shape=(1,0) meaning that it must be an empty iterable.
And by looking at your code, it's easy to see why. x_test starts of as empty, and in your code, the only way to add things to it is if i > le. But since your loop is defined as for i in range(le), by definition i will never be greater than le since the last value of range is le-1