numpy

Fastest way to read huge MySQL table in python

孤街醉人 提交于 2021-02-07 07:56:11
问题 I was trying to read a very huge MySQL table made of several millions of rows. I have used Pandas library and chunks . See the code below: import pandas as pd import numpy as np import pymysql.cursors connection = pymysql.connect(user='xxx', password='xxx', database='xxx', host='xxx') try: with connection.cursor() as cursor: query = "SELECT * FROM example_table;" chunks=[] for chunk in pd.read_sql(query, connection, chunksize = 1000): chunks.append(chunk) #print(len(chunks)) result = pd

Fastest way to read huge MySQL table in python

二次信任 提交于 2021-02-07 07:56:00
问题 I was trying to read a very huge MySQL table made of several millions of rows. I have used Pandas library and chunks . See the code below: import pandas as pd import numpy as np import pymysql.cursors connection = pymysql.connect(user='xxx', password='xxx', database='xxx', host='xxx') try: with connection.cursor() as cursor: query = "SELECT * FROM example_table;" chunks=[] for chunk in pd.read_sql(query, connection, chunksize = 1000): chunks.append(chunk) #print(len(chunks)) result = pd

Numpy taking only first character of string

こ雲淡風輕ζ 提交于 2021-02-07 07:52:50
问题 Following is the simplified version of my problem. I want to create a (N, 1) shape numpy array, which would have strings as their values. However, when I try to insert the string, only the first character of the string gets inserted. What am I doing wrong here? >>> import numpy as np >>> N = 23000 >>> Y = np.empty((N, 1), dtype=str) >>> Y.shape (23000, 1) >>> for i in range(N): ... Y[i] = "random string" ... >>> Y[10] array(['r'], dtype='<U1') 回答1: By default data type str takes length of 1 .

NumPy: convert decimals to fractions

寵の児 提交于 2021-02-07 07:37:32
问题 I compute the reverse of matrix A , for instance, import numpy as np A = np.diag([1, 2, 3]) A_inv = np.linalg.pinv(A) print(A_inv) I got, [[ 1. 0. 0. ] [ 0. 0.5 0. ] [ 0. 0. 0.33333333]] But, I want this, [[ 1. 0. 0. ] [ 0. 1/2 0. ] [ 0. 0. 1/3]] I tried np.set_printoptions , import fractions np.set_printoptions(formatter={'all':lambda x: str(fractions.Fraction(x))}) print(A_inv) but I got this, [[1 0 0] [0 1/2 0] [0 0 6004799503160661/18014398509481984]] How do I convert decimals to

compressed files bigger in h5py

蓝咒 提交于 2021-02-07 07:29:57
问题 I'm using h5py to save numpy arrays in HDF5 format from python. Recently, I tried to apply compression and the size of the files I get is bigger... I went from things (every file has several datasets) like this self._h5_current_frame.create_dataset( 'estimated position', shape=estimated_pos.shape, dtype=float, data=estimated_pos) to things like this self._h5_current_frame.create_dataset( 'estimated position', shape=estimated_pos.shape, dtype=float, data=estimated_pos, compression="gzip",

How to read a super huge file into numpy array N lines at a time

妖精的绣舞 提交于 2021-02-07 07:19:17
问题 I have a huge file (around 30GB), each line includes coordination of a point on a 2D surface. I need to load the file into Numpy array: points = np.empty((0, 2)) , and apply scipy.spatial.ConvexHull over it. Since the size of the file is very large I couldn't load it at once into the memory, I want to load it as batch of N lines and apply scipy.spatial.ConvexHull on the small part and then load the next N rows! What's an efficient to do it? I found out that in python you can use islice to

How to read a super huge file into numpy array N lines at a time

老子叫甜甜 提交于 2021-02-07 07:19:01
问题 I have a huge file (around 30GB), each line includes coordination of a point on a 2D surface. I need to load the file into Numpy array: points = np.empty((0, 2)) , and apply scipy.spatial.ConvexHull over it. Since the size of the file is very large I couldn't load it at once into the memory, I want to load it as batch of N lines and apply scipy.spatial.ConvexHull on the small part and then load the next N rows! What's an efficient to do it? I found out that in python you can use islice to

Numpy, python: automatically expand dimensions of arrays when broadcasting

谁都会走 提交于 2021-02-07 05:22:06
问题 Consider the following exercise in Numpy array broadcasting. import numpy as np v = np.array([[1.0, 2.0]]).T # column array A2 = np.random.randn(2,10) # 2D array A3 = np.random.randn(2,10,10) # 3D v * A2 # works great # causes error: v * A3 # error I know the Numpy rules for broadcasting, and I'm familiar with bsxfun functionality in Matlab. I understand why attempting to broadcast a (2,1) array into a (2,N,N) array fails, and that I have to reshape the (2,1) array into a (2,1,1) array before

Tensorflow: How can I assign numpy pre-trained weights to subsections of graph?

▼魔方 西西 提交于 2021-02-07 05:21:51
问题 This is a simple thing which I just couldn't figure out how to do. I converted a pre-trained VGG caffe model to tensorflow using the github code from https://github.com/ethereon/caffe-tensorflow and saved it to vgg16.npy... I then load the network to my sess default session as "net" using: images = tf.placeholder(tf.float32, [1, 224, 224, 3]) net = VGGNet_xavier({'data': images, 'label' : 1}) with tf.Session() as sess: net.load("vgg16.npy", sess) After net.load, I get a graph with a list of

Numpy, python: automatically expand dimensions of arrays when broadcasting

谁说胖子不能爱 提交于 2021-02-07 05:21:32
问题 Consider the following exercise in Numpy array broadcasting. import numpy as np v = np.array([[1.0, 2.0]]).T # column array A2 = np.random.randn(2,10) # 2D array A3 = np.random.randn(2,10,10) # 3D v * A2 # works great # causes error: v * A3 # error I know the Numpy rules for broadcasting, and I'm familiar with bsxfun functionality in Matlab. I understand why attempting to broadcast a (2,1) array into a (2,N,N) array fails, and that I have to reshape the (2,1) array into a (2,1,1) array before