Creating 3-D Numpy array with parameters - python

So I need to write code that accomplishes the following:
Write a Python code that produces a variable op_table that is a numpy array with three
axes, i, j, and k. Define three arrays:
xi ranges from 0 (included) to 9 (included) in steps of 1,
yj ranges form 10 (included) to 11 (included) in 20 equal-size steps,
zk ranges form 10 to 106 in five steps (i.e. with six entries total), where zk=10zk−1.
Then create the final array op_table that satisfies:
op_table[i,j,k]=sin(xi)⋅yj+zk
My question lies in how to initially set the values. I've only seen numpy arrays created in manners such as np.array([1,2,3,4]) or np.arrange(10). Also, how is this set-up? Is the first column the x-axis, second the y-axis and so forth?
import numpy as np
import math
xi = np.linspace(0,9, num=10)
yj = np.linspace(10,11,20, endpoint=True)
zk = [10, 10**2, 10**3, 10**4, 10**5, 10**6]
op_table = np.random.rand(10,20,6)
for i in range (0,10):
for j in range (0,20):
for k in range (0,6):
op_table[i,j,k] = math.sin(xi[i]) * yj[j] + zk[k]

Don't personally believe in spoon-feeding answers, but it looks like you've misinterpreted the problem. The problem doesn't actually require that you generate any matrix, except by solving the second equation. Numpy happens to have a very helpful function called linspace that does almost exactly this.
import numpy as np
xi = np.linspace(0, 10)
yj = np.linspace(10, 11, 20)
Other than that, this seems to be a math problem, and this should get you 80% of the way to a solution. If you need help with the math, there's another stackexchange for that.
More np.linspace docs: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
Math stackexchange: https://math.stackexchange.com/

Related

a uniform data structure that can represent an ndarray with various size along a given axis

I can use the following code to generate three dimensional array.
import numpy as np
x1 = np.random.rand(8,9,10)
In some scenarios, the studied data set (or array) have various length along the axis 0, In other words. A subset may be of shape (8, 9, 10), and another subset maybe of shape (7,9,10). All these subsets are of the same size along the second and the third axis. If I still want to represent the whole data set using the same data structure, how to achieve this goal?
One solution would be to use awkward-array:
https://github.com/scikit-hep/awkward-1.0
>>> import numpy as np
>>> import awkward as ak
>>> numpy_arrays = [np.random.rand(8,9,10), np.random.rand(7,9,10)]
>>> irregular_array = ak.Array(numpy_arrays)
>>> irregular_array
<Array [[[ ... ]]] type='var * 9 * 10 * int64'>

Normal Distribution using Numpy

I want to generate a dataset with m random data points of k dimensions each. Thus resulting in data size of shape (m, k). These points should be i.i.d. from a normal distribution with mean 0 and standard deviation 1. There are 2 ways of generating these points.
First way:
import numpy as np
# Initialize the array
Z = np.zeros((m, k))
# Generate each point of each dimension independent of each other
for datapoint in range(m):
z = [np.random.standard_normal() for _ in range(k)]
Z[datapoint] = z[:]
Second way:
import numpy as np
# Directly sample the points
Z = np.random.normal(0, 1, (m, k))
What I think is the 2nd way gives a resulting dataset not independent of each other but the 1st one gives i.i.d dataset of points. Is this the difference between the 2 pieces of code?
My assumption would be that standard_normal is just normal with "standard" parameters (mean=0 and std=1).
Let's test that:
import numpy as np
rng0 = np.random.default_rng(43210)
rng1 = np.random.default_rng(43210)
print(rng0.standard_normal(10))
print(rng1.normal(0, 1, 10))
which gives:
[ 0.62824213 -1.18535536 -1.18141382 -0.74127753 -0.41945915 1.02656223 -0.64935657 1.70859865 0.47731614 -1.12700957]
[ 0.62824213 -1.18535536 -1.18141382 -0.74127753 -0.41945915 1.02656223 -0.64935657 1.70859865 0.47731614 -1.12700957]
So I think that assumption was correct.

Newton method in python for multivariables (system of equations)

