首页 > 解决方案 > 如何将带有 String(number) 条目的列表转换为带有可与 numpy.dot (dotproduct) 一起使用的数字的列表

问题描述

我目前的问题是做 dotproduct 但我的清单

            label= list2 = example[4]
            doc  = list2 = example[0:4]
            a=train[0]
            print(a)
            >> ['4.9', '3.0', '1.4', '0.2', '0 ']



    def train(self, train):
    for i in range(self.N):
        for example in train:
            label= list2 = map(float,example[4])
            doc  = list2 = map(float,example[0:4])
            prediction = np.dot(doc, self.weights)
            if prediction != label:
                self.weights += self.lr * (label - prediction) * doc
            else:
                self.weights = self.weights
    return self.weights

这里的问题是

 TypeError: unsupported operand type(s) for *: 'map' and 'float'

如果我删除文档和标签上的地图浮动,我会得到

  TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'.

我知道我的列表包含字符串,但有人知道如何转换这个字符串,以便它可以在我的类中进行计算,例如 * 或 numpy.dot

将 numpy 导入为 np def read_iris(文件名):

with open (filename) as f:
    linelist=list()
    for line in f:
        line = line.replace("Iris-setosa", "0").replace("Iris-versicolor", "1").replace("Iris-virginica", "2")
        if "\n" in line:
            line = line.replace("\n", " ") 
            for element in line.split(','):
                linelist.append(element)
            data = [ linelist [i:i + 5] for i in range(0, len(linelist), 5) ]
    return data

def split_data(data):
return tuple(data[1:]), tuple(data[::5])

data = read_iris("iris.data")
train, test = split_data(data)

import numpy as np
class MCP():

def __init__(self, features=4, labels=3, epochs=250, learning_rate=0.1):
    self.f = features
    self.l = labels
    self.N = epochs
    self.lr = learning_rate
    self.weights = self.initialize_weights() 

def initialize_weights(self):
    weights = np.zeros(self.f)
    return weights

def train(self, train):
    for i in range(self.N):
        for example in train:
            label= np.array(example[4])
            doc  = np.array(example[0:4])
            prediction = np.dot(doc, self.weights)
            if prediction != label:
                self.weights += self.lr * (label - prediction) * doc
            else:
                self.weights = self.weights
    return self.weights

mcp = MCP() mcp.initialize_weights()

mcp.train(火车)

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'



5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa

这就是虹膜的样子,我想我把这个虹膜转换错了,因为文本要我 1. 将虹膜文件读入内存。2. 文件名:字符串。文件名 3. 返回:列表列表(每个数据点一个)。内部列表包含一个 numpy 数组(特征)和一个整数(虹膜类型)。

有人可以帮我弄清楚如何将 iris 转换为一个列表,其中包含一个用于 4 个浮点数的数组,然后是一个用于 Iris-test 的整数

标签: pythonarraysnumpy

解决方案


推荐阅读