<jupyter_start><jupyter_text>1.Data Types & Attributes  A) NumPy Array<jupyter_code># main data type is ndarray
a1 = np.array([1,2,3])
a1
type(a1)
a1.shape
a2 = np.array([[1, 2.1, 3.2],
              [4.1, 5.2, 6.3]])
a2
a2.shape
a3 = np.array([
    [[1,2,3],[4,5,6],[1.1,1.2,1.3]],
    [[7,8,9],[10,11,12],[2.2,2.3,2.4]]
])
a3
a3.shape
a1.shape
a2.shape
# checking number of dimensions
a1.ndim, a2.ndim, a3.ndim
# checking data type
a1.dtype, a2.dtype, a3.dtype
# checking size
a1.size, a2.size, a3.size
# checking type
type(a1), type(a2), type(a3)<jupyter_output><empty_output><jupyter_text>creating data frame from ndarray<jupyter_code>import pandas as pd
df = pd.DataFrame(a2)
df
arr = np.array([[1,2,3],[4,5,6]], 
                dtype=np.float32)
print(arr)
print(repr(arr))<jupyter_output>[[1. 2. 3.]
 [4. 5. 6.]]
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)<jupyter_text>np.array upcasting<jupyter_code># np.array upcasting
arr = np.array([1,2.0,3])
print(repr(arr))
print(arr.dtype)<jupyter_output>array([1., 2., 3.])
float64<jupyter_text>B) Copying<jupyter_code>a = np.array([0, 1])
b = np.array([9, 8])
c = a
print('Array a: {}'.format(repr(a)))
c[0] = 5
print('Array a: {}'.format(repr(a)))

d = b.copy()
d[0] = 6
print('Array b: {}'.format(repr(b)))<jupyter_output>Array a: array([0, 1])
Array a: array([5, 1])
Array b: array([9, 8])<jupyter_text>C) Casting<jupyter_code>arr = np.array([0, 1, 2])
print(arr.dtype)
arr = arr.astype(np.float32)
print(arr.dtype)<jupyter_output>int32
float32<jupyter_text>D) NaN<jupyter_code>#NaN as place holder
#NaN cannot take integer

arr = np.array([np.nan, 1, 2])
print(repr(arr))

arr = np.array([np.nan, 'abc'])
print(repr(arr))

# Will result in a ValueError
np.array([np.nan, 1, 2], dtype=np.int32)<jupyter_output>array([nan,  1.,  2.])
array(['nan', 'abc'], dtype='<U32')<jupyter_text>E) Infinity<jupyter_code># infinity can be postive or negative
# infinity cannot take integer type

print(np.inf > 1000000)

arr = np.array([np.inf, 5])
print(repr(arr))

arr = np.array([-np.inf, 1])
print(repr(arr))

# Will result in an OverflowError
np.array([np.inf, 3], dtype=np.int32)<jupyter_output>True
array([inf,  5.])
array([-inf,   1.])<jupyter_text>Exercise<jupyter_code># Set arr equal to np.array applied to the specified list.

arr = np.array([np.nan, 2,3,4,5])
type(arr)
print(repr(arr))
#copy and change element of first array

arr2 = arr.copy()
arr2[0] = 10
print(arr2)
print(arr)
# type cast the array element
float_arr = np.array([1, 5.4, 3])
float_arr2 = arr2.astype(np.float32)

print(repr(float_arr))
print(repr(float_arr2))
# multi dimensional array
matrix = np.array([[1,2,3],[4,5,6]], dtype=np.float32)

print(repr(matrix))
print(matrix)<jupyter_output>array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)
[[1. 2. 3.]
 [4. 5. 6.]]<jupyter_text>2. Creating NumPy Arrays- np.array()- np.ones()- np.zeros()- np.random.rand(5, 3)- np.random.randint(10, size=5)- np.random.seed() - pseudo random numbers- Searching the documentation example (finding np.unique() and using it)<jupyter_code>sample_array = np.array([1,2,3])
sample_array
ones = np.ones((2,3), dtype=float)
ones
# usually ones and zeros are used for placeholder
zeros = np.zeros((3,2,3))
zeros
range_array = np.arange(0, 20, 2)
range_array
# random 
random_array = np.random.randint(0, 20, size=(2,3))
random_array
random_array.size
random_array.shape
random_array2 = np.random.random(size=(2,3,5))
random_array2
random_array2.shape
random_array2.size
random_array3 = np.random.rand(2,3)
random_array3
#random seed
np.random.seed(0)

