<jupyter_start><jupyter_text>Numpy array is nothing but collection of items or elements of same data type.<jupyter_code>import array as ar<jupyter_output><empty_output><jupyter_text>Note: In the above code __import__ and __as__ are keywords.__Array__ is a built-in module.And __ar__ is an __alias__ of an array(and alias name can be anything). Note1: Array in python is inherited from c++<jupyter_code>#example for creating an array in python.
import array as ar
a = ar.array('i', [1, 2, 3, 4])
print(a)
print(type(a))
ar.ArrayType?
#example to add 2 arrays
x = ar.array('i', [1, 2, 3])
y = ar.array('i', [4, 5, 6])
print(x+y)<jupyter_output>array('i', [1, 2, 3, 4, 5, 6])<jupyter_text>Note: When you add 2 array you get a concatenated array as an output, instead of addition of each array element. Note1: Another disadvantage is we need to specify the data type while creating an array.    And multi-dimensional array creation is difficult.So to overcome these disadvantages we use numpy(numerical python) instead of array. Note2: All the list data structure methods are applicable to array data type. Note3: Functionalities of numpy are imported from mat-lab.Numpy is nothing but n-dimensional array. Important terminologies in numpy.Scalar: Which has only magnitude.    Vector: Which has both magnitude and direction.    Matrix: Which is nothing but collection of scalars with direction or multiple vectors in it.Tensorflow: Is nothing but the collection of matrices.<jupyter_code>#example for a scalar.
import numpy as np
a = np.array(2)
print(a)
print(type(a))
#example for a vector.
a = np.array([1, 2, 3])
print(a)
print(type(a))
#example to create a matrix.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(type(a))<jupyter_output>[[1 2 3]
 [4 5 6]]
<class 'numpy.ndarray'><jupyter_text>Note: Computational time taken by python is more for large values.Another disadvantage of python is that you need to write N-for loops for creating N-dimensional array(This is not required in numpy).<jupyter_code>#example demonstrating the above Note.
a = range(100000)
print(a) #write this code in python IDLE.<jupyter_output>range(0, 100000)<jupyter_text>Installing numpy using pip command.!pip install numpy(Type this code in jupyter lab).<jupyter_code>#example for creating an array using numpy.
import numpy as np
a = np.array([1, 2, 3])
print(a)<jupyter_output>[1 2 3]<jupyter_text>Seven basic methods of numpy array. Note: These seven methods can be applied for all types like scalar, vector, matrix and tensorflow. shape: This method prints row and column of numpy array.dtype: Is used to find the data type of the array elements.item: Method is used to convert to scalar.itemsize: This method is used denote size of each element present in an array.ndim: Is used to find the dimension of numpy array.astype: Method is used to convert the array element to other data type.data: Method is used to know the address of numpy array.<jupyter_code>#appplying all the above seven methods on a scalar numpy array.
a = np.array(10)
print(a.shape)
print(a.dtype)
print(a.item)
print(a.itemsize)
print(a.ndim)
print(a.astype(complex))
print(a.data)
#appplying all the above seven methods on vector numpy array.
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a.shape)
print(a.dtype)
print(a.item)
print(a.itemsize)
print(a.ndim)
print(a.astype(float))
print(a.data)<jupyter_output>(5,)
int32
<built-in method item of numpy.ndarray object at 0x000001F4E5ADC3A0>
4
1
[1. 2. 3. 4. 5.]
<memory at 0x000001F4E5991E88><jupyter_text>Note: If you create a numpy array, of complex data type and convert it into int data type using astype method it returns the real part of the complex number.<jupyter_code>#example for numpy array with zeros in it(1D).
import numpy as np
a = np.zeros(5, dtype = int)
print(a)<jupyter_output>[0 0 0 0 0]<jupyter_text>Note: Zeros command, outputs an array containing zeros in it. The number of zeros to be printed is specified inside the parenthesis.Default output zeros will be in float format.<jupyter_code>#example for numpy array with zeros in it(2D).
a = np.zeros((3, 3)) #order of the matrix is specified inside the parenthesis.
print(a)
#example for numpy array with ones in it(1D).
b = np.ones(5)
print(b)
#example for numpy array with ones in it(2D).
a = np.ones((2, 2), dtype = int)
print(a)
#example for numpy array with empty values in it(1D).
import numpy as np
c = np.empty(4) #note this function will print junk values and zeros as an output
print(c) 
#example for numpy array with empty values in it(2D).
d = np.empty((5, 5))
print(d)<jupyter_output>[[1.06282618e-311 7.80623720e-322 0.00000000e+000 0.00000000e+000
  9.34611147e-307]
 [1.16096346e-028 1.04857803e-142 1.16467185e-028 7.26221256e+199
  1.63723574e+165]
 [1.81451584e-152 2.45068625e+198 2.96410912e+222 1.43616552e+161
  2.88980647e+204]
 [7.10523799e-077 1.36179230e+030 1.03363560e-071 1.37466001e-070
  3.25113983e-086]
 [9.45457959e+218 7.10523799e-077 1.36179230e+030 1.24557024e-037
  6.79769279e-310]]<jupyter_text>Note: Indexing of matrix or an array is possible.<jupyter_code>#example demonstrating the above note.
import numpy as np
d = np.empty((3, 3))
print(a)
print(a[1][2])
print(a[2][1])<jupyter_output>[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
0.0
0.0
{'fraction_non_alphanumeric': 0.10323312535450936, 'fraction_numerical': 0.08281338627339761, 'mean_word_length': 4.923852183650616, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '374358', 'license': 'Apache-2.0', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1800, 'n_words': 701, 'path': 'Basics of Numpy.ipynb', 'repo_name': 'Raghu010/Learn-Numpy-Numerical-Python-', 'size': '5289'}