|
| 1 | +## Numpy |
| 2 | +- Python library used heavily in ML and DS to perform mathematical and logical operations on arrays |
| 3 | + |
| 4 | + |
| 5 | +### How To Install |
| 6 | +```bash |
| 7 | +sudo apt-get install python-numpy |
| 8 | +``` |
| 9 | + |
| 10 | +### Data Types |
| 11 | +- Boolean -> bool |
| 12 | +- Integer(default) -> int_...same as C's long data type |
| 13 | +- Integer(C family) -> intc ... same as C's int |
| 14 | +- intp->: Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
| 15 | +- int8-> integer |
| 16 | +- int16 -> integer with a bigger range than int8 |
| 17 | +- int32 -> integer with a bigger range than int16 |
| 18 | +- int64 -> integer with a bigger range than int32 |
| 19 | +- uint8 -> unsigned int |
| 20 | +- uint16 -> unsigned int |
| 21 | +- uint32 -> unsigned int |
| 22 | +- uint64 -> unsigned int |
| 23 | +- float_ -> floating data type |
| 24 | +- float16 -> floating data type has a bigger range and is more precise than a float |
| 25 | +- float32 -> floating data type has a bigger range and is more precise than a float16 |
| 26 | +- float64 -> floating data type has a bigger range and is more precise than a float32 |
| 27 | +- complex_ -> complex data type |
| 28 | +- complex64 -> complex data type with more precision than an ordinary complex |
| 29 | +- complex128 -> complex data type with more prevision than a complex64 |
| 30 | + |
| 31 | +### Character code for data type in Numpy |
| 32 | + |
| 33 | +1. 'b': boolean |
| 34 | +2. 'i': signed integer |
| 35 | +3. 'u': unsigned integer |
| 36 | +4. 'f': floating point |
| 37 | +5. 'c': complex floating point |
| 38 | +6. 'm': time delta |
| 39 | +7. 'M': datetime |
| 40 | +8. 'O': Python Objects |
| 41 | +9. 'S', 'a': String |
| 42 | +10. 'U': Unicode |
| 43 | + |
| 44 | +11. 'V': raw data |
| 45 | + |
| 46 | + |
| 47 | +### How To resize an ndimensional array |
| 48 | +```python |
| 49 | +import numpy as np |
| 50 | + |
| 51 | +a = np.array([[2,4,9],[3,5,7]]) #this is a 2x3 array |
| 52 | +a.shape = (3,2) # it will now become a 3x2 aka transpose the array |
| 53 | + |
| 54 | +print a |
| 55 | +#way number 2 |
| 56 | +m = np.arange(24) #an array of 24 elements aka 0 through 23 |
| 57 | +a.ndim |
| 58 | + |
| 59 | +# now reshape its 2 separate arrays, 4 rows, 3 columns |
| 60 | +b = a.reshape(2,4,3) |
| 61 | +print b |
| 62 | +``` |
| 63 | + |
| 64 | +### Array Indexing |
| 65 | +```python |
| 66 | +import numpy as np |
| 67 | +a = np.arange(10) # fill array with 10 elements |
| 68 | +s = slice(2,7,2) # start at 2 stop 7 step 2 aka 2,4,6 |
| 69 | +print a[s] |
| 70 | +``` |
| 71 | + |
| 72 | +### How To Iterate Through An Array Using a range-style built-in function of numpy |
| 73 | +```python |
| 74 | +import numpy as np |
| 75 | +ftn = np.arange(0,60,5) # start at 0 stop at 60 step 5 numbers |
| 76 | +ftn = ftn.reshape(3,4) # turn the array into 2d |
| 77 | + |
| 78 | +print 'Original array is:' |
| 79 | +print ftn |
| 80 | +print '\n' |
| 81 | + |
| 82 | +# Transpose of the array |
| 83 | +print 'Transpose of Array:' |
| 84 | +b = ftn.T |
| 85 | +print b |
| 86 | +print '\n' |
| 87 | + |
| 88 | +print 'Modified array is:' |
| 89 | +for x in np.nditer(b): |
| 90 | + print x |
| 91 | +``` |
0 commit comments