[英]Numpy error while defining gradient descent function
# Reading the dataset
data= pd.read_csv("energydata_complete.csv")
X = data.drop(['Appliances','date'], axis=1)
Y = data['Appliances'].values
y_np = Y.shape[0]
y = Y.reshape(y_np,1)
L = 0.001
n = X.shape[1]
m = y.size
ones = np.ones((X.shape[0],1))
X = np.concatenate((ones,X), axis = 1)
X = X.astype("float64")
y = y.astype("float64")
thetas = np.random.random([1,n+1])
# Splitting data into training and testing
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.25, random_state = 5)
# Partition the dataset in train + validation sets
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
def batch_grad_desc(X,y,m,thetas,L):
cost_list = []
thetas_list = []
L = 0.001
L = []
m = y.size
y_hat_list = []
grad = True
i = 0
cost_list.append (1e10)
while grad:
y_hat = np.dot(X,thetas.T)
y_hat_list.append(y_hat)
Error = (y_hat - y)
cost = 1/(2*m) * np.dot(Error.T, Error)
cost_list.append(cost)
thetas = thetas - (L*(1/m) * np.dot(X.T, Error))
thetas_list.append(thetas)
if cost_list - cost_list[i+1] < 1e-9:
grad = False
i+= 1
cost_list.pop(0)
return y_hat_list, cost_list, thetas_list
y_hat_list, cost_list, thetas_list = batch_grad_desc(X, y, m, thetas, L)
thetas = thetas_list[-1]
Error: 错误:
TypeErrorTraceback (most recent call last)
<ipython-input-66-5b85a5574e32> in <module>
----> 1 y_hat_list, cost_list, thetas_list = batch_grad_desc(X, y, m, `thetas, L)`
2 thetas = thetas_list[-1]
<ipython-input-65-9097ee62fbd8> in batch_grad_desc(X, y, m, thetas, L)
19
20 cost_list.append(cost)
---> 21 thetas = thetas - (L*(1/m) * np.dot(X.T, Error))
22
23 thetas_list.append(thetas)
TypeError: can't multiply sequence by non-int of type 'float'
I have implemented a basic batch gradient function but now I am getting error where I am updating theta and I am unable to guess how to make it work. 我已经实现了基本的批处理渐变功能,但是现在在更新theta时出现错误,无法猜测如何使其正常工作。 The variables are already declared in float.
变量已在float中声明。 I do not know what the error is all about?
我不知道所有错误是什么?
Remove the line 删除线
L = []
You're not storing lold L
's so it's not necessary (and should be another name anyway), and I think you want L
to be a float
. 您没有存储
L
,因此没有必要(无论如何都应该是另一个名称),我想您希望L
成为float
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.