NMSIS_LIB_SORTED := $(sort $(NMSIS_LIB))
时间: 2023-09-11 14:11:01 浏览: 224
这是一个Makefile中的语句,用于将变量NMSIS_LIB中的内容去重排序,并将结果存储到变量NMSIS_LIB_SORTED中。Makefile是一种用于自动化构建的脚本语言,通常用于编译、打包、部署等任务。在这个语句中,sort函数用于对变量NMSIS_LIB中的内容去重排序,然后将排序后的结果存储到变量NMSIS_LIB_SORTED中。
相关问题
from scipy.sparse.linalg import eigsh, LinearOperator from scipy.sparse import isspmatrix, is_pydata_spmatrix class SVDRecommender: def init(self, k=50, ncv=None, tol=0, which='LM', v0=None, maxiter=None, return_singular_vectors=True, solver='arpack'): self.k = k self.ncv = ncv self.tol = tol self.which = which self.v0 = v0 self.maxiter = maxiter self.return_singular_vectors = return_singular_vectors self.solver = solver def svds(self, A): largest = self.which == 'LM' if not largest and self.which != 'SM': raise ValueError("which must be either 'LM' or 'SM'.") if not (isinstance(A, LinearOperator) or isspmatrix(A) or is_pydata_spmatrix(A)): A = np.asarray(A) n, m = A.shape if self.k <= 0 or self.k >= min(n, m): raise ValueError("k must be between 1 and min(A.shape), k=%d" % self.k) if isinstance(A, LinearOperator): if n > m: X_dot = A.matvec X_matmat = A.matmat XH_dot = A.rmatvec XH_mat = A.rmatmat else: X_dot = A.rmatvec X_matmat = A.rmatmat XH_dot = A.matvec XH_mat = A.matmat dtype = getattr(A, 'dtype', None) if dtype is None: dtype = A.dot(np.zeros([m, 1])).dtype else: if n > m: X_dot = X_matmat = A.dot XH_dot = XH_mat = _herm(A).dot else: XH_dot = XH_mat = A.dot X_dot = X_matmat = _herm(A).dot def matvec_XH_X(x): return XH_dot(X_dot(x)) def matmat_XH_X(x): return XH_mat(X_matmat(x)) XH_X = LinearOperator(matvec=matvec_XH_X, dtype=A.dtype, matmat=matmat_XH_X, shape=(min(A.shape), min(A.shape))) eigvals, eigvec = eigsh(XH_X, k=self.k, tol=self.tol ** 2, maxiter=self.maxiter, ncv=self.ncv, which=self.which, v0=self.v0) eigvals = np.maximum(eigvals.real, 0) t = eigvec.dtype.char.lower() factor = {'f': 1E3, 'd': 1E6} cond = factor[t] * np.finfo(t).eps cutoff = cond * np.max(eigvals) above_cutoff = (eigvals > cutoff) nlarge = above_cutoff.sum() nsmall = self.k - nlarge slarge = np.sqrt(eigvals[above_cutoff]) s = np.zeros_like(eigvals) s[:nlarge] = slarge if not self.return_singular_vectors: return np.sort(s) if n > m: vlarge = eigvec[:, above_cutoff] ularge = X_matmat(vlarge) / slarge if self.return_singular_vectors != 'vh' else None vhlarge = _herm(vlarge) else: ularge = eigvec[:, above_cutoff] vhlarge = _herm(X_matmat(ularge) / slarge) if self.return_singular_vectors != 'u' else None u = _augmented_orthonormal_cols(ularge, nsmall) if ularge is not None else None vh = _augmented_orthonormal_rows(vhlarge, nsmall) if vhlarge is not None else None indexes_sorted = np.argsort(s) s = s[indexes_sorted] if u is not None: u = u[:, indexes_sorted] if vh is not None: vh = vh[indexes_sorted] return u, s, vh def _augmented_orthonormal_cols(U, n): if U.shape[0] <= n: return U Q, R = np.linalg.qr(U) return Q[:, :n] def _augmented_orthonormal_rows(V, n): if V.shape[1] <= n: return V Q, R = np.linalg.qr(V.T) return Q[:, :n].T def _herm(x): return np.conjugate(x.T)这段代码中使用的scipy包太旧了,导致会出现报错信息为:cannot import name 'is_pydata_spmatrix' from 'scipy.sparse' (D:\Anaconda\lib\site-packages\scipy\sparse_init.py),将这段代码修改为使用最新版的scipy包
好的,将使用最新版的scipy包进行修改。首先需要将代码中的`is_pydata_spmatrix`替换为`isspmatrix_csr`。同时需要将代码开头的导入语句修改为`from scipy.sparse.linalg import eigsh, LinearOperator from scipy.sparse import isspmatrix_csr`。
fw = open("data/step3_route.js", "w", encoding='utf-8') fw.write("var step3_route = [\n") for i in range(1, len(ls)): ls[i] = dict(zip(ls[0], ls[i])) a = json.dumps(ls, sort_keys=True, indent=4, ensure_ascii=False) print(a) fw.write(a) fw.close()File "E:\Work\Python\work_longsun\longsun_data1\step3_route.py", line 37, in process a = json.dumps(ls, sort_keys=True, indent=4, ensure_ascii=False) File "C:\Users\24788\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 234, in dumps return cls( File "C:\Users\24788\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 201, in encode chunks = list(chunks) File "C:\Users\24788\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 429, in _iterencode yield from _iterencode_list(o, _current_indent_level) File "C:\Users\24788\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 325, in _iterencode_list yield from chunks File "C:\Users\24788\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 353, in _iterencode_dict items = sorted(dct.items()) TypeError: '<' not supported between instances of 'str' and 'int'
这个错误是由于在对字典进行排序时,字典中的键包含了字符串和整数,而在 Python 中字符串和整数不能直接比较所导致的。
要解决这个问题,你可以使用 `key` 参数来指定排序的键。你可以将键转换为字符串类型,并使用这个字符串进行排序。
下面是一个修改后的代码示例:
```python
import json
fw = open("data/step3_route.js", "w", encoding='utf-8')
fw.write("var step3_route = [\n")
for i in range(1, len(ls)):
ls[i] = dict(zip(ls[0], ls[i]))
a = json.dumps(ls, sort_keys=True, indent=4, ensure_ascii=False, key=str)
print(a)
fw.write(a)
fw.close()
```
在这个示例中,我们在 `json.dumps()` 方法中添加了 `key=str` 参数。这将把字典的键转换为字符串类型,以便进行排序。
请注意,你需要将代码中的 `ls` 替换为你实际使用的变量名,并确保 `ls` 中的元素都是字典类型。
通过这样的修改,你应该能够避免该错误并成功将字典转换为 JSON 字符串并写入文件。
阅读全文