Create array of lists with random values [duplicate] - python

This question already has answers here:
How to generate n dimensional random variables in a specific range in python
(2 answers)
Closed 4 years ago.
I’m trying to create sample data like the smpl_dt array example below. I want to create an array where each element is a list of 8 random numbers between 0.0001 and 1.
I can easily create the list of 8 random numbers between 0 and 1 using:
Code:
[rd.uniform(0.0001,1) for _ in range(8)]
But I’m having trouble creating the array. Any tips are greatly appreciated.
Sample Data:
print(smpl_dt[0:5])
array([[0.0001, 0.0001, 0.3 , 0.0001, 0.2 , 0.0001, 0.2 , 0.3 ],
[0.1 , 0.1 , 0.1 , 0.2 , 0.2 , 0.2 , 0.1 , 0.0001],
[0.1 , 0.0001, 0.2 , 0.0001, 0.1 , 0.2 , 0.0001, 0.4 ],
[0.3 , 0.0001, 0.0001, 0.2 , 0.1 , 0.2 , 0.2 , 0.0001],
[0.2 , 0.3 , 0.1 , 0.0001, 0.2 , 0.1 , 0.1 , 0.0001]])

Use the size argument. For example,
>>> arr = np.random.uniform(0.0001, 1, size=[4,8])
array([[0.67011692, 0.06662612, 0.13316262, 0.80666553, 0.88362879, 0.21492319, 0.22063457, 0.90038505],
[0.87799324, 0.6486384 , 0.27700837, 0.54103365, 0.52688455, 0.93159481, 0.09245974, 0.54593494],
[0.4680346 , 0.17802325, 0.21506341, 0.95917602, 0.20481784, 0.53165515, 0.1657028 , 0.39784648],
[0.38951888, 0.03457946, 0.90076103, 0.13769038, 0.303991 , 0.57457931, 0.64236861, 0.85915101]])

Without using numpy:
import random as rd
arr = [[rd.uniform(0.0001,1) for _ in range(8)] for _ in range(arrlen)]

Related

Use Matplotlib to plot 100% Stacked bar from Excel data

I can plot a 100% stacked bar from Excel. is it possible to achieve the same with matplotlib ?
import pandas as pd
import matplotlib.pyplot as plt
data = [
[0.4 , 0.3 , 0.2 , 0.1],
[0.5 , 0.3 , 0.6 , 0.1],
[0.1 , 0.4 , 0.2 , 0.8],
]
columns = ["A","B","C","D" ]
df = pd.DataFrame(data=data , columns=columns , index = ["Empty" , "Wrong" , "Correct"] )
df.plot(kind="barh" , stacked=True )
plt.ylabel("Percentages")
plt.show()
print (df)

Unable to turn off scientific notation in Matplotlib [duplicate]

This question already has an answer here:
Prevent scientific notation
(1 answer)
Closed 2 years ago.
I am plotting a simple plot in Matplotlib, Python using the following code:
temp=np.array([1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 1. ,
1. , 1. , 1. , 1. , 0.99999999,
0.99999999, 0.99999998, 0.99999996, 0.99999993, 0.99999989,
0.99999982, 0.99999972, 0.99999958, 0.99999933, 0.99999906,
0.99999857, 0.99999791, 0.9999971 , 0.99999611, 0.99999459,
0.99999276, 0.99999014, 0.99998735, 0.99998418, 0.99997975,
0.99997557, 0.99997059, 0.9999657 , 0.99996077])
temp2=np.array([0.025, 0.05 , 0.075, 0.1 , 0.125, 0.15 , 0.175, 0.2 , 0.225,
0.25 , 0.275, 0.3 , 0.325, 0.35 , 0.375, 0.4 , 0.425, 0.45 ,
0.475, 0.5 , 0.525, 0.55 , 0.575, 0.6 , 0.625, 0.65 , 0.675,
0.7 , 0.725, 0.75 , 0.775, 0.8 , 0.825, 0.85 , 0.875, 0.9 ,
0.925, 0.95 , 0.975])
plt.plot(temp2,temp)
plt.xlabel(r'$\frac{\tau}{\tau_c}$')
plt.ylabel(r'$\frac{\alpha ^{ss}}{\alpha {_0} ^{ss}}$')
plt.ticklabel_format(style='plain')
plt.rcParams.update({'font.size': 16})
I am getting the following figure in a scientific notation despite specifying the style to be plain.
What is the issue here and how do I resolve this ?
Setting useOffset=False, will do it, like this:
plt.ticklabel_format(style='plain', useOffset=False)

