TypeError: ufunc ‘subtract‘ did not contain a loop with signature matching types dtype(‘<U32‘) dtype

import math
import numpy as np

def loadDataSet():
    dataMat = []
    labelMat = []
    fr = open("testSet.txt")
    for line in fr.readlines():
        lineArr = line.strip().split()
        dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
        labelMat.append(lineArr[2])
    return dataMat, labelMat


def sigmoid(inX):
    return 1.0 / (1 + np.exp(-inX))  # 使用math.exp()会报错,因为inx是numpy类型的


def gradAscent(dataMatIn, classLabels):
    dataMatrix = np.mat(dataMatIn)
    labelMat = np.mat(classLabels).transpose()  # transpose在不指定参数是默认是矩阵转置
    # labelMat = np.mat(classLabels).T
    m, n = np.shape(dataMatrix)
    alpha = 0.001
    maxCycles = 500
    weights = np.ones((n, 1))
    for k in range(maxCycles):
        h = sigmoid(dataMatrix * weights)
        # print(type(h))
        # print(labelMat)
        # print(h)
        error = (labelMat - h)
        weights = weights + alpha * dataMatrix.transpose() * error
    return weights


dataArr, labelMat = loadDataSet()
# print(dataArr)
# print(labelMat)
print(gradAscent(dataArr, labelMat))

运行报错:

C:\Users\ktcuser\AppData\Local\Programs\Python\Python36-32\python.exe F:/Logistic/logistic.py
Traceback (most recent call last):
  File "F:/Logistic/logistic.py", line 41, in <module>
    print(gradAscent(dataArr, labelMat))
  File "F:/Logistic/logistic.py", line 33, in gradAscent
    error = (labelMat - h)
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

Process finished with exit code 1

原因:
labelMat.append(int(lineArr[2])) #此处要转换一下为int 否则error = (labelMat - h) 报错

def loadDataSet():
    dataMat = []
    labelMat = []
    fr = open("testSet.txt")
    for line in fr.readlines():
        lineArr = line.strip().split()
        dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
        labelMat.append(int(lineArr[2])) #此处要转换一下为int 否则error = (labelMat - h) 报错
        # labelMat.append(lineArr[2])
    return dataMat, labelMat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值