使用Value Error预测数据的Python代码错误

时间:2018-09-12 13:36:19

标签: python

我正在尝试使用您的代码使用python进行时间序列预测,并且我的stukc错误为"ValueError: Found array with 0 sample(s) (shape=(0,)) while a minimum of 1 is required.",请您帮我解决这个问题。

附加了我的.py文件和示例数据。另外,如何使用SVM或ANN模型而不是Python中的线性回归。

{from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
import numpy as np
import pandas as pd


#MAPE calculation function
def mape(y_pred,y_true):

    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100


#Loading the data

data = pd.read_csv('August.csv',header=0,index_col=0).sort_index()

x_data = []
y_data = []

###data.head()


# Creates a feature matrix with values from previous 6 months
for d in range(6,data.shape[0]):

    x = data.iloc[d-6:d].values.ravel()
    y = data.iloc[d].values[0]

##print(y)

    x_data.append(x)
    y_data.append(y)


x_data = np.array(x_data)
y_data = np.array(y_data)

##print(x_data)


#Lists to store the predictions of the models
y_pred = []
y_pred_last = []
y_pred_ma = []
y_true = []



#Iterate over the time series creating a new model each month
end = y_data.shape[0]
for i in range(8,end):

    x_train = x_data[:i,:]
    y_train = y_data[:i]

    x_test = x_data[i,:]
    y_test = y_data[i]



    model = LinearRegression(normalize=True)
    model.fit(x_train,y_train)

    y_pred.append(model.predict(x_test.reshape(1, -1))[0])
    y_pred_last.append(x_test[-1])
    y_pred_ma.append(x_test.mean())
    y_true.append(y_test)


#Transforms the lists into numpy arrays
y_pred = np.array(y_pred)
y_pred_last = np.array(y_pred_last)
y_pred_ma = np.array(y_pred_ma)
y_true = np.array(y_true)



#Print errors
print ('\nMean Absolute Percentage Error')
print (('MAPE Linear Regression'), mape(y_pred,y_true))
print (('MAPE Last Value Benchmark'), mape(y_pred_last,y_true))
print (('MAPE Moving Average Benchmark'), mape(y_pred_ma,y_true))


print ('\nMean Absolute Error')
print (('MAE Linear Regression'), mean_absolute_error(y_pred,y_true))
print (('MAE Last Value Benchmark'), mean_absolute_error(y_pred_last,y_true))
print (('MAE Moving Average Benchmark'), mean_absolute_error(y_pred_ma,y_true))



from matplotlib import pyplot as plt
plt.title('Prime Rate - 2005 to 2014')
plt.ylabel('Prime Rate')
plt.xlabel(u'Periods (Months)')
reg_val, = plt.plot(y_pred,color='b',label=u'Linear Regression')
true_val, = plt.plot(y_true,color='g', label='True Values')
plt.xlim([0,85])
plt.legend(handles=[true_val,reg_val])
plt.show() 


        sample data
        Month   Entity_Name Entityid    value
        18-Jan  ABC 44  20,000
        18-Feb  ABC 44  120,000
        18-Mar  ABC 44  100,000
        18-Apr  ABC 44  100,000
        18-May  ABC 44  100,000
        18-Jun  ABC 44  100,000
        18-Jul  ABC 44  100,000
        18-Aug  ABC 44  100,000}

0 个答案:

没有答案