在使用numpy和sklearn自主实现逻辑回归的过程中,矩阵无法相乘
```python
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer(return_X_y=True)
X = np.array(data[0])
y = np.array(data[1])
def sigmod(x):
return 1/(1+pow(np.e,(-x)))
def Logistic_Regression(feature_data,target_data,learning_rate,account):
m = feature_data.shape[0]
feature_data = np.hstack((np.full((m,1),1),feature_data))
m,n = feature_data.shape
para = np.random.uniform(-1,1,n).reshape(n,1) # n*1
para = np.mat(para)
feature_data = np.mat(feature_data) # m*n
target_data = np.mat(target_data)
# =========问题代码,两矩阵阵无法相乘????============#
d = pd.DataFrame(np.array(feature_data))
print(d.describe())
print(type(feature_data), type(para))
print(feature_data.shape, para.shape)
print(feature_data @ para)
Error = (-1/m)*np.sum(np.multiply(target_data,np.log(sigmod(feature_data @ para))) +
np.multiply(1-target_data,np.log(1-sigmod(feature_data @ para))))
count = 1
error_list = [Error]
while True:
grad_vector = (1/m) * [feature_data.T @ (sigmod(feature_data @ para) - target_data)]
para = para - learning_rate * grad_vector
Error = (-1 / m) * np.sum(np.multiply(target_data, np.log(sigmod(feature_data @ para))) +
np.multiply(1 - target_data, np.log(1 - sigmod(feature_data @ para))))
error_list.append(Error)
count = count + 1
if count == account:
break
return para,error_list
para,e = Logistic_Regression(X,y,0.01,100)
plt.plot(e)
plt.show()
###### 无显式报错,但程序中止
###### 试过更改库版本,无果