#Pseudo - random numbers
random_array4 = np.random.randint(10, size=(4,3))
random_array4
np.random.seed(7)
random_array5 = np.random.random(size=(4,3))
random_array5<jupyter_output><empty_output><jupyter_text>3. Viewing arrays and matrics<jupyter_code>#finding unique numbers in array
np.unique(random_array4)
a1
a2
a3
a1[0]
a2[0]
a2[0][1]
a3.shape
a3[0]
a3
a3[1][1][1]
a3.shape
a3[:2, :2, :2]
a4 = np.random.randint(10, size=(2,3,4,5))
a4.shape, a4.ndim
a4
# get the first 5 numbers of inner most arrays
a4[1:, 2: , :1, :]
# if I want 1 and 4
a4[:, 1:2, 1:2, 2:3]<jupyter_output><empty_output><jupyter_text>4. Manipulation and Comparing arrays  Arithmetics<jupyter_code>a1
ones = np.ones(3)
ones
a1 + ones
a1 - ones
a1 * ones
a1
a2
a1 * a2
a3
a2 * a3
# how can we make a2 and a3 compitbale to make calculations ?
a3_new = a3.reshape((6,3))
a3_new
a2
a1 / ones
a2 / a1
# Floor division or integer division
a2 // a1
a2
a2 ** 2
np.square(a2)
np.add(a1, ones)
a1
a1 % 2
np.mod(a1, 2)
np.exp(a1)
np.log(a1)<jupyter_output><empty_output><jupyter_text>Aggregration<jupyter_code>a1
a1.sum()
np.sum(a1)
sum(a1)<jupyter_output><empty_output><jupyter_text>Use Python's method `sum()` on python data types.Use NumPy's method `np.sum()` on numpy arrays<jupyter_code># create massive array
massive_array = np.random.random(10000)
massive_array.size
massive_array[:10]
%timeit sum(massive_array)
%timeit massive_array.sum()
a2
a2.mean()
np.mean(a2)
np.max(a2)
a2.min()
a2.std()
a2.var()
np.sqrt(a2.var())
# std dev , variance
high_var_array = np.array([100, 200, 500, 1000, 5000])
low_var_array = np.array([2,4,6,8,10])

high_var_array.var(), low_var_array.var()
high_var_array.std(), low_var_array.std()
%matplotlib inline
import matplotlib.pyplot as plt

plt.hist(high_var_array)
plt.show()
plt.hist(low_var_array)
plt.show()<jupyter_output><empty_output><jupyter_text>Reshape and Transpose<jupyter_code>a2
a2.shape
a3
a3.shape
a2 * a3
a2
a2.shape
#reshape a2
a2_reshape = a2.reshape(2,3,1)
a2_reshape
a2_reshape.shape
a2_reshape * a3
a2
# Transpose switches the axies
a2.T
a2.shape
a3.shape
a3
a3.T
a3.shape<jupyter_output><empty_output><jupyter_text>Dot Product & Element Wise  Dot Product<jupyter_code>np.random.seed(0)

mat1 = np.random.randint(10, size=(5,3))
mat2 = np.random.randint(10, size=(5,3))
mat1
mat2
mat1.shape, mat2.shape
#Element wise multiplication
mat1 * mat2
# Dot Product
np.dot(mat1, mat2)
#using reshape
mat2_reshape = mat2.reshape((3,5))
mat2_reshape
mat1.dot(mat2_reshape)
# using transpose
mat1_T = mat1.T
mat1_T.shape
mat1_T.dot(mat2)<jupyter_output><empty_output><jupyter_text>Dot Product (Nut Butter Sales)<jupyter_code>import pandas as pd

np.random.seed(0)

# number of jars sold
sales_amt = np.random.randint(20, size=(5,3))
sales_amt
# create weekly sales dataframe
weekly_sales_df= pd.DataFrame(sales_amt, 
                                              index=["Mon", "Tue", "Wed", "Thu", "Fri"], 
                                              columns = ["Almond Butter", "Peanut Butter", "Cashew Butter"])
weekly_sales_df
weekly_sales_df.shape
# butter prices
prices = np.array([[10], [8], [12]])
prices.shape
prices_df = pd.DataFrame(prices, index= ["Almond Butter", "Peanut Butter", "Cashew Butter"], columns = ["price"])
prices_df
prices_df.shape
#calculating total weekly sales
daily_sales_df = weekly_sales_df.dot(prices_df)
daily_sales_df
#combine weekly total sales to weekly sales df
weekly_sales_df["Total Sales $"] = daily_sales_df
weekly_sales_df<jupyter_output><empty_output><jupyter_text>Comparison Operators<jupyter_code>a1
a2
a1 < a2
 a1 == a2
a1 == a1
a1 > a2
a1 >= a2<jupyter_output><empty_output><jupyter_text>Sorting Arrays<jupyter_code>random_array = np.random.randint(10, size=(3,5))
random_array
np.sort(random_array, axis=0)
np.sort(random_array, axis=1)<jupyter_output><empty_output><jupyter_text>Sorting the indexes<jupyter_code>np.argsort(random_array, axis=0)
a1
np.argsort(a1, axis=0)
# index of min value
np.argmin(a1)
# index of max value
np.argmax(a1)
random_array
np.argmax(random_array)
random_array[0,4]
np.argmax(random_array, axis=1)
#comparing along the columns
#9 is at the 4th index for first row, another 9 is at the 1st index for second row, 5 is at 1st index  of third row
np.argmax(random_array, axis=0)
#comparing along the rows
#8 is at the 1st index of first column, 9 is at the 1st index of second column, 4 is at 1st index of third column
# 5 is at 0th index of fourth column, 9 is at 0th index of fifth column<jupyter_output><empty_output><jupyter_text>Pratical Example: NumPy in Action !<jupyter_code>#Turn image into numPy Array
from matplotlib.image import imread

panda = imread("images/panda.png")
panda
type(panda)
panda.size, panda.shape, panda.ndim<jupyter_output><empty_output><jupyter_text><jupyter_code>car = imread("images/car-photo.png")
type(car)<jupyter_output><empty_output><jupyter_text><jupyter_code>dog = imread("images/dog-photo.png")
type(dog)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1563048823322796, 'fraction_numerical': 0.0551457674745346, 'mean_word_length': 5.4032983508245875, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '514369', 'license': 'MIT', 'n_tokens_mistral': 3801, 'n_tokens_neox': 3611, 'n_words': 793, 'path': 'Complete Machine Learning and Data Science - Zero to Mastery - AN/07.NumPy/numpy data analysis.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '8541'}