python矩陣變化_用numpy改變矩陣的形狀

我的問題有兩個方面。我有下面的代碼來處理一些矩陣。在import numpy

tupleList = [(0, 122), (1, 246), (2, 157), (3, 166), (4, 315), (5, 108), (6, 172), (7, 20), (8, 173), (9, 38), (10, 28), (11, 72), (12, 102), (13, 277), (14, 318), (15, 316), (16, 283), (17, 31), (18, 160), (19, 97), (20, 26), (21, 252), (22, 105), (23, 133), (24, 162), (25, 116), (26, 284), (27, 25), (28, 80), (29, 225), (30, 107), (31, 111), (32, 208), (33, 121), (34, 249), (35, 314), (36, 163), (37, 170), (38, 48), (39, 142), (40, 95), (41, 113), (42, 285), (43, 88), (44, 184), (45, 63), (46, 129), (47, 137), (48, 87), (49, 135), (50, 207), (51, 276), (52, 174), (53, 143), (54, 92), (55, 313), (56, 85), (57, 185), (58, 96), (59, 86), (60, 222), (61, 274), (62, 0), (63, 256), (64, 27), (65, 81), (66, 219), (67, 271), (68, 115), (69, 212), (70, 83), (71, 302), (72, 69), (73, 211), (74, 139), (75, 110), (76, 2), (77, 298), (78, 244), (79, 299), (80, 248), (81, 57), (82, 293), (83, 241), (84, 188), (85, 250), (86, 29), (87, 149), (88, 51), (89, 75), (90, 264), (91, 59), (92, 33), (93, 10), (94, 210), (95, 90), (96, 262), (97, 73), (98, 138), (99, 74), (100, 89), (101, 124), (102, 118), (103, 112), (104, 295), (105, 56), (106, 100), (107, 305), (108, 273), (109, 220), (110, 66), (111, 218), (112, 141), (113, 267), (114, 47), (115, 61), (116, 224), (117, 123), (118, 136), (119, 127), (120, 126), (121, 125), (122, 292), (123, 64), (124, 84), (125, 18), (126, 134), (127, 24), (128, 279), (129, 13), (130, 1), (131, 6), (132, 282), (133, 290), (134, 151), (135, 245), (136, 307), (137, 257), (138, 187), (139, 148), (140, 234), (141, 158), (142, 161), (143, 268), (144, 209), (145, 140), (146, 35), (147, 8), (148, 291), (149, 177), (150, 7), (151, 11), (152, 194), (153, 9), (154, 195), (155, 82), (156, 186), (157, 270), (158, 280), (159, 104), (160, 101), (161, 98), (162, 50), (163, 99), (164, 216), (165, 117), (166, 215), (167, 62), (168, 297), (169, 39), (170, 176), (171, 150), (172, 60), (173, 197), (174, 183), (175, 237), (176, 192), (177, 189), (178, 23), (179, 303), (180, 272), (181, 213), (182, 37), (183, 217), (184, 236), (185, 147), (186, 199), (187, 41), (188, 55), (189, 175), (190, 67), (191, 193), (192, 46), (193, 196), (194, 278), (195, 251), (196, 204), (197, 53), (198, 258), (199, 179), (200, 247), (201, 260), (202, 238), (203, 159), (204, 114), (205, 223), (206, 308), (207, 243), (208, 45), (209, 52), (210, 269), (211, 152), (212, 154), (213, 146), (214, 198), (215, 190), (216, 203), (217, 319), (218, 242), (219, 294), (220, 130), (221, 68), (222, 311), (223, 155), (224, 36), (225, 281), (226, 17), (227, 310), (228, 296), (229, 12), (230, 153), (231, 120), (232, 4), (233, 65), (234, 180), (235, 202), (236, 226), (237, 54), (238, 289), (239, 254), (240, 109), (241, 144), (242, 205), (243, 132), (244, 240), (245, 178), (246, 263), (247, 232), (248, 58), (249, 214), (250, 275), (251, 306), (252, 309), (253, 181), (254, 231), (255, 103), (256, 227), (257, 165), (258, 286), (259, 171), (260, 32), (261, 70), (262, 312), (263, 301), (264, 287), (265, 288), (266, 206), (267, 230), (268, 16), (269, 91), (270, 182), (271, 43), (272, 191), (273, 228), (274, 317), (275, 265), (276, 145), (277, 239), (278, 259), (279, 167), (280, 34), (281, 106), (282, 131), (283, 76), (284, 266), (285, 49), (286, 300), (287, 201), (288, 93), (289, 44), (290, 42), (291, 40), (292, 3), (293, 229), (294, 304), (295, 14), (296, 94), (297, 261), (298, 221), (299, 168), (300, 255), (301, 156), (302, 233), (303, 253), (304, 77), (305, 235), (306, 79), (307, 15), (308, 19), (309, 119), (310, 78), (311, 200), (312, 5), (313, 169), (314, 128), (315, 21), (316, 22), (317, 164), (318, 30), (319, 71)]

