您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – ML的数据分离

5b51 2022/1/14 8:21:35 python 字数 5750 阅读 499 来源 www.jb51.cc/python

我为Machine Learning项目导入了一个数据集.我需要在我的第一个输入图层中的每个“神经元”包含一个数字数据.但是,我一直无法做到这一点.这是我的代码:import math import numpy as np import pandas as pd; v = pd.read_csv('atestred.csv', error_bad_line

概述

我为Machine Learning项目导入了一个数据集.我需要在我的第一个输入图层中的每个“神经元”包含一个数字数据.但是,我一直无法做到这一点.这是我的代码

import math
import numpy as np
import pandas as pd; v = pd.read_csv('atestred.csv',error_bad_lines=False).values
rw = 1
print(v)
for x in range(0,10):
    rw += 1
    s = (v[rw])
list(s)
#s is one row of the dataset 
print(s)#Just a debug.
myvar = s
class l1neuron(object):
    def gi():
        for n in range(0,len(s)):
            x = (s[n])
            print(x)#Just another debug 
n11 = l1neuron
n11.gi()

理想情况下我想要的是这样的变体,其中代码为从数据中提取的每个新行创建一个新变量(我在第一个循环中尝试做的)以及从每行提取的每个数据的新变量(我尝试在课堂和第二个循环中做什么).

如果我完全忽略了我的代码,那么请随意指出我正确的方向进行完整的重写.

以下是我的数据集的前几行:

fixed acidity;"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5
7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5

提前致谢.

两个评论

>如果您只需要阅读数据,那么您可能需要寻找一种不那么复杂的解决方
> L1Neuron类不是非常透明,而它的成员不能从代码中读取,而是由attrs中的变量列表创建运行时.您可能需要查看namedTuples以获得更好的可读性.

`

import pandas as pd 
from io import StringIO
import numbers


# example data:
atestred = StringIO("""fixed acidity;volatile acidity;citric acid;\
residual sugar;chlorides;free sulfur dioxide;total sulfur dioxide;\
density;pH;sulphates;alcohol;quality
7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5
7.8;0.76;0.04;2.3;0.092;15;54;0.997;3.26;0.65;9.8;5
""")



# read example data into dataframe 'data'; extract values and column names:
data     = pd.read_csv(atestred,error_bad_lines=False,sep=';') 
colNames = list(data)



class L1Neuron(object):
    "neuron class that holds the variables of one data line"

    def __init__(self,**attr):
        """
        attr is a dict (like {'alcohol': 12,'pH':7.4});
        every pair in attr will result in a member variable 
        of this object with that name and value"""
        for name,value in attr.items():
            setattr(self,name.replace(" ","_"),value)

    def gi(self):
        "print all numeric member variables whose names don't start with an underscore:"
        for v in sorted(dir(self)):
            if not v.startswith('_'):
                value = getattr(self,v) 
                if isinstance(value,numbers.Number): 
                    print("%-20s = %5.2f" % (v,value))
        print('-'*50)


# read csv into variables (one for each line):        
neuronVariables = []        
for s in data.values:
    variables   = dict(zip(colNames,s))
    neuron      = L1Neuron(**variables)
    neuronVariables.append(neuron)

# Now the variables in neuronVariables are ready to be used:     
for n11 in neuronVariables:
    print("free sulphur dioxide in  this variable:",n11.free_sulfur_dioxide,end = " of ")
    print(n11.total_sulfur_dioxide,"total sulphur dioxide" )
    n11.gi()

总结

以上是编程之家为你收集整理的python – ML的数据分离全部内容,希望文章能够帮你解决python – ML的数据分离所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