Numpy array references behaving strangely when inside a for loop

I'm writing a Python implementation of Euler's method, using an example from Paul's math notes here.
I'm using a n x 3 numpy array to store the results. The goal is to have the t-value in the first column, y in the second, and the value of y' computed using the current row in the third column.
When I did the first problem listed on the page, using only ten iterations, everything behaved exactly as expected. The step size was 0.1, so the values in the first column incremented by 0.1 with each iteration of the for loop.
But now that I've copied the code over and attempted to apply it to problem 3, the first column behaves very strangely. I inputted the step size as 0.01, but for the first ten iterations it increments by 0.1, then after the tenth iteration it appears to reset to zero, then uses the expected 0.01, but later on it resets again in a similar fashion.
Here's my code:
import numpy as np
def ex3(t,y):
return y + (-0.5 * np.exp(t/2) * np.sin(5*t)) + (5 * np.exp(t/2) * np.cos(5*t))
ex3out = np.empty((0,3), float)
# Input the initial conditions and first y' computation
ex3out = np.append(ex1out, np.array([[0,0,ex3(0,0)]]), axis=0)
h = 0.01
n = 500
for i in range(1,n+1):
# Compute the new t and y values and put in 0 as a dummy y' for now
new = np.array([[ex3out[i - 1,0] + h, ex3out[i - 1,1] + h * ex3out[i - 1,2],0]])
# Append the new row
ex3out = np.append(ex3out,new,axis=0)
# Replace the dummy 0 with y' based on the new values
ex3out[i,2] = ex3(ex3out[i,0],ex3out[i,1])
And here are the first several rows of ex3out after running the above code:
array([[ 0. , 1. , -1. ],
[ 0.1 , 0.9 , 5.2608828 ],
[ 0.2 , 0.852968 , 3.37361534],
[ 0.3 , 0.8374415 , 0.6689041 ],
[ 0.4 , 0.83983378, -2.25688988],
[ 0.5 , 0.85167737, -4.67599317],
[ 0.6 , 0.86780837, -5.90918813],
[ 0.7 , 0.8851749 , -5.51040903],
[ 0.8 , 0.90205891, -3.40904125],
[ 0.9 , 0.91757091, 0.031139 ],
[ 1. , 0.93132436, 4.06022317],
[ 0. , 0. , 5. ],
[ 0.01 , 0.99 , 5.98366774],
[ 0.02 , 0.95260883, 5.92721107],
[ 0.03 , 0.88670415, 5.82942804],
[ 0.04 , 0.84413054, 5.74211536],
[ 0.05 , 0.81726488, 5.65763415],
[ 0.06 , 0.80491744, 5.57481145],
[ 0.07 , 0.80871649, 5.4953251 ],
[ 0.08 , 0.83007081, 5.42066644],
[ 0.09 , 0.8679685 , 5.34993924],
[ 0.1 , 0.9178823 , 5.2787651 ],
[ 0.11 , 0.97192659, 5.19944036],
[ 0.12 , 0.05 , 4.13207859],
[ 0.13 , 1.04983668, 4.97466166],
[ 0.14 , 1.01188094, 4.76791408],
[ 0.15 , 0.94499843, 4.5210138 ],
[ 0.16 , 0.90155169, 4.28666725],
[ 0.17 , 0.87384122, 4.0575499 ],
[ 0.18 , 0.86066555, 3.83286568],
[ 0.19 , 0.86366974, 3.61469476],
[ 0.2 , 0.88427747, 3.40492482],
[ 0.21 , 0.92146789, 3.20302701],
I wondered if this might be a floating point issue, so I tried enclosing various parts of the for loop in float() with the same results.
I must've made a typo somewhere, right?
Simpler loop:
ex3out = [[0, 0, ex3(0,0)]]
h = 0.01
n = 50
for i in range(1,n+1):
# Compute the new t and y values and put in 0 as a dummy y' for now
last = ex3out[-1]
new = [last[0] + h, last[1] + h * last[2], 0]
new[2] = ex3(new[0], new[1])
# Append the new row
ex3out.append(new)
print(np.array(ex3out)) # for pretty numpy display

Tensorflow' pb and pbtxt files don't work with OpenCV after retraining MobileNet SSD V1 COCO

I have followed this tutorial to retrain MobileNet SSD V1 using Tensorflow GPU as described and got 0.5 loss after training using GPU (below more info about config) and got model.ckpt.
This is the command I used for Training:
python ../models/research/object_detection/legacy/train.py --logtostderr --train_dir=./data/ --pipeline_config_path=./ssd_mobilenet_v1_pets.config
And this is the command for freezing (generate pb file):
python ../models/research/object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path ./ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix ./data/model.ckpt-1407 --output_directory ./data/
This is the error I get when I use frozen pb and pbtxt:
Traceback (most recent call last):
File "Object_detection_image.py", line 29, in <module>
cvOut = cvNet.forward()
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:565: error: (-215:Assertion failed) inputs.size() == requiredOutputs in function 'cv::dnn::experimental_dnn_34_v7::DataLayer::getMemoryShapes'
This is the Object_detection_image.py file I used:
import cv2 as cv
import os
import time
import logging
logger = logging.getLogger()
fh = logging.FileHandler('xyz.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'object_detection.pbtxt')
dir_x = "C:\\Users\\Omen\\Desktop\\LP_dataset\\anno"
for filename in os.listdir(dir_x):
print(filename)
if not (filename.endswith(".png") or filename.endswith(".jpg")):
continue
print('daz')
img = cv.imread(os.path.join(dir_x,filename))
img = cv.resize(img, (300,300))
#cv.imshow('i',img)
#cv.waitKey(0)
img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
img = cv.cvtColor(img,cv.COLOR_GRAY2RGB)
rows = img.shape[0]
cols = img.shape[1]
#cvNet.setInput(cv.dnn.blobFromImage(img, size=(cols,rows), swapRB=True, crop=False))
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), crop=False))
t0 = time.time()
cvOut = cvNet.forward()
print(time.time() - t0)
for detection in cvOut[0,0,:,:]:
score = float(detection[2])
#print(score)
if score > 0.80:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)
cv.imshow('img', img)
cv.waitKey(0)
This is pbtxt file (also I tried the exported pbtxt and generated pbtxt from pb but not working):
item {
id: 1
name: 'licenseplate'
}
Config:
What is the top-level directory of the model you are using: object_detetion
Have I written custom code: no
OS Platform and Distribution: win10
TensorFlow installed from: binary
TensorFlow GPU version: 1.13.0
CUDA/cuDNN version: 10
GPU model: 1050 GTX
I can provide any files you ask, please help me. In tensorflow's github they told me to ask in Stackoverflow...
Update:
I got the problem solved thanks to the answer, here is the content of cvOut:
[[[[-0.00476191 -0.00361736 0. 0.25361738 -0.07576995
0.03405379 0.40910327]
[ 0.21594621 0.04544836 0. 0.28788495 0.30689242
-0.13025634 0.05074273]
[ 0.46358964 0.19925728 0. -0.09778295 0.26563603
0.34778297 -0.02014329]
[-0.01515752 0.3534766 0. 0.32857144 -0.00361736
0.67142856 0.25361738]
[ 0.25756338 0.03405379 0. 0.21594621 0.3787817
-0.05689242 0.6212183 ]
[ 0.30689242 0.203077 0. 0.796923 0.19925728
0.40103063 -0.09778295]
[ 0.5989694 0.34778297 0. -0.01515752 0.68680996
0.26515752 0.66190475]
[-0.00361736 1.0047619 0. 0.59089667 0.03405379
1.0757699 0.21594621]
[ 0.712115 -0.05689242 0. 0.30689242 0.53641033
0.05074273 1.1302563 ]
[ 0.19925728 0.7343639 0. 0.93230265 0.34778297
0.64652336 -0.01515752]
[ 1.0201433 0.26515752 0. 0.24638264 0.33809522
0.50361735 -0.07576995]
[ 0.2840538 0.40910327 0. 0.04544836 0.19310758
0.28788495 0.5568924 ]
[-0.13025634 0.30074272 0. 0.44925728 0.06769729
0.15221705 0.26563603]
[ 0.59778297 -0.02014329 0. 0.3534766 0.5151575
0.32857144 0.24638264]
[ 0.67142856 0.50361735 0. 0.2840538 0.7424366
0.4659462 0.3787817 ]
[ 0.19310758 0.6212183 0. 0.203077 0.30074272
0.796923 0.44925728]
[ 0.40103063 0.15221705 0. 0.59778297 0.31319004
0.23484248 0.68680996]
[ 0.5151575 0.66190475 0. 1.0047619 0.50361735
0.59089667 0.2840538 ]
[ 1.0757699 0.4659462 0. 0.19310758 0.95455164
0.5568924 0.53641033]
[ 0.30074272 1.1302563 0. 0.7343639 0.15221705
0.93230265 0.59778297]
[ 0.64652336 0.23484248 0. 0.5151575 -0.00476191
0.49638262 0.33809522]
[ 0.75361735 -0.07576995 0. 0.40910327 0.7159462
0.04544836 0.44310758]
[ 0.28788495 0.8068924 0. 0.55074275 0.46358964
0.69925725 0.06769729]
[ 0.40221703 0.26563603 0. -0.02014329 0.48484248
0.3534766 0.7651575 ]
[ 0.32857144 0.49638262 0. 0.75361735 0.25756338
0.5340538 0.7424366 ]
[ 0.7159462 0.3787817 0. 0.6212183 0.8068924
0.203077 0.55074275]
[ 0.796923 0.69925725 0. 0.40221703 0.5989694
0.84778297 0.31319004]
[ 0.48484248 0.68680996 0. 0.66190475 0.49638262
1.0047619 0.75361735]
[ 0.59089667 0.5340538 0. 0.7159462 0.712115
0.44310758 0.95455164]
[ 0.8068924 0.53641033 0. 1.1302563 0.69925725
0.7343639 0.40221703]
[ 0.93230265 0.84778297 0. 0.48484248 1.0201433
0.7651575 -0.00476191]
[ 0.74638265 0.33809522 0. -0.07576995 0.7840538
0.40910327 0.9659462 ]
[ 0.04544836 0.6931076 0. 1.0568924 -0.13025634
0.80074275 0.46358964]
[ 0.94925725 0.06769729 0. 0.26563603 1.0977829
-0.02014329 0.7348425 ]
[ 0.3534766 1.0151576 0. 0.74638265 0.67142856
1.0036174 0.25756338]
[ 0.7840538 0.7424366 0. 0.3787817 0.6931076
0.6212183 1.0568924 ]
[ 0.203077 0.80074275 0. 0.94925725 0.40103063
0.65221703 0.5989694 ]
[ 1.0977829 0.31319004 0. 0.68680996 1.0151576
0.66190475 0.74638265]
[ 1.0047619 1.0036174 0. 0.7840538 1.0757699
0.9659462 0.712115 ]
[ 0.6931076 0.95455164 0. 0.53641033 0.80074275
1.1302563 0.94925725]
[ 0.7343639 0.65221703 0. 1.0977829 0.64652336
0.7348425 1.0201433 ]
[ 1.0151576 0.1 0. 0.2 0.2
0.1 0.1 ]
[ 0.2 0.2 0. 0.1 0.2
0.2 0.1 ]
[ 0.1 0.2 0. 0.1 0.1
0.2 0.2 ]
[ 0.1 0.1 0. 0.2 0.1
0.1 0.2 ]
[ 0.2 0.1 0. 0.2 0.2
0.1 0.1 ]
[ 0.2 0.2 0. 0.1 0.2
0.2 0.1 ]
[ 0.1 0.2 0. 0.1 0.1
0.2 0.2 ]
[ 0.1 0.1 0. 0.2 0.1
0.1 0.2 ]
[ 0.2 0.1 0. 0.2 0.2
0.1 0.1 ]
[ 0.2 0.2 0. 0.1 0.2
0.2 0.1 ]
[ 0.1 0.2 0. 0.1 0.1
0.2 0.2 ]
[ 0.1 0.1 0. 0.2 0.1
0.1 0.2 ]
[ 0.2 0.1 0. 0.2 0.2
0.1 0.1 ]
[ 0.2 0.2 0. 0.1 0.2
0.2 0.1 ]
[ 0.1 0.2 0. 0.1 0.1
0.2 0.2 ]
[ 0.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.1828533 ]
[ 1.1838211 0.6728102 0. -0.785988 1.2751837
1.1616383 0.933811 ]
[ 0.4684658 0.2719049 0. 1.2093123 0.66612804
0.66964823 0.55971766]
[ 0.17104894 -1.0688283 0. 0.6494252 0.6844874
0.66586125 0.01329695]
[-1.2607187 -0.22749203 0. -0.8741171 -0.9443728
-0.9659323 -0.03422031]
[-0.0364061 0.54829746 0. 0.6263525 0.66758543
0.04167109 -0.11780822]
[ 0.48400337 0.4685324 0. -0.04594427 0.02469592
-0.3487326 0.08831279]
[ 0.4161314 0.23332608 0. -0.13553022 -0.31008872
0.04969648 0.5674252 ]
[ 0.36492363 -0.07475745 0. -0.03859219 0.2016789
-0.39845943 -0.07058203]
[-0.08173721 0.1720942 0. 0.02323131 0.07122216
0.07469177 0.12792486]
[-0.24689877 0.196296 0. 0.5564647 0.535513
0.22528338 -0.37152448]
[-1.7235181 -1.8204601 0. -1.5040898 -1.8099409
-1.8550183 -1.1855855 ]
[-1.6341007 -1.3448519 0. -1.6656716 -1.6564709
-1.2735447 -1.3357594 ]
[-1.2829769 -1.2869868 0. -1.6657944 -1.4066424
-1.4230443 -1.4196167 ]
[-1.3691044 -1.656098 0. -1.4339573 -1.5685135
-1.633306 -1.4437945 ]]]]
The error was caused by the wrong input .pbtxt file passed into the function readNetFromTensorflow because the .pbtxt has to be geneated by tf_text_graph_ssd.py as describe here:
Run this script to get a text graph of SSD model from TensorFlow Object Detection API. Then pass it with .pb file to cv::dnn::readNetFromTensorflow function.
For other models such as faster r-cnn and mask r-cnn, there are also corresponding scripts.
PS: I just found there is a very good official tutorial here.

