Position of numpy random seed
I have the following code for running an autoregressive process:
import numpy as np
def AR1(mu,phi,x0, Nt, a, b, neg = False):
AR_1 = []
x1 = np.zeros(len(x0))
#np.random.seed(0)
for j in range(Nt):
#np.random.seed(0)
for i in range(len(x0)):
x1[i] = mu[i] + phi*(x0[i]  mu[i]) + ss.truncnorm.rvs(a[i],b[i])
AR_1.append(x1)
x0 = x1.copy()
x1 = np.zeros(len(x0))
return AR_1
As you can see I have a couple of positions where I might put np.random.seed(0)
. I want the same set of random numbers to be chosen for every run so that I can see the effect of varying phi. With this in mind, where should i place np.random.seed
in the code? Thanks!
1 answer

It's best to pass it to
rvs()
as therandom_state
keyword argument.
See also questions close to this topic

tdms file read error using python
When there are two channels in a file, the value of the second channel is read strangely. The code is shown below. It is normal in Excel file, but it becomes strange when it is read by nptdms module. data1 is meaningless.
from nptdms import TdmsFile tdms_file=TdmsFile('./DB/Test02/CN10_Normal_N_1500_100.tdms') data0=tdms_file.channel_data('Vibration','cDAQ1Mod1/ai0') data1=tdms_file.channel_data('Vibration','cDAQ1Mod1/ai1')

How to scrape data from tooltip of a dynamic graph using python?
This is the link which has the graphs i am trying to scrape, http://www.upsldc.org/dynamicgraph
Could anyone please guide me on this?

Django save uploaded file
I want to upload the file which I am able to do from below code , but I also nned to save all the uploads in Different folder with different names , if 2 users upload same file from browser then in the folder it should been saved by different name or Unique Identification number. Following is my code:
views.py
from django.shortcuts import render import openpyxl def index(request): if "GET" == request.method: return render(request, 'myapp/index.html', {}) else: excel_file = request.FILES["excel_file"] # you may put validations here to check extension or file size wb = openpyxl.load_workbook(excel_file) # getting all sheets sheets = wb.sheetnames print(sheets) # getting a particular sheet worksheet = wb["Sheet1"] print(worksheet) # getting active sheet active_sheet = wb.active print(active_sheet) # reading a cell print(worksheet["A1"].value) excel_data = list() # iterating over the rows and # getting value from each cell in row for row in worksheet.iter_rows(): row_data = list() for cell in row: row_data.append(str(cell.value)) print(cell.value) excel_data.append(row_data) return render(request, 'myapp/index.html', {"excel_data":excel_data})

Logarithm over x
Since the following expansion for the logarithm holds:
log(1x)=xx^2/2x^3/3...
one can calculate the following functions which have removable singularities at x:
log(1x)/x=1x/2...
(log(1x)/x+1)/x=1/2x/3...
((log(1x)/x+1)/x+1/2)/x=1/3x/4...
I am trying to use NumPy for these calculations, and specifically the log1p function, which is accurate near x=0. However, convergence for the aforementioned functions is still problematic.
Do you have any ideas for any existing functions implementing these formulas or should I write one myself using the previous expansions, which will not be as efficient, however? Thank you.

Creating a new 2 column numpy array from filtering through the first coumn/array
I am trying to create a new 2 dimensional or 2 column array, which will consist of (data value <=20000) from the first column, and their associated ID values in the second column. Mathematically I am doing the following: I am reading data from a text file. I am finding distance to all the points from the last point.
# ID M1 M2 M3 M4 R4 M5 R5 x y z 10217 11.467 11.502 13.428 13.599 432.17 13.266 281.06 34972.8 42985.9 14906 7991 11.529 11.559 13.438 13.520 435.23 13.224 272.23 8538.05 33219.8 43375.1 2100 11.526 11.573 13.478 13.490 448.97 13.356 301.27 9371.75 13734.1 43398.6 9467 11.557 11.621 13.481 13.537 449.99 13.367 303.67 33200.3 36008.9 12735.8 4002 11.454 11.530 13.502 13.583 457.34 13.327 294.53 44607.2 10410.9 9090 2971 11.475 11.563 13.506 13.558 458.77 13.391 309.43 29818.3 98.65 11718.6 1243 11.538 11.581 13.509 13.513 459.62 13.377 306.09 16238.4 11067.9 25048 9953 11.523 11.544 13.559 13.913 477.72 13.440 321.20 34589.6 42869 14878.6 7411 11.547 11.576 13.610 13.658 496.81 13.479 330.96 31436 42092.8 12307.8 1820 11.606 11.619 13.652 12.543 513.11 13.571 355.21 1758.75 15809.8 40473.6 2792 11.647 11.679 13.744 13.877 550.82 13.643 375.38 24393 6774.8 8346.35 510 11.687 11.717 13.771 13.810 562.27 13.642 375.14 22340.3 9316.4 13209.9 1721 11.602 11.646 13.821 14.139 584.37 13.770 413.84 2144.95 15769.1 40470.1
After I get the distances, I only want to take distances<=20,000 from my calculations and also their associated ID column.
So far I wrote this code to return calculated distances and IDs:
# Find nearest neighbors import numpy as np import matplotlib.pyplot as plt halo = 'nntest.txt' ID, m,r,x,y,z= np.loadtxt(halo, usecols=(0,6,7,8,9,10), unpack =True) # selet the last point m_mass = m[1:] ID_mass = ID[1:] r_mass = r[1:] x_mass = x[1:] y_mass = y[1:] z_mass = z[1:] ####################################### #Find distance to all points from our targeted point nearest_neighbors = [] def neighbors(ID_mass, cx,cy,cz, ID, x, y, z): dist = np.sqrt((cxx)**2 + (cyy)**2 + (czz)**2) return dist, ID for i in range(len(ID_mass)): hist = neighbors(ID_mass[i], x_mass[i], y_mass[i], z_mass[i], ID, x, y, z) print hist #print all the IDs which are associated with dist<=20000 if (hist[0]<=20000): print ID nearest_neighbors.append(hist) print nearest_neighbors
But I am having problem returning the new array, which will only contain distances<=20000, and associated IDs. I apologize in advance if this is not a good working example. But I will very much appreciate your suggestion to get that desired output.

