import numpy as np
one_dimensional_list = [1,2,4]
one_dimensional_arr = np.array(one_dimensional_list)
print("1D array is : ",one_dimensional_arr)
import numpy as np
two_dimensional_list=[[1,2,3],[4,5,6]]
two_dimensional_arr = np.array(two_dimensional_list)
print("2D array is : ",two_dimensional_arr)
import numpy as np
three_dimensional_list=[[[1,2,3],[4,5,6],[7,8,9]]]
three_dimensional_arr = np.array(three_dimensional_list)
print("3D array is : ",three_dimensional_arr)
import numpy as np
ndArray = np.array([1, 2, 3, 4], ndmin=6)
print(ndArray)
print('Dimensions of array:', ndArray.ndim)
import numpy as np
a = [[1,2,3], [3,4,5], [23, 45,1]
Print (a.sum(axis=1))
Conversely, for column sum,
a = [[1,2,3], [3,4,5], [23, 45,1]
Print (a.sum(axis=0))
[27, 51, 9]
print(‘\n data type num 1 ‘,num.dtype)
print(‘\n data type num 2 ‘,num2.dtype)
print(‘\n data type num 3 ‘,num3.dtype)
a= [14, 98, 87].
import numpy as np
import sys
print(sys.getsizeof(a)* len(a))
a= np.array([14,98,87])
print(“The size in bytes of one element in array: “, a.itemsize)
print(“The size of array: “, a.size)
print(“The bytes of all elements in a: “, a.size * a.itemsize)
import numpy as np
a= np.array([14,98,87])
print(“The total size of space or memory the array occupies: “, a.nbytes)
[[35 53 63]
[72 12 22]
[43 84 56]]
[
20
30
40
]
import NumPy as np
#inputs
inputArray = np.array([[35,53,63],[72,12,22],[43,84,56]])
new_col = np.array([[20,30,40]])
# delete 2nd column
arr = np.delete(sampleArray , 1, axis = 1)
#insert new_col to array
arr = np.insert(arr , 1, new_col, axis = 1)
print (arr)
numpy.loadtxt()
which can automatically read the file’s header and footer lines and the comments if any.arange
function is a built function in the python class that helps generate a sequence of integer values within a certain range. However, the arange
function is a built-in function in the python library called Numpy, and so to use the arange function, you will have to install the NumPy package. Both range
and arange
functions take the same parameters shown below. (start, stop, and step). The main difference is that the range function takes only integers arguments. Otherwise, it returns an error message while the arange function will generate or return an instance of the NumPy ndarray.range([start], stop[, step])
numpy.arange([start, ]stop, [step, ]dtype=None)
bincount()
function. It should be noted that the bincount()
function accepts positive integers or boolean expressions as its argument. Negative integers cannot be used. NumPy.bincount()
. The resulting array isarr = NumPy.array([0, 5, 4, 0, 4, 4, 3, 0, 0, 5, 2, 1, 1, 9])
NumPy.bincount(arr)
genfromtxt()
method by setting the delimiter as a comma.from numpy import genfromtxt
csv_data = genfromtxt('sample_doc.csv', delimiter=',')
arr = np.array([[8, 3, 2],
[3, 6, 5],
[6, 1, 4]])
[[6, 1, 4],
[8, 3, 2],
[3, 6, 5]]
import numpy as np
arr = np.array([[8, 3, 2],
[3, 6, 5],
[6, 1, 4]])
#sort the array using np.sort
arr = np.sort(arr.view('i8,i8,i8'),
order=['f1'],
axis=0).view(np.int)
arr.view('i8,i8,i8').sort(order=['f1'], axis=0)
len()
.a = NumPy.zeros((1,0))
a.size
0
len(a)
1​
import numpy as np
def find_nearest_value(arr, value):
arr = np.asarray(arr)
idx = (np.abs(arr - value)).argmin()
return arr[idx]
#Driver code
arr = np.array([ 0.21169, 0.61391, 0.6341, 0.0131, 0.16541, 0.5645, 0.5742])
value = 0.52
print(find_nearest_value(arr, value)) # Prints 0.5645
import numpy as np
arr_two_dim = np.array([("x1","x2", "x3","x4"),("x5","x6", "x7","x8" )])
arr_one_dim = np.array([3,2,4,5,6])
# find and print shape
print("2-D Array Shape: ", arr_two_dim.shape)
print("1-D Array Shape: ", arr_one_dim.shape)
"""
​
2-D Array Shape: (2, 4)
1-D Array Shape: (5,)
"""
​
a = np.arange(15)
index = np.where((a >= 5) & (a <= 10))
a[index]
index = np.where(np.logical_and(a>=5, a<=10))
a[index]
# (array([6, 9, 10]),)
a[(a >= 5) & (a <= 10)]
length = 10
start = 5
step = 3
def seq(start, length, step):
end = start + (step*length)
return np.arange(start, end, step)
seq(start, length, step)
# array([ 5, 8, 11, 14, 17, 20, 23, 26, 29, 32])
int16
. And print the following Attributes :import numpy
firstArray = numpy.empty([4,2], dtype = numpy.uint16)
print("Printing Array")
print(firstArray)
print("Printing numpy array Attributes")
print("1> Array Shape is: ", firstArray.shape)
print("2>. Array dimensions are ", firstArray.ndim)
print("3>. Length of each element of array in bytes is ", firstArray.itemsize)
import numpy
print("Printing Original array")
sampleArray = numpy.array([[34,43,73],[82,22,12],[53,94,66]])
print (sampleArray)
print("Array after deleting column 2 on axis 1")
sampleArray = numpy.delete(sampleArray , 1, axis = 1)
print (sampleArray)
arr = numpy.array([[10,10,10]])
print("Array after inserting column 2 on axis 1")
sampleArray = numpy.insert(sampleArray , 1, arr, axis = 1)
print (sampleArray)
import numpy
print("Creating 5X2 array using numpy.arange")
sampleArray = numpy.arange(100, 200, 10)
sampleArray = sampleArray.reshape(5,2)
print (sampleArray)
import NumPy
sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]])
newColumn = NumPy.array([[10,10,10]])
Printing Original array
[[34 43 73]
[82 22 12]
[53 94 66]]
Array after deleting column 2 on axis 1
[[34 73]
[82 12]
[53 66]]
Array after inserting column 2 on axis 1
[[34 10 73]
[82 10 12]
[53 10 66]]
import NumPy
print(“Printing Original array”)
sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]])
print (sampleArray)
print(“Array after deleting column 2 on axis 1”)
sampleArray = NumPy.delete(sampleArray , 1, axis = 1)
print (sampleArray)
arr = NumPy.array([[10,10,10]])
print(“Array after inserting column 2 on axis 1”)
sampleArray = NumPy.insert(sampleArray , 1, arr, axis = 1)
print (sampleArray)
# Input : a numpy datetime64 object
dt64 = np.datetime64('2018-02-25 22:10:10')
# Solution
from datetime import datetime
dt64.tolist()
# or
dt64.astype(datetime)
# datetime.datetime(2018, 2, 25, 22, 10, 10)