numpy

Why does strptime from datetime not give me the correct hours?

烂漫一生 提交于 2021-01-29 20:17:22
问题 I am dealing with hourly data in which the date is stored in 4 different arrays, one for day, month, year, and hour. I am running a for-loop to rather store these dates as strings with this format: '01/01/1946 0' My code looks something like this: import numpy as np from datetime import datetime as dt import matplotlib.dates as mdates for nn in range(nnn): y1 = int(yr[nn]) m1 = int(mon[nn]) d1 = int(day[nn]) h1 = int(hr[nn]) #In the the last string we are specifying the format datenow = dt

Drawing histogram of probability density function of a matrix in python [closed]

情到浓时终转凉″ 提交于 2021-01-29 20:12:30
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 days ago . Improve this question I have a 2D matrix of p-values. I want to draw a histogram of the pdf of this 2D matrix. How can I do that? 回答1: Just so you know: Seaborn's distplot does all of this. import seaborn as sns, numpy as np sns.set_palette("cividis"); np.random.seed(0) x = np.random.randn(100) ax =

Convert MNIST data from numpy arrays to original ubyte data

怎甘沉沦 提交于 2021-01-29 20:02:28
问题 I used this code almost exactly, just changing the line: f = gzip.open("../data/mnist.pkl.gz", 'rb') training_data, validation_data, test_data = cPickle.load(f) to these lines: import pickle as cPickle f = gzip.open("mnist.pkl.gz", 'rb') u = cPickle._Unpickler(f) u.encoding='latin1' training_data, validation_data, test_data = u.load() to account for pickling issues.The original mnist.pkl.gz was downloaded from his repo (available here), or the code to generate the .pkl.gz is here. The output

How can extract data from .h5 file and save it in .txt or .csv properly?

扶醉桌前 提交于 2021-01-29 19:35:46
问题 After searching a lot I couldn't find a simple way to extract data from .h5 and pass it to a data.Frame by Numpy or Pandas in order to save in .txt or .csv file. import h5py import numpy as np import pandas as pd filename = 'D:\data.h5' f = h5py.File(filename, 'r') # List all groups print("Keys: %s" % f.keys()) a_group_key = list(f.keys())[0] # Get the data data = list(f[a_group_key]) pd.DataFrame(data).to_csv("hi.csv") Keys: <KeysViewHDF5 ['dd48']> When I print data I see following results:

Numpy.where uses

放肆的年华 提交于 2021-01-29 19:32:18
问题 Use numpy.where to get all (R, G,B) in a numpy.array with a definite value of R, G and B The problem is i'm not sure i can use numpy.where to get what i want : i tried the following code : L = numpy.array([[1,2,3],[1,1,1],[1,1,1]]) print(numpy.where(L==(1,1,1))) (array([0, 1, 1, 1, 2, 2, 2], dtype=int64), array([0, 0, 1, 2, 0, 1, 2], dtype=int64)) and i understand it's returning me the coordinates of every element == 1 but i would like it to return the index in L of the element equal to (1,1

Keras - ValueError: could not convert string to float

丶灬走出姿态 提交于 2021-01-29 19:30:42
问题 I have the code shown below, but get the following error: ValueError: could not convert string to float: BRAF Provided that this is a sample of my data ( | is just a separator I added here for demonstration, you can imagine each value in a separate cell in a CSV file): c.401C>T | skin | 23:141905805-141905805 | 9947 | BRAF Could the strings be the issue? How can I read and pass strings in this case? from keras.models import Sequential from keras.layers import Dense from keras.models import

Different numpy version in Anaconda and numpy.__version__ in IPython Shell

别等时光非礼了梦想. 提交于 2021-01-29 19:26:36
问题 I used How do I check which version of NumPy I'm using? to learn how to get the version of numpy. However, when I run conda list | grep numpy , I get: numpy 1.15.2 py36ha559c80_0 numpy-base 1.15.2 py36h8128ebf_0 numpydoc 0.8.0 py36_0 However, when I run version from IPython shell, I get: import numpy as np np.__version__ Out: '1.13.3' np.version.version Out: '1.13.3' np.version.full_version Out: '1.13.3' Why are the two versions different? Which one should I trust? Thanks for any help. Please

Gaussian kernel performance

為{幸葍}努か 提交于 2021-01-29 19:18:26
问题 Following method calculates a gaussian kernel: import numpy as np def gaussian_kernel(X, X2, sigma): """ Calculate the Gaussian kernel matrix k_ij = exp(-||x_i - x_j||^2 / (2 * sigma^2)) :param X: array-like, shape=(n_samples_1, n_features), feature-matrix :param X2: array-like, shape=(n_samples_2, n_features), feature-matrix :param sigma: scalar, bandwidth parameter :return: array-like, shape=(n_samples_1, n_samples_2), kernel matrix """ norm = np.square(np.linalg.norm(X[None,:,:] - X2[:

numpy reading a csv file to an numpy array

若如初见. 提交于 2021-01-29 18:41:13
问题 I am new to python and using numpy to read a csv into an array .So I used two methods: Approach 1 train = np.asarray(np.genfromtxt(open("/Users/mac/train.csv","rb"),delimiter=",")) Approach 2 with open('/Users/mac/train.csv') as csvfile: rows = csv.reader(csvfile) for row in rows: newrow = np.array(row).astype(np.int) train.append(newrow) I am not sure what is the difference between these two approaches? What is recommended to use? I am not concerned which is faster since my data size is

Numpy array segmentation

断了今生、忘了曾经 提交于 2021-01-29 18:35:55
问题 I have a numpy array import numpy as np arr = np.arange(20).reshape(2,10) arr[1,:] = 0 arr[1,2] = arr[1,5] = arr[1,7] = 1 print(arr) >>>[[0 1 2 3 4 5 6 7 8 9] >>> [0 0 1 0 0 1 0 1 0 0]] I want to extract overlapping arrays, starting at a 1 and ending behind the next 1 . Expected output: [[0 1 2 3] [0 0 1 0]] [[2 3 4 5 6] [1 0 0 1 0]] [[5 6 7 8] [1 0 1 0]] [[7 8 9] [1 0 0]] At the moment, I have an index-based for-loop that feels awkward in a numpy context and also has to treat the first and