如何实现矩阵转换,空值用0填充
时间: 2023-06-02 17:06:24 浏览: 106
可以使用numpy库来实现矩阵转换,具体方式如下:
import numpy as np
# 构造原始矩阵
matrix_ori = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 构造新的矩阵,将原始矩阵转换
matrix_tgt = np.zeros((4, 4), dtype=int)
matrix_tgt[:3, :3] = matrix_ori
# 输出转换后的矩阵
print(matrix_tgt)
输出结果为:
[[1 2 3 0]
[4 5 6 0]
[7 8 9 0]
[0 0 0 0]]
其中,np.zeros((4, 4), dtype=int)表示构造一个4行4列的0矩阵,matrix_tgt[:3, :3] = matrix_ori表示将原始矩阵的数据填充到新矩阵的前3行3列中,后面一行一列都用0填充。
相关问题
inputfile='/Users/mac/Desktop/apriori.txt' data1 = pd.read_csv(inputfile, header=None, dtype = object) ct = lambda x : pd.Series(1, index = x[pd.notnull(x)]) #转换0-1矩阵的过渡函数 b = map(ct, data1.values()) #用map方式执行 data = pd.DataFrame(b).fillna(0) #实现矩阵转换,空值用0填充 print('\n转换完毕') del b #删除中间变量b,节省内存. 报错内容'numpy.ndarray' object is not callable 怎么改
这个错误通常是因为使用了一个numpy数组作为函数。在你的代码中,可能是因为将一个numpy数组作为了参数传递给了`map`函数。你可以尝试将`data1.values()`转换为列表类型,然后再使用`map`函数。修改后的代码如下:
```
import pandas as pd
inputfile='/Users/mac/Desktop/apriori.txt'
data1 = pd.read_csv(inputfile, header=None, dtype=object)
ct = lambda x: pd.Series(1, index=x[pd.notnull(x)])
# 转换0-1矩阵的过渡函数
b = map(ct, data1.values.tolist())
# 用map方式执行
data = pd.DataFrame(b).fillna(0)
# 实现矩阵转换,空值用0填充
print('\n转换完毕')
del b # 删除中间变量b,节省内存
```
这样就能够避免这个错误了。
from mlxtend.frequent_patterns import apriori import numpy as np from __future__ import print_function import pandas as pd inputfile = '/Users/mac/Desktop/apr.txt' #输入事务集文件 data1 = pd.read_csv(inputfile, header=None, dtype = object) print(u'\n转换原始数据至0-1矩阵...') ct = lambda x : pd.Series(1, index = x[pd.notnull(x)]) #转换0-1矩阵的过渡函数 b = map(ct, data1.values()) #用map方式执行 data1 = pd.DataFrame(b).fillna(0) #实现矩阵转换,空值用0填充 print('\n转换完毕') del b #删除中间变量b,节省内存 support = 0.06 #最小支持度 confidence = 0.75 #最小置信度 ms = '---' #连接符,默认'--',用来区分不同元素,如A--B。需要保证原始表格中不含有该字符 print(u'\n开始搜索关联规则...') find_rule(data1, support, confidence, ms) print(u'\n搜索完成') 报错'numpy.ndarray' object is not callable 求修改代码
根据你提供的信息,代码中没有包含 `find_rule` 函数的定义,因此我无法确定该函数中是否存在其他错误。但是,可以看出代码中存在一个问题,即在使用 `map` 函数时,应该将其结果转换为列表。以下是修改后的代码:
```python
from mlxtend.frequent_patterns import apriori
import numpy as np
from __future__ import print_function
import pandas as pd
inputfile = '/Users/mac/Desktop/apr.txt' #输入事务集文件
data1 = pd.read_csv(inputfile, header=None, dtype = object)
print(u'\n转换原始数据至0-1矩阵...')
ct = lambda x : pd.Series(1, index = x[pd.notnull(x)]) #转换0-1矩阵的过渡函数
b = list(map(ct, data1.values)) #用map方式执行,并将结果转换为列表
data1 = pd.DataFrame(b).fillna(0) #实现矩阵转换,空值用0填充
print('\n转换完毕')
del b #删除中间变量b,节省内存
support = 0.06 #最小支持度
confidence = 0.75 #最小置信度
ms = '---' #连接符,默认'--',用来区分不同元素,如A--B。需要保证原始表格中不含有该字符
print(u'\n开始搜索关联规则...')
#在这里调用 find_rule 函数
find_rule(data1, support, confidence, ms)
print(u'\n搜索完成')
```
请注意,此处仅仅是对代码中的一个问题进行了修改,如果代码中还有其他问题,还需要进一步调试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)