import numpy as np
integers=np.array([10,20,30,40,50])
integers
array([10, 20, 30, 40, 50])
integers[0]
10
# Redefining the value of the first index in integers
integers[0]=20
integers
array([20, 20, 30, 40, 50])
# Cannot assign a float to integers. Because in Numpy, all of the
# data has to be of the same type, and this array is already
# type int64. See dtype in next cell.
integers[0]=21.5
integers
array([21, 20, 30, 40, 50])
integers.dtype
dtype('int64')
# Use dtype= to redefine the data type of the array.
# The choice of data type can have a large affect on memory consumption.
# See the differences in memory usage below.
smallerIntegers=np.array(integers,dtype=np.int8)
# This is dtype int8 vs the previous int64
smallerIntegers
array([21, 20, 30, 40, 50], dtype=int8)
integers.nbytes
40
# int8 uses 1/8 the memory of int64
smallerIntegers.nbytes
5
# Numpy will help manage data overflow, but we have to be cautious
# when choosing data types to avoid problems.
overflow = np.array([127,128,129], dtype = np.int8)
overflow
array([ 127, -128, -127], dtype=int8)
floats=np.array([1.2,2.3,3.4,5.1,8.3])
floats
array([1.2, 2.3, 3.4, 5.1, 8.3])
floats.dtype
dtype('float64')
floats.nbytes
40
nums=np.array([[1,2,3,4,5],[6,7,8,9,10]])
nums
array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
nums[0,0]
1
nums[1,4]
10
# Returns the number of dimensions of the array
nums.ndim
2
multi_arr=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
multi_arr
array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]])
multi_arr[1,0,2]
9
first_list=[1,2,3,4,5,6,7,8,9,10]
first_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# np.array() will convert a list to a Numpy array
first_array=np.array(first_list)
first_array
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# The np.array() function will convert all of the items to the type
# that is necessary to show all items
second_list=[1,2,3,-1.23,50,128000.56,4.56]
second_array=np.array(second_list)
second_array
array([ 1.0000000e+00, 2.0000000e+00, 3.0000000e+00, -1.2300000e+00, 5.0000000e+01, 1.2800056e+05, 4.5600000e+00])
# There were floats in the original list before it was converted.
# So all items had to be converted to floats to accommodate the
# original floats
second_array.dtype
dtype('float64')
third_list=['Ann',111111,'Peter',111112,'Susan',111113,'John',111114]
third_array=np.array(third_list)
# All elements are turned into strings to accommodate the strings
# in the original list.
third_array
array(['Ann', '111111', 'Peter', '111112', 'Susan', '111113', 'John', '111114'], dtype='<U21')
# Tuple to array
first_tuple=(5, 10, 15, 20, 25, 30)
array_from_tuple=np.array(first_tuple)
array_from_tuple
array([ 5, 10, 15, 20, 25, 30])
array_from_tuple.dtype
dtype('int64')
# Array from a multi-dimensional list.
multi_dim_list=[[[0,1,2], [3,4,5]], [[6,7,8],[9,10,11]]]
arr_from_multi_dim_list=np.array(multi_dim_list)
arr_from_multi_dim_list
array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]])
np.arange()
¶np.arange(start, stop, increment)
range
function, but returns an ndarray rather than a list.
When using a non-integer step, such as 0.1, the results will often not
be consistent. It is better to use `numpy.linspace` for these cases.
Parameters
----------
start : integer or real, optional
Start of interval. The interval includes this value. The default
start value is 0.
stop : integer or real
End of interval. The interval does not include this value, except
in some cases where `step` is not an integer and floating point
round-off affects the length of `out`.
step : integer or real, optional
Spacing between values. For any output `out`, this is the distance
between two adjacent values, ``out[i+1] - out[i]``. The default
step size is 1. If `step` is specified as a position argument,
`start` must also be given.
dtype : dtype
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.
# np.arange() - if you pass 1 int, you get a one-dimensional array
# going up to the number passed
integers_array=np.arange(10)
integers_array
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# passing 2 gives the stop and start
integers_second_array=np.arange(100,130)
integers_second_array
array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129])
# 3 gives you the start, the stop, and the step
integers_third_array=np.arange(100,151,2)
integers_third_array
array([100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150])
np.linspace()
¶np.linspace(start, stop, sample_count)
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) Return evenly spaced numbers over a specified interval.
Returns `num` evenly spaced samples, calculated over the
interval [`start`, `stop`].
The endpoint of the interval can optionally be excluded.
# Takes the start, the stop, and the number of samples desired,
# the default sample count is 50.
first_floats_arr=np.linspace(10,20)
first_floats_arr
array([10. , 10.20408163, 10.40816327, 10.6122449 , 10.81632653, 11.02040816, 11.2244898 , 11.42857143, 11.63265306, 11.83673469, 12.04081633, 12.24489796, 12.44897959, 12.65306122, 12.85714286, 13.06122449, 13.26530612, 13.46938776, 13.67346939, 13.87755102, 14.08163265, 14.28571429, 14.48979592, 14.69387755, 14.89795918, 15.10204082, 15.30612245, 15.51020408, 15.71428571, 15.91836735, 16.12244898, 16.32653061, 16.53061224, 16.73469388, 16.93877551, 17.14285714, 17.34693878, 17.55102041, 17.75510204, 17.95918367, 18.16326531, 18.36734694, 18.57142857, 18.7755102 , 18.97959184, 19.18367347, 19.3877551 , 19.59183673, 19.79591837, 20. ])
second_floats_arr=np.linspace(10,20,5)
second_floats_arr
array([10. , 12.5, 15. , 17.5, 20. ])
np.random.rand()
¶np.random.rand(sample_count, column_count)
gives random floats between 0 and 1
Parameters
----------
d0, d1, ..., dn : int, optional
The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
Returns
-------
out : ndarray, shape ``(d0, d1, ..., dn)``
Random values.
first_rand_arr=np.random.rand(10)
first_rand_arr
array([0.40109656, 0.68563284, 0.55711714, 0.68809425, 0.19604999, 0.25660991, 0.54011874, 0.40838049, 0.55767224, 0.98873018])
second_rand_arr=np.random.rand(4,4)
second_rand_arr
array([[0.39301171, 0.01824687, 0.61679899, 0.37538241], [0.05313891, 0.74948724, 0.31047951, 0.38531519], [0.96255685, 0.27819059, 0.06456424, 0.86727658], [0.35461419, 0.24622019, 0.71915661, 0.99312943]])
np.random.randint()
¶np.random.randint(low_end, high_end, sample_count)
randint(...) method of numpy.random.mtrand.RandomState instance randint(low, high=None, size=None, dtype=int)
Return random integers from `low` (inclusive) to `high` (exclusive).
Return random integers from the "discrete uniform" distribution of
the specified dtype in the "half-open" interval [`low`, `high`). If
`high` is None (the default), then results are from [0, `low`).
third_rand_arr=np.random.randint(0,100,20)
third_rand_arr
array([62, 93, 43, 29, 32, 36, 84, 51, 93, 8, 42, 12, 31, 4, 61, 70, 75, 99, 15, 23])
np.zeros()
¶np.zeros(sample_count)
np.zeros(rows, columns)
zeros(shape, dtype=float, order='C', *, like=None)
Return a new array of given shape and type, filled with zeros.
Parameters
----------
shape : int or tuple of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
order : {'C', 'F'}, optional, default: 'C'
Whether to store multi-dimensional data in row-major
(C-style) or column-major (Fortran-style) order in
memory.
first_z_array=np.zeros(5)
first_z_array
array([0., 0., 0., 0., 0.])
second_z_array=np.zeros((4,5))
second_z_array
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])
first_ones_array=np.ones(6)
first_ones_array
array([1., 1., 1., 1., 1., 1.])
second_ones_array=np.ones((7,8))
second_ones_array
array([[1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.]])
third_ones_array=np.ones((4,5),dtype=int)
third_ones_array
array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
np.empty(), np.fill(), np.full()
¶np.empty()
- empty(shape, dtype=float, order='C', *, like=None) - Return a new array of given shape and type, without initializing entries.
np.ndarray.fill()
- creates 1 dimensional arrays, used to fill an already existing array with similar values
a.fill(value)
Fill the array with a scalar value.
Parameters
----------
value : scalar
All elements of `a` will be assigned this value.
Examples
--------
>>> a = np.array([1, 2])
>>> a.fill(0)
>>> a
array([0, 0])
>>> a = np.empty(2)
>>> a.fill(1)
>>> a
array([1., 1.])
np.full()
- creates 1 or 2 dimensional arrays
full(shape, fill_value, dtype=None, order='C', *, like=None)
Return a new array of given shape and type, filled with fill_value
.
Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
fill_value : scalar or array_like
Fill value.
dtype : data-type, optional
The desired data-type for the array The default, None, means
``np.array(fill_value).dtype``.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory.
# Create an empty 1D array of 10 items
first_fill_array=np.empty(10,dtype=int)
# Fill the 10 items with all 12s
first_fill_array.fill(12)
first_fill_array
array([12, 12, 12, 12, 12, 12, 12, 12, 12, 12])
# Create an array with 4 items, all 10s
first_full_array=np.full(5,10)
first_full_array
array([10, 10, 10, 10, 10])
# Crete an array with 4 rows, 5 columns, all 8s
second_full_array=np.full((4,5),8)
second_full_array
array([[8, 8, 8, 8, 8], [8, 8, 8, 8, 8], [8, 8, 8, 8, 8], [8, 8, 8, 8, 8]])
array = a container with a fixed size that has elements of the same type
shape = the number of elements in each dimension, represented as a tuple of non-negative integers.
first_arr=np.arange(20)
first_arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
# Create a 2D array with a start of (1,2), an end of (10, 20)
# and a sample size of 10 1D arrays, creating a 2D array
second_arr=np.linspace((1,2),(10,20),10)
second_arr
array([[ 1., 2.], [ 2., 4.], [ 3., 6.], [ 4., 8.], [ 5., 10.], [ 6., 12.], [ 7., 14.], [ 8., 16.], [ 9., 18.], [10., 20.]])
# Create a 3 dimensional array of all 10s
# 2 rows each, with 2 columns each
third_arr=np.full((2,2,2),10)
third_arr
array([[[10, 10], [10, 10]], [[10, 10], [10, 10]]])
np.shape()
- returns a tuple of the shape of each dimension of the elements within the array.¶# 20 elements in 1 dimension.
np.shape(first_arr)
(20,)
# 10 rows, 2 columns
np.shape(second_arr)
(10, 2)
# 2 dimensions, 2 rows, 2 columns
np.shape(third_arr)
(2, 2, 2)
np.size()
- returns the number of elements in an array¶np.size(first_arr)
20
np.size(second_arr)
20
np.size(third_arr)
8