-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_intro.py
More file actions
executable file
·81 lines (63 loc) · 2.93 KB
/
numpy_intro.py
File metadata and controls
executable file
·81 lines (63 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#------------------------------------------------------------------------------------#
# Numpy
#------------------------------------------------------------------------------------#
#from array import *
# in array 2 D array doesn't support, so we need to user the numpy
#arr = array('i',[1,2,35,56], [2,3,5,6])
#------------------------------------------------------------------------------------#
# Use the Numpy for the multi Dimension Arrays
#------------------------------------------------------------------------------------#
from numpy import *
arr = array([1,2,35,56])
arr1 = array([1,2,35,56],int) # can use the type to mention in the end
print(arr)
#------------------------------------------------------------------------------------#
# Ways for creating Array using the Numpy
#------------------------------------------------------------------------------------#
'''
There is 6 ways for creating the arrays
1. array() ,2. linspace() 3. logspace() , 4. arange() , 5. zeros(), ones()
'''
#------------------------------------------------------------------------------------#
# 1. array()
#------------------------------------------------------------------------------------#
arr = array([1,2,3,4,5])
print(arr.dtype)
arr2 = array([1,2,3,4,5.0]) # all the values will be converted into the float implicitly bcz all th value should be same type
arr2 = array([1,3,4,5,6],float)
print(arr.dtype)
#------------------------------------------------------------------------------------#
# 2. linspace()
#------------------------------------------------------------------------------------#
arr = linspace(0,16,10)
# Here 15 is included in the range,and the 10 is the parts to divide the
# range into parts
print(arr)
arr1 = linspace(0,15,16)
print(arr1)
arr2 = linspace(0,15) # 15 parts by default
print()
print(arr2)
#--------------------------------------------------------------------------------------#
# 3. arange()
#--------------------------------------------------------------------------------------#
arr = arange(1,15,2) # start , stop , step
print(arr)
#--------------------------------------------------------------------------------------#
# 4. logspace()
#--------------------------------------------------------------------------------------#
arr = logspace(1,40,5)
# the number 5 is for 10 raise to 1 to 10 raise to 40 and will be divided into 5 parts
print(arr)
print('%.2f' %arr[1])
#--------------------------------------------------------------------------------------#
# 5. Zeros()
#--------------------------------------------------------------------------------------#
arr = zeros(5,int) # by default it give the float
print(arr)
#--------------------------------------------------------------------------------------#
# 6. Ones()
#--------------------------------------------------------------------------------------#
arr = ones(5,str)
print(arr)
#--------------------------------------------------------------------------------------#