代码有效,但插入方法内部时无效
Posted
技术标签:
【中文标题】代码有效,但插入方法内部时无效【英文标题】:Code works, but not when inserted inside of a method 【发布时间】:2019-03-14 15:36:55 【问题描述】:我正在做一些简单的机器学习项目,我有一个简单的测试场景来测试我的代码:
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】:这是因为您在accept_value()
中调用new_model.model_data
。它没有正确访问您正在使用的实际实例 (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)
【讨论】:
我现在很惭愧。当我在实际课程之外处理 accept_value 时,我把它复制错了。非常感谢楼主! 我想我可能已经犯了大约一千次这样的错误。 @Chris HA! - 给我五千次。以上是关于代码有效,但插入方法内部时无效的主要内容,如果未能解决你的问题,请参考以下文章