numpy

Initial value numpy array before starting the function in python

南楼画角 提交于 2021-01-29 13:38:49
问题 I want to put data of a image in a numpy array, but every time I get the error ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 4 dimension(s) or I get that my array is zero-dimensional and that's also not right. How should I initialise my variable before the function? Here some code: from extra_keras_datasets import emnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers

How to create (correctly) a NumPy array from Pandas DF

 ̄綄美尐妖づ 提交于 2021-01-29 13:19:23
问题 I'm trying to create a NumPy array for the "label" column from a pandas data-frame. My df: label vector 0 0 1:0.044509422 2:-0.03092437 3:0.054365806 4:-... 1 0 1:-0.007471546 2:-0.062329583 3:0.012314787 4... 2 0 1:-0.009525825 2:0.0028720177 3:0.0029517233 ... 3 1 1:-0.0040618754 2:-0.03754585 3:0.008025528 4... 4 0 1:0.039150625 2:-0.08689039 3:0.09603256 4:0.... ... ... ... 59996 1 1:0.01846487 2:-0.012882819 3:0.035375785 4:-... 59997 1 1:0.01435293 2:-0.00683616 3:0.009475072 4:-0...

Problem with “ValueError: Expected 2D array, got 1D array instead”

点点圈 提交于 2021-01-29 13:18:55
问题 I need to run a SVR (supported vector regression). I have a CSV data frame.I had no problems to run the OLS regression, with one target variable and multiple regressors. But I have a problem with this part of the code. So, here is my code: import matplotlib.pyplot as plt import pandas as pd from sklearn.preprocessing import StandardScaler from sklearn.svm import SVR sc_X = StandardScaler() sc_y = StandardScaler() X = sc_X.fit_transform(X) y = sc_y.fit_transform(y) y_pred = sc_y.inverse

Plotting the graph in networkx from the numpy array

痴心易碎 提交于 2021-01-29 13:14:07
问题 I have a DataFrame in pandas with information about people location in time. It is about 300+ million rows. Here is the sample where each Name is assigned to a unique index by group.by and sorted by "Name" and "Year": import pandas as pd inp = [{'Name': 'John', 'Year':2018, 'Address':'Beverly hills'}, {'Name': 'John','Year':2018, 'Address':'Beverly hills'}, {'Name': 'John', 'Year':2019, 'Address':'Beverly hills'}, {'Name': 'John', 'Year':2019, 'Address':'Orange county'}, {'Name': 'John',

How to faster iterate over a Python numpy.ndarray with 2 dimensions

荒凉一梦 提交于 2021-01-29 13:06:10
问题 So, i simply want to make this faster: for x in range(matrix.shape[0]): for y in range(matrix.shape[1]): if matrix[x][y] == 2 or matrix[x][y] == 3 or matrix[x][y] == 4 or matrix[x][y] == 5 or matrix[x][y] == 6: if x not in heights: heights.append(x) Simply iterate over a 2x2 matrix (usually round 18x18 or 22x22) and check it's x. But its kinda slow, i wonder which is the fastest way to do this. Thank you very much! 回答1: For a numpy based approach, you can do: np.flatnonzero(((a>=2) & (a<=6))

How to ensure each worker use exactly one CPU?

大憨熊 提交于 2021-01-29 12:57:49
问题 I'm implementing SEED using ray, and therefore, I define a Worker class as follows import numpy as np import gym class Worker: def __init__(self, worker_id, env_name, n): import os os.environ['OPENBLAS_NUM_THREADS'] = '1' self._id = worker_id self._n_envs = n self._envs = [gym.make(env_name) for _ in range(self._n_envs)] def reset_env(self, env_id): return self._envs[env_id].reset() def env_step(self, env_id, action): return self._envs[env_id].step(action) Besides that, there is a loop in the

NumPy type hint that something is both an array and float32? [duplicate]

大兔子大兔子 提交于 2021-01-29 12:55:54
问题 This question already has answers here : Type hint for NumPy ndarray dtype? (4 answers) Closed 1 year ago . How can I type hint that a value being returned by a function is both an NumPy array and holds NumPy float32 data? I can specify that the returned value is an array using: def func() -> np.ndarray: ... However, this does not enforce the knowledge that it is a float32 array. I can specify that the returned value is of type float32 using: def func() -> np.float32: ... However, this does

Getting descriptive statistics with (analytic) weighting using describe() in python

梦想的初衷 提交于 2021-01-29 12:34:53
问题 I was trying to translate code from Stata to Python The original code in Stata: by year, sort : summarize age [aweight = wt] Normally a simply describe() function will do dataframe.groupby("year")["age"].describe() But I could not find a way to translate the aweight option into the language of python i.e. to give descriptive statistics of a dataset under analytic/ variance weighting. codes to generate the dataset in python: dataframe = {'year': [2016,2016,2020, 2020], 'age': [41,65, 35,28],

TypeError: only integer scalar arrays can be converted to a scalar index , while trying kfold cv

时光毁灭记忆、已成空白 提交于 2021-01-29 12:30:30
问题 Trying to perform Kfold cv on a dataset containing 279 files , the files are of shape ( 279 , 5 , 90) after performing a k-means. I reshaped it in order to fit it on a svm. Now the shape is ( 279, 5*90 ) . Trying the Kfold cv approach gives me the error "TypeError: only integer scalar arrays can be converted to a scalar index " #input with open("dataset.pkl", "rb") as file: dataset = pkl.load(file) print(len(dataset)) x = [i[0] for i in dataset] #k-means cc y = [i[1] for i in dataset] #label

How to ensure each worker use exactly one CPU?

走远了吗. 提交于 2021-01-29 12:17:47
问题 I'm implementing SEED using ray, and therefore, I define a Worker class as follows import numpy as np import gym class Worker: def __init__(self, worker_id, env_name, n): import os os.environ['OPENBLAS_NUM_THREADS'] = '1' self._id = worker_id self._n_envs = n self._envs = [gym.make(env_name) for _ in range(self._n_envs)] def reset_env(self, env_id): return self._envs[env_id].reset() def env_step(self, env_id, action): return self._envs[env_id].step(action) Besides that, there is a loop in the