scipy

How to concatenate two matrices in Python?

我与影子孤独终老i 提交于 2021-02-18 05:33:50
问题 I have two csr_matrix , uniFeature and biFeature . I want a new matrix Feature = [uniFeature, biFeature] . But if I directly concatenate them this way, there's an error that says the matrix Feature is a list. How can I achieve the matrix concatenation and still get the same type of matrix, i.e. a csr_matrix ? And it doesn't work if I do this after the concatenation: Feature = csr_matrix(Feature) It gives the error: Traceback (most recent call last): File "yelpfilter.py", line 91, in <module>

Fast Interpolation / Resample of Numpy Array - Python

冷暖自知 提交于 2021-02-18 05:25:09
问题 Currently, I have written some Python code that is inserted into a pipeline. The incoming data comes in in a numpy array of shape (1,512,19,25). I use the scipy.ndimage.interpolation.zoom to bring the array up to shape (1,512,38,50). This can be accomplished with one call to the function. Basically, it resizes each (19,25) piece to size (38,50). Later in the code, when the data is moving the other way, different data is again resized the in the other direction (38,50) to (19,25). Everything

机器学习入门 【二】

微笑、不失礼 提交于 2021-02-16 23:34:47
常用库简介 Numpy 基础科学计算库 Scipy 强大的科学计算工具集 Pandas 数据分析的利器 Matplotlib 画出优美的图形 Scikit-learn 机器学习库 Numpy 【sklearn使用numpy数组形式的数据进行处理,所以需要把数据转换为numpy数组形式,其中的多维数组也是numpy的核心功能之一】 import numpy i = numpy.array([[520,13,14],[25,9,178]]) print("i: \n{}".format(i)) 给变量i复制为一个数组 i是一个典型的numpy数组 结果: sklearn需要使用 scipy 来对算法进行执行 sparse函数,用来 生成稀疏矩阵,而稀疏矩阵用来存储那些大部分数组为0的np数组 【常用】 sparse用法 : import numpy as np from scipy import sparse matrix = np.eye(6) #用eye函数生成一个6*6对角矩阵 #矩阵中对角线上的元素数值为1,其余都是0 sparse_matrix = sparse.csr_matrix(matrix) #这一行把np数组转化为CSR格式的scripy稀疏矩阵(sparse matrix) #sparse函数只会存储非0元素 print("对角矩阵:\n {}".format

How would I find the mode (stats) of pixel values of an image?

…衆ロ難τιáo~ 提交于 2021-02-16 21:05:58
问题 I'm using opencv and I'm able to get a pixel of an image-- a 3-dimensional tuple, via the code below. However, I'm not quite sure how to calculate the mode of the pixels values in the image. import cv2 import numpy as np import matplotlib.pyplot as plt import numpy as np import cv2 img =cv2.imread('C:\\Users\Moondra\ABEO.png') #px = img[100,100] #gets pixel value #print (px) I tried, from scipy import stats stats.mode(img)[0] But this returns an array shape of stats.mode(img)[0].shape (1, 800

How would I find the mode (stats) of pixel values of an image?

北慕城南 提交于 2021-02-16 21:04:56
问题 I'm using opencv and I'm able to get a pixel of an image-- a 3-dimensional tuple, via the code below. However, I'm not quite sure how to calculate the mode of the pixels values in the image. import cv2 import numpy as np import matplotlib.pyplot as plt import numpy as np import cv2 img =cv2.imread('C:\\Users\Moondra\ABEO.png') #px = img[100,100] #gets pixel value #print (px) I tried, from scipy import stats stats.mode(img)[0] But this returns an array shape of stats.mode(img)[0].shape (1, 800

How to column_stack a numpy array with a scipy sparse matrix?

倖福魔咒の 提交于 2021-02-16 19:24:49
问题 I have the following matrices: A.toarray() array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], dtype=int64) type(A) scipy.sparse.csr.csr_matrix A.shape (878049, 942) And matrix B: B array([2248, 2248, 2248, ..., 0, 0, 0]) type(B) numpy.ndarray B.shape (878049,) I would like to column stack A and B in C, I tried the folowing: C = sparse.column_stack([A,B]) Then: /usr/local/lib/python3

Use scipy.integrate.quad with Tensorflow

ぐ巨炮叔叔 提交于 2021-02-16 15:30:11
问题 I am trying to use scipy.integrate.quad with Tensorflow as following. time and Lambda are two Tensors with shape (None, 1). def f_t(self, time, Lambda): h = Lambda * self.shape * time ** (self.shape - 1) S = tf.exp(-1 * Lambda * time ** self.shape) return h * S def left_censoring(self, time, Lambda): return tf.map_fn(lambda x: integrate.quad(self.f_t, 0.0, x[0], # it is not a float before evaluation args=(x[1],)), tf.concat([time, Lambda], 1)) However, I get an error as below: File "J:

Plotting graph using scipy.optimize.curve_fit

↘锁芯ラ 提交于 2021-02-15 07:49:10
问题 I am having trouble in understanding the optimize.curve_fit function. My fitting function is a power law. But I don't know exactly what should be the second value in the plot command? First we have to call function ff(L,v) it will return us fitting line but we are not calling this function. How this command is working I want to know that. x=Ls y=Tc #fitting function def ff(L,v): return L**(-1/v) pfit,perr=optimize.curve_fit(ff,x,y) plt.plot(x,...) 回答1: The plot command plots x vs y values, so

Plotting graph using scipy.optimize.curve_fit

我与影子孤独终老i 提交于 2021-02-15 07:47:01
问题 I am having trouble in understanding the optimize.curve_fit function. My fitting function is a power law. But I don't know exactly what should be the second value in the plot command? First we have to call function ff(L,v) it will return us fitting line but we are not calling this function. How this command is working I want to know that. x=Ls y=Tc #fitting function def ff(L,v): return L**(-1/v) pfit,perr=optimize.curve_fit(ff,x,y) plt.plot(x,...) 回答1: The plot command plots x vs y values, so

scipy.optimize get's trapped in local minima. What can I do?

对着背影说爱祢 提交于 2021-02-13 17:30:09
问题 from numpy import *; from scipy.optimize import *; from math import * def f(X): x=X[0]; y=X[1] return x**4-3.5*x**3-2*x**2+12*x+y**2-2*y bnds = ((1,5), (0, 2)) min_test = minimize(f,[1,0.1], bounds = bnds); print(min_test.x) My function f(X) has a local minima at x=2.557, y=1 which I should be able to find. The code showed above will only give result where x=1 . I have tried with different tolerance and alle three method: L-BFGS-B, TNC and SLSQP. This is the thread I have been looking at so