How to normalize data in a text file while preserving the first variable

I have a text file with this format:
1 10.0e+08 1.0e+04 1.0
2 9.0e+07 9.0e+03 0.9
2 8.0e+07 8.0e+03 0.8
3 7.0e+07 7.0e+03 0.7
I would like to preserve the first variable of every line and to then normalize the data for all lines by the data on the first line. The end result would look something like;
1 1.0 1.0 1.0
2 0.9 0.9 0.9
2 0.8 0.8 0.8
3 0.7 0.7 0.7
so essentially, we are doing the following:
1 10.0e+08/10.0e+08 1.0e+04/1.0e+04 1.0/1.0
2 9.0e+07/10.0e+08 9.0e+03/1.0e+04 0.9/1.0
2 8.0e+07/10.0e+08 8.0e+03/1.0e+04 0.8/1.0
3 7.0e+07/10.0e+08 7.0e+03/1.0e+04 0.7/1.0
I'm still researching and reading on how to do this. I'll upload my attempt shortly. Also can anyone point me to a place where I can learn more about manipulating data files?
Read your file into a numpy array and use numpy broadcast feature:
import numpy as np
data = np.loadtxt('foo.txt')
data = data / data[0]
#array([[ 1. , 1. , 1. , 1. ],
# [ 2. , 0.09, 0.9 , 0.9 ],
# [ 2. , 0.08, 0.8 , 0.8 ],
# [ 3. , 0.07, 0.7 , 0.7 ]])
np.savetxt('new.txt', data)

Categories