代码有效,但是当插入方法内部时无效

时间:2018-10-09 19:31:28

标签: python pandas scikit-learn linear-regression

我正在做一些简单的ML项目,并且有一个简单的测试方案来测试我的代码:

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from numpy.random import randn
import random
import matplotlib.pyplot as plt

class Model():
    def __init__(self, model_name):
        self.model_name = model_name
        self.model = LinearRegression()
        self.model_data = pd.DataFrame(columns=['X','Y'])


    def retrain(self):
        self.model.fit(self.model_data[['X']],self.model_data['Y'])

    def choose_value(self):
        y_intercept = self.model.intercept_
        pass


    def accept_value(self,x_value,y_value):
        temp_df = pd.DataFrame(data=[[x_value,y_value]],columns=['X','Y'])
        new_model.model_data = new_model.model_data.append(temp_df)

    def __str__(self):
        return f'The formula for the line is y = {list(self.model.coef_)[0]}x + {self.model.intercept_}'

情况如下:

df = pd.DataFrame(data = 5*randn(50,1),columns=['X'])
new_list = []
for i in df['X']:
    new_list.append(-6.5*i + 100 + random.normalvariate(0,10))
df['Y'] = new_list

new_model = Model('1')
for i in range(len(df)):
    x_value, y_value = df.loc[i]
    new_model.accept_value(x_value,y_value)
    new_model.retrain()
print(new_model)
plt.scatter(x=df['X'],y=df['Y'])

当我执行场景时,一切工作正常,但是当我在这样的方法中复制相同的代码时:

def simulate():
    df = pd.DataFrame(data = 5*randn(50,1),columns=['X'])
    new_list = []
    for i in df['X']:
        new_list.append(-6.5*i + 100 + random.normalvariate(0,10))
    df['Y'] = new_list

    new_model = Model('1')
    for i in range(len(df)):
        x_value, y_value = df.loc[i]
        new_model.accept_value(x_value,y_value)
        new_model.retrain()
    print(new_model)
    plt.scatter(x=df['X'],y=df['Y'])  
simulate()

然后我收到以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-250-2d4e86d2a592> in <module>()
     14     plt.scatter(x=df['X'],y=df['Y'])
     15 
---> 16 simulate()

<ipython-input-250-2d4e86d2a592> in simulate()
     10         x_value, y_value = df.loc[i]
     11         new_model.accept_value(x_value,y_value)
---> 12         new_model.retrain()
     13     print(new_model)
     14     plt.scatter(x=df['X'],y=df['Y'])

<ipython-input-245-63f3d0a40d67> in retrain(self)
     10 
     11     def retrain(self):
---> 12         self.model.fit(self.model_data[['X']],self.model_data['Y'])
     13 
     14     def choose_value(self):

/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py in fit(self, X, y, sample_weight)
    480         n_jobs_ = self.n_jobs
    481         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482                          y_numeric=True, multi_output=True)
    483 
    484         if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    571     X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
    572                     ensure_2d, allow_nd, ensure_min_samples,
--> 573                     ensure_min_features, warn_on_dtype, estimator)
    574     if multi_output:
    575         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    460                              " minimum of %d is required%s."
    461                              % (n_samples, shape_repr, ensure_min_samples,
--> 462                                 context))
    463 
    464     if ensure_min_features > 0 and array.ndim == 2:

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

似乎在方法内部时不接受值。我不知道为什么会收到这样的错误,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:5)

这是因为您正在new_model.model_data中呼叫accept_value()。它无法正确访问您正在使用的实际实例(self)。当它不在函数中时,它可以正常工作,因为它可以访问ipython工作区中的new_model。

class Model():
    def __init__(self, model_name):
        self.model_name = model_name
        self.model = LinearRegression()
        self.model_data = pd.DataFrame(columns=['X','Y'])
    def retrain(self):
        self.model.fit(self.model_data[['X']],self.model_data['Y'])
    def choose_value(self):
        y_intercept = self.model.intercept_
        pass
    def accept_value(self,x_value,y_value):
        temp_df = pd.DataFrame(data=[[x_value,y_value]],columns=['X','Y'])
        self.model_data = self.model_data.append(temp_df)