var = 320

def binaryMatrix(list):

size = len(list)

matrix = numpy.zeros((size,size))

for tuple in list:

matrix[tuple[0],tuple[1]] = 1

#for row in matrix:

# print sum(row)

# if sum(row) > 1:

# print "Incorrect"

# break

#print matrix

return matrix

matrix = binaryMatrix(tupleList)

matrix = numpy.asarray(matrix,int)

newMatrix = numpy.eye(var)

#print newMatrix

print numpy.shape(newMatrix)

newMatrix = newMatrix[matrix]

print newMatrix

print numpy.shape(newMatrix)

該函數獲取一個元組列表并構造一個方形二進制矩陣,其中每個元組位置的條目為1,其他元素為0。注釋掉的代碼只是為了確保所有行的總和都是1,這是一個有效的二進制矩陣。在

我遇到的問題是在這一行:newMatrix=newMatrix[matrix]

當打印之后的形狀,我得到它的尺寸是320*320*320;但我要的是320*320。在

有人能給我解釋一下A)為什么會發生這種情況,以及B)如何將“newMatrix”重塑為320×320?在

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541448.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541448.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541448.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最小硬幣問題_進行更改的最小硬幣數量

最小硬幣問題Description: 描述: This is classic dynamic programming problem to find minimum number of coins to make a change. This problem has been featured in interview rounds of Amazon, Morgan Stanley, Paytm, Samsung etc. 這是經典的動態編程問題…

java 生成xml亂碼_jdom解決中文亂碼問題 JAVA生成xml文件幫了我很大的忙

決解了數據庫讀取出來 再保存到xml 產生的亂碼問題import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import org.jdom.Attribute;import org.jdom.Document;import org.jdom.Element;import org.jdom.output.Format;import org.…

給定重量上限,背包問題_滿足給定重量的袋子的最低成本

給定重量上限,背包問題Problem statement: 問題陳述: You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of i kg packet of oranges. cost[i] -1 means t…

springMVC rest風格

1.dispatcherServlet的配置<!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springfram…

sql2008能否打開mysql數據庫_mysql數據庫數據能不能導入到sql server中

點“測試”按鈕確認你的鏈接是正確的。 Press the "Test" button to ensure your connection settings are set properly and then the "OK" button when youre done.二. 創建Microsoft SQL到MySQL的鏈接1.在SQL Server Management Studio中打開一個new qu…

c語言 函數的參數傳遞示例_isunordered()函數與C ++中的示例

c語言 函數的參數傳遞示例C isunordered()函數 (C isunordered() function) isunordered() function is a library function of cmath header, it is used to check whether the given values are unordered (if one or both values are Not-A-Number (NaN)), then they are u…

java進一_JAVA小白進:基礎入門知識

1.注釋&#xff0c;關鍵字&#xff0c;標識符1.注釋(1)注釋&#xff1a;解釋說明程序的而文字。(2)注釋的分類&#xff1a;單行注釋 格式&#xff1a; //注釋的文字多行注釋 格式&#xff1a;/*注釋的文字*/文檔注釋 格式&#xff1a;/**注釋的文字*/(3)注釋的作用&#xff1a;…

補丁(patch)的制作與應用

為什么80%的碼農都做不了架構師&#xff1f;>>> 轉自http://linux-wiki.cn/wiki/zh-hans/%E8%A1%A5%E4%B8%81(patch)%E7%9A%84%E5%88%B6%E4%BD%9C%E4%B8%8E%E5%BA%94%E7%94%A8 如果hack了開源代碼&#xff0c;為了方便分享&#xff08;如提交Bug&#xff09;或自己…

php知識點匯總與解答_PHP操作員能力傾向問題與解答

