# Python numpy.ravel() Examples

The following are 30 code examples for showing how to use numpy.ravel(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module , or try the search function .

Example 1
def test_knn(datasets_dimred, genes, labels, idx, distr, xlabels):
knns = [ 5, 10, 50, 100 ]
len_distr = len(distr)
for knn in knns:
integrated = assemble(datasets_dimred[:], knn=knn, sigma=150)
X = np.concatenate(integrated)
distr.append(sil(X[idx, :], labels[idx]))
for d in distr[:len_distr]:
print(ttest_ind(np.ravel(X[idx, :]), np.ravel(d)))
xlabels.append(str(knn))
print('')

plt.figure()
plt.boxplot(distr, showmeans=True, whis='range')
plt.xticks(range(1, len(xlabels) + 1), xlabels)
plt.ylabel('Silhouette Coefficient')
plt.ylim((-1, 1))
plt.savefig('param_sensitivity_{}.svg'.format('knn')) 
Example 2
def compute_depth_gravity_profiles(pressures, densities, surface_gravity, outer_radius):
gravity = [surface_gravity] * len(pressures) # starting guess
n_gravity_iterations = 5
for i in range(n_gravity_iterations):
# Integrate the hydrostatic equation
# Make a spline fit of densities as a function of pressures
rhofunc = UnivariateSpline(pressures, densities)
# Make a spline fit of gravity as a function of depth
gfunc = UnivariateSpline(pressures, gravity)

# integrate the hydrostatic equation
depths = np.ravel(odeint((lambda p, x: 1./(gfunc(x) * rhofunc(x))), 0.0, pressures))

poisson = lambda p, x: 4.0 * np.pi * burnman.constants.G * rhofunc(x) * x * x
return depths, gravity

# BEGIN USER INPUTS

# Declare the rock we want to use 
Example 3
def prediction(self, input_data='', mode='test_data'):

prediction = {}
vote = []

for model in self.models:

prediction = model.prediction(input_data, mode)
vote.append(prediction['prediction'])

prediction_return = max(set(vote), key=vote.count)

if mode == 'future_data':
data = input_data.split()
input_data_x = [float(v) for v in data]
input_data_x = np.ravel(input_data_x)
return {"input_data_x": input_data_x, "input_data_y": None, "prediction": prediction_return}
else:
data = input_data.split()
input_data_x = [float(v) for v in data[:-1]]
input_data_x = np.ravel(input_data_x)
input_data_y = float(data[-1])
return {"input_data_x": input_data_x, "input_data_y": input_data_y, "prediction": prediction_return} 
Example 4
def prediction(self, input_data='', mode='test_data'):

prediction = {}
prediction_sum = 0

for model in self.models:

prediction = model.prediction(input_data, mode)
prediction_sum = prediction_sum + prediction['prediction']

prediction_return = float(prediction_sum / len(self.models))

if mode == 'future_data':
data = input_data.split()
input_data_x = [float(v) for v in data]
input_data_x = np.ravel(input_data_x)
return {"input_data_x": input_data_x, "input_data_y": None, "prediction": prediction_return}
else:
data = input_data.split()
input_data_x = [float(v) for v in data[:-1]]
input_data_x = np.ravel(input_data_x)
input_data_y = float(data[-1])
return {"input_data_x": input_data_x, "input_data_y": input_data_y, "prediction": prediction_return} 
Example 5
def test_sigma(datasets_dimred, genes, labels, idx, distr, xlabels):
sigmas = [ 10, 50, 100, 200 ]
len_distr = len(distr)
for sigma in sigmas:
integrated = assemble(datasets_dimred[:], sigma=sigma)
X = np.concatenate(integrated)
distr.append(sil(X[idx, :], labels[idx]))
for d in distr[:len_distr]:
print(ttest_ind(np.ravel(X[idx, :]), np.ravel(d)))
xlabels.append(str(sigma))
print('')

plt.figure()
plt.boxplot(distr, showmeans=True, whis='range')
plt.xticks(range(1, len(xlabels) + 1), xlabels)
plt.ylabel('Silhouette Coefficient')
plt.ylim((-1, 1))
plt.savefig('param_sensitivity_{}.svg'.format('sigma')) 
Example 6
def test_alpha(datasets_dimred, genes, labels, idx, distr, xlabels):
alphas = [ 0, 0.05, 0.20, 0.50 ]
len_distr = len(distr)
for alpha in alphas:
integrated = assemble(datasets_dimred[:], alpha=alpha, sigma=150)
X = np.concatenate(integrated)
distr.append(sil(X[idx, :], labels[idx]))
for d in distr[:len_distr]:
print(ttest_ind(np.ravel(X[idx, :]), np.ravel(d)))
xlabels.append(str(alpha))
print('')

plt.figure()
plt.boxplot(distr, showmeans=True, whis='range')
plt.xticks(range(1, len(xlabels) + 1), xlabels)
plt.ylabel('Silhouette Coefficient')
plt.ylim((-1, 1))
plt.savefig('param_sensitivity_{}.svg'.format('alpha')) 
Example 7
def test_approx(datasets_dimred, genes, labels, idx, distr, xlabels):
integrated = assemble(datasets_dimred[:], approx=False, sigma=150)
X = np.concatenate(integrated)
distr.append(sil(X[idx, :], labels[idx]))
len_distr = len(distr)
for d in distr[:len_distr]:
print(ttest_ind(np.ravel(X[idx, :]), np.ravel(d)))
xlabels.append('Exact NN')
print('')

plt.figure()
plt.boxplot(distr, showmeans=True, whis='range')
plt.xticks(range(1, len(xlabels) + 1), xlabels)
plt.ylabel('Silhouette Coefficient')
plt.ylim((-1, 1))
plt.savefig('param_sensitivity_{}.svg'.format('approx')) 
Example 8
def test_perplexity(datasets_dimred, genes, labels, idx,
distr, xlabels):
X = np.concatenate(datasets_dimred)

perplexities = [ 10, 100, 500, 2000 ]
len_distr = len(distr)
for perplexity in perplexities:
embedding = fit_tsne(X, perplexity=perplexity)
distr.append(sil(embedding[idx, :], labels[idx]))
for d in distr[:len_distr]:
print(ttest_ind(np.ravel(X[idx, :]), np.ravel(d)))
xlabels.append(str(perplexity))
print('')

plt.figure()
plt.boxplot(distr, showmeans=True, whis='range')
plt.xticks(range(1, len(xlabels) + 1), xlabels)
plt.ylabel('Silhouette Coefficient')
plt.ylim((-1, 1))
plt.savefig('param_sensitivity_{}.svg'.format('perplexity')) 
Example 9
def kernel_qchem_inter_rf_pos_neg(self, **kw):
""" This is constructing the E_m-E_n and E_n-E_m matrices """
h_rpa = diagflat(concatenate((ravel(self.FmE),-ravel(self.FmE))))
print(h_rpa.shape)

nf = self.nfermi[0]
nv = self.norbs-self.vstart[0]
vs = self.vstart[0]
neh = nf*nv
x = self.mo_coeff[0,0,:,:,0]
pab2v = self.pb.get_ac_vertex_array()
self.pmn2v = pmn2v = einsum('nb,pmb->pmn', x[:nf,:], einsum('ma,pab->pmb', x[vs:,:], pab2v))
pmn2c = einsum('qp,pmn->qmn', self.hkernel_den, pmn2v)
meri = einsum('pmn,pik->mnik', pmn2c, pmn2v).reshape((nf*nv,nf*nv))
#print(meri.shape)
#meri.fill(0.0)
h_rpa[:neh, :neh] = h_rpa[:neh, :neh]+meri
h_rpa[:neh, neh:] = h_rpa[:neh, neh:]+meri
h_rpa[neh:, :neh] = h_rpa[neh:, :neh]-meri
h_rpa[neh:, neh:] = h_rpa[neh:, neh:]-meri
edif, s2z = np.linalg.eig(h_rpa)
print(abs(h_rpa-h_rpa.transpose()).sum())
print('edif', edif.real*27.2114)

return 
Example 10
def test_big_cell():
import time

a = 1
ncell = (2, 2, 2)
Lvecs = np.diag(ncell) * a
unit_cell = np.zeros((4, 3))
unit_cell[1:] = (np.ones((3, 3)) - np.eye(3)) * a / 2

grid = np.meshgrid(*map(np.arange, ncell), indexing="ij")
shifts = np.stack(list(map(np.ravel, grid)), axis=1)
supercell = (shifts[:, np.newaxis] + unit_cell[np.newaxis]).reshape(1, -1, 3)

configs = supercell.repeat(1000, axis=0)
configs += np.random.randn(*configs.shape) * 0.1

df = run(Lvecs, configs, 8)
df = df.groupby("qmag").mean().reset_index()

large_q = df[-35:-10]["Sq"]
mean = np.mean(large_q - 1)
rms = np.sqrt(np.mean((large_q - 1) ** 2))
assert np.abs(mean) < 0.01, mean
assert rms < 0.1, rms 
Example 11
def __init__(self, qlist=None, Lvecs=None, nq=4):
"""
Inputs:
qlist: (n, 3) array-like. If qlist is provided, Lvecs and nq are ignored
Lvecs: (3, 3) array-like of lattice vectors. Required if qlist is None
nq: int, if qlist is nonzero, use a uniform grid of shape (nq, nq, nq)
"""
if qlist is not None:
self.qlist = qlist
else:
assert (
Lvecs is not None
), "need to provide either list of q vectors or lattice vectors"
Gvecs = np.linalg.inv(Lvecs).T * 2 * np.pi
qvecs = list(map(np.ravel, np.meshgrid(*[np.arange(nq)] * 3)))
qvecs = np.stack(qvecs, axis=1)
self.qlist = np.dot(qvecs, Gvecs) 
Example 12
def __call__(self, transform_xy, x1, y1, x2, y2):
x_, y_ = np.linspace(x1, x2, self.nx), np.linspace(y1, y2, self.ny)
x, y = np.meshgrid(x_, y_)
lon, lat = transform_xy(np.ravel(x), np.ravel(y))

with np.errstate(invalid='ignore'):
if self.lon_cycle is not None:
lon0 = np.nanmin(lon)
# Changed from 180 to 360 to be able to span only
# 90-270 (left hand side)
lon -= 360. * ((lon - lon0) > 360.)
if self.lat_cycle is not None:
lat0 = np.nanmin(lat)
# Changed from 180 to 360 to be able to span only
# 90-270 (left hand side)
lat -= 360. * ((lat - lat0) > 360.)

lon_min, lon_max = np.nanmin(lon), np.nanmax(lon)
lat_min, lat_max = np.nanmin(lat), np.nanmax(lat)

lon_min, lon_max, lat_min, lat_max = \

return lon_min, lon_max, lat_min, lat_max 
Example 13
def block2row(array, row, folder, block_id=None):
if array.shape[0] == windowSize:
# Parameters
name_string = str(block_id[0] + 1)
m,n = array.shape
u = m + 1 - windowSize
v = n + 1 - windowSize

# Get Starting block indices
start_idx = np.arange(u)[:,None]*n + np.arange(v)

# Get offsetted indices across the height and width of input array
offset_idx = np.arange(windowSize)[:,None]*n + np.arange(windowSize)

# Get all actual indices & index into input array for final output
flat_array = np.take(array,start_idx.ravel()[:,None] + offset_idx.ravel())

# Save to (dask) array in .zarr format
file_name = path + folder + name_string + 'r' + row + '.zarr'
zarr.save(file_name, flat_array)

return array

# Divide an image in overlapping blocks 
Example 14
def test_minmax_func(self):
# Tests minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
# max doesn't work if shaped
xr = np.ravel(x)
xmr = ravel(xm)
# following are true because of careful selection of data
assert_equal(max(xr), maximum.reduce(xmr))
assert_equal(min(xr), minimum.reduce(xmr))

assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
x = arange(5)
y = arange(5) - 2
assert_equal(minimum(x, y), where(less(x, y), x, y))
assert_equal(maximum(x, y), where(greater(x, y), x, y))
assert_(minimum.reduce(x) == 0)
assert_(maximum.reduce(x) == 4)

x = arange(4).reshape(2, 2)
assert_equal(maximum.reduce(x, axis=None), 2) 
Example 15
def test_ravel(self):
# Tests ravel
a = array([[1, 2, 3, 4, 5]], mask=[[0, 1, 0, 0, 0]])
aravel = a.ravel()
a = array([0, 0], mask=[1, 1])
aravel = a.ravel()
# Checks that small_mask is preserved
a = array([1, 2, 3, 4], mask=[0, 0, 0, 0], shrink=False)
# Test that the fill_value is preserved
a.fill_value = -99
a.shape = (2, 2)
ar = a.ravel()
assert_equal(ar._data, [1, 2, 3, 4])
assert_equal(ar.fill_value, -99)
# Test index ordering
assert_equal(a.ravel(order='C'), [1, 2, 3, 4])
assert_equal(a.ravel(order='F'), [1, 3, 2, 4]) 
Example 16
def _update_diagnostics(state, diagnostics):
# Update logscore.
cc_logscore = diagnostics.get('logscore', np.array([]))
new_logscore = map(float, np.ravel(cc_logscore).tolist())
state.diagnostics['logscore'].extend(new_logscore)

# Update column_crp_alpha.
cc_column_crp_alpha = diagnostics.get('column_crp_alpha', [])
new_column_crp_alpha = map(float, np.ravel(cc_column_crp_alpha).tolist())
state.diagnostics['column_crp_alpha'].extend(list(new_column_crp_alpha))

# Update column_partition.
def convert_column_partition(assignments):
return [
(col, int(assgn))
for col, assgn in zip(state.outputs, assignments)
]
new_column_partition = diagnostics.get('column_partition_assignments', [])
if len(new_column_partition) > 0:
assert len(new_column_partition) == len(state.outputs)
trajectories = np.transpose(new_column_partition)[0].tolist()
state.diagnostics['column_partition'].extend(
map(convert_column_partition, trajectories)) 
Example 17
def _check_transformer_output(transformer, dataset, expected):
"""
Given a transformer and a spark dataset, check if the transformer
produces the expected results.
"""
analyzed_df = tfs.analyze(dataset)
out_df = transformer.transform(analyzed_df)

# Collect transformed values
out_colnames = list(_output_mapping.values())
_results = []
for row in out_df.select(out_colnames).collect():
curr_res = [row[colname] for colname in out_colnames]
_results.append(np.ravel(curr_res))
out_tgt = np.hstack(_results)

_err_msg = 'not close => shape {} != {}, max_diff {} > {}'
max_diff = np.max(np.abs(expected - out_tgt))
err_msg = _err_msg.format(expected.shape, out_tgt.shape,
max_diff, _all_close_tolerance)
assert np.allclose(expected, out_tgt, atol=_all_close_tolerance), err_msg 
Example 18
def _compute_gravity(self, density, gravity_bottom):
"""
Computes the gravity of a layer
Used by _evaluate_eos()
"""
# Create a spline fit of density as a function of radius
# Numerically integrate Poisson's equation

def poisson(p, x): return 4.0 * np.pi * \
constants.G * rhofunc(x) * x * x
grav = np.ravel(
odeint( poisson, gravity_bottom *

grav[0] = 0
else:
return grav 
Example 19
def _compute_pressure(self, density, gravity, pressure_top):
"""
Calculate the pressure profile based on density and gravity.  This integrates
the equation for hydrostatic equilibrium  P = rho g z.
Used by _evaluate_eos()
"""
# flip radius, density and gravity to increasing pressure
density = density[::-1]
gravity = gravity[::-1]
# Make a spline fit of density as a function of depth
rhofunc = UnivariateSpline(depthfromtop, density)
# Make a spline fit of gravity as a function of depth
gfunc = UnivariateSpline(depthfromtop, gravity)

# integrate the hydrostatic equation
pressure = np.ravel(
odeint((lambda p, x: gfunc(x) * rhofunc(x)), pressure_top, depthfromtop))

return pressure[::-1] 
Example 20
def inpaint(img, threshold=1):
h, w = img.shape[:2]

if len(img.shape) == 3:  # RGB
mask = np.all(img == 0, axis=2).astype(np.uint8)

else:  # depth
xx, yy = np.meshgrid(np.arange(w), np.arange(h))
interp = interpolate.NearestNDInterpolator(xym, img)
img = interp(np.ravel(xx), np.ravel(yy)).reshape(xx.shape)

return img 
Example 21
def hyperball(ndim, radius):
"""Return a binary morphological filter containing pixels within radius.

Parameters
----------
ndim : int
The number of dimensions of the filter.

Returns
-------
ball : array of bool, shape [2 * radius + 1,] * ndim
The required structural element
"""
size = 2 * radius + 1

coords = np.mgrid[[slice(None, size),] * ndim].reshape(ndim, -1).T
distances = np.ravel(spatial.distance_matrix(coords, center))

ball = np.zeros((size,) * ndim, dtype=bool)
ball.ravel()[selector] = True
return ball 
Example 22
def morista_index(points):
# Morisita Index of Dispersion

N = points.shape[1]

ims = []
for i in range(1, N):
bins, _, _ = np.histogram2d(points[0], points[1], i)

# I_M  = Q * (\sum_{k=1}^{Q}{n_k * (n_k - 1)})/(N * (N _ 1))

# Eqn 1.
I_M = Q * np.sum(np.ravel(bins) * (np.ravel(bins) - 1)) / (N * (N - 1))
ims.append([i, I_M])

return np.array(ims).T[1].max() 
Example 23
def ravel(parameter, random_state=None):
"""
Flatten a Parameter.

Parameters
----------
parameter: Parameter
A Parameter object

Returns
-------
flatvalue: ndarray
a flattened array of shape (prod(parameter.shape),)
flatbounds: list
a list of bound tuples of length prod(parameter.shape)
"""
flatvalue = np.ravel(parameter.rvs(random_state=random_state))
flatbounds = [parameter.bounds
for _ in range(np.prod(parameter.shape, dtype=int))]

return flatvalue, flatbounds 
Example 24
def cost0(params, Y, R, num_features):
Y = np.matrix(Y)  # (1682, 943)
R = np.matrix(R)  # (1682, 943)
num_movies = Y.shape[0]
num_users = Y.shape[1]

# reshape the parameter array into parameter matrices
X = np.matrix(np.reshape(params[:num_movies * num_features], (num_movies, num_features)))  # (1682, 10)
Theta = np.matrix(np.reshape(params[num_movies * num_features:], (num_users, num_features)))  # (943, 10)

# initializations
J = 0
X_grad = np.zeros(X.shape)  # (1682, 10)
Theta_grad = np.zeros(Theta.shape)  # (943, 10)

# compute the cost
error = np.multiply((X * Theta.T) - Y, R)  # (1682, 943)
squared_error = np.power(error, 2)  # (1682, 943)
J = (1. / 2) * np.sum(squared_error)

# unravel the gradient matrices into a single array

return J, grad 
Example 25
def cost1(params, Y, R, num_features, learning_rate):
Y = np.matrix(Y)  # (1682, 943)
R = np.matrix(R)  # (1682, 943)
num_movies = Y.shape[0]
num_users = Y.shape[1]

# reshape the parameter array into parameter matrices
X = np.matrix(np.reshape(params[:num_movies * num_features], (num_movies, num_features)))  # (1682, 10)
Theta = np.matrix(np.reshape(params[num_movies * num_features:], (num_users, num_features)))  # (943, 10)

# initializations
J = 0
X_grad = np.zeros(X.shape)  # (1682, 10)
Theta_grad = np.zeros(Theta.shape)  # (943, 10)

# compute the cost
error = np.multiply((X * Theta.T) - Y, R)  # (1682, 943)
squared_error = np.power(error, 2)  # (1682, 943)
J = (1. / 2) * np.sum(squared_error)

J = J + ((learning_rate / 2) * np.sum(np.power(Theta, 2)))
J = J + ((learning_rate / 2) * np.sum(np.power(X, 2)))

# calculate the gradients with regularization
X_grad = (error * Theta) + (learning_rate * X)
Theta_grad = (error.T * X) + (learning_rate * Theta)

# unravel the gradient matrices into a single array

return J, grad 
Example 26
def load_data():
"""for ex5
d['X'] shape = (12, 1)
pandas has trouble taking this 2d ndarray to construct a dataframe, so I ravel
the results
"""
return map(np.ravel, [d['X'], d['y'], d['Xval'], d['yval'], d['Xtest'], d['ytest']]) 
Example 27
def prediction(self, input_data='', mode='test_data', with_missing_data=False):

prediction = {}

if (self.status != 'train'):
print("Please load train data and init W then train the W first.")
return prediction

if (input_data == ''):
print("Please input test data for prediction.")
return prediction

if mode == 'future_data':
data = input_data.split()
input_data_x = [v for v in data]
input_data_x = np.ravel(input_data_x)
input_data_x = np.insert(input_data_x, 0, '1')
prediction = self.score_function(input_data_x, self.W, with_missing_data)
return {"input_data_x": input_data_x, "input_data_y": None, "prediction": prediction}
else:
data = input_data.split()
input_data_x = [v for v in data[:-1]]
input_data_x = np.ravel(input_data_x)
input_data_x = np.insert(input_data_x, 0, '1')
input_data_y = data[-1]
prediction = self.score_function(input_data_x, self.W, with_missing_data)
return {"input_data_x": input_data_x, "input_data_y": input_data_y, "prediction": prediction} 
Example 28
def prediction(self, input_data='', mode='test_data'):

'''
Make prediction
input test data
output the prediction
'''

prediction = {}

if (self.status != 'train'):
print("Please load train data and init W then train the W first.")
return prediction

if (input_data == ''):
print("Please input test data for prediction.")
return prediction

if mode == 'future_data':
data = input_data.split()
input_data_x = [float(v) for v in data]
np.array(input_data_x).reshape(1, -1),
self.feature_transform_mode,
self.feature_transform_degree
)
input_data_x = np.ravel(input_data_x)
prediction = self.score_function(input_data_x, self.W)
return {"input_data_x": input_data_x, "input_data_y": None, "prediction": prediction}
else:
data = input_data.split()
input_data_x = [float(v) for v in data[:-1]]
np.array(input_data_x).reshape(1, -1),
self.feature_transform_mode,
self.feature_transform_degree
)
input_data_x = np.ravel(input_data_x)
input_data_y = float(data[-1])
prediction = self.score_function(input_data_x, self.W)
return {"input_data_x": input_data_x, "input_data_y": input_data_y, "prediction": prediction} 
Example 29
def calculate_alpha_s(self, weak_learner, s):

alpha = 0.0
new_s = s

data_num = len(weak_learner.train_Y)
X = []

for i in range(data_num):
predict_string = np.array(list(map(str, weak_learner.train_X[i])))
predict_string = ' '.join(predict_string[1:]) + ' ' + str(weak_learner.train_Y[i])
prediction = weak_learner.prediction(predict_string, 'test_data')
X.append([float(prediction['prediction'])])

X = np.array(X)

linear = linear_regression.LinearRegression()
linear.train_X = X
linear.train_Y = weak_learner.train_Y - s
linear.set_param()
linear.init_W()
linear.train()

alpha = linear.W[0]
new_s = s + alpha * np.ravel(X)

return alpha, new_s 
Example 30
def write_data_csv(fname, frames, preproc):
return result