<jupyter_start><jupyter_text>Is Numpy faster than usual python lists<jupyter_code>import numpy as np
my_arr = np.arange(1000000)
my_list = list(range(1000000))
%time for _ in range(10):my_arr2 = my_arr * 2
%time for _ in range(10): my_list2 = [x * 2 for x in my_list]
import numpy as np
# Generate some random data
data = np.random.randn(2,3)
data
data * 10
data + data
data.shape
data.dtype<jupyter_output><empty_output><jupyter_text>Creating ndarrays<jupyter_code>data1 = [6,7.5,8,0,1]
arr1 = np.array(data1)
arr1
data2 = [[1,2,3,4],[5,6,7,8]]
arr2 = np.array(data2)
arr2
arr2.ndim
arr2.shape
arr1.dtype
arr2.dtype<jupyter_output><empty_output><jupyter_text>Other ways of Creating numpy arrays<jupyter_code>np.zeros(10)
np.zeros((3,6))
np.ones((2,3))
np.empty((2,3,2))
np.arange(9)<jupyter_output><empty_output><jupyter_text>DataTypes for numpy arrays<jupyter_code>arr1 = np.array([1,2,3], dtype = np.float64)
arr2 = np.array([1,2,3], dtype = np.int32)

print(arr1.dtype)
print(arr2.dtype)
arr = np.array([1,2,3,4,5])
print(arr.dtype)

float_arr = arr.astype(np.float64) # covert int datatype to float using astype
print(float_arr.dtype)
arr = np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
arr
# if we convert the datatype of arr array some value is truncated 
arr.astype(np.int32)
numeric_strings = np.array(['1.25','9.6','4.2'],dtype = np.string_)
numeric_strings.astype(float)
init_array = np.arange(10)
print(init_array)
calibers = np.array([.22,.270,.350,.380,.44,.50],dtype = np.float64)
init_array.astype(calibers.dtype)
empty_uint32 = np.empty(8,dtype='u4')
empty_uint32<jupyter_output><empty_output><jupyter_text>Arithmetic Operations Indexing and Slicing<jupyter_code>arr = np.array(([1.,2.,3.],[4.,5.,6.]))
arr
arr * arr
arr - arr
1/arr
# square root of a number
arr ** 0.5
arr2 = np.array([[0.,4.,1.],[7.,2.,12.]])
arr2
# comparsion of arrays 
arr2 > arr
# Basic Indexing and slicing
arr = np.arange(10)
arr
arr[5]
arr[5:8]
# slicing in numpy is view not copies.below is the examples
arr[5:8] = 12 
arr
arr_slice = arr[5:8]
arr_slice
arr_slice[1] = 12345
arr_slice
arr
# slicing in python lists are not view is the copies .Below is the example
l = [1,2,3,4,5,6]
l_slice = l[2:4]
l_slice
l_slice[0]= 10
l_slice
l
arr_slice[:]= 64
arr_slice
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2d[2]
arr2d[0][2]
arr2d[0,2]<jupyter_output><empty_output><jupyter_text>Handling Multidimensional array using numpy<jupyter_code>arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
arr3d
arr3d[0]
arr3d[1]
old_values = arr3d[0].copy()
old_values
arr3d[0] = 42
arr3d
arr3d[0] = old_values
arr3d
arr3d[1,0]<jupyter_output><empty_output><jupyter_text>Boolean and Fancy Indexing<jupyter_code>arr
arr[1:6]
arr2d
arr2d[:2]
arr2d[:2,1:]
arr2d[1,:2]
arr2d[:2,2]
arr2d[:,:1]
arr2d[:2,1:]
arr2d[:,:2]
arr2d[1:2,:2]
names = np.array(['Bob','joe','will','Bob','will','joe','joe'])
names
data = np.random.randn(7,4)
data
names == 'Bob'
data[names == 'Bob']
data[names == 'Bob',2:]
data[names == 'Bob',3]
names != 'Bob'
data[~(names == 'Bob')]
cond = names == 'Bob'
data[~cond]
mask = (names == 'Bob')|(names == 'will')
mask
data[mask]
# noise removal or clean up data
data[data < 0] = 0
data
data[names != 'joe']  = 7
data<jupyter_output><empty_output><jupyter_text>Fancy Indexing<jupyter_code>arr = np.empty((8,4))
for i in range(8):
    arr[i] = i
