Python创建一个空的稀疏矩阵(Python create an empty sparse matrix)
我试图将一些真实数据解析成一个
.mat
对象,以便在我的matlab脚本中加载。我得到这个错误:
TypeError:'coo_matrix'对象不支持项目分配
我找到了coo_matrix 。 但是,我无法为其分配值。
data.txt中
10 45 11 12 4 1
我想得到一个大小为100x100的稀疏矩阵。 并指定1
Mat(10, 45) = 1 Mat(11, 12) = 1 Mat(4, 1) = 1
码
import numpy as np from scipy.sparse import coo_matrix def pdata(pathToFile): M = coo_matrix(100, 100) with open(pathToFile) as f: for line in f: s = line.split() x, y = [int(v) for v in s] M[x, y] = 1 return M if __name__ == "__main__": M = pdata('small.txt')
有什么建议吗?
I am trying to parse some real data into a
.mat
object to be loaded in my matlab script.I am getting this error:
TypeError: 'coo_matrix' object does not support item assignment
I found coo_matrix. However, I am not able to assign values to it.
data.txt
10 45 11 12 4 1
I would like to get a sparse matrix of size 100x100. And to assign 1's to
Mat(10, 45) = 1 Mat(11, 12) = 1 Mat(4, 1) = 1
CODE
import numpy as np from scipy.sparse import coo_matrix def pdata(pathToFile): M = coo_matrix(100, 100) with open(pathToFile) as f: for line in f: s = line.split() x, y = [int(v) for v in s] M[x, y] = 1 return M if __name__ == "__main__": M = pdata('small.txt')
Any suggestions please ?
原文:https://stackoverflow.com/questions/45547924
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
为什么我的视图中的按钮不能被UIAutomation看到?(Why is it the button in my view cant be seen by UIAutomation?)[2022-10-18]
确保按钮的可访问性属性不会被其容器视图隐藏。 只有那些用户可以访问的元素应该被标记为这样。 如果在界面构建器中将容器视图设置为启用“Accessability”,则UIAutomation将不会显示hierarchie中的所有子元素。 这有帮助吗? Ensure that the accessability property of the button is not hidden by its container views. Only those elements, which should be acc ... -
如果你想隐藏一个UIBarButtonItem,你实际上必须修改父条的内容。 如果是UIToolBar,则需要将该栏的items数组设置为不包含项的数组。 NSMutableArray *items = [[myToolbar.items mutableCopy] autorelease]; [items removeObject: myButton]; myToolbar.items = items; If you're trying to hide a UIBarButtonItem, you ...
-
你的问题很简单。 您不能在同一元素上使用if和repeat 。 在你的情况下,第一行的p也是多余的。 这样做很简单: ... 你可以在这里找到更多信息 Your problem is simple. You cannot use if and repeat on the same element. Also in your case are redundant w ...
-
添加注释/页面后,您想要更改$ scope.templates中的内容吗? 如果要更改模板中每个笔记的内容,您可以: var app = angular.module('plunker', []); app.controller('ctrl',['$scope', function($scope){ $scope.templates = []; $scope.addTemplate = function(){ $scope.templates.push({ content1 : " ...
-
动态按钮没有开火(dynamic button not firing)[2020-02-22]
$('body').on('click', '#btnTest' function () { alert('hey'); }); 您需要阅读.on文档并了解它正在做得更好。 它与旧的.live方法不同。 $('body').on('click', '#btnTest' function () { alert('hey'); }); You need to read the .on documentation and understand what it's doing better. ... -
使用视图中复选框的动态列表,如何创建模型(Using a dynamic list of checkboxes in a view, how to create the model)[2023-07-03]
这就是我要做的事情: 您是否有动态生成复选框的问题? 如果没有,请在ViewModel上创建一个属性: public ListCheckboxResults { get; set; } 当您在视图中生成复选框时,请确保它们共享name =“CheckboxResults”。 当MVC看到您的ViewModel作为action方法的参数时,它将自动绑定并将所有“CheckboxResults”结果放入List(以及您的其他ViewModel属性)。 现在,您有一个动态列表,基于您的用户选中 ... -
您必须使用.on()为动态创建的元素绑定单击处理程序,请参阅下面的代码 $(document).ready(function(){ $(document).on("click","button",function(){ alert(this.id); }); }); 更多信息 - .on() You have to bind click handler using .on() for dynamically created elements, see below code $(docume ...
-
您可以向左浮动它们,这将设置自动宽度。
Reveal Identity View similar candidates
.btn { float: left; margin-right: 10px; } http://jsfiddle.net/sV8LH/2/ You can float them left, which will s ...