我很困惑:
class lin_reg:
def __init__(self):
''' Executes the program '''
Indep_Array, Dep_Array = self.Prob_Def()
Total_Array = Indep_Array.append(Dep_Array)
print Indep_Array, Dep_Array, Total_Array
NumArray = len(Total_Array)
def Prob_Def(self):
Analy_Type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
Num_IndepVar = eval(raw_input('Number of Independent Variables: '))
Indep_Array = []
for IndepVar in range(Num_IndepVar):
ArrayInput = eval(raw_input('Enter the array: '))
Indep_Array.append(ArrayInput)
Dep_Array = eval(raw_input('Enter the dependent array: '))
return Indep_Array, Dep_Array
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到如下输出:
obs=lin_reg.lin_reg()
Type of Regression(linear-n,nonlinear-nl): nl
Number of Independent Variables: 3
Enter the array: [1,2,3]
Enter the array: [2,3,4]
Enter the array: [3,4,5]
Enter the dependent array: [5,6,7]
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [5, 6, 7]] [5, 6, 7] None
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
obs=lin_reg.lin_reg()
File "C:\Python27\DataAnalysis\lin_reg.py", line 13, in __init__
NumArray=len(Total_Array)
TypeError: object of type 'NoneType' has no len()
Run Code Online (Sandbox Code Playgroud)
如何Dep_Array自动附加依赖数组Indep_Array以及为什么Total_Array返回None?
我期待上面的输入看到这样的输出:[[1,2,3],[2,3,4],[3,4,5]] [5,6,7] [[1,2, 3],[2,3,4],[3,4,5],[5,6,7]
.append修改原始列表并返回None.你想要的+.
我冒昧地将代码重写为Python的外观.
class lin_reg(object):
def __init__(self):
self.indep, self.dep = self.initialise_from_user()
self.total = self.indep + self.dep
print self.indep, self.dep, self.total
self.n = len(self.total)
def initialise_from_user(self):
analysis_type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
n = int(raw_input('Number of Independent Variables: '))
indep = [np.matrix(raw_input('Enter the array: ')) for _ in range(self.n)]
dep = np.matrix(raw_input('Enter the dependent array: '))
return indep, dep
Run Code Online (Sandbox Code Playgroud)