<jupyter_start><jupyter_text>______ NumPy Lab, Session  A sample solve in Linear Algebra using NumPy<jupyter_code>import numpy as np
# 3*x + y = 9
# x + 2*y = 8
a = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
a
b
np.linalg.solve(a, b)<jupyter_output><empty_output><jupyter_text>Using array-generating functions For larger arrays it is inpractical to initialize the data manually, using explicit python lists. Instead we can use one of the many functions in `numpy` that generate arrays of different forms. Some of the more common are:  arange<jupyter_code># create a range
# arguments: start, stop, step
x = np.arange(0, 10, 1)

x


y = np.arange(-1, 1, 0.2)
y<jupyter_output><empty_output><jupyter_text>linspace and logspace<jupyter_code># using linspace, both end points ARE included
np.linspace(0, 10, 25)
np.logspace(0, 10, 10, base=np.e)<jupyter_output><empty_output><jupyter_text>random data<jupyter_code># uniform random numbers in [0,1]
# seed() must be underlined to the student. what is it used for?
np.random.seed(0)
np.random.rand(5,5)
import numpy as np
np.random.seed(0)
np.random.rand(3,3)
np.random.rand(3,3)
np.random.seed(0)
np.random.rand(3,3)
np.random.rand(3,3)
np.random.rand(5,5)
# standard normal distributed random numbers
np.random.randn(3,3)<jupyter_output><empty_output><jupyter_text>diag<jupyter_code># a diagonal matrix
np.diag([1,2,3,4])
# diagonal with offset from the main diagonal
np.diag([1,2,3,4], k=-1)
np.diag([1,2,3,4], k=2)<jupyter_output><empty_output><jupyter_text>Manipulating arrays  Fancy indexing We can also use index masks: If the index mask is an Numpy array of data type `bool`, then an element is selected (True) or not (False) depending on the value of the index mask at the position of each element:<jupyter_code>B = np.array([i for i in range(5)])
B
B = np.array(range(5))
B
row_mask = np.array([True, False, True, False, False])
B[row_mask]
row_mask = np.array([1, 0, 1, 0, 0], dtype = bool)
B[row_mask]<jupyter_output><empty_output><jupyter_text>This feature is very useful to conditionally select elements from an array, using for example comparison operators:<jupyter_code>x = np.arange(0, 10, 0.5)
x
mask = (5 < x) & (x < 7.5)
mask
x[mask]<jupyter_output><empty_output><jupyter_text>Functions for extracting data from arrays and creating arrays  ``where`` The index mask can be converted to position index using the `where` function<jupyter_code>mask
indices = np.where(mask)
indices
x[indices]
x[mask]<jupyter_output><empty_output><jupyter_text>``diag`` With the diag function we can also extract the diagonal and subdiagonals of an array:<jupyter_code>A = np.array([[1, 2, 3, 4], [5, 6, 7, 8,], [1, 2, 3, 4], [5, 6, 7, 8,]])
A
np.diag(A)
np.diag([1, 6, 3, 8])
A
np.diag(A, k=1)
np.diag(A, k=3)<jupyter_output><empty_output><jupyter_text>``take`` The `take` function is similar to fancy indexing described above:<jupyter_code>v2 = np.arange(-3, 3)
v2
# fancy indexing
row_index= [1, 3, 5]
v2[row_index]
v2.take(row_index)<jupyter_output><empty_output><jupyter_text>But `take` also works on lists and other objects:  ``choose`` Constructs an array by picking elements from several arrays:<jupyter_code>which = [1, 0, 1, 2]
choices = [[-2, -3, -4, -5], [6, 7, 8, 9], [1,2,3,4]]
np.choose(which, choices)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1393019726858877, 'fraction_numerical': 0.03732928679817906, 'mean_word_length': 5.909853249475891, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 45, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '88363', 'license': 'MIT', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1256, 'n_words': 379, 'path': 'Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/CLARUSWAY/PYTHON/DATA_ANALYSIS_WITH_PYTHON/NUMPY/NumPy_LabSession_1_Student.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '3295'}