> For the complete documentation index, see [llms.txt](https://zh-1-peng.gitbook.io/data-prog-with-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zh-1-peng.gitbook.io/data-prog-with-python/week3.md).

# Week3

## Arrays

```python
y=np.array(x,float)
np.zeros()
np.ones()
np.full()
np.eye()
np.random.random()
np.arange(3,7,2)
array.sum/prod/shape/min/max/sorted/mean/unique/dot
array.flatten
np.concatenate((a,b),axis=0)
```

## Matrix

```python
np.matrix
matrix.diagonal
matrix.transpose
matrix_power(x,n)==>x**n
npl.svd
npl.cond #check if it is a singular matrix
npl.solve
lstsq
npl.inv
npl.det
npl.matrix_rank
```

## Datasets

pd.read\_csv

## Exercise

### 1 - Write a Python program to convert a list and tuple into arrays

**np.asarray**

```python
import numpy as np
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))
```

### 2 – Write a Python program to create a 3x3 matrix with values ranging from 2 to 10.

```python
import numpy as np
x = np.arange(2, 11).reshape(3,3)
print(x)
```

### 3- Write a Python program to reverse an array (first element becomes last).

**x = x\[::-1]**

```python
import numpy as np
import numpy as np
x = np.arange(12, 38)
print("Original array:")
print(x)
print("Reverse array:")
x = x[::-1]
print(x)
```

### 4- Write a Python program to convert the values of Centigrade degrees into Fahrenheit degrees.

Centigrade values are stored into a NumPy array. Sample Array \[0, 12, 45.21 ,34, 99.91]

```python
import numpy as np
fvalues = [0, 12, 45.21, 34, 99.91]
F = np.array(fvalues)
print("Values in Fahrenheit degrees:")
print(F)
print("Values in Centigrade degrees:")
print(5*F/9 - 5*32/9)
```

### 5 - Write a Python program to find the real and imaginary parts of an array of complex numbers

```python
import numpy as np
x = np.sqrt([1+0j, 0+1j])
print("Original array",x)
print("Real part of the array:")
print(x.real)
print("Imaginary part of the array:")
print(x.imag)
```

### 6 - Write a Python program to find the union of two arrays. Union will return the unique, sorted array of values that are in either of the two input arrays.

**np.union1d** Array1: \[ 0 10 20 40 60 80] Array2: \[10, 30, 40, 50, 70]

```python
import numpy as np
array1 = np.array([0, 10, 20, 40, 60, 80])
print("Array1: ",array1)
array2 = [10, 30, 40, 50, 70]
print("Array2: ",array2)
print("Unique sorted array of values that are in either of the two input arrays:")
print(np.union1d(array1, array2))
```

### 7-Write a Python program to create a 2-D array whose diagonal equals \[4, 5, 6, 8] and 0's elsewhere.

**np.diagflat**

```python
import numpy as np
x = np.diagflat([4, 5, 6, 8])
print(x)
```

### 8-Write a Python program to concatenate two 2-dimensional arrays. Sample arrays:

**np.concatenate((a, b), 1)**

```python
import numpy as np
a = np.array([[0, 1, 3], [5, 7, 9]])
b = np.array([[0, 2, 4], [6, 8, 10]])
c = np.concatenate((a, b), 1)
print(c)
```

### 9-Consider the vector \[1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value?

```python
import numpy as np
Z = np.array([1,2,3,4,5])
nz = 3
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
Z0[::nz+1] = Z
print(Z0)
```

### 10-Create a null vector of size 10 but the fifth value which is 1

```python
Z = np.zeros(10)
Z[4] = 1
print(Z)
```

### 11- Import the ‘Car’ data from following url

```python
cars = read_csv('https://raw.githubusercontent.com/''sassoftware/sas-viyaprogramming/master/data/cars.csv')
print(cars)
```