Python Numpy vectorize nested forloops for combinatorics
Given an nxn array A of real positive numbers, I'm trying to find the minimum of the maximum of the elementwise minimum of all combinations of three rows of the 2d array. Using forloops, that comes out to something like this:
global_best = 1000000000000000.0 for i in range(len(A)): for j in range(i+1,len(A)): for k in range(j+1,len(A)): local_best = np.amax(np.array([A[i,:], A[j,:], A[k,:]]).min(0)) if (local_best < global_best): global_best = local_best save1, save2, save3 = i, j, k
I have a feeling though that I could do this much faster using Numpy vectorization, and would certainly appreciate any help on doing this. Also, in general, what's a good way to learn how to think using vectorization rather than forloops? Thanks.

Java Android CheckBox String To TextView on Different Class Using SharedPreference
So in my MainActivity, the users are allowed to select 1 or more than 1 checkbox. And I want to retrieve it to a string and store in shared preference to my Capture so that it can be added into a array and randomise it before displaying it on a TextView.
No errors so far or what not. But if I select "Cool" only, I realize there are times where the Capture will generate a "Heat" for me. If I select Cool or Heat only it should generate the respective selections. And if I select both it should generate only either one of them (random).
MainActicity.java
SharedPreferences sharedMode = getSharedPreferences("MySharedMode", Context.MODE_PRIVATE); final SharedPreferences.Editor editor = sharedMode.edit(); if ( cbCool.isChecked()  cbHeat.isChecked()) { String cool = cbCool.getText().toString(); String heat = cbHeat.getText().toString(); editor.putString("ModeCool", cool); editor.putString("ModeHeat", heat); editor.commit();
Capture.java
SharedPreferences sharedMode = getSharedPreferences("MySharedMode", Context.MODE_PRIVATE); String restCool = sharedMode.getString("ModeCool", null); String restHeat = sharedMode.getString("ModeHeat", null); if (restCool != null && restHeat != null) { String cool = sharedMode.getString("ModeCool", " "); String heat = sharedMode.getString("ModeHeat", " "); List<String> list = new ArrayList<String>(); list.add(cool); list.add(heat); Collections.shuffle(list); String randMode = list.get(0); tvMode.setText(randMode); }

Random string generator in swift
I have this method that generates a random string of words in java. I have been trying to convert this to swift but without success. Can someone tell me what the implementation would be in swift.
public String generateString(String characters, int length) { StringBuilder stringBuilder = new StringBuilder(); Random random = new Random(); for (int i = 0; i < length; i++) { stringBuilder .append(characters.charAt(random.nextInt(characters .length()))); } return stringBuilder; }

Generating many, good, uniformly distributed random numbers
I am running analyses on very large graphs with random weights. I know that using the
rand()
function is poor, so I have been using this function based on what I have read to be proper random number generation:double randomZeroToOne() { random_device rd; mt19937 mt(rd()); uniform_real_distribution<double> dist(0.0, 1.0); double randomRealBetweenZeroAndOne = dist(mt); return randomRealBetweenZeroAndOne; }
Everytime I want to put a weight in my graph, I call this function and insert it into my adjacency matrix. However, I am worried that perhaps I am using a slow method to generate these numbers. This has worked fine for small graphs, but my larger graphs are very slow (likely something else is slowing it down, but I just wanted to double check and learn the proper way). What is the best way to preserve the quality of the numbers but to generate them as quickly as possible?
Additionally, if you know a way to initialize a vector of a known size with fast, good, uniformly distributed random numbers, that would be even better (although I am still curious about the answer to my main question).
EDIT:
This is my new proposed solution:
#include <iostream> #include <random> #include<vector> #include<iomanip> #include<cmath> using namespace std; random_device rd; mt19937 mt(rd()); uniform_real_distribution<double> dist(0.0, 1.0); int main() { double randomRealBetweenZeroAndOne = dist(mt); double anotherRandomRealBetweenZeroAndOne = dist(mt); double anothernother = dist(mt); vector<double> randoman(10,dist(mt)); cout << randomRealBetweenZeroAndOne << endl; cout << anotherRandomRealBetweenZeroAndOne <<endl; cout << anothernother <<endl; }
Please let me know if you see any issues with this, especially if this function will be called many times.