arr
# Fancy indexing
arr[[4,3,0,6]]
arr[[-3,-5,-7]]
arr = np.arange(32).reshape((8,4))
arr
# 2d fancy indexing
arr[[1,5,7,2],[0,3,1,2]]
# re-order column using fancy indexing
arr[[1,5,7,2]][:,[0,3,1,2]]<jupyter_output><empty_output><jupyter_text>Basic ndarrays Operations<jupyter_code># Transposing arrays
arr = np.arange(15).reshape((3,5))
arr
arr.T
# dot function for multiplication
np.dot(arr.T,arr)<jupyter_output><empty_output><jupyter_text>Transposing and Swap array axes<jupyter_code>arr = np.arange(16).reshape((2,2,4))
arr
arr.transpose((1 , 0 , 2))
#swap axes using swapaxes function
arr.swapaxes(1,2)<jupyter_output><empty_output><jupyter_text>Universal Functions in numpy<jupyter_code>arr = np.arange(5)
arr
np.sqrt(arr)
np.exp(arr)
x = np.random.randn(5)
y = np.random.randn(5)
x
y
np.maximum(x,y)
# splits function using modf function
arr = np.random.randn(5)*5
arr
remainder,whole_part = np.modf(arr)
remainder
whole_part<jupyter_output><empty_output><jupyter_text>File IO using numpy<jupyter_code>arr =  np.arange(10)
np.save('outfile',arr)
b = np.load('outfile.npy')
b
#  it helps to store multiple arrays
np.savez('outfile.npz',a=arr,b=arr)
# this file return dictionary that why we use arch
arch = np.load('outfile.npz')
arch['b']
#compressed file 
np.savez_compressed('outfile.npz',a=arr,b=arr)<jupyter_output><empty_output><jupyter_text>Linear Algebra using numpy<jupyter_code>x = np.array(([[1.,2.,3.],[4.,5.,6.]]))
y = np.array(([6.,23.],[-1,7],[8,9]))
x
y
x.dot(y)
np.dot(x,y)
np.dot(x , np.ones(3))
x @ np.ones(3)
from numpy.linalg import inv , qr
x = np.random.randn(3,3)
mat = x.T.dot(x) # laplasian
inv(mat)
mat.dot(inv(mat)) # Identity matrix
q , r = qr(mat) # matrix decomposition
r<jupyter_output><empty_output><jupyter_text>Broadcasting Rule in numpy<jupyter_code>arr = np.arange(5)
arr
arr * 4
# Deleting mean from row
arr = np.random.randn(4,3)
arr.mean(0)
demeaned = arr - arr.mean(0)
demeaned
demeaned.mean(0)
# deleting mean from column
arr
row_means = arr.mean(1)
row_means.shape
row_means.reshape(4,1)
demeaned = arr - row_means.reshape(4,1)
demeaned.mean(1)<jupyter_output><empty_output><jupyter_text>Sorting and Set Operations in numpy<jupyter_code>arr = np.random.randn(4)
arr
arr.sort()
arr
arr = np.random.randn(4,3)
arr
arr.sort(1) # row wise sort 
arr
arr.sort(0) # column wise sort
arr
large_arr = np.random.randn(1000)
large_arr.sort()
# 5% Quantile
large_arr[int(0.05 * len(large_arr))]
names = np.array(['Bob' ,'Joe' ,'Will','Bob','Will','Joe','Joe'])
np.unique(names) # first sort then return unique
ints = np.array([3,3,3,2,2,1,1,4,4])
np.unique(ints)
sorted(set(names))
values = np.array([6,0,3,2,5,6])
np.in1d(values,[2,3,6])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18309626199139928, 'fraction_numerical': 0.06252067482633146, 'mean_word_length': 6.383394383394384, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '38725', 'license': 'Apache-2.0', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2675, 'n_words': 507, 'path': 'Numpy  for Data Analysis.ipynb', 'repo_name': 'Shivamsharma009/Python-For-Data-Analysis', 'size': '6046'}