by

## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Repo info
• • • • • • • • • • • • • • • • • • • • • • • • • ##### Activity winash12
@winash12
@jklymak yes that is what i wrote above. initially fill dddx with -999.99
then with np.where fill in the 3 if else clauses Denis Lisov
@tanriol
Something like this should work
has_value = s[1:-1, :] > -999.99
has_left = s[:-2, :] > -999.99
has_right = s[2:, :] > -999.99
dsdx = np.where(has_right & has_value, (s[2:, :] - s[1:-1, :]) / di, -1000)
dsdx = np.where(has_left & has_value, (s[1:-1, :] - s[:-2, :]) / di, dsdx)
dsdx = np.where(has_left & has_right, (s[2:, :] - s[:-2, :]) / (2. * di), dsdx) winash12
@winash12
@tanriol Excellent. Will that out and get back to you ! Thanks so much ! Denis Lisov
@tanriol
Although this one may be problematic if you're limited in terms of RAM... Bart Feenstra
@bartfeenstra
Good evening! I've got a 3D array, and I want to map the values along a specific axis. apply_along_axis() does this, however, the mapping function's return value is a list/array, and apply_along_axis() then changes the result's outer shape to match that, while I want the inner array to be reshaped
In other words, my data set is an array of matrices of 200x200 integers, and I want the result to be an array of matrices of 200x200 lists/arrays
I've checked the API docs for apply_along_axis() as well as for other functions, but nothing seems to stand out.
How would I get the shape I'm looking for? winash12
@winash12
@tanriol why would it be problematic in terms of RAM ? Denis Lisov
@tanriol
@winash12 When the last np.where starts executing you have s, previous dsdx, (s[2:, :] - s[:-2, :]) / (2. * di) and the new dsdx - 4 almost s-sized arrays in total. And still three boolean arrays of roughly the same size (but they are smaller).
That's a bit suboptimal. geophysics91
@geophysics91

I have a python script that takes the text file as input(inputfile defined inside a python script) and does some calculation(myscript.py). Now i want to run the same python script(myscript.py) inside the shell script so i did like

python my_shell.sh and i defined the input file a inputfile='data.txt' inside the shell script. (in simple language i need to run a python script over a data file but data file should be defined inside a shell script) I tried a lot but i am getting error like input file
1 2 3
4 5 6
7 8 9

NameError: name 'inputfile' is not defined.

my python programme is like this

import numpy as np

def function(x):

  c1,c2,c3 = x
d=(c1+c2+c3)
return d

print(function([c1,c2,c3]))

then i call this programme from shell script

# !/bin/sh

inputfile='data.txt'
python myscript.py Denis Lisov
@tanriol
You cannot simply use shell variables as python variables. You'd need to pass it explicitly, probably either as a command line argument or as an environment variable. geophysics91
@geophysics91
how to do it ?? Denis Lisov
@tanriol
See sys.argv geophysics91
@geophysics91 Denis Lisov
@tanriol
These are basics, so you likely need to read and understand something like this explanation geophysics91
@geophysics91
hii experts, i need to install numpy and scipy on opensuse linux where python2 is default programming after installation....how the installation be carried out.please suggest Denis Lisov
@tanriol
IIRC, python3 and packages for python3 should be available on all modern opensuse versions?.. geophysics91
@geophysics91

Dear experts i have a problem, I have a file that contain both numerics,strings and separator like .....how to import it using numpy.loadtxt

import numpy as np

i am getting error like Convert each value according to its column and store

ValueError: Wrong number of columns at line 3

data file

10.0        c1
80.0        c2
...............
10.0        mr
2.0         no Morning rhkleijn
@rhkleijn
Good evening! I tried updating numpy to 1.19 with conda but it does not seem to be available neither at the anaconda channel nor the conda-forge channel. This search showed 1.18.5 but not 1.19: https://anaconda.org/search?q=numpy. Is a conda release of 1.19 in the works? Jonas Krueger
@JK87iab

Hi, what is an easy way to duplicate data for testing. Lets say I have an (1, 1, 52, 16, 62) nd array.

