本文整理汇总了Python中numpy.arange函数的典型用法代码示例。如果您正苦于以下问题:Python arange函数的具体用法?Python arange怎么用?Python arange使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了arange函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _update_datamap
def _update_datamap(self):
self._last_region = []
# Create a new grid of the appropriate size, initialize it with new
# Cell instance (of type self.celltype), and perform point insertion
# on the new data.
if self._data is None:
self._cellgrid = array([], dtype=object)
self._cell_lefts = array([])
self._cell_bottoms = array([])
else:
num_x_cells, num_y_cells = self._calc_grid_dimensions()
self._cellgrid = zeros((num_x_cells, num_y_cells), dtype=object)
for i in range(num_x_cells):
for j in range(num_y_cells):
self._cellgrid[i,j] = self.celltype(parent=self)
ll, ur = self._extents
cell_width = ur[0]/num_x_cells
cell_height = ur[1]/num_y_cells
# calculate the left and bottom edges of all the cells and store
# them in two arrays
self._cell_lefts = arange(ll[0], ll[0]+ur[0]-cell_width/2, step=cell_width)
self._cell_bottoms = arange(ll[1], ll[1]+ur[1]-cell_height/2, step=cell_height)
self._cell_extents = (cell_width, cell_height)
# insert the data points
self._basic_insertion(self.celltype)
return
示例2: test_basic_instantiation
def test_basic_instantiation(self):
'''
Tests the basic instantiation of the SHIFT class
'''
# Instantiatiation with float
self.model = Shift(5.0)
np.testing.assert_array_almost_equal(self.model.target_magnitudes,
np.array([5.0]))
self.assertEqual(self.model.number_magnitudes, 1)
# Instantiation with a numpy array
self.model = Shift(np.arange(5., 8., 0.5))
np.testing.assert_array_almost_equal(self.model.target_magnitudes,
np.arange(5., 8., 0.5))
self.assertEqual(self.model.number_magnitudes, 6)
# Instantiation with list
self.model = Shift([5., 6., 7., 8.])
np.testing.assert_array_almost_equal(self.model.target_magnitudes,
np.array([5., 6., 7., 8.]))
self.assertEqual(self.model.number_magnitudes, 4)
# Otherwise raise an error
with self.assertRaises(ValueError) as ae:
self.model = Shift(None)
self.assertEqual(ae.exception.message,
'Minimum magnitudes must be float, list or array')
# Check regionalisation - assuming defaults
self.model = Shift(5.0)
for region in self.model.regionalisation.keys():
self.assertDictEqual(BIRD_GLOBAL_PARAMETERS[region],
self.model.regionalisation[region])
np.testing.assert_array_almost_equal(np.log10(self.model.base_rate),
np.array([-20.74610902]))
示例3: compute_dr_wrt
def compute_dr_wrt(self, wrt):
if wrt not in (self.v, self.rt, self.t):
return
if wrt is self.t:
if not hasattr(self, '_drt') or self._drt.shape[0] != self.v.r.size:
IS = np.arange(self.v.r.size)
JS = IS % 3
data = np.ones(len(IS))
self._drt = sp.csc_matrix((data, (IS, JS)))
return self._drt
if wrt is self.rt:
rot, rot_dr = cv2.Rodrigues(self.rt.r)
rot_dr = rot_dr.reshape((3,3,3))
dr = np.einsum('abc, zc -> zba', rot_dr, self.v.r).reshape((-1,3))
return dr
if wrt is self.v:
rot = cv2.Rodrigues(self.rt.r)[0]
IS = np.repeat(np.arange(self.v.r.size), 3)
JS = np.repeat(np.arange(self.v.r.size).reshape((-1,3)), 3, axis=0)
data = np.vstack([rot for i in range(self.v.r.size/3)])
result = sp.csc_matrix((data.ravel(), (IS.ravel(), JS.ravel())))
return result
示例4: scree_plot
def scree_plot(pca_obj, fname=None):
'''
Scree plot for variance & cumulative variance by component from PCA.
Arguments:
- pca_obj: a fitted sklearn PCA instance
- fname: path to write plot to file
Output:
- scree plot
'''
components = pca_obj.n_components_
variance = pca.explained_variance_ratio_
plt.figure()
plt.plot(np.arange(1, components + 1), np.cumsum(variance), label='Cumulative Variance')
plt.plot(np.arange(1, components + 1), variance, label='Variance')
plt.xlim([0.8, components]); plt.ylim([0.0, 1.01])
plt.xlabel('No. Components', labelpad=11); plt.ylabel('Variance Explained', labelpad=11)
plt.legend(loc='best')
plt.tight_layout()
if fname is not None:
plt.savefig(fname)
plt.close()
else:
plt.show()
return
示例5: test_array_richcompare_legacy_weirdness
def test_array_richcompare_legacy_weirdness(self):
# It doesn't really work to use assert_deprecated here, b/c part of
# the point of assert_deprecated is to check that when warnings are
# set to "error" mode then the error is propagated -- which is good!
# But here we are testing a bunch of code that is deprecated *because*
# it has the habit of swallowing up errors and converting them into
# different warnings. So assert_warns will have to be sufficient.
assert_warns(FutureWarning, lambda: np.arange(2) == "a")
assert_warns(FutureWarning, lambda: np.arange(2) != "a")
# No warning for scalar comparisons
with warnings.catch_warnings():
warnings.filterwarnings("error")
assert_(not (np.array(0) == "a"))
assert_(np.array(0) != "a")
assert_(not (np.int16(0) == "a"))
assert_(np.int16(0) != "a")
for arg1 in [np.asarray(0), np.int16(0)]:
struct = np.zeros(2, dtype="i4,i4")
for arg2 in [struct, "a"]:
for f in [operator.lt, operator.le, operator.gt, operator.ge]:
if sys.version_info[0] >= 3:
# py3
with warnings.catch_warnings() as l:
warnings.filterwarnings("always")
assert_raises(TypeError, f, arg1, arg2)
assert_(not l)
else:
# py2
assert_warns(DeprecationWarning, f, arg1, arg2)
示例6: __unit_test_onset_function
def __unit_test_onset_function(metric):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
# First, test for a warning on empty onsets
metric(np.array([]), np.arange(10))
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
assert str(w[-1].message) == "Reference onsets are empty."
metric(np.arange(10), np.array([]))
assert len(w) == 2
assert issubclass(w[-1].category, UserWarning)
assert str(w[-1].message) == "Estimated onsets are empty."
# And that the metric is 0
assert np.allclose(metric(np.array([]), np.array([])), 0)
# Now test validation function - onsets must be 1d ndarray
onsets = np.array([[1., 2.]])
nose.tools.assert_raises(ValueError, metric, onsets, onsets)
# onsets must be in seconds (so not huge)
onsets = np.array([1e10, 1e11])
nose.tools.assert_raises(ValueError, metric, onsets, onsets)
# onsets must be sorted
onsets = np.array([2., 1.])
nose.tools.assert_raises(ValueError, metric, onsets, onsets)
# Valid onsets which are the same produce a score of 1 for all metrics
onsets = np.arange(10, dtype=np.float)
assert np.allclose(metric(onsets, onsets), 1)
示例7: barplot
def barplot(grouped_df, _column, statistic, levels=[0]):
means = grouped_df.groupby(level=levels).mean()
bar_width = 1.0/(len(means.index))
error_config = {'ecolor': '0.1'}
sems = grouped_df.groupby(level=levels).sem().fillna(0)
fig = plt.figure()
fig.set_size_inches(10,6)
ax = fig.add_subplot(1,1,1)
plt.bar(np.arange(0.1,(len(means.index)+0.1),1),
means[_column].fillna(0),
color= '#AAAAAA',
yerr=sems[_column].fillna(0),
error_kw=error_config,
label=list(means.index))
if means[_column].values.min() >= 0:
ax.set_ylim(0,1.1*((means[_column] + sems[_column]).values.max()))
else:
ax.set_ylim(1.1*((means[_column] - sems[_column]).values.min()),1.1*((means[_column] + sems[_column]).values.max()))
ax.set_ylabel(statistic + ' ' + u"\u00B1" + ' SEM', fontsize=20) # +/- sign is u"\u00B1"
ax.set_xticks(np.arange(0.1+bar_width/2.0,(len(means.index)+0.1+(bar_width/2.0)),1))
ax.set_xticklabels(list(means.index), rotation=90)
ax.tick_params(axis='y', labelsize=16 )
ax.set_xlabel('Group', fontsize=20)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
return fig
示例8: test_sort_index_multicolumn
def test_sort_index_multicolumn(self):
import random
A = np.arange(5).repeat(20)
B = np.tile(np.arange(5), 20)
random.shuffle(A)
random.shuffle(B)
frame = DataFrame({'A': A, 'B': B,
'C': np.random.randn(100)})
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['A', 'B'])
result = frame.sort_values(by=['A', 'B'])
indexer = np.lexsort((frame['B'], frame['A']))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['A', 'B'], ascending=False)
result = frame.sort_values(by=['A', 'B'], ascending=False)
indexer = np.lexsort((frame['B'].rank(ascending=False),
frame['A'].rank(ascending=False)))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
frame.sort_index(by=['B', 'A'])
result = frame.sort_values(by=['B', 'A'])
indexer = np.lexsort((frame['A'], frame['B']))
expected = frame.take(indexer)
assert_frame_equal(result, expected)
示例9: test_sort_index_different_sortorder
def test_sort_index_different_sortorder(self):
A = np.arange(20).repeat(5)
B = np.tile(np.arange(5), 20)
indexer = np.random.permutation(100)
A = A.take(indexer)
B = B.take(indexer)
df = DataFrame({'A': A, 'B': B,
'C': np.random.randn(100)})
# use .sort_values #9816
with tm.assert_produces_warning(FutureWarning):
df.sort_index(by=['A', 'B'], ascending=[1, 0])
result = df.sort_values(by=['A', 'B'], ascending=[1, 0])
ex_indexer = np.lexsort((df.B.max() - df.B, df.A))
expected = df.take(ex_indexer)
assert_frame_equal(result, expected)
# test with multiindex, too
idf = df.set_index(['A', 'B'])
result = idf.sort_index(ascending=[1, 0])
expected = idf.take(ex_indexer)
assert_frame_equal(result, expected)
# also, Series!
result = idf['C'].sort_index(ascending=[1, 0])
assert_series_equal(result, expected['C'])
示例10: get_gabors
def get_gabors(self, rf):
lams = float(rf[0])/self.sfs # lambda = 1./sf #1./np.array([.1,.25,.4])
sigma = rf[0]/2./np.pi
# rf = [100,100]
gabors = np.zeros(( len(oris),len(phases),len(lams), rf[0], rf[1] ))
i = np.arange(-rf[0]/2+1,rf[0]/2+1)
#print i
j = np.arange(-rf[1]/2+1,rf[1]/2+1)
ii,jj = np.meshgrid(i,j)
for o, theta in enumerate(self.oris):
x = ii*np.cos(theta) + jj*np.sin(theta)
y = -ii*np.sin(theta) + jj*np.cos(theta)
for p, phase in enumerate(self.phases):
for s, lam in enumerate(lams):
fxx = np.cos(2*np.pi*x/lam + phase) * np.exp(-(x**2+y**2)/(2*sigma**2))
fxx -= np.mean(fxx)
fxx /= np.linalg.norm(fxx)
#if p==0:
#plt.subplot(len(oris),len(lams),count+1)
#plt.imshow(fxx,cmap=mpl.cm.gray,interpolation='bicubic')
#count+=1
gabors[o,p,s,:,:] = fxx
plt.show()
return gabors
示例11: test_groupby_groups_datetimeindex
def test_groupby_groups_datetimeindex(self):
# GH#1430
periods = 1000
ind = pd.date_range(start='2012/1/1', freq='5min', periods=periods)
df = DataFrame({'high': np.arange(periods),
'low': np.arange(periods)}, index=ind)
grouped = df.groupby(lambda x: datetime(x.year, x.month, x.day))
# it works!
groups = grouped.groups
assert isinstance(list(groups.keys())[0], datetime)
# GH#11442
index = pd.date_range('2015/01/01', periods=5, name='date')
df = pd.DataFrame({'A': [5, 6, 7, 8, 9],
'B': [1, 2, 3, 4, 5]}, index=index)
result = df.groupby(level='date').groups
dates = ['2015-01-05', '2015-01-04', '2015-01-03',
'2015-01-02', '2015-01-01']
expected = {pd.Timestamp(date): pd.DatetimeIndex([date], name='date')
for date in dates}
tm.assert_dict_equal(result, expected)
grouped = df.groupby(level='date')
for date in dates:
result = grouped.get_group(date)
data = [[df.loc[date, 'A'], df.loc[date, 'B']]]
expected_index = pd.DatetimeIndex([date], name='date')
expected = pd.DataFrame(data,
columns=list('AB'),
index=expected_index)
tm.assert_frame_equal(result, expected)
示例12: get_new_columns
def get_new_columns(self):
if self.value_columns is None:
return self.removed_level
stride = len(self.removed_level)
width = len(self.value_columns)
propagator = np.repeat(np.arange(width), stride)
if isinstance(self.value_columns, MultiIndex):
new_levels = self.value_columns.levels + [self.removed_level]
new_names = self.value_columns.names + [self.removed_name]
new_labels = [lab.take(propagator)
for lab in self.value_columns.labels]
new_labels.append(np.tile(np.arange(stride), width))
else:
new_levels = [self.value_columns, self.removed_level]
new_names = [self.value_columns.name, self.removed_name]
new_labels = []
new_labels.append(propagator)
new_labels.append(np.tile(np.arange(stride), width))
return MultiIndex(levels=new_levels, labels=new_labels,
names=new_names)
示例13: test_stratified_shuffle_split_init
def test_stratified_shuffle_split_init():
X = np.arange(7)
y = np.asarray([0, 1, 1, 1, 2, 2, 2])
# Check that error is raised if there is a class with only one sample
assert_raises(ValueError, next,
StratifiedShuffleSplit(3, 0.2).split(X, y))
# Check that error is raised if the test set size is smaller than n_classes
assert_raises(ValueError, next, StratifiedShuffleSplit(3, 2).split(X, y))
# Check that error is raised if the train set size is smaller than
# n_classes
assert_raises(ValueError, next,
StratifiedShuffleSplit(3, 3, 2).split(X, y))
X = np.arange(9)
y = np.asarray([0, 0, 0, 1, 1, 1, 2, 2, 2])
# Check that errors are raised if there is not enough samples
assert_raises(ValueError, StratifiedShuffleSplit, 3, 0.5, 0.6)
assert_raises(ValueError, next,
StratifiedShuffleSplit(3, 8, 0.6).split(X, y))
assert_raises(ValueError, next,
StratifiedShuffleSplit(3, 0.6, 8).split(X, y))
# Train size or test size too small
assert_raises(ValueError, next,
StratifiedShuffleSplit(train_size=2).split(X, y))
assert_raises(ValueError, next,
StratifiedShuffleSplit(test_size=2).split(X, y))
示例14: test_as_float_array
def test_as_float_array():
# Test function for as_float_array
X = np.ones((3, 10), dtype=np.int32)
X = X + np.arange(10, dtype=np.int32)
# Checks that the return type is ok
X2 = as_float_array(X, copy=False)
np.testing.assert_equal(X2.dtype, np.float32)
# Another test
X = X.astype(np.int64)
X2 = as_float_array(X, copy=True)
# Checking that the array wasn't overwritten
assert_true(as_float_array(X, False) is not X)
# Checking that the new type is ok
np.testing.assert_equal(X2.dtype, np.float64)
# Here, X is of the right type, it shouldn't be modified
X = np.ones((3, 2), dtype=np.float32)
assert_true(as_float_array(X, copy=False) is X)
# Test that if X is fortran ordered it stays
X = np.asfortranarray(X)
assert_true(np.isfortran(as_float_array(X, copy=True)))
# Test the copy parameter with some matrices
matrices = [
np.matrix(np.arange(5)),
sp.csc_matrix(np.arange(5)).toarray(),
sparse_random_matrix(10, 10, density=0.10).toarray()
]
for M in matrices:
N = as_float_array(M, copy=True)
N[0, 0] = np.nan
assert_false(np.isnan(M).any())
示例15: test_series
def test_series(self):
# GH6407
# inferring series
# invalid type of Series
for s in [ Series(np.arange(10)),
Series(np.arange(10.))]:
self.assertRaises(TypeError, lambda : infer_freq(s))
# a non-convertible string
self.assertRaises(ValueError, lambda : infer_freq(Series(['foo','bar'])))
# cannot infer on PeriodIndex
for freq in [None, 'L', 'Y']:
s = Series(period_range('2013',periods=10,freq=freq))
self.assertRaises(TypeError, lambda : infer_freq(s))
# DateTimeIndex
for freq in ['M', 'L', 'S']:
s = Series(date_range('20130101',periods=10,freq=freq))
inferred = infer_freq(s)
self.assertEqual(inferred,freq)
s = Series(date_range('20130101','20130110'))
inferred = infer_freq(s)
self.assertEqual(inferred,'D')