我收到一条错误消息,指出“数组包含 NaN 或无穷大”。我已经检查了我的数据,包括训练/测试缺失值,没有遗漏任何东西。
我可能对“数组包含 NaN 或无穷大”的含义有错误的解释。
import numpy as np
from sklearn import linear_model
from numpy import genfromtxt, savetxt
def main():
#create the training & test sets, skipping the header row with [1:]
dataset = genfromtxt(open('C:\\Users\\Owner\\training.csv','r'), delimiter=',')[0:50]
target = [x[0] for x in dataset]
train = [x[1:50] for x in dataset]
test = genfromtxt(open('C:\\Users\\Owner\\test.csv','r'), delimiter=',')[0:50]
#create and train the SGD
sgd = linear_model.SGDClassifier()
sgd.fit(train, target)
predictions = [x[1] for x in sgd.predict(test)]
savetxt('C:\\Users\\Owner\\Desktop\\preds.csv', predictions, delimiter=',', fmt='%f')
if __name__=="__main__":
main()
我认为数据类型可能会引发循环算法(它们是 float )。
我知道 SGD 可以处理 float ,所以我不确定此设置是否要求我声明数据类型。
例如以下之一:
>>> dt = np.dtype('i4') # 32-bit signed integer
>>> dt = np.dtype('f8') # 64-bit floating-point number
>>> dt = np.dtype('c16') # 128-bit complex floating-point number
>>> dt = np.dtype('a25') # 25-character string
下面是完整的错误信息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-62-af5537e7802b> in <module>()
19
20 if __name__=="__main__":
---> 21 main()
<ipython-input-62-af5537e7802b> in main()
13 #create and train the SGD
14 sgd = linear_model.SGDClassifier()
---> 15 sgd.fit(train, target)
16 predictions = [x[1] for x in sgd.predict(test)]
17
C:\Anaconda\lib\site-packages\sklearn\linear_model\stochastic_gradient.pyc in fi
t(self, X, y, coef_init, intercept_init, class_weight, sample_weight)
518 coef_init=coef_init, intercept_init=intercept_i
nit,
519 class_weight=class_weight,
--> 520 sample_weight=sample_weight)
521
522
C:\Anaconda\lib\site-packages\sklearn\linear_model\stochastic_gradient.pyc in _f
it(self, X, y, alpha, C, loss, learning_rate, coef_init, intercept_init, class_w
eight, sample_weight)
397 self.class_weight = class_weight
398
--> 399 X = atleast2d_or_csr(X, dtype=np.float64, order="C")
400 n_samples, n_features = X.shape
401
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in atleast2d_or_csr(X
, dtype, order, copy)
114 """
115 return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix
,
--> 116 "tocsr")
117
118
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _atleast2d_or_spar
se(X, dtype, order, copy, sparse_class, convmethod)
94 _assert_all_finite(X.data)
95 else:
---> 96 X = array2d(X, dtype=dtype, order=order, copy=copy)
97 _assert_all_finite(X)
98 return X
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in array2d(X, dtype,
order, copy)
79 'is required. Use X.toarray() to convert to dens
e.')
80 X_2d = np.asarray(np.atleast_2d(X), dtype=dtype, order=order)
---> 81 _assert_all_finite(X_2d)
82 if X is X_2d and copy:
83 X_2d = safe_copy(X_2d)
C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in _assert_all_finite
(X)
16 if (X.dtype.char in np.typecodes['AllFloat'] and not np.isfinite(X.s
um())
17 and not np.isfinite(X).all()):
---> 18 raise ValueError("Array contains NaN or infinity.")
19
20
ValueError: Array contains NaN or infinity.
如有任何想法,我们将不胜感激。
最佳答案
作为错误报告,您的数据中某处有 np.nan 或 np.inf 或 -np.inf。由于您正在从文本文件中读取并且您说您的数据不包含缺失值,这可能是由列标题或文件中无法自动解释的其他一些值引起的。 p>
genfromtxt 的文档显示,读入数组的默认dtype 是float,这意味着您读取的所有值都必须通过相当于 float(x)。
如果您不确定这是否是导致错误的原因,您可以按如下方式从 numpy 数组中删除非有限数字:
dataset[ ~np.isfinite(dataset) ] = 0 # Set non-finite (nan, inf, -inf) to zero
如果这消除了错误,您可以确定您的变量中有无效值,某处。要查找位置,您可以使用以下命令:
np.where(~np.isfinite(dataset))
这将返回无效值所在的索引列表,例如
>>> import numpy as np
>>> dataset = np.array([[0,1,1],[np.nan,0,0],[1,2,np.inf]])
>>> dataset
array([[ 0., 1., 1.],
[ nan, 0., 0.],
[ 1., 2., inf]])
>>> np.where(~np.isfinite(dataset))
(array([1, 2]), array([0, 2]))
关于python - Sci-Kit 学习 SGD 算法时出错 - "Array contains NaN or infinity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598988/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub