To check in which modules np.inner
and np.sum
are implemented I type
>>> np.inner.__module__ 'numpy.core.multiarray' >>>
np.sum.__module__ 'numpy.core.fromnumeric' >>>
np.__file__ '/Users/uweschmitt/venv_so/lib/python3.5/site-packages/numpy/__init__.py'
To prove my assumption I run the timing with a larger array and get
In[8]: a = np.random.random(1000000)
In[9]: % timeit np.inner(a, a)
1000 loops, best of 3: 673 µs per loop
In[10]: % timeit np.sum(a)
1000 loops, best of 3: 584 µs per loop
Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.,If axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.,Sum of array elements over a given axis.,If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.
>>> np.sum([])
0.0
>>> np.sum([0.5, 1.5]) 2.0 >>> np.sum([0.5, 0.7, 0.2, 1.5], dtype = np.int32) 1 >>> np.sum([ [0, 1], [0, 5] ]) 6 >>> np.sum([ [0, 1], [0, 5] ], axis = 0) array([0, 6]) >>> np.sum([ [0, 1], [0, 5] ], axis = 1) array([1, 5]) >>> np.sum([ [0, 1], [np.nan, 5] ], where = [False, True], axis = 1) array([1., 5.])
>>> np.ones(128, dtype = np.int8).sum(dtype = np.int8) - 128
>>> np.sum([10], initial = 5) 15
We can initialize numpy arrays from nested Python lists, and access elements using square brackets:,Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:,Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. If you are already familiar with MATLAB, you might find this tutorial useful to get started with Numpy.,A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x
for x in arr
if x < pivot
] middle = [x
for x in arr
if x == pivot
] right = [x
for x in arr
if x > pivot
]
return quicksort(left) + middle + quicksort(right)
print(quicksort([3, 6, 8, 10, 1, 2, 1])) # Prints "[1, 1, 2, 3, 6, 8, 10]"
x = 3
print(type(x)) # Prints "<class 'int'>"
print(x) # Prints "3"
print(x + 1) # Addition; prints "4"
print(x - 1) # Subtraction; prints "2"
print(x * 2) # Multiplication; prints "6"
print(x ** 2) # Exponentiation; prints "9"
x += 1
print(x) # Prints "4"
x *= 2
print(x) # Prints "8"
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
print(t and f) # Logical AND; prints "False"
print(t or f) # Logical OR; prints "True"
print(not t) # Logical NOT; prints "False"
print(t != f) # Logical XOR; prints "True"
hello = 'hello' # String literals can use single quotes world = "world" # or double quotes; it does not matter. print(hello) # Prints "hello" print(len(hello)) # String length; prints "5" hw = hello + ' ' + world # String concatenation print(hw) # prints "hello world" hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting print(hw12) # prints "hello world 12"
s = "hello" print(s.capitalize()) # Capitalize a string; prints "Hello" print(s.upper()) # Convert a string to uppercase; prints "HELLO" print(s.rjust(7)) # Right - justify a string, padding with spaces; prints " hello" print(s.center(7)) # Center a string, padding with spaces; prints " hello " print(s.replace('l', '(ell)')) # Replace all instances of one substring with another; # prints "he(ell)(ell)o" print(' world '.strip()) # Strip leading and trailing whitespace; prints "world"
xs = [3, 1, 2] # Create a list print(xs, xs[2]) # Prints "[3, 1, 2] 2" print(xs[-1]) # Negative indices count from the end of the list; prints "2" xs[2] = 'foo' # Lists can contain elements of different types print(xs) # Prints "[3, 1, 'foo']" xs.append('bar') # Add a new element to the end of the list print(xs) # Prints "[3, 1, 'foo', 'bar']" x = xs.pop() # Remove and return the last element of the list print(x, xs) # Prints "bar [3, 1, 'foo']"
Computes the sum of elements across dimensions of a tensor.,If axis is None, all dimensions are reduced, and a tensor with a single element is returned.,Reduces input_tensor along the dimensions given in axis. Unless keepdims is true, the rank of the tensor is reduced by 1 for each of the entries in axis, which must be unique. If keepdims is true, the reduced dimensions are retained with length 1.,Equivalent to np.sum apart the fact that numpy upcast uint8 and int32 to int64 while tensorflow returns the same dtype as the input.
View aliases
Main aliases
tf.math.reduce_sum( input_tensor, axis = None, keepdims = False, name = None )
>>> # x has a shape of (2, 3)(two rows and three columns):
>>>
x = tf.constant([
[1, 1, 1],
[1, 1, 1]
]) >>>
x.numpy()
array([
[1, 1, 1],
[1, 1, 1]
], dtype = int32) >>>
# sum all the elements >>>
# 1 + 1 + 1 + 1 + 1 + 1 = 6 >>>
tf.reduce_sum(x).numpy()
6
>>>
# reduce along the first dimension >>>
# the result is[1, 1, 1] + [1, 1, 1] = [2, 2, 2] >>>
tf.reduce_sum(x, 0).numpy()
array([2, 2, 2], dtype = int32) >>>
# reduce along the second dimension >>>
# the result is[1, 1] + [1, 1] + [1, 1] = [3, 3] >>>
tf.reduce_sum(x, 1).numpy()
array([3, 3], dtype = int32) >>>
# keep the original dimensions >>>
tf.reduce_sum(x, 1, keepdims = True).numpy()
array([
[3],
[3]
], dtype = int32) >>>
# reduce along both dimensions >>>
# the result is 1 + 1 + 1 + 1 + 1 + 1 = 6 >>>
# or, equivalently, reduce along rows, then reduce the resultant array >>>
#[1, 1, 1] + [1, 1, 1] = [2, 2, 2] >>>
# 2 + 2 + 2 = 6 >>>
tf.reduce_sum(x, [0, 1]).numpy()
6
>>> # x has a shape of (2, 3)(two rows and three columns):
>>>
x = tf.constant([
[1, 1, 1],
[1, 1, 1]
]) >>>
x.numpy()
array([
[1, 1, 1],
[1, 1, 1]
], dtype = int32) >>>
# sum all the elements >>>
# 1 + 1 + 1 + 1 + 1 + 1 = 6 >>>
tf.reduce_sum(x).numpy()
6
>>>
# reduce along the first dimension >>>
# the result is[1, 1, 1] + [1, 1, 1] = [2, 2, 2] >>>
tf.reduce_sum(x, 0).numpy()
array([2, 2, 2], dtype = int32) >>>
# reduce along the second dimension >>>
# the result is[1, 1] + [1, 1] + [1, 1] = [3, 3] >>>
tf.reduce_sum(x, 1).numpy()
array([3, 3], dtype = int32) >>>
# keep the original dimensions >>>
tf.reduce_sum(x, 1, keepdims = True).numpy()
array([
[3],
[3]
], dtype = int32) >>>
# reduce along both dimensions >>>
# the result is 1 + 1 + 1 + 1 + 1 + 1 = 6 >>>
# or, equivalently, reduce along rows, then reduce the resultant array >>>
#[1, 1, 1] + [1, 1, 1] = [2, 2, 2] >>>
# 2 + 2 + 2 = 6 >>>
tf.reduce_sum(x, [0, 1]).numpy()
6