My code is running fine for first iteration but after that it outputs the following error:
ValueError: matrix must be 2-dimensional
To the best of my knowledge (which is not much in python), my code is correct. but I don't know, why it is not running correctly for all given iterations. Could anyone help me in this problem.
from __future__ import division
import numpy as np
import math
import matplotlib.pylab as plt
import sympy as sp
from numpy.linalg import inv
#initial guesses
x = -2
y = -2.5
i1 = 0
while i1<5:
F= np.matrix([[(x**2)+(x*y**3)-9],[(3*y*x**2)-(y**3)-4]])
theta = np.sum(F)
J = np.matrix([[(2*x)+y**3, 3*x*y**2],[6*x*y, (3*x**2)-(3*y**2)]])
Jinv = inv(J)
xn = np.array([[x],[y]])
xn_1 = xn - (Jinv*F)
x = xn_1[0]
y = xn_1[1]
#~ print theta
print xn
i1 = i1+1
I believe xn_1 is a 2D matrix. Try printing it you and you will see [[something], [something]]
Therefore to get the x and y, you need to use multidimensional indexing. Here is what I did
x = xn_1[0,0]
y = xn_1[1,0]
This works because within the 2D matrix xn_1 are two single element arrays. Therefore we need to further index 0 to get that single element.
Edit: To clarify, xn_1[1,0] means to index 1 and then take that subarray and index 0 on that. And although according to Scipy it may seem that it should be functionally equivalent to xn_1[1][0], that only applies to the general np.array type and not the np.matrix type. Here is an excellent thread on SO that explains this.
So you should use the xn_1[1,0] way to get the element you want.
xn_1 is a numpy matrix, so it's elements are accessed with the item() method, not like an array. (with []s)
So just change
x = xn_1[0]
y = xn_1[1]
to
x = xn_1.item(0)
y = xn_1.item(1)

Mean of a specific part of the matrix

I must calculate the mean in this specific part of the matrix, that was generated with random numbers, my work so far:
import random as rd
import numpy as np
matriz= np.zeros([12, 12])
for i in range(0,12):
for j in range(0,12):
matriz[i,j]=rd.randint(0,10)
Your problem is trying to fit an algorithm. There's clearly a structure to the "marked" section of your matrix, so your problem is in trying to see/identify this structure, in order to fit it.
What I see is a pattern: starting with row 0, you're taking columns from 1 to n-1, then in row 1 you're taking columns 2 to n-2, etc. So basically, you're summing the coordinates for each row in range(rowIndex+1, len(columns)-(rowIndex+1))
There may be some more elegant ways to achieve this, but I think this will work:
import random as rd
import numpy as np
l, w = 12, 12 # matrix has dimensions l=12, w=12
matriz= np.zeros([l, w])
for i in range(l):
for j in range(w):
matriz[i,j]=rd.randint(0,10)
vals = []
for i in range(int(l/2)): # iterate through rows 0 to 4
for j in range(i+1, w-(i+1)):
vals.append(matrix[i,j])
# get the mean:
print 'mean is {}'.format(sum(vals)/len(vals))
Note this probably doesn't work for a non-square matrix.

Spliting a numpy ndarray using slices

Python 2.7.3
numpy 1.8.0
Hi all,
I am using numpy for a few months and I need help with some basic stuff. The code below should work and the bit I need help with is highlighted (# <<<<<<<):
import numpy as np
rng = np.random.RandomState(12345)
samples = np.array(np.arange(400).reshape(50, 8))
nSamples = samples.shape[0]
FOLDS = 15
foldSize = nSamples / FOLDS
indices = np.arange(nSamples)
rng.shuffle(indices)
slices = [slice(i * foldSize ,
(i + 1) * foldSize, 1) for i in xrange(FOLDS + 1)]
for i in xrange(len(slices)):
y = samples[indices[slices[i]]]
x = np.array([x for x in samples if x not in samples[slices[i]]]) # <<<<<<<
#do some processing with x and y
Basically random slices a 2D array row-wisely, use the full array to process and test in the sliced bit, then repeat for the for another slice util everything is done (It called an cross-validation experiment).
My question is: Is there a better way to select all rows in a ndarray but a slice? Am I missing something? What is the advised way to [x for x in samples if x not in samples[indices][0:3]] ?
Thanks in advance.
ps: masked arrays does not solve my problem.
ps1: I know it's already implemented elsewhere, I just need to learn.
You can create a boolean array for the rows to select as follows:
indices_to_ignore = [1, 2, 3]
mask = np.ones(samples.shape[:1], dtype=np.bool)
mask[indices_to_ignore] = 0
samples[mask].shape

Categories