我正在学习Windows上的python 2.7.13中的类构造。我收到错误:Sales实例没有属性__float_
任何提示如何使我的代码工作?如何避免这种错误? 以下是我的代码
class Sales():
# Sales predicts
def __init__(self, df):
# Each sales dataset has descriptive statistics.
self.desc = df.describe()
self.rows = len(df)
self.cols = len(df.columns)
self.counts = df['TARGET'].value_counts()
self.plot = sns.countplot(x = 'TARGET', data = df, palette='hls')
def sales_prob(self, df1, df2):
"""
Function that computes logistic regression given a training and testing dataframes
:param df1: historical dataframe (training)
:param df2: monthly dataframe (testing)
:return: df_out : dataframe containing predictions for the target 2
"""
X_train0 = list(df1.loc[:, df1.columns != 'TARGET'])
X_train = df1[X_train0]
y_train = df1[['TARGET']]
X_test0 = list(df2.loc[:, df2.columns != 'TARGET'])
X_test = df2[X_test0]
y_test = df2[['TARGET']]
classifier = LogisticRegression(random_state=0)
classifier.fit(self, X_train, y_train.values.ravel())
y_pred2 =pd.DataFrame(classifier.predict_proba(self, X_test))
y_pred2['prediction2'] = y_pred2[[2]]
df_out = pd.merge(df2, y_pred2[['prediction2']], how = 'left', left_index=True, right_index=True)
return (df_out)
# Example to use Sales class
venta = Sales(data1)
venta.desc
venta.cols
proba = venta.sales_prob(data1, data2)
这是我的追溯
Traceback(最近一次调用最后一次):文件 “C:\文档\ Anaconda2 \ LIB \站点包\ IPython的\核心\ interactiveshell.py” 第2881行,在run_code中 exec(code_obj,self.user_global_ns,self.user_ns)文件“”,第1行,in proba = venta.sales_prob(data1,data2)文件“”,第29行,在sales_prob中 classifier.fit(self,X_train,y_train.values.ravel())文件“C:\ Documents \ Anaconda2 \ Lib \ site-packages \ sklearn \ linear_model \ logistic.py”, 第1173行,合适 order =“C”)文件“C:\ Documents \ Anaconda2 \ Lib \ site-packages \ sklearn \ utils \ validation.py”, 第521行,在check_X_y中 ensure_min_features,warn_on_dtype,estimator)文件“C:\ Documents \ Anaconda2 \ Lib \ site-packages \ sklearn \ utils \ validation.py”, 第382行,在check_array中 array = np.array(array,dtype = dtype,order = order,copy = copy)AttributeError:Sales实例没有属性' float ' traceback.print_tb