Этот ролик Махди Юсуфа, в предыдущем посте я "законспектировал" его пример, а этот пост тоже оправдал ожидания. Здесь есть примеры: создания индекса по времени DatetimeIndex, ресемплинга (resampling)временных рядов... и пример импорта курса акций c Yahoo.finance. Вот только визуализация (vincent)не получилась
Рассуждения об итогах и текущих планах (рекомендуется пропустить)¶
Вчера я опубликовал пост Pandas Data Frames and Kevin Durant 2012-13 Game Log with Mahdi Yusuf. Это "конспект-практикум" видеоролика, который показался мне очень полезным. Однако, в процессе выполнения практикума я потратил много времени на устранения ошибки при показе диаграмм. Оказалось, что надо было явно загрузить javascript... " ("from IPython.display import display, HTML, Javascript")
Наверное, для того чтобы прояснить мои весьма смутные понятия о механизме построения графиков... остаток вчерашнего дня я потратил на изучение Vincent, начаты три поста, которые будут опубликованы позже...
Сегодня (или завтра) я решил вернуться к теме "Pandas DataFrames"... Мне нужно найти примеры работы с многотабличными запросами. Надо бы понять возможности и принципы работы Vincent...
Интересен пост Sphinx & Read the Docs Махди Юсуфа... Если его посты так мне нравятся, то это значит, что этот материал мне легче всего усвоить..., и, очевидно, что эти посты мне разонравятся, как только я освою материал..., так что... надо получать удовольствие сейчас... Вот такие резоны мне нужно использовать при планировании следующей недели.
Наверное, для того чтобы прояснить мои весьма смутные понятия о механизме построения графиков... остаток вчерашнего дня я потратил на изучение Vincent, начаты три поста, которые будут опубликованы позже...
Сегодня (или завтра) я решил вернуться к теме "Pandas DataFrames"... Мне нужно найти примеры работы с многотабличными запросами. Надо бы понять возможности и принципы работы Vincent...
Интересен пост Sphinx & Read the Docs Махди Юсуфа... Если его посты так мне нравятся, то это значит, что этот материал мне легче всего усвоить..., и, очевидно, что эти посты мне разонравятся, как только я освою материал..., так что... надо получать удовольствие сейчас... Вот такие резоны мне нужно использовать при планировании следующей недели.
In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo('8j0lHe_BZgA')
Out[1]:
In [1]:
from pandas import DataFrame, Series
In [2]:
s=Series([1,2,3,4],index=['a','b','c','d'])
In [3]:
s
Out[3]:
a 1 b 2 c 3 d 4 dtype: int64
Как создать последовательность случайных чисел с индексом по минутам.¶
In [6]:
import random
In [13]:
from pandas import DatetimeIndex
from datetime import datetime
In [14]:
#create some data
data=[random.randint(0,10000) for x in xrange(10000)]
#create a datetime index
index=DatetimeIndex(start='01-01-2013', periods=len(data),freq='T')
s=Series(data,index=index)
s
Out[14]:
2013-01-01 00:00:00 3324 2013-01-01 00:01:00 4854 2013-01-01 00:02:00 6548 2013-01-01 00:03:00 8416 2013-01-01 00:04:00 6705 2013-01-01 00:05:00 2649 2013-01-01 00:06:00 9161 2013-01-01 00:07:00 2652 2013-01-01 00:08:00 8430 2013-01-01 00:09:00 5795 2013-01-01 00:10:00 9098 2013-01-01 00:11:00 8746 2013-01-01 00:12:00 4110 2013-01-01 00:13:00 7987 2013-01-01 00:14:00 409 ... 2013-01-07 22:25:00 2635 2013-01-07 22:26:00 2309 2013-01-07 22:27:00 1320 2013-01-07 22:28:00 717 2013-01-07 22:29:00 1003 2013-01-07 22:30:00 9903 2013-01-07 22:31:00 672 2013-01-07 22:32:00 1426 2013-01-07 22:33:00 1665 2013-01-07 22:34:00 2490 2013-01-07 22:35:00 4845 2013-01-07 22:36:00 3774 2013-01-07 22:37:00 4835 2013-01-07 22:38:00 954 2013-01-07 22:39:00 3339 Freq: T, Length: 10000
In [15]:
help(DatetimeIndex)
Help on class DatetimeIndex in module pandas.tseries.index: class DatetimeIndex(pandas.core.index.Int64Index) | Immutable ndarray of datetime64 data, represented internally as int64, and | which can be boxed to Timestamp objects that are subclasses of datetime and | carry metadata such as frequency information. | | Parameters | ---------- | data : array-like (1-dimensional), optional | Optional datetime-like data to construct index with | copy : bool | Make a copy of input ndarray | freq : string or pandas offset object, optional | One of pandas date offset strings or corresponding objects | start : starting value, datetime-like, optional | If data is None, start is used as the start point in generating regular | timestamp data. | periods : int, optional, > 0 | Number of periods to generate, if generating index. Takes precedence | over end argument | end : end time, datetime-like, optional | If periods is none, generated index will extend to first conforming | time on or just past end argument | closed : string or None, default None | Make the interval closed with respect to the given frequency to | the 'left', 'right', or both sides (None) | name : object | Name to be stored in the index | | Method resolution order: | DatetimeIndex | pandas.core.index.Int64Index | pandas.core.index.Index | pandas.core.base.FrozenNDArray | pandas.core.base.PandasObject | pandas.core.base.StringMixin | numpy.ndarray | __builtin__.object | | Methods defined here: | | __add__(self, other) | | __array_finalize__(self, obj) | | __contains__(self, key) | | __eq__ = wrapper(self, other) | | __ge__ = wrapper(self, other) | | __getitem__(self, key) | Override numpy.ndarray's __getitem__ method to work as desired | | __gt__ = wrapper(self, other) | | __iter__(self) | | __le__ = wrapper(self, other) | | __lt__ = wrapper(self, other) | | __ne__ = wrapper(self, other) | | __reduce__(self) | Necessary for making this object picklable | | __setstate__(self, state) | Necessary for making this object picklable | | __sub__(self, other) | | __unicode__(self) | | append(self, other) | Append a collection of Index options together | | Parameters | ---------- | other : Index or list/tuple of indices | | Returns | ------- | appended : Index | | argmin(self) | | astype(self, dtype) | | delete(self, loc) | Make new DatetimeIndex with passed location deleted | | Returns | ------- | new_index : DatetimeIndex | | equals(self, other) | Determines if two Index objects contain the same elements. | | get_duplicates(self) | | get_loc(self, key) | Get integer location for requested label | | Returns | ------- | loc : int | | get_value(self, series, key) | Fast lookup of value from 1-dimensional ndarray. Only use this if you | know what you're doing | | get_value_maybe_box(self, series, key) | | groupby(self, f) | | indexer_at_time(self, time, asof=False) | Select values at particular time of day (e.g. 9:30AM) | | Parameters | ---------- | time : datetime.time or string | tz : string or pytz.timezone | Time zone for time. Corresponding timestamps would be converted to | time zone of the TimeSeries | | Returns | ------- | values_at_time : TimeSeries | | indexer_between_time(self, start_time, end_time, include_start=True, include_end=True) | Select values between particular times of day (e.g., 9:00-9:30AM) | | Parameters | ---------- | start_time : datetime.time or string | end_time : datetime.time or string | include_start : boolean, default True | include_end : boolean, default True | tz : string or pytz.timezone, default None | | Returns | ------- | values_between_time : TimeSeries | | insert(self, loc, item) | Make new Index inserting new item at location | | Parameters | ---------- | loc : int | item : object | if not either a Python datetime or a numpy integer-like, returned | Index dtype will be object rather than datetime. | | Returns | ------- | new_index : Index | | intersection(self, other) | Specialized intersection for DatetimeIndex objects. May be much faster | than Index.intersection | | Parameters | ---------- | other : DatetimeIndex or array-like | | Returns | ------- | y : Index or DatetimeIndex | | is_type_compatible(self, typ) | | isin(self, values) | Compute boolean array of whether each index value is found in the | passed set of values | | Parameters | ---------- | values : set or sequence of values | | Returns | ------- | is_contained : ndarray (boolean dtype) | | join(self, other, how='left', level=None, return_indexers=False) | See Index.join | | map(self, f) | # Try to run function on index first, and then on elements of index | # Especially important for group-by functionality | | max(self, axis=None) | Overridden ndarray.max to return a Timestamp | | min(self, axis=None) | Overridden ndarray.min to return a Timestamp | | normalize(self) | Return DatetimeIndex with times to midnight. Length is unaltered | | Returns | ------- | normalized : DatetimeIndex | | order(self, return_indexer=False, ascending=True) | Return sorted copy of Index | | repeat(self, repeats, axis=None) | Analogous to ndarray.repeat | | searchsorted(self, key, side='left') | | shift(self, n, freq=None) | Specialized shift which produces a DatetimeIndex | | Parameters | ---------- | n : int | Periods to shift by | freq : DateOffset or timedelta-like, optional | | Returns | ------- | shifted : DatetimeIndex | | slice_indexer(self, start=None, end=None, step=None) | Index.slice_indexer, customized to handle time slicing | | slice_locs(self, start=None, end=None) | Index.slice_locs, customized to handle partial ISO-8601 string slicing | | snap(self, freq='S') | Snap time stamps to nearest occurring frequency | | summary(self, name=None) | | take(self, indices, axis=0) | Analogous to ndarray.take | | to_datetime(self, dayfirst=False) | | to_period(self, freq=None) | Cast to PeriodIndex at a particular frequency | | to_pydatetime(self) | Return DatetimeIndex as object ndarray of datetime.datetime objects | | Returns | ------- | datetimes : ndarray | | tolist(self) | See ndarray.tolist | | tz_convert(self, tz) | Convert DatetimeIndex from one time zone to another (using pytz) | | Returns | ------- | normalized : DatetimeIndex | | tz_localize(self, tz, infer_dst=False) | Localize tz-naive DatetimeIndex to given time zone (using pytz) | | Parameters | ---------- | tz : string or pytz.timezone | Time zone for time. Corresponding timestamps would be converted to | time zone of the TimeSeries | infer_dst : boolean, default False | Attempt to infer fall dst-transition hours based on order | | Returns | ------- | localized : DatetimeIndex | | union(self, other) | Specialized union for DatetimeIndex objects. If combine | overlapping ranges with the same DateOffset, will be much | faster than Index.union | | Parameters | ---------- | other : DatetimeIndex or array-like | | Returns | ------- | y : Index or DatetimeIndex | | union_many(self, others) | A bit of a hack to accelerate unioning a collection of indexes | | unique(self) | Index.unique with handling for DatetimeIndex metadata | | Returns | ------- | result : DatetimeIndex | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(cls, data=None, freq=None, start=None, end=None, periods=None, copy=False, name=None, tz=None, verify_integrity=True, normalize=False, closed=None, **kwds) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | asobject | Convert to Index of datetime objects | | date | Returns numpy array of datetime.date. The date part of the Timestamps. | | day | | dayofweek | The day of the week with Monday=0, Sunday=6 | | dayofyear | | dtype | | freq | | freqstr | | hour | | inferred_freq | | inferred_type | | is_all_dates | | is_normalized | | microsecond | | minute | | month | The month as January=1, December=12 | | nanosecond | | quarter | | resolution | | second | | time | Returns numpy array of datetime.time. The time part of the Timestamps. | | tzinfo | Alias for tz attribute | | week | | weekday | The day of the week with Monday=0, Sunday=6 | | weekofyear | | year | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | offset = None | | ---------------------------------------------------------------------- | Data descriptors inherited from pandas.core.index.Int64Index: | | asi8 | | ---------------------------------------------------------------------- | Methods inherited from pandas.core.index.Index: | | __and__(self, other) | | __deepcopy__(self, memo={}) | | __hash__(self) | | __iadd__ = __add__(self, other) | | __or__(self, other) | | argsort(self, *args, **kwargs) | See docstring for ndarray.argsort | | asof(self, label) | For a sorted index, return the most recent label up to and including | the passed label. Return NaN if not found | | asof_locs(self, where, mask) | where : array of timestamps | mask : array of booleans where data is not NA | | copy(self, names=None, name=None, dtype=None, deep=False) | Make a copy of this object. Name and dtype sets those attributes on | the new object. | | Parameters | ---------- | name : string, optional | dtype : numpy dtype or pandas type | | Returns | ------- | copy : Index | | Notes | ----- | In most cases, there should be no functional difference from using | ``deep``, but if ``deep`` is passed it will attempt to deepcopy. | | diff(self, other) | Compute sorted set difference of two Index objects | | Notes | ----- | One can do either of these and achieve the same result | | >>> index - index2 | >>> index.diff(index2) | | Returns | ------- | diff : Index | | drop(self, labels) | Make new Index with passed list of labels deleted | | Parameters | ---------- | labels : array-like | | Returns | ------- | dropped : Index | | format(self, name=False, formatter=None, **kwargs) | Render a string representation of the Index | | get_indexer(self, target, method=None, limit=None) | Compute indexer and mask for new index given the current index. The | indexer should be then used as an input to ndarray.take to align the | current data to the new index. The mask determines whether labels are | found or not in the current index | | Parameters | ---------- | target : Index | method : {'pad', 'ffill', 'backfill', 'bfill'} | pad / ffill: propagate LAST valid observation forward to next valid | backfill / bfill: use NEXT valid observation to fill gap | | Notes | ----- | This is a low-level method and probably should be used at your own risk | | Examples | -------- | >>> indexer = index.get_indexer(new_index) | >>> new_values = cur_values.take(indexer) | | Returns | ------- | indexer : ndarray | | get_indexer_non_unique(self, target, **kwargs) | return an indexer suitable for taking from a non unique index | return the labels in the same order as the target, and | return a missing indexer into the target (missing are marked as -1 | in the indexer); target must be an iterable | | get_level_values(self, level) | Return vector of label values for requested level, equal to the length | of the index | | Parameters | ---------- | level : int | | Returns | ------- | values : ndarray | | get_values(self) | | holds_integer(self) | | identical(self, other) | Similar to equals, but check that other comparable attributes are | also equal | | is_(self, other) | More flexible, faster check like ``is`` but that works through views | | Note: this is *not* the same as ``Index.identical()``, which checks | that metadata is also the same. | | Parameters | ---------- | other : object | other object to compare against. | | Returns | ------- | True if both have same underlying data, False otherwise : bool | | is_floating(self) | | is_integer(self) | | is_lexsorted_for_tuple(self, tup) | | is_mixed(self) | | is_numeric(self) | | reindex(self, target, method=None, level=None, limit=None, copy_if_needed=False, takeable=False) | For Index, simply returns the new index and the results of | get_indexer. Provided here to enable an interface that is amenable for | subclasses of Index whose internals are different (like MultiIndex) | | Returns | ------- | (new_index, indexer, mask) : tuple | | rename(self, name, inplace=False) | Set new names on index. Defaults to returning new index. | | Parameters | ---------- | name : str or list | name to set | inplace : bool | if True, mutates in place | | Returns | ------- | new index (of same type and class...etc) [if inplace, returns None] | | set_names(self, names, inplace=False) | Set new names on index. Defaults to returning new index. | | Parameters | ---------- | names : sequence | names to set | inplace : bool | if True, mutates in place | | Returns | ------- | new index (of same type and class...etc) [if inplace, returns None] | | set_value(self, arr, key, value) | Fast lookup of value from 1-dimensional ndarray. Only use this if you | know what you're doing | | sort(self, *args, **kwargs) | | to_native_types(self, slicer=None, **kwargs) | slice and dice then format | | to_series(self) | return a series with both index and values equal to the index keys | useful with map for returning an indexer based on an index | | view(self, *args, **kwargs) | | ---------------------------------------------------------------------- | Data descriptors inherited from pandas.core.index.Index: | | is_monotonic | | is_unique | | names | | nlevels | | values | | ---------------------------------------------------------------------- | Data and other attributes inherited from pandas.core.index.Index: | | name = None | | ---------------------------------------------------------------------- | Methods inherited from pandas.core.base.FrozenNDArray: | | __delitem__ = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | __delslice__ = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | __setitem__ = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | __setslice__ = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | fill = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | itemset = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | put = _disabled(self, *args, **kwargs) | This method will not function because object is immutable. | | ---------------------------------------------------------------------- | Data descriptors inherited from pandas.core.base.FrozenNDArray: | | __dict__ | dictionary for instance variables (if defined) | | ---------------------------------------------------------------------- | Methods inherited from pandas.core.base.PandasObject: | | __dir__(self) | Provide method name lookup and completion | Only provide 'public' methods | | ---------------------------------------------------------------------- | Methods inherited from pandas.core.base.StringMixin: | | __bytes__(self) | Return a string representation for a particular object. | | Invoked by bytes(obj) in py3 only. | Yields a bytestring in both py2/py3. | | __repr__(self) | Return a string representation for a particular object. | | Yields Bytestring in Py2, Unicode String in py3. | | __str__(self) | Return a string representation for a particular Object | | Invoked by str(df) in both py2/py3. | Yields Bytestring in Py2, Unicode String in py3. | | ---------------------------------------------------------------------- | Data descriptors inherited from pandas.core.base.StringMixin: | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Methods inherited from numpy.ndarray: | | __abs__(...) | x.__abs__() <==> abs(x) | | __array__(...) | a.__array__(|dtype) -> reference if type unchanged, copy otherwise. | | Returns either a new reference to self if dtype is not given or a new array | of provided data type if dtype is different from the current dtype of the | array. | | __array_prepare__(...) | a.__array_prepare__(obj) -> Object of same type as ndarray object obj. | | __array_wrap__(...) | a.__array_wrap__(obj) -> Object of same type as ndarray object a. | | __copy__(...) | a.__copy__([order]) | | Return a copy of the array. | | Parameters | ---------- | order : {'C', 'F', 'A'}, optional | If order is 'C' (False) then the result is contiguous (default). | If order is 'Fortran' (True) then the result has fortran order. | If order is 'Any' (None) then the result has fortran order | only if the array already is in fortran order. | | __div__(...) | x.__div__(y) <==> x/y | | __divmod__(...) | x.__divmod__(y) <==> divmod(x, y) | | __float__(...) | x.__float__() <==> float(x) | | __floordiv__(...) | x.__floordiv__(y) <==> x//y | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __hex__(...) | x.__hex__() <==> hex(x) | | __iand__(...) | x.__iand__(y) <==> x&=y | | __idiv__(...) | x.__idiv__(y) <==> x/=y | | __ifloordiv__(...) | x.__ifloordiv__(y) <==> x//y | | __ilshift__(...) | x.__ilshift__(y) <==> x<<=y | | __imod__(...) | x.__imod__(y) <==> x%=y | | __imul__(...) | x.__imul__(y) <==> x*=y | | __index__(...) | x[y:z] <==> x[y.__index__():z.__index__()] | | __int__(...) | x.__int__() <==> int(x) | | __invert__(...) | x.__invert__() <==> ~x | | __ior__(...) | x.__ior__(y) <==> x|=y | | __ipow__(...) | x.__ipow__(y) <==> x**=y | | __irshift__(...) | x.__irshift__(y) <==> x>>=y | | __isub__(...) | x.__isub__(y) <==> x-=y | | __itruediv__(...) | x.__itruediv__(y) <==> x/y | | __ixor__(...) | x.__ixor__(y) <==> x^=y | | __len__(...) | x.__len__() <==> len(x) | | __long__(...) | x.__long__() <==> long(x) | | __lshift__(...) | x.__lshift__(y) <==> x<<y | | __mod__(...) | x.__mod__(y) <==> x%y | | __mul__(...) | x.__mul__(y) <==> x*y | | __neg__(...) | x.__neg__() <==> -x | | __nonzero__(...) | x.__nonzero__() <==> x != 0 | | __oct__(...) | x.__oct__() <==> oct(x) | | __pos__(...) | x.__pos__() <==> +x | | __pow__(...) | x.__pow__(y[, z]) <==> pow(x, y[, z]) | | __radd__(...) | x.__radd__(y) <==> y+x | | __rand__(...) | x.__rand__(y) <==> y&x | | __rdiv__(...) | x.__rdiv__(y) <==> y/x | | __rdivmod__(...) | x.__rdivmod__(y) <==> divmod(y, x) | | __rfloordiv__(...) | x.__rfloordiv__(y) <==> y//x | | __rlshift__(...) | x.__rlshift__(y) <==> y<<x | | __rmod__(...) | x.__rmod__(y) <==> y%x | | __rmul__(...) | x.__rmul__(y) <==> y*x | | __ror__(...) | x.__ror__(y) <==> y|x | | __rpow__(...) | y.__rpow__(x[, z]) <==> pow(x, y[, z]) | | __rrshift__(...) | x.__rrshift__(y) <==> y>>x | | __rshift__(...) | x.__rshift__(y) <==> x>>y | | __rsub__(...) | x.__rsub__(y) <==> y-x | | __rtruediv__(...) | x.__rtruediv__(y) <==> y/x | | __rxor__(...) | x.__rxor__(y) <==> y^x | | __truediv__(...) | x.__truediv__(y) <==> x/y | | __xor__(...) | x.__xor__(y) <==> x^y | | all(...) | a.all(axis=None, out=None) | | Returns True if all elements evaluate to True. | | Refer to `numpy.all` for full documentation. | | See Also | -------- | numpy.all : equivalent function | | any(...) | a.any(axis=None, out=None) | | Returns True if any of the elements of `a` evaluate to True. | | Refer to `numpy.any` for full documentation. | | See Also | -------- | numpy.any : equivalent function | | argmax(...) | a.argmax(axis=None, out=None) | | Return indices of the maximum values along the given axis. | | Refer to `numpy.argmax` for full documentation. | | See Also | -------- | numpy.argmax : equivalent function | | argpartition(...) | a.argpartition(kth, axis=-1, kind='quickselect', order=None) | | Returns the indices that would partition this array. | | Refer to `numpy.argpartition` for full documentation. | | .. versionadded:: 1.8.0 | | See Also | -------- | numpy.argpartition : equivalent function | | byteswap(...) | a.byteswap(inplace) | | Swap the bytes of the array elements | | Toggle between low-endian and big-endian data representation by | returning a byteswapped array, optionally swapped in-place. | | Parameters | ---------- | inplace : bool, optional | If ``True``, swap bytes in-place, default is ``False``. | | Returns | ------- | out : ndarray | The byteswapped array. If `inplace` is ``True``, this is | a view to self. | | Examples | -------- | >>> A = np.array([1, 256, 8755], dtype=np.int16) | >>> map(hex, A) | ['0x1', '0x100', '0x2233'] | >>> A.byteswap(True) | array([ 256, 1, 13090], dtype=int16) | >>> map(hex, A) | ['0x100', '0x1', '0x3322'] | | Arrays of strings are not swapped | | >>> A = np.array(['ceg', 'fac']) | >>> A.byteswap() | array(['ceg', 'fac'], | dtype='|S3') | | choose(...) | a.choose(choices, out=None, mode='raise') | | Use an index array to construct a new array from a set of choices. | | Refer to `numpy.choose` for full documentation. | | See Also | -------- | numpy.choose : equivalent function | | clip(...) | a.clip(a_min, a_max, out=None) | | Return an array whose values are limited to ``[a_min, a_max]``. | | Refer to `numpy.clip` for full documentation. | | See Also | -------- | numpy.clip : equivalent function | | compress(...) | a.compress(condition, axis=None, out=None) | | Return selected slices of this array along given axis. | | Refer to `numpy.compress` for full documentation. | | See Also | -------- | numpy.compress : equivalent function | | conj(...) | a.conj() | | Complex-conjugate all elements. | | Refer to `numpy.conjugate` for full documentation. | | See Also | -------- | numpy.conjugate : equivalent function | | conjugate(...) | a.conjugate() | | Return the complex conjugate, element-wise. | | Refer to `numpy.conjugate` for full documentation. | | See Also | -------- | numpy.conjugate : equivalent function | | cumprod(...) | a.cumprod(axis=None, dtype=None, out=None) | | Return the cumulative product of the elements along the given axis. | | Refer to `numpy.cumprod` for full documentation. | | See Also | -------- | numpy.cumprod : equivalent function | | cumsum(...) | a.cumsum(axis=None, dtype=None, out=None) | | Return the cumulative sum of the elements along the given axis. | | Refer to `numpy.cumsum` for full documentation. | | See Also | -------- | numpy.cumsum : equivalent function | | diagonal(...) | a.diagonal(offset=0, axis1=0, axis2=1) | | Return specified diagonals. | | Refer to :func:`numpy.diagonal` for full documentation. | | See Also | -------- | numpy.diagonal : equivalent function | | dot(...) | a.dot(b, out=None) | | Dot product of two arrays. | | Refer to `numpy.dot` for full documentation. | | See Also | -------- | numpy.dot : equivalent function | | Examples | -------- | >>> a = np.eye(2) | >>> b = np.ones((2, 2)) * 2 | >>> a.dot(b) | array([[ 2., 2.], | [ 2., 2.]]) | | This array method can be conveniently chained: | | >>> a.dot(b).dot(b) | array([[ 8., 8.], | [ 8., 8.]]) | | dump(...) | a.dump(file) | | Dump a pickle of the array to the specified file. | The array can be read back with pickle.load or numpy.load. | | Parameters | ---------- | file : str | A string naming the dump file. | | dumps(...) | a.dumps() | | Returns the pickle of the array as a string. | pickle.loads or numpy.loads will convert the string back to an array. | | Parameters | ---------- | None | | flatten(...) | a.flatten(order='C') | | Return a copy of the array collapsed into one dimension. | | Parameters | ---------- | order : {'C', 'F', 'A'}, optional | Whether to flatten in C (row-major), Fortran (column-major) order, | or preserve the C/Fortran ordering from `a`. | The default is 'C'. | | Returns | ------- | y : ndarray | A copy of the input array, flattened to one dimension. | | See Also | -------- | ravel : Return a flattened array. | flat : A 1-D flat iterator over the array. | | Examples | -------- | >>> a = np.array([[1,2], [3,4]]) | >>> a.flatten() | array([1, 2, 3, 4]) | >>> a.flatten('F') | array([1, 3, 2, 4]) | | getfield(...) | a.getfield(dtype, offset=0) | | Returns a field of the given array as a certain type. | | A field is a view of the array data with a given data-type. The values in | the view are determined by the given type and the offset into the current | array in bytes. The offset needs to be such that the view dtype fits in the | array dtype; for example an array of dtype complex128 has 16-byte elements. | If taking a view with a 32-bit integer (4 bytes), the offset needs to be | between 0 and 12 bytes. | | Parameters | ---------- | dtype : str or dtype | The data type of the view. The dtype size of the view can not be larger | than that of the array itself. | offset : int | Number of bytes to skip before beginning the element view. | | Examples | -------- | >>> x = np.diag([1.+1.j]*2) | >>> x[1, 1] = 2 + 4.j | >>> x | array([[ 1.+1.j, 0.+0.j], | [ 0.+0.j, 2.+4.j]]) | >>> x.getfield(np.float64) | array([[ 1., 0.], | [ 0., 2.]]) | | By choosing an offset of 8 bytes we can select the complex part of the | array for our view: | | >>> x.getfield(np.float64, offset=8) | array([[ 1., 0.], | [ 0., 4.]]) | | item(...) | a.item(*args) | | Copy an element of an array to a standard Python scalar and return it. | | Parameters | ---------- | \*args : Arguments (variable number and type) | | * none: in this case, the method only works for arrays | with one element (`a.size == 1`), which element is | copied into a standard Python scalar object and returned. | | * int_type: this argument is interpreted as a flat index into | the array, specifying which element to copy and return. | | * tuple of int_types: functions as does a single int_type argument, | except that the argument is interpreted as an nd-index into the | array. | | Returns | ------- | z : Standard Python scalar object | A copy of the specified element of the array as a suitable | Python scalar | | Notes | ----- | When the data type of `a` is longdouble or clongdouble, item() returns | a scalar array object because there is no available Python scalar that | would not lose information. Void arrays return a buffer object for item(), | unless fields are defined, in which case a tuple is returned. | | `item` is very similar to a[args], except, instead of an array scalar, | a standard Python scalar is returned. This can be useful for speeding up | access to elements of the array and doing arithmetic on elements of the | array using Python's optimized math. | | Examples | -------- | >>> x = np.random.randint(9, size=(3, 3)) | >>> x | array([[3, 1, 7], | [2, 8, 3], | [8, 5, 3]]) | >>> x.item(3) | 2 | >>> x.item(7) | 5 | >>> x.item((0, 1)) | 1 | >>> x.item((2, 2)) | 3 | | mean(...) | a.mean(axis=None, dtype=None, out=None) | | Returns the average of the array elements along given axis. | | Refer to `numpy.mean` for full documentation. | | See Also | -------- | numpy.mean : equivalent function | | newbyteorder(...) | arr.newbyteorder(new_order='S') | | Return the array with the same data viewed with a different byte order. | | Equivalent to:: | | arr.view(arr.dtype.newbytorder(new_order)) | | Changes are also made in all fields and sub-arrays of the array data | type. | | | | Parameters | ---------- | new_order : string, optional | Byte order to force; a value from the byte order specifications | above. `new_order` codes can be any of:: | | * 'S' - swap dtype from current to opposite endian | * {'<', 'L'} - little endian | * {'>', 'B'} - big endian | * {'=', 'N'} - native order | * {'|', 'I'} - ignore (no change to byte order) | | The default value ('S') results in swapping the current | byte order. The code does a case-insensitive check on the first | letter of `new_order` for the alternatives above. For example, | any of 'B' or 'b' or 'biggish' are valid to specify big-endian. | | | Returns | ------- | new_arr : array | New array object with the dtype reflecting given change to the | byte order. | | nonzero(...) | a.nonzero() | | Return the indices of the elements that are non-zero. | | Refer to `numpy.nonzero` for full documentation. | | See Also | -------- | numpy.nonzero : equivalent function | | partition(...) | a.partition(kth, axis=-1, kind='introselect', order=None) | | Rearranges the elements in the array in such a way that value of the | element in kth position is in the position it would be in a sorted array. | All elements smaller than the kth element are moved before this element and | all equal or greater are moved behind it. The ordering of the elements in | the two partitions is undefined. | | .. versionadded:: 1.8.0 | | Parameters | ---------- | kth : int or sequence of ints | Element index to partition by. The kth element value will be in its | final sorted position and all smaller elements will be moved before it | and all equal or greater elements behind it. | The order all elements in the partitions is undefined. | If provided with a sequence of kth it will partition all elements | indexed by kth of them into their sorted position at once. | axis : int, optional | Axis along which to sort. Default is -1, which means sort along the | last axis. | kind : {'introselect'}, optional | Selection algorithm. Default is 'introselect'. | order : list, optional | When `a` is an array with fields defined, this argument specifies | which fields to compare first, second, etc. Not all fields need be | specified. | | See Also | -------- | numpy.partition : Return a parititioned copy of an array. | argpartition : Indirect partition. | sort : Full sort. | | Notes | ----- | See ``np.partition`` for notes on the different algorithms. | | Examples | -------- | >>> a = np.array([3, 4, 2, 1]) | >>> a.partition(a, 3) | >>> a | array([2, 1, 3, 4]) | | >>> a.partition((1, 3)) | array([1, 2, 3, 4]) | | prod(...) | a.prod(axis=None, dtype=None, out=None) | | Return the product of the array elements over the given axis | | Refer to `numpy.prod` for full documentation. | | See Also | -------- | numpy.prod : equivalent function | | ptp(...) | a.ptp(axis=None, out=None) | | Peak to peak (maximum - minimum) value along a given axis. | | Refer to `numpy.ptp` for full documentation. | | See Also | -------- | numpy.ptp : equivalent function | | ravel(...) | a.ravel([order]) | | Return a flattened array. | | Refer to `numpy.ravel` for full documentation. | | See Also | -------- | numpy.ravel : equivalent function | | ndarray.flat : a flat iterator on the array. | | reshape(...) | a.reshape(shape, order='C') | | Returns an array containing the same data with a new shape. | | Refer to `numpy.reshape` for full documentation. | | See Also | -------- | numpy.reshape : equivalent function | | resize(...) | a.resize(new_shape, refcheck=True) | | Change shape and size of array in-place. | | Parameters | ---------- | new_shape : tuple of ints, or `n` ints | Shape of resized array. | refcheck : bool, optional | If False, reference count will not be checked. Default is True. | | Returns | ------- | None | | Raises | ------ | ValueError | If `a` does not own its own data or references or views to it exist, | and the data memory must be changed. | | SystemError | If the `order` keyword argument is specified. This behaviour is a | bug in NumPy. | | See Also | -------- | resize : Return a new array with the specified shape. | | Notes | ----- | This reallocates space for the data area if necessary. | | Only contiguous arrays (data elements consecutive in memory) can be | resized. | | The purpose of the reference count check is to make sure you | do not use this array as a buffer for another Python object and then | reallocate the memory. However, reference counts can increase in | other ways so if you are sure that you have not shared the memory | for this array with another Python object, then you may safely set | `refcheck` to False. | | Examples | -------- | Shrinking an array: array is flattened (in the order that the data are | stored in memory), resized, and reshaped: | | >>> a = np.array([[0, 1], [2, 3]], order='C') | >>> a.resize((2, 1)) | >>> a | array([[0], | [1]]) | | >>> a = np.array([[0, 1], [2, 3]], order='F') | >>> a.resize((2, 1)) | >>> a | array([[0], | [2]]) | | Enlarging an array: as above, but missing entries are filled with zeros: | | >>> b = np.array([[0, 1], [2, 3]]) | >>> b.resize(2, 3) # new_shape parameter doesn't have to be a tuple | >>> b | array([[0, 1, 2], | [3, 0, 0]]) | | Referencing an array prevents resizing... | | >>> c = a | >>> a.resize((1, 1)) | Traceback (most recent call last): | ... | ValueError: cannot resize an array that has been referenced ... | | Unless `refcheck` is False: | | >>> a.resize((1, 1), refcheck=False) | >>> a | array([[0]]) | >>> c | array([[0]]) | | round(...) | a.round(decimals=0, out=None) | | Return `a` with each element rounded to the given number of decimals. | | Refer to `numpy.around` for full documentation. | | See Also | -------- | numpy.around : equivalent function | | setfield(...) | a.setfield(val, dtype, offset=0) | | Put a value into a specified place in a field defined by a data-type. | | Place `val` into `a`'s field defined by `dtype` and beginning `offset` | bytes into the field. | | Parameters | ---------- | val : object | Value to be placed in field. | dtype : dtype object | Data-type of the field in which to place `val`. | offset : int, optional | The number of bytes into the field at which to place `val`. | | Returns | ------- | None | | See Also | -------- | getfield | | Examples | -------- | >>> x = np.eye(3) | >>> x.getfield(np.float64) | array([[ 1., 0., 0.], | [ 0., 1., 0.], | [ 0., 0., 1.]]) | >>> x.setfield(3, np.int32) | >>> x.getfield(np.int32) | array([[3, 3, 3], | [3, 3, 3], | [3, 3, 3]]) | >>> x | array([[ 1.00000000e+000, 1.48219694e-323, 1.48219694e-323], | [ 1.48219694e-323, 1.00000000e+000, 1.48219694e-323], | [ 1.48219694e-323, 1.48219694e-323, 1.00000000e+000]]) | >>> x.setfield(np.eye(3), np.int32) | >>> x | array([[ 1., 0., 0.], | [ 0., 1., 0.], | [ 0., 0., 1.]]) | | setflags(...) | a.setflags(write=None, align=None, uic=None) | | Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively. | | These Boolean-valued flags affect how numpy interprets the memory | area used by `a` (see Notes below). The ALIGNED flag can only | be set to True if the data is actually aligned according to the type. | The UPDATEIFCOPY flag can never be set to True. The flag WRITEABLE | can only be set to True if the array owns its own memory, or the | ultimate owner of the memory exposes a writeable buffer interface, | or is a string. (The exception for string is made so that unpickling | can be done without copying memory.) | | Parameters | ---------- | write : bool, optional | Describes whether or not `a` can be written to. | align : bool, optional | Describes whether or not `a` is aligned properly for its type. | uic : bool, optional | Describes whether or not `a` is a copy of another "base" array. | | Notes | ----- | Array flags provide information about how the memory area used | for the array is to be interpreted. There are 6 Boolean flags | in use, only three of which can be changed by the user: | UPDATEIFCOPY, WRITEABLE, and ALIGNED. | | WRITEABLE (W) the data area can be written to; | | ALIGNED (A) the data and strides are aligned appropriately for the hardware | (as determined by the compiler); | | UPDATEIFCOPY (U) this array is a copy of some other array (referenced | by .base). When this array is deallocated, the base array will be | updated with the contents of this array. | | All flags can be accessed using their first (upper case) letter as well | as the full name. | | Examples | -------- | >>> y | array([[3, 1, 7], | [2, 0, 0], | [8, 5, 9]]) | >>> y.flags | C_CONTIGUOUS : True | F_CONTIGUOUS : False | OWNDATA : True | WRITEABLE : True | ALIGNED : True | UPDATEIFCOPY : False | >>> y.setflags(write=0, align=0) | >>> y.flags | C_CONTIGUOUS : True | F_CONTIGUOUS : False | OWNDATA : True | WRITEABLE : False | ALIGNED : False | UPDATEIFCOPY : False | >>> y.setflags(uic=1) | Traceback (most recent call last): | File "<stdin>", line 1, in <module> | ValueError: cannot set UPDATEIFCOPY flag to True | | squeeze(...) | a.squeeze(axis=None) | | Remove single-dimensional entries from the shape of `a`. | | Refer to `numpy.squeeze` for full documentation. | | See Also | -------- | numpy.squeeze : equivalent function | | std(...) | a.std(axis=None, dtype=None, out=None, ddof=0) | | Returns the standard deviation of the array elements along given axis. | | Refer to `numpy.std` for full documentation. | | See Also | -------- | numpy.std : equivalent function | | sum(...) | a.sum(axis=None, dtype=None, out=None) | | Return the sum of the array elements over the given axis. | | Refer to `numpy.sum` for full documentation. | | See Also | -------- | numpy.sum : equivalent function | | swapaxes(...) | a.swapaxes(axis1, axis2) | | Return a view of the array with `axis1` and `axis2` interchanged. | | Refer to `numpy.swapaxes` for full documentation. | | See Also | -------- | numpy.swapaxes : equivalent function | | tofile(...) | a.tofile(fid, sep="", format="%s") | | Write array to a file as text or binary (default). | | Data is always written in 'C' order, independent of the order of `a`. | The data produced by this method can be recovered using the function | fromfile(). | | Parameters | ---------- | fid : file or str | An open file object, or a string containing a filename. | sep : str | Separator between array items for text output. | If "" (empty), a binary file is written, equivalent to | ``file.write(a.tostring())``. | format : str | Format string for text file output. | Each entry in the array is formatted to text by first converting | it to the closest Python type, and then using "format" % item. | | Notes | ----- | This is a convenience function for quick storage of array data. | Information on endianness and precision is lost, so this method is not a | good choice for files intended to archive data or transport data between | machines with different endianness. Some of these problems can be overcome | by outputting the data as text files, at the expense of speed and file | size. | | tostring(...) | a.tostring(order='C') | | Construct a Python string containing the raw data bytes in the array. | | Constructs a Python string showing a copy of the raw contents of | data memory. The string can be produced in either 'C' or 'Fortran', | or 'Any' order (the default is 'C'-order). 'Any' order means C-order | unless the F_CONTIGUOUS flag in the array is set, in which case it | means 'Fortran' order. | | Parameters | ---------- | order : {'C', 'F', None}, optional | Order of the data for multidimensional arrays: | C, Fortran, or the same as for the original array. | | Returns | ------- | s : str | A Python string exhibiting a copy of `a`'s raw data. | | Examples | -------- | >>> x = np.array([[0, 1], [2, 3]]) | >>> x.tostring() | '\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00' | >>> x.tostring('C') == x.tostring() | True | >>> x.tostring('F') | '\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00' | | trace(...) | a.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) | | Return the sum along diagonals of the array. | | Refer to `numpy.trace` for full documentation. | | See Also | -------- | numpy.trace : equivalent function | | transpose(...) | a.transpose(*axes) | | Returns a view of the array with axes transposed. | | For a 1-D array, this has no effect. (To change between column and | row vectors, first cast the 1-D array into a matrix object.) | For a 2-D array, this is the usual matrix transpose. | For an n-D array, if axes are given, their order indicates how the | axes are permuted (see Examples). If axes are not provided and | ``a.shape = (i[0], i[1], ... i[n-2], i[n-1])``, then | ``a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])``. | | Parameters | ---------- | axes : None, tuple of ints, or `n` ints | | * None or no argument: reverses the order of the axes. | | * tuple of ints: `i` in the `j`-th place in the tuple means `a`'s | `i`-th axis becomes `a.transpose()`'s `j`-th axis. | | * `n` ints: same as an n-tuple of the same ints (this form is | intended simply as a "convenience" alternative to the tuple form) | | Returns | ------- | out : ndarray | View of `a`, with axes suitably permuted. | | See Also | -------- | ndarray.T : Array property returning the array transposed. | | Examples | -------- | >>> a = np.array([[1, 2], [3, 4]]) | >>> a | array([[1, 2], | [3, 4]]) | >>> a.transpose() | array([[1, 3], | [2, 4]]) | >>> a.transpose((1, 0)) | array([[1, 3], | [2, 4]]) | >>> a.transpose(1, 0) | array([[1, 3], | [2, 4]]) | | var(...) | a.var(axis=None, dtype=None, out=None, ddof=0) | | Returns the variance of the array elements, along given axis. | | Refer to `numpy.var` for full documentation. | | See Also | -------- | numpy.var : equivalent function | | ---------------------------------------------------------------------- | Data descriptors inherited from numpy.ndarray: | | T | Same as self.transpose(), except that self is returned if | self.ndim < 2. | | Examples | -------- | >>> x = np.array([[1.,2.],[3.,4.]]) | >>> x | array([[ 1., 2.], | [ 3., 4.]]) | >>> x.T | array([[ 1., 3.], | [ 2., 4.]]) | >>> x = np.array([1.,2.,3.,4.]) | >>> x | array([ 1., 2., 3., 4.]) | >>> x.T | array([ 1., 2., 3., 4.]) | | __array_interface__ | Array protocol: Python side. | | __array_priority__ | Array priority. | | __array_struct__ | Array protocol: C-struct side. | | base | Base object if memory is from some other object. | | Examples | -------- | The base of an array that owns its memory is None: | | >>> x = np.array([1,2,3,4]) | >>> x.base is None | True | | Slicing creates a view, whose memory is shared with x: | | >>> y = x[2:] | >>> y.base is x | True | | ctypes | An object to simplify the interaction of the array with the ctypes | module. | | This attribute creates an object that makes it easier to use arrays | when calling shared libraries with the ctypes module. The returned | object has, among others, data, shape, and strides attributes (see | Notes below) which themselves return ctypes objects that can be used | as arguments to a shared library. | | Parameters | ---------- | None | | Returns | ------- | c : Python object | Possessing attributes data, shape, strides, etc. | | See Also | -------- | numpy.ctypeslib | | Notes | ----- | Below are the public attributes of this object which were documented | in "Guide to NumPy" (we have omitted undocumented public attributes, | as well as documented private attributes): | | * data: A pointer to the memory area of the array as a Python integer. | This memory area may contain data that is not aligned, or not in correct | byte-order. The memory area may not even be writeable. The array | flags and data-type of this array should be respected when passing this | attribute to arbitrary C-code to avoid trouble that can include Python | crashing. User Beware! The value of this attribute is exactly the same | as self._array_interface_['data'][0]. | | * shape (c_intp*self.ndim): A ctypes array of length self.ndim where | the basetype is the C-integer corresponding to dtype('p') on this | platform. This base-type could be c_int, c_long, or c_longlong | depending on the platform. The c_intp type is defined accordingly in | numpy.ctypeslib. The ctypes array contains the shape of the underlying | array. | | * strides (c_intp*self.ndim): A ctypes array of length self.ndim where | the basetype is the same as for the shape attribute. This ctypes array | contains the strides information from the underlying array. This strides | information is important for showing how many bytes must be jumped to | get to the next element in the array. | | * data_as(obj): Return the data pointer cast to a particular c-types object. | For example, calling self._as_parameter_ is equivalent to | self.data_as(ctypes.c_void_p). Perhaps you want to use the data as a | pointer to a ctypes array of floating-point data: | self.data_as(ctypes.POINTER(ctypes.c_double)). | | * shape_as(obj): Return the shape tuple as an array of some other c-types | type. For example: self.shape_as(ctypes.c_short). | | * strides_as(obj): Return the strides tuple as an array of some other | c-types type. For example: self.strides_as(ctypes.c_longlong). | | Be careful using the ctypes attribute - especially on temporary | arrays or arrays constructed on the fly. For example, calling | ``(a+b).ctypes.data_as(ctypes.c_void_p)`` returns a pointer to memory | that is invalid because the array created as (a+b) is deallocated | before the next Python statement. You can avoid this problem using | either ``c=a+b`` or ``ct=(a+b).ctypes``. In the latter case, ct will | hold a reference to the array until ct is deleted or re-assigned. | | If the ctypes module is not available, then the ctypes attribute | of array objects still returns something useful, but ctypes objects | are not returned and errors may be raised instead. In particular, | the object will still have the as parameter attribute which will | return an integer equal to the data attribute. | | Examples | -------- | >>> import ctypes | >>> x | array([[0, 1], | [2, 3]]) | >>> x.ctypes.data | 30439712 | >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)) | <ctypes.LP_c_long object at 0x01F01300> | >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)).contents | c_long(0) | >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_longlong)).contents | c_longlong(4294967296L) | >>> x.ctypes.shape | <numpy.core._internal.c_long_Array_2 object at 0x01FFD580> | >>> x.ctypes.shape_as(ctypes.c_long) | <numpy.core._internal.c_long_Array_2 object at 0x01FCE620> | >>> x.ctypes.strides | <numpy.core._internal.c_long_Array_2 object at 0x01FCE620> | >>> x.ctypes.strides_as(ctypes.c_longlong) | <numpy.core._internal.c_longlong_Array_2 object at 0x01F01300> | | data | Python buffer object pointing to the start of the array's data. | | flags | Information about the memory layout of the array. | | Attributes | ---------- | C_CONTIGUOUS (C) | The data is in a single, C-style contiguous segment. | F_CONTIGUOUS (F) | The data is in a single, Fortran-style contiguous segment. | OWNDATA (O) | The array owns the memory it uses or borrows it from another object. | WRITEABLE (W) | The data area can be written to. Setting this to False locks | the data, making it read-only. A view (slice, etc.) inherits WRITEABLE | from its base array at creation time, but a view of a writeable | array may be subsequently locked while the base array remains writeable. | (The opposite is not true, in that a view of a locked array may not | be made writeable. However, currently, locking a base object does not | lock any views that already reference it, so under that circumstance it | is possible to alter the contents of a locked array via a previously | created writeable view onto it.) Attempting to change a non-writeable | array raises a RuntimeError exception. | ALIGNED (A) | The data and all elements are aligned appropriately for the hardware. | UPDATEIFCOPY (U) | This array is a copy of some other array. When this array is | deallocated, the base array will be updated with the contents of | this array. | FNC | F_CONTIGUOUS and not C_CONTIGUOUS. | FORC | F_CONTIGUOUS or C_CONTIGUOUS (one-segment test). | BEHAVED (B) | ALIGNED and WRITEABLE. | CARRAY (CA) | BEHAVED and C_CONTIGUOUS. | FARRAY (FA) | BEHAVED and F_CONTIGUOUS and not C_CONTIGUOUS. | | Notes | ----- | The `flags` object can be accessed dictionary-like (as in ``a.flags['WRITEABLE']``), | or by using lowercased attribute names (as in ``a.flags.writeable``). Short flag | names are only supported in dictionary access. | | Only the UPDATEIFCOPY, WRITEABLE, and ALIGNED flags can be changed by | the user, via direct assignment to the attribute or dictionary entry, | or by calling `ndarray.setflags`. | | The array flags cannot be set arbitrarily: | | - UPDATEIFCOPY can only be set ``False``. | - ALIGNED can only be set ``True`` if the data is truly aligned. | - WRITEABLE can only be set ``True`` if the array owns its own memory | or the ultimate owner of the memory exposes a writeable buffer | interface or is a string. | | Arrays can be both C-style and Fortran-style contiguous simultaneously. | This is clear for 1-dimensional arrays, but can also be true for higher | dimensional arrays. | | Even for contiguous arrays a stride for a given dimension | ``arr.strides[dim]`` may be *arbitrary* if ``arr.shape[dim] == 1`` | or the array has no elements. | It does *not* generally hold that ``self.strides[-1] == self.itemsize`` | for C-style contiguous arrays or ``self.strides[0] == self.itemsize`` for | Fortran-style contiguous arrays is true. | | flat | A 1-D iterator over the array. | | This is a `numpy.flatiter` instance, which acts similarly to, but is not | a subclass of, Python's built-in iterator object. | | See Also | -------- | flatten : Return a copy of the array collapsed into one dimension. | | flatiter | | Examples | -------- | >>> x = np.arange(1, 7).reshape(2, 3) | >>> x | array([[1, 2, 3], | [4, 5, 6]]) | >>> x.flat[3] | 4 | >>> x.T | array([[1, 4], | [2, 5], | [3, 6]]) | >>> x.T.flat[3] | 5 | >>> type(x.flat) | <type 'numpy.flatiter'> | | An assignment example: | | >>> x.flat = 3; x | array([[3, 3, 3], | [3, 3, 3]]) | >>> x.flat[[1,4]] = 1; x | array([[3, 1, 3], | [3, 1, 3]]) | | imag | The imaginary part of the array. | | Examples | -------- | >>> x = np.sqrt([1+0j, 0+1j]) | >>> x.imag | array([ 0. , 0.70710678]) | >>> x.imag.dtype | dtype('float64') | | itemsize | Length of one array element in bytes. | | Examples | -------- | >>> x = np.array([1,2,3], dtype=np.float64) | >>> x.itemsize | 8 | >>> x = np.array([1,2,3], dtype=np.complex128) | >>> x.itemsize | 16 | | nbytes | Total bytes consumed by the elements of the array. | | Notes | ----- | Does not include memory consumed by non-element attributes of the | array object. | | Examples | -------- | >>> x = np.zeros((3,5,2), dtype=np.complex128) | >>> x.nbytes | 480 | >>> np.prod(x.shape) * x.itemsize | 480 | | ndim | Number of array dimensions. | | Examples | -------- | >>> x = np.array([1, 2, 3]) | >>> x.ndim | 1 | >>> y = np.zeros((2, 3, 4)) | >>> y.ndim | 3 | | real | The real part of the array. | | Examples | -------- | >>> x = np.sqrt([1+0j, 0+1j]) | >>> x.real | array([ 1. , 0.70710678]) | >>> x.real.dtype | dtype('float64') | | See Also | -------- | numpy.real : equivalent function | | shape | Tuple of array dimensions. | | Notes | ----- | May be used to "reshape" the array, as long as this would not | require a change in the total number of elements | | Examples | -------- | >>> x = np.array([1, 2, 3, 4]) | >>> x.shape | (4,) | >>> y = np.zeros((2, 3, 4)) | >>> y.shape | (2, 3, 4) | >>> y.shape = (3, 8) | >>> y | array([[ 0., 0., 0., 0., 0., 0., 0., 0.], | [ 0., 0., 0., 0., 0., 0., 0., 0.], | [ 0., 0., 0., 0., 0., 0., 0., 0.]]) | >>> y.shape = (3, 6) | Traceback (most recent call last): | File "<stdin>", line 1, in <module> | ValueError: total size of new array must be unchanged | | size | Number of elements in the array. | | Equivalent to ``np.prod(a.shape)``, i.e., the product of the array's | dimensions. | | Examples | -------- | >>> x = np.zeros((3, 5, 2), dtype=np.complex128) | >>> x.size | 30 | >>> np.prod(x.shape) | 30 | | strides | Tuple of bytes to step in each dimension when traversing an array. | | The byte offset of element ``(i[0], i[1], ..., i[n])`` in an array `a` | is:: | | offset = sum(np.array(i) * a.strides) | | A more detailed explanation of strides can be found in the | "ndarray.rst" file in the NumPy reference guide. | | Notes | ----- | Imagine an array of 32-bit integers (each 4 bytes):: | | x = np.array([[0, 1, 2, 3, 4], | [5, 6, 7, 8, 9]], dtype=np.int32) | | This array is stored in memory as 40 bytes, one after the other | (known as a contiguous block of memory). The strides of an array tell | us how many bytes we have to skip in memory to move to the next position | along a certain axis. For example, we have to skip 4 bytes (1 value) to | move to the next column, but 20 bytes (5 values) to get to the same | position in the next row. As such, the strides for the array `x` will be | ``(20, 4)``. | | See Also | -------- | numpy.lib.stride_tricks.as_strided | | Examples | -------- | >>> y = np.reshape(np.arange(2*3*4), (2,3,4)) | >>> y | array([[[ 0, 1, 2, 3], | [ 4, 5, 6, 7], | [ 8, 9, 10, 11]], | [[12, 13, 14, 15], | [16, 17, 18, 19], | [20, 21, 22, 23]]]) | >>> y.strides | (48, 16, 4) | >>> y[1,1,1] | 17 | >>> offset=sum(y.strides * np.array((1,1,1))) | >>> offset/y.itemsize | 17 | | >>> x = np.reshape(np.arange(5*6*7*8), (5,6,7,8)).transpose(2,3,1,0) | >>> x.strides | (32, 4, 224, 1344) | >>> i = np.array([3,5,2,2]) | >>> offset = sum(i * x.strides) | >>> x[3,5,2,2] | 813 | >>> offset / x.itemsize | 813
Варианты ресемплинга¶
In [17]:
s_dayly=s.resample('D')
s_dayly
Out[17]:
2013-01-01 4978.620833 2013-01-02 5055.786111 2013-01-03 5078.461806 2013-01-04 5081.625694 2013-01-05 5070.797222 2013-01-06 5011.445833 2013-01-07 4945.418382 Freq: D, dtype: float64
In [20]:
s_dayly=s.resample('D', how='sum')
s_dayly
Out[20]:
2013-01-01 7169214 2013-01-02 7280332 2013-01-03 7312985 2013-01-04 7317541 2013-01-05 7301948 2013-01-06 7216482 2013-01-07 6725769 Freq: D, dtype: int64
Переходим к таблицам - объект DataFrames¶
In [23]:
data1=[random.randint(0,10) for x in xrange(10)]
data2=[random.randint(0,10000) for x in xrange(10)]
df=DataFrame({'data1':data1,'data2':data2})
df['sum']=df['data1']+df['data2']
df
Out[23]:
data1 | data2 | sum | |
---|---|---|---|
0 | 5 | 9907 | 9912 |
1 | 6 | 1462 | 1468 |
2 | 10 | 8704 | 8714 |
3 | 1 | 633 | 634 |
4 | 10 | 6351 | 6361 |
5 | 4 | 8775 | 8779 |
6 | 7 | 129 | 136 |
7 | 0 | 8402 | 8402 |
8 | 3 | 6296 | 6299 |
9 | 10 | 2000 | 2010 |
Stock from Yahoo and Vincent¶
In [4]:
import vincent
from vincent.ipynb import init_d3, init_vg, display_vega
init_d3()
init_vg()
from pandas.io.data import DataReader
In [5]:
data=DataReader('tsla','yahoo',start='01-01-2010')
data.tail()
Out[5]:
Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2014-02-10 | 189.34 | 199.30 | 189.32 | 196.56 | 12970700 | 196.56 |
2014-02-11 | 198.97 | 202.20 | 192.70 | 196.62 | 10709900 | 196.62 |
2014-02-12 | 195.78 | 198.27 | 194.32 | 195.32 | 5173700 | 195.32 |
2014-02-13 | 193.34 | 202.72 | 193.25 | 199.63 | 8029300 | 199.63 |
2014-02-14 | 198.10 | 201.88 | 197.00 | 198.23 | 6158000 | 198.23 |
In [29]:
help(DataReader)
Help on function DataReader in module pandas.io.data: DataReader(name, data_source=None, start=None, end=None, retry_count=3, pause=0.001) Imports data from a number of online sources. Currently supports Yahoo! Finance, Google Finance, St. Louis FED (FRED) and Kenneth French's data library. Parameters ---------- name : str or list of strs the name of the dataset. Some data sources (yahoo, google, fred) will accept a list of names. data_source: str the data source ("yahoo", "google", "fred", or "ff") start : {datetime, None} left boundary for range (defaults to 1/1/2010) end : {datetime, None} right boundary for range (defaults to today) Examples ---------- # Data from Yahoo! Finance gs = DataReader("GS", "yahoo") # Data from Google Finance aapl = DataReader("AAPL", "google") # Data from FRED vix = DataReader("VIXCLS", "fred") # Data from Fama/French ff = DataReader("F-F_Research_Data_Factors", "famafrench") ff = DataReader("F-F_Research_Data_Factors_weekly", "famafrench") ff = DataReader("6_Portfolios_2x3", "famafrench") ff = DataReader("F-F_ST_Reversal_Factor", "famafrench")
In [6]:
high=data['High']
high.head(10)
Out[6]:
Date 2010-06-29 25.00 2010-06-30 30.42 2010-07-01 25.92 2010-07-02 23.10 2010-07-06 20.00 2010-07-07 16.63 2010-07-08 17.52 2010-07-09 17.90 2010-07-12 18.07 2010-07-13 18.64 Name: High, dtype: float64
In [7]:
high_d=high.resample('D',how='sum')
In [8]:
high_d.head()
Out[8]:
Date 2010-06-29 25.00 2010-06-30 30.42 2010-07-01 25.92 2010-07-02 23.10 2010-07-03 NaN Freq: D, Name: High, dtype: float64
Функции работы с выборками очень интересны, здесь есть и пропущенные значения..., которые должны отрабатываться..., ресемплинг надо бы подробнее изучить. Зачем понадобилось менять дни на дни?
In [43]:
line=vincent.Line()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-43-2ee57a8e7f59> in <module>() ----> 1 line=vincent.Line() C:\Users\kiss\Anaconda\lib\site-packages\vincent\charts.pyc in __init__(self, *args, **kwargs) 117 """Create a Vega Line Chart""" 118 --> 119 super(Line, self).__init__(*args, **kwargs) 120 121 # Scales C:\Users\kiss\Anaconda\lib\site-packages\vincent\charts.pyc in __init__(self, data, columns, key_on, iter_idx, width, height, grouped, no_data, *args, **kwargs) 91 #Data 92 if data is None and not no_data: ---> 93 raise ValueError('Please initialize the chart with data.') 94 95 if not no_data: ValueError: Please initialize the chart with data.
Далее мой браузер не захотел выполнять javascript... И остаток этого поста отражает мои попытки решить проблему методом тыка. Так что, далее лучше не читать... Работа над этими ошибками заставила надолго отвлечься на инфраструктурные задачи. Сначлаа пришлось обновить Анаконду, которао прошло не без проблем. Об этом отдельный пост далее. Потом я изучал способы "визуализации". Стек Python, Vincent, Vega, D3, nvD3 - в следующих постах...
In []:
# До этих команд дело не доходит
line.tabular_data(high)
display_vega(line)
Итак, я запускаю команды, после которых должен построится график, но выскакивает ошибка. Пробуем сначал вспомнить, что мы забыли импортировать:
In [41]:
from IPython.display import display, HTML, Javascript
Не помогло, но в мануале vincent есть еще подсказака ...для IPython надо использовать:
In [44]:
vincent.core.initialize_notebook()
<IPython.core.display.Javascript at 0xb230978>
<IPython.core.display.Javascript at 0xb230978>
Ага, в консоли браузера (Chrom) находим сообщение:
In []:
#Failed to load resource: the server responded with a status of 404 (Not Found)
http://127.0.0.1:8888/static/components/jquery/jquery-2.0.2.min.map
Находим, папку /jquery/ из которой сервер должен грузить скрипт... и убеждаемся, что дебаггеры не врут, там действительно нет такого файла:
In [46]:
!chcp 65001
!dir C:\Users\kiss\Anaconda\Lib\site-packages\IPython\html\static\components\jquery
Active code page: 65001 Volume in drive C has no label. Volume Serial Number is 6017-2A0B Directory of C:\Users\kiss\Anaconda\Lib\site-packages\IPython\html\static\components\jquery 28.11.2013 22:49 <DIR> . 28.11.2013 22:49 <DIR> .. 16.09.2013 20:14 747 bower.json 16.09.2013 20:14 243 component.json 16.09.2013 20:14 881 composer.json 16.09.2013 20:14 16В 178 jquery-migrate.js 16.09.2013 20:14 7В 086 jquery-migrate.min.js 16.09.2013 20:14 242В 915 jquery.js 16.09.2013 20:14 83В 501 jquery.min.js 16.09.2013 20:14 144 package.json 16.09.2013 20:14 336 README.md 9 File(s) 352В 031 bytes 2 Dir(s) 410В 146В 713В 600 bytes free
Гуглим и находим прямой, как правда совет Try conda update ipython Пробуем из консоли... и находим обновления... сохраняемся, выключаемся и обновляемся
In [10]:
line=vincent.Line()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-10-2ee57a8e7f59> in <module>() ----> 1 line=vincent.Line() C:\Users\kiss\Anaconda\lib\site-packages\vincent\charts.pyc in __init__(self, *args, **kwargs) 117 """Create a Vega Line Chart""" 118 --> 119 super(Line, self).__init__(*args, **kwargs) 120 121 # Scales C:\Users\kiss\Anaconda\lib\site-packages\vincent\charts.pyc in __init__(self, data, columns, key_on, iter_idx, width, height, grouped, no_data, *args, **kwargs) 91 #Data 92 if data is None and not no_data: ---> 93 raise ValueError('Please initialize the chart with data.') 94 95 if not no_data: ValueError: Please initialize the chart with data.
In [13]:
from IPython.display import display, HTML, Javascript
In [17]:
import vincent
vincent.core.initialize_notebook()
<IPython.core.display.Javascript at 0xa5baa58>
<IPython.core.display.Javascript at 0xa5baa58>
In [14]:
# До этих команд дело не доходит
line.tabular_data(high)
display_vega(line)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-14-b34b72470485> in <module>() 1 # До этих команд дело не доходит ----> 2 line.tabular_data(high) 3 display_vega(line) NameError: name 'line' is not defined
In []:
Посты чуть ниже также могут вас заинтересовать
Комментариев нет:
Отправить комментарий