I want to get (10, 10, 52, 16, 62) nd array by just duplicating the data InessaPawson
@InessaPawson
The inaugural NumPy community survey is LIVE! Spare 15 minutes of your time to help the project in a big way! Your responses will help the leadership team to better guide and prioritize decision-making about the development of NumPy as software and a community.
To engage non-English speaking stakeholders, the survey is offered in 8 additional languages: Bangla, French, Hindi, Japanese, Mandarin, Portuguese, Russian, and Spanish. Gustavo Sena Mafra
@gsmafra
Is this a reliable way to make a "deterministic" random sampler? Any way the output actually deviates from desired probabilities?
def _random_choice(probabilities, identifier):
np.random.seed(identifier)
choice = np.random.choice(range(len(probabilities)), p=probabilities)
return choice Eric Wieser
@eric-wieser
Those two questions don't make sense together. For a given identifier, the probability of each element is exactly 0 or 1. Gustavo Sena Mafra
@gsmafra
Yes, I want it to be deterministic according to identifier
But across different identifiers I want them to be random
If I take only one choice for each identifier, will the proportion of choices match probabilities, and is there any noticeable correlation between some characteristic of the identifier and the choice?
It's like I want to make a random sampling by identifier but I don't know who they are beforehand Matti Picus
@mattip
Dont use np.random. Use rg= np.random.default_rng(seed) which return an instance of a Generator that holds the state of the underlying BitGenerator that actually produces a stream of pseudorandom bytes. Then you call methods of that instance: rg.choice(...) etc. check out the documentation https://numpy.org/devdocs/reference/random/index.html for more info Interview
@interviewer_gitlab
pd.pivot_table(df, values="z", index=["x"], columns=["y"]).fillna(-1).to_numpy() work? geophysics91
@geophysics91
hii experts...how can i csv file to .mat matlab file using python...can anybody share some idea Jean-Baptiste Lespiau
@jblespiau

Hi. I am writing Python logic in C++ for speed (with pybind11). I am receiving arguments, which are expected to be numpy array or convertible to such object.
In practice I am doing a check py::isinstance(arg, py::array). When it's not a numpy array, I would like to convert it. Thus, I would like to call np.array(a) but from C++.
First option is to import numpy.array, and call if from C++, but it will go back to Python to go immediately back to C++ (seems to be unwanted).
I have found the C++ function associated to this:
https://github.com/numpy/numpy/blob/d3eae8be4d783948a0d71363bc07558524e905e5/numpy/core/src/multiarray/multiarraymodule.c#L3979 _array_fromobject.

In practice, i guess I would like to call it, but it's not exposed in any header. Is there an alternative? Do you have any advice?
EDIT: I guess I want asarray and not just array. Matti Picus
@mattip
Did you try cython? geophysics91
@geophysics91
experts, i need to calculate signal-to-noise ratio of a 2d array . how can it be done.please suggest Pierre-Andre Noel
@PierreAndreNoel
Hi! It appears that numpy.ma.masked behaves somewhat like a 0d array, but not exactly. In particular, it appears to have no array scalar counterpart (e.g., assert numpy.ma.masked[()] is numpy.ma.masked). Am I understanding this correctly? Is this a bug? Pierre-Andre Noel
@PierreAndreNoel
My motivating use case for understanding this: hash works on array scalar, but not on 0d arrays (and not on numpy.ma.masked). Eric Wieser
@eric-wieser
Normal 0d arrays are mutable, so that behavior is expected 6 replies
It might be warranted to make ma.masked support hashing, although that might not actually help with anything given that masked == masked doesnt return true 1 reply octave99
@octave99
I am unable to understand why the quantile calculation using numpy differ with the manual calculation, could someone please suggest on below code:
#!/usr/bin/python3
import numpy as np

data = np.array([29.714498, 31.593990, 35.978494, 71.621345, 73.060123,
79.839773, 91.201094, 93.538623, 95.967625, 111.286755,
152.131551, 158.890394, 162.079048, 183.674307, 200.263680,
204.271583, 204.919013, 205.322027, 213.583217, 234.791289,
242.200068, 292.154278, 301.285016, 302.870495, 331.163927,
368.068559, 391.237075, 431.096627, 1607.974257, 1900.303876
])
# Using numpy value = 94.1458735
print("Numpy Calculation = ", np.quantile(data, .25, interpolation='linear'))

# Manual calculation value = 92.95424075
# Calculate location manually Ly = (n + 1) X (y/100)
location = (data.size + 1)*(25/100)
print("Location = ", location)

# Location comes out to be 7.75 which means 7th element + .75 of (8th - 7th)
value = data + ((data - data) * .75)
print("Manual Calculation = ", value) Denis Lisov
@tanriol
@octave99 IIUC, location = (data.size - 1)*(25/100), and it's already zero-based after that (no need to subtract one from the indices) octave99
@octave99
@tanriol Well, I am using the formula Ly = (n + 1) X (y/100) where n = data.size = 30. octave99
@octave99
Here is the detail on the formula: https://ift.world/concept1/concept-6-quartiles-quintiles-deciles-percentiles/ and I am trying to find value of first quartile i.e. 25% Denis Lisov
@tanriol
Wikipedia suggests that there are a number of different interpolation formulas for that. Eric Wieser
@eric-wieser The red line is a mode not supported by numpy, that might be the one you're asking for octave99
@octave99
@tanriol True. Interpolation that my formula uses is linear and it matches numpy documentation: linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j. Eric Wieser
@eric-wieser
Numpy does not have a mode that uses +1 like your formula does octave99
@octave99
@eric-wieser Sure. Thanks