1. 程式人生 > >sklearn sparse matrix 的構造方法

sklearn sparse matrix 的構造方法

1、SciPy中 sparse matrix的格式:

2、寫入稀疏矩陣,一個元素,一個元素寫入。

from scipy.sparse import *
from scipy import *
from numpy import *
import csv
S = dok_matrix((10000,10000), dtype=bool)
f = open("your_file_name")
reader = csv.reader(f)
for line in reader:
    S[int(line[0]),int(line[1])] = True
3、

import numpy as np
import matplotlib.pyplot as plt
import pickle

from sklearn import linear_model, datasets, metrics
from sklearn.cross_validation import train_test_split
from sklearn.neural_network import BernoulliRBM
from sklearn.pipeline import Pipeline
from scipy.sparse import csr_matrix
from scipy.sparse import *
from scipy import *

#X = np.random.randint(2, size=(10000, 100))
#X2 = csr_matrix(X)
X2 = dok_matrix((100,100), dtype=int)
for i in range(10):
        X2[i,i] = 1
print X2