php知識點匯總與解答This section contains Aptitude Questions and Answers on PHP Operators. 本節包含有關PHP運算符的 Aptitude問答。 1) Which of the following types of operators are used in PHP? Arithmetic OperatorsLogical OperatorsArray OperatorsString Oper…

csv導入mysql phpmyadmin_【轉】從phpMyAdmin批量導入Excel內容到MySQL(親測非常簡潔有效)...

今天做項目遇到需要用phpMyAdmin批量導入Excel內容到MySQL數據庫。分析了我的踏坑經歷并且總結一最便捷的一套導入數據的方法&#xff0c;非常實用簡潔&#xff1a;1、修改Excel表的數據&#xff0c;使得Excel中的字段與數據庫字段要一一對應&#xff0c;并加上自增id。2、然后…

weakhashmap_Java WeakHashMap putAll()方法與示例

weakhashmapWeakHashMap類的putAll()方法 (WeakHashMap Class putAll() method) putAll() method is available in java.util package. putAll()方法在java.util包中可用。 putAll() method is used to copy all of the associations or mappings from the given map and assig…

c語言memcmp和java的_C語言memcmp()函數:比較m字節長的兩個字符串s1和s2

函數名&#xff1a;memcmp頭文件&#xff1a;函數原型&#xff1a; void* memcmp(void *s1,void *s2,unsigned m);功能&#xff1a;比較m字節長的兩個字符串s1和s2參數&#xff1a; s1 和 s2 為要比較的字符串unsigned m 為要比較的前m個字節返回值&#xff1a;s1s1s2 返回等…

[下載地址] Subclipse 1.10.9(SVN) _附說明

http://pan.baidu.com/s/1bp6EFHt轉載于:https://www.cnblogs.com/gscq073240/articles/6814429.html

hbase 偽分布安裝 java_HBase基礎和偽分布式安裝配置

一、HBase(NoSQL)的數據模型1.1 表(table)&#xff0c;是存儲管理數據的。1.2 行鍵(row key)&#xff0c;類似于MySQL中的主鍵&#xff0c;行鍵是HBase表天然自帶的&#xff0c;創建表時不需要指定1.3 列族(column family)&#xff0c;列的集合。一張表中有多個行健&#xff0c…

java treemap_Java TreeMap firstEntry()方法及示例

java treemapTreeMap類的firstEntry()方法 (TreeMap Class firstEntry() method) firstEntry() method is available in java.util package. firstEntry()方法在java.util包中可用。 firstEntry() method is used to retrieve the key-value pairs linked with the lowest valu…

linux常用網絡命令ping和arping

linux常用網絡命令ping和arping ping 向目標主機發送icmp請求包 常用來測試當前主機與目標主機網絡連接狀況 常見選項 -c 設置發包的個數 -s 設置發的包的塊大小&#xff0c;最大不超過65507 -W 設置接收回應超時時間 -i …

java printwriter實例_PrintWriter做過濾流+FileWriter案例分析

package com.mstf.ui;import java.io.*;public class TestWriter{public static void main(String args[]){//PrintWriter做過濾流FileWriter//doFilter1();//2、PrintWriter做過濾流OutputStreamWriter//doFilter2();//3、PrintWriter可以作為節點流//doNode();//4、PrintWrit…

treemap比較器_Java TreeMap比較器()方法與示例

treemap比較器TreeMap類的compare()方法 (TreeMap Class comparator() method) comparator() method is available in java.util package. 比較器()方法在java.util包中可用。 comparator() method is used to return the key element in this TreeMap based on the Comparator…

洛谷P1204 [USACO1.2]擠牛奶Milking Cows 前綴和

這題數據比較水 暴搜都能夠過去 1 #include <cstdio>2 #include <cmath>3 #include <cstdlib>4 #include <algorithm>5 #include <string>6 #include <iostream>7 #include <iomanip>8 #include <cstring>9 using namespace s…

java script創建對象_JavaScript七種非常經典的創建對象方式

JavaScript創建對象的方式有很多&#xff0c;通過Object構造函數或對象字面量的方式也可以創建單個對象&#xff0c;顯然這兩種方式會產生大量的重復代碼&#xff0c;并不適合量產。接下來介紹七種非常經典的創建對象的方式&#xff0c;他們也各有優缺點。一、工廠模式可以無數…