<commit_before>---
layout: post
title: NumPy Tutorial
categories: [engineering]
tags: [python, data science]
description: NumPy is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.
---
## Multidimensional data structure - ndarray
	In [3]: import numpy as np

### Initiation
- vector column  

		In [4]: vector = [1, 2, 3]
		In [5]: arr_vec = np.array(vector)
		In [6]: arr_vec
		Out[6]: array([1, 2, 3])

- matrix

		In [10]: matrix = [[1, 2, 3], [4, 5, 6]]
		In [11]: arr_mat = np.array(matrix)
		In [12]: arr_mat
		Out[12]: 
		array([[1, 2, 3],
		       [4, 5, 6]])

- others

		In [13]: np.zeros(3)
		Out[13]: array([ 0.,  0.,  0.])
		In [14]: np.ones((2, 3))
		Out[14]: 
		array([[ 1.,  1.,  1.],
		       [ 1.,  1.,  1.]])
		In [15]: np.eye(2)
		Out[15]: 
		array([[ 1.,  0.],
		       [ 0.,  1.]])

### Scalar operations (+ - * / **)
Instead of the multiplication between matrix, * is the multiplication between 2 elements of the same index

	In [16]: arr_mat
	Out[16]: 
	array([[1, 2, 3],
	       [4, 5, 6]])
	In [17]: arr_mat * arr_mat
	Out[17]: 
	array([[ 1,  4,  9],
	       [16, 25, 36]])

### Index and slice
- vector

		In [19]: arr = np.arange(10)
		In [20]: arr
		Out[20]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
		In [21]: arr[3]
		Out[21]: 3
		In [22]: arr[0:3]
		Out[22]: array([0, 1, 2])
		In [23]: arr[0:3] = 10
		In [24]: arr
		Out[24]: array([10, 10, 10,  3,  4,  5,  6,  7,  8,  9])
  
  A slice is part of the array. If the value of a slice is changed, so will be the value in the original array

		In [28]: slice = arr[5:7]
		In [29]: slice[1] = 0
		In [30]: arr
		Out[30]: array([10, 10, 10,  3,  4,  5,  0,  7,  8,  9])
  
  But if the slice is only a single element, or followed by ```arr[5:7].copy()```, then it's a copy
  
		In [36]: slice = arr[0]
		In [37]: slice = 99
		In [38]: arr
		Out[38]: array([10, 10, 10,  3,  4,  5,  0,  7,  8,  9])<commit_msg>Update 2015-07-23-NumPy-Basic.md<commit_after>---
layout: post
title: NumPy Tutorial
categories: [engineering]
tags: [python, data science]
description: NumPy is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.
---
## Multidimensional data structure - ndarray
	In [3]: import numpy as np

### Initiation
- vector column  

		In [4]: vector = [1, 2, 3]
		In [5]: arr_vec = np.array(vector)
		In [6]: arr_vec
		Out[6]: array([1, 2, 3])

- matrix

		In [10]: matrix = [[1, 2, 3], [4, 5, 6]]
		In [11]: arr_mat = np.array(matrix)
		In [12]: arr_mat
		Out[12]: 
		array([[1, 2, 3],
		       [4, 5, 6]])

- others

		In [13]: np.zeros(3)
		Out[13]: array([ 0.,  0.,  0.])
		In [14]: np.ones((2, 3))
		Out[14]: 
		array([[ 1.,  1.,  1.],
		       [ 1.,  1.,  1.]])
		In [15]: np.eye(2)
		Out[15]: 
		array([[ 1.,  0.],
		       [ 0.,  1.]])

### Scalar operations (+ - * / **)
Instead of the multiplication between matrix, * is the multiplication between 2 elements of the same index

	In [16]: arr_mat
	Out[16]: 
	array([[1, 2, 3],
	       [4, 5, 6]])
	In [17]: arr_mat * arr_mat
	Out[17]: 
	array([[ 1,  4,  9],
	       [16, 25, 36]])

### Index and slice
- vector

		In [19]: arr = np.arange(10)
		In [20]: arr
		Out[20]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
		In [21]: arr[3]
		Out[21]: 3
		In [22]: arr[0:3]
		Out[22]: array([0, 1, 2])
		In [23]: arr[0:3] = 10
		In [24]: arr
		Out[24]: array([10, 10, 10,  3,  4,  5,  6,  7,  8,  9])
  
  A slice is part of the array. If the value of a slice is changed, so will be the value in the original array

		In [28]: slice = arr[5:7]
		In [29]: slice[1] = 0
		In [30]: arr
		Out[30]: array([10, 10, 10,  3,  4,  5,  0,  7,  8,  9])
  
  But if the slice is only a single element, or followed by ```arr[5:7].copy()```, then it's a copy
  
		In [36]: slice = arr[0]
		In [37]: slice = 99
		In [38]: arr
		Out[38]: array([10, 10, 10,  3,  4,  5,  0,  7,  8,  9])

- matrix

		In [44]: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
		In [45]: arr2d = np.array(matrix)
		In [46]: arr2d[2]
		Out[46]: array([7, 8, 9])
		In [47]: arr2d[0][2]
		Out[47]: 3
{'fraction_non_alphanumeric': 0.18552338530066814, 'fraction_numerical': 0.09376391982182627, 'mean_word_length': 2.7518796992481205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '645887', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1800, 'n_words': 671}