I'm curious if there is a built in function to transform an array of values into a cumulative array of values.
Example:
input = np.asarray([0.000,1.500,2.100,5.000])
into
[0.000,1.500,3.600,8.600]
Thanks!
Use in-built cumsum from NumPy to get the cumulative sum of your array inputt as
inputt = np.asarray([0.000,1.500,2.100,5.000])
print (np.cumsum(inputt))
# [0. 1.5 3.6 8.6]
I renamed your array because input is already an in-built function in python to get the user input from the keyboard
Related
(new to python so I apologize if this question is basic)
Say I create a function that will calculate some equation
def plot_ev(accuracy,tranChance,numChoices,reward):
ev=(reward-numChoices)*1-np.power((1-accuracy),numChoices)*tranChance)
return ev
accuracy, tranChance, and numChoices are each float arrays
e.g.
accuracy=np.array([.6,.7,.8])
tranChance=np.array([.6,.7,8])
numChoices=np.array([2,.3,4])
how would I run and plot plot_ev over my 3 arrays so that I end up with an output that has all combinations of elements (ideally not running 3 forloops)
ideally i would have a single plot showing the output of all combinations (1st element from accuracy with all elements from transChance and numChoices, 2nd element from accuracy with all elements from transChance and numChoices and so on )
thanks in advance!
Use numpy.meshgrid to make an array of all the combinations of values of the three variables.
products = np.array(np.meshgrid(accuracy, tranChance, numChoices)).T.reshape(-1, 3)
Then transpose this again and extract three longer arrays with the values of the three variables in every combination:
accuracy_, tranChance_, numChoices_ = products.T
Your function contains only operations that can be carried out on numpy arrays, so you can then simply feed these arrays as parameters into the function:
reward = ?? # you need to set the reward value
results = plot_ev(accuracy_, tranChance_, numChoices_, reward)
Alternatively consider using a pandas dataframe which will provide clearer labeling of the columns.
import pandas as pd
df = pd.DataFrame(products, columns=["accuracy", "tranChance", "numChoices"])
df["ev"] = plot_ev(df["accuracy"], df["tranChance"], df["numChoices"], reward)
I have this code that contains a for loop to print out this result for me.
Could I transfer this to Numpy Array instead of for loop and less memory?
categorical__unique = df.select_dtypes(['object']).columns
for col in categorical__unique:
print('{} : {} unique value(s)'.
format(col, df[col].nunique()))
I am trying to make this categorial_unique value to an array and then use the functions of the NumPy array instead of for loop.
To convert a dataframe into a matrix you have to use the "to_numpy" function from pandas
categorical_unique = df.select_dtypes(['object']).to_numpy()
I want to calculate a resultant State Matrix by multiplying initial state matrix and transition matrix for given amount of time.
For example if period is 1 month, then State1 [matrix] will be State[]*Transition[]
If period is 2 then State2[] = State1[]*Transition
3 then State3[]=State2[]* Transition
...and so on
I'm having a problem to iterate the values of resultant matrix using loops:
I don't know how to iterate values via multiplication in python.
Here's my code:
import numpy as np
statevector=np.array([0.2,0.8])
transition=np.array([[0.9,0.1],[0.7,0.3]])
for product in range(0,1):
product=statevector
product=np.dot(statevector,transition)
product=product+1
r=np.dot(product,transition)
print(r)
If I understand you correctly, you want to repeatedly multiply the statevector with the transition matrix. One way to do this is in a for loop like this:
import numpy as np
statevector=np.array([0.2,0.8])
transition=np.array([[0.9,0.1],[0.7,0.3]])
states = [statevector]
for i in range(10):
statevector=np.dot(statevector,transition)
states.append(statevector)
print(states)
Every iteration I'm adding the new state to the list states. The end result is:
[array([0.2, 0.8]), array([0.74, 0.26]), array([0.848, 0.152]), array([0.8696, 0.1304]), array([0.87392, 0.12608]), array([0.874784, 0.125216]), array([0.8749568, 0.1250432]), array([0.87499136, 0.12500864]), array([0.87499827, 0.12500173]), array([0.87499965, 0.12500035]), array([0.87499993, 0.12500007])]
I have a specific requirement for this problem. I need it to be simple and fast.
My problem:
I have two 2D arrays and I need to replace values in 1. array by values in 2. array according to condition. That is if element in x,y position in 1. array is smaller than element in x,y position in 2. array, then replace element in 1. array by element in 2. array.
what I tried and is not working:
import numpy as np
arr = np.random.randint(3,size=(2, 2))
arr2 = np.random.randint(3,size=(2, 2))
print(arr)
print(arr2)
arr[arr<arr2]=arr2 # Doesnt work.
This raises TypeError:
TypeError: NumPy boolean array indexing assignment requires a 0 or 1-dimensional input, input has 2 dimensions.
I can see, that it would be possible to iterate through columns or rows, but I believe it can be done without iteration.
Thanks in advance
I have a numpy array of indexes e.g. [1,3,12]. I want to create another array from this such that at these indexes, I get a non-zero elements e.g. 1. So in this case, with input [1,3,12], I should get [0,1,0,1,0,0,0,0,0,0,0,0,1]. I can do it in a for loop, is there a short numpy function to achieve this?
With numpy you can index with lists directly:
a = [1,3,12]
vector = numpy.zeros(shape=max(a) + 1)
vector[a] = 1