How to use values inside range() [closed] - python

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 8 years ago.
Improve this question
I have a loop
for i in range(0,1000,100):
and inside it I compute a list which holds 10 values but the loop goes from 0 to 1000.I want to relate these 10 values with the 1000 values; namely, create a list (or array) which will hold these values (the 10 to 1000 values).
UPDATED------------------------------------------
I want to make a plot which will have in the horizontal axis values from 0 to 1000 and in the vertical axis the 10 values of the list that i computed.

Your question is very unclear.
From your comment it seems like you're asking about matplotlib? Do you want something like this?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 1000, 100)
# As a placeholder for your calculation...
y = np.cos(x / 100.0)
plt.plot(x, y, marker='o', mfc='red')
plt.show()

Do you want to access the list elements while retaining the range from 0 to 1000 in steps of 100? If so, this should be a way.
mylst=[12,5,6,34,6,11,78,1,1,88]
for i in range(0,1000,100):
print mylst[i/100]
Not sure... a more detailed question could help.

Related

Numpy - how interp function works? [closed]

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 2 years ago.
Improve this question
I need to understand how numpy.interp function works. This function has a couple of parameters which are vague to me, like: xp, fp, left and right.
I saw an example of this function as below:
X is a numpy ndarray and y is one-dimensional array:
X = np.interp(X, (X.min(), X.max()), (0, 10))
y = np.interp(y, (y.min(), y.max()), (30000, 100000))
Thanks for helping me out!
x are the points which are not in xp but you want their y values i.e. points where you want to perform interpolation
xp and yp are the main inputs based on which 1D interpolation works (these are the discrete data points through which you want to interpolate)
left and right handle the edge case scenarios when x is out of the closed interval range of [xp_minimum, xp_maximum] i.e. the output values to generate incase of extrapolation

Phase diagram from a list of points sharing different labels [closed]

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 3 years ago.
Improve this question
I have a two lists x and y , corresponding to the possible values of two parameters, for example
x=[1.,1.,1.,2.,2.,2.,3.,3.,3.]
y=[1.,2.,3.,1.,2.,3.,1.,2.,3.]
I also have a list l giving a label to each point (x[i],y[i]) where i is an integer between 0 and len(x)-1. Suppose each point can have one of 3 labels:
l=[2,3,3,1,1,2,3,1,2]
In this example, the point (1.,1.) has the label 2, the point (1.,2.) the label 3 and so on.
I need to make a phase diagram x vs y in which areas surrounding points with the same label will have a precise color. For example, areas containing only points (x[j],y[j]) such that l[j]=1 for all j shall be green, while for l[j]=2 shall be red, l[j]=3 shall be blue.
The color of those regions containing no points is arbitrary, but the boundaries between different regions should be as smooth as possible.
You need scipy.spatial.ConvexHull, matplotlib.patches.Polygon, and matplotlib.collections.PatchCollection:
# create data frame for better handling data
df = pd.DataFrame({'x':x, 'y':y, 'l':l})
fig, ax = plt.subplots()
# containing hulls and colors to plot
hulls = []
colors = []
for i in set(l):
hull = ConvexHull(df.loc[df.l==i, ['x','y']]).points
hulls.append(Polygon(hull))
colors.append(i)
# create patches collection and set color
p = PatchCollection(hulls, alpha=0.4)
p.set_array(np.array(colors))
ax.add_collection(p)
ax.scatter(df.x,df.y)
plt.show()
Output:

How can I draw a plot for getting probability of binary outcome [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My data has the following structure and it has 50 000 simples in wich duration is ranged between 2 secondes and 10 day
Id duration y
1 23 0
2 64 1
3 15 1
I would like to draw a plot that can visualize the vriation of duration and the probability to get outcome positive.
I need to work with python this problem
what you want is a histgram of duration and a probability distribution of y.
import math
import matplotlib.pyplot as plt
duration=[23,64,15,18,1,63,21,27]
y=[0,1,1,0,0,1,0,1]
BUCKET_SIZE=10
numToBucket=lambda x: round(math.ceil(x/BUCKET_SIZE))
yCount=[0]*numToBucket(max(duration))
hist=[0]*numToBucket(max(duration))
for i in range(len(duration)):
index=numToBucket(duration[i])-1
hist[index]+=1
yCount[index]+=y[i]
probDist=[0 if (yCount[i]==0) else yCount[i]/hist[i] for i in range(len(hist))]
axis=[i*BUCKET_SIZE for i in range(len(hist))]
print(hist)
print(yCount)
print(probDist)
plt.bar(axis, hist, align='center', alpha=0.5)
plt.title('Histogram')
plt.show()

Crop a square shape around a centroid (numpy) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an numpy array image which contain circles. I extracted the whole x,y centroids (in pixels) of these circles (a numpy array as well). Now, I want to crop a square around each x,y centroid.
Can someone instruct me how to solve it?
Note that I didn't find any question in Stack that deals with crop around a specific coordinate.
Thank you!
If your centroid has indices i,j and you want to crop a square of size 2*w+1 around it on a numpy array a, you can do
a[i-w:i+w+1,j-w:j+w+1]
This is provided your indices are always more than w from the boundary.
If they're not, you can do
imin = max(0,i-w)
imax = min(a.shape[0],i+w+1)
jmin = max(0,j-w)
jmax = min(a.shape[1],j+w+1)
a[imin:imax,jmin:jmax]

How to generate a graph from reading numbers in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that there are several ways to graph using APIs in Python. But let's say I have the following code:
from random import randint
i = 1
while i < 1000:
num = randint(0,9)
print num
// Add num to a graph that is Random Number versus Time
// Show graph
What should I do such that I can generate a graph that has Random numbers (from 0->9) versus Time.
Following your code (changing 1000 to 100 for a clearer plot), you can do:
import matplotlib.pyplot as plt
import numpy as np
from random import randint
i = 1
numbers = [0]*100
while i < 100:
numbers[i] = randint(0,9)
i += 1
plt.plot(np.arange(1,101),numbers, 'o')
plt.xlabel('Time')
plt.show()
This will give you:

Categories

Resources