Python 代码可以工作,但插入方法内部时不能工作
我正在做一些简单的ML项目,我有一个简单的测试场景来测试我的代码:Python 代码可以工作,但插入方法内部时不能工作,python,pandas,scikit-learn,linear-regression,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)
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.
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
14 plt.散射(x=df['x'],y=df['y'])
15
--->16模拟()
在simulate()中
10 x_值,y_值=df.loc[i]
11新的_模型。接受_值(x_值,y_值)
--->12新的_模型。再培训()
13打印(新款)
14 plt.散射(x=df['x'],y=df['y'])
再培训(自我)
10
11 def再培训(自我):
--->12 self.model.fit(self.model_数据['X']],self.model_数据['Y']))
13
14 def选择_值(自身):
/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py in fit(自身、X、y、样本重量)
480个n_作业=自我n_作业
481 X,y=check_X_y(X,y,accept_sparse=['csr','csc','coo'],
-->482 y_数值=真,多输出=真)
483
484如果样本重量不是无且np.至少1d(样本重量)。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,sure_2d,allow_nd,multi_output,sure_min_samples,sure_min_features,y_numeric,warn_on_dtype,assister)
571 X=检查数组(X,接受稀疏,数据类型,顺序,复制,强制所有有限,
572确保2d,允许nd,确保最小样本,
-->573确保功能、警告(数据类型、估计器)
574如果多输出:
575 y=检查数组(y,'csr',强制所有有限=真,确保2d=假,
/检查数组中的anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py(数组、接受稀疏、数据类型、顺序、复制、强制所有有限、确保2d、允许nd、确保最小样本、确保最小特征、警告数据类型、估计器)
460“至少需要%d个%s。”
461%(n_样本、形状报告、确保最小样本、,
-->462(上下文)
463
464如果确保_min_features>0且array.ndim==2:
ValueError:找到具有0个样本(形状=(0,1))的数组,但至少需要1个样本。
在方法内部时,它似乎不接受这些值。我不知道为什么会出现这样的错误,有人能帮我吗?这是因为你在
接受值()
中调用new\u model.model\u data
。它无法正确访问您正在使用的实际实例(self
)。当它不在函数中时,它可以正常工作,因为它可以访问ipython工作区中的新模型
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_值时,我把它复制错了。非常感谢,先生!我想我可能犯了一千次同样的错误。@chrisha!-给我五千次。