Skip to content

Commit 172108c

Browse files
Add files via upload
1 parent 4191363 commit 172108c

File tree

3 files changed

+816
-0
lines changed

3 files changed

+816
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Load the libraries
2+
import numpy as np
3+
4+
5+
# Creating arrays
6+
# Method 1 : From a list/tuple
7+
arr_list = np.array([[[1,2,3,4],
8+
[5,6,7,8],
9+
[9,10,11,12]],
10+
[[13,14,15,16],
11+
[17,18,19,20],
12+
[21,22,23,24]]],dtype="int32")
13+
14+
print(f'Numpy array from a list :\n{arr_list}')
15+
16+
17+
# Method 2 : From Built-in routines
18+
19+
print(f"\n Using np.ones to create an array : \
20+
\n{np.ones((3,2),dtype=float)}")
21+
22+
print(f"\n Using np.zeros to create an array : \
23+
\n{np.zeros((4,3),dtype=int)}")
24+
25+
print(f"\n Using np.full to create an array : \
26+
\n{np.full((3,4,5),0.34)}")
27+
28+
# Uses numerical range built-in functions
29+
30+
print(f'\n Using np.arange with step of 5 to create ndarray : \
31+
\n{np.arange(25,40,5)}')
32+
33+
print(f'\n Using np.linspace with equally spaced elements to create ndarray : \
34+
\n{np.linspace(25,40,4)}')
35+
36+
# Uses np.random
37+
print(f'\n Using np.random.rand to create a uniform random array : \
38+
\n{np.random.rand(2,4)}')
39+
40+
print(f'\n Using np.random.randn to create a normal random array : \
41+
\n{np.random.randn(2,4)}')
42+
43+
print(f'\n Using np.random.normal to create a \
44+
random array of float in given interval : \
45+
\n{np.random.normal(-15,15,(2,3,4))}')
46+
47+
print(f'\n Using np.random.randint to create a \
48+
random array of integers in given interval : \
49+
\n{np.random.randint(25,75,(2,3,4))}')
50+
51+
# Uses np.eye
52+
print(f'\n Using np.eye to create a matrix with kth diagonal \
53+
elements set to 1 and others as 0: \
54+
\n{np.eye(4,k=1)}')
55+
56+
57+
58+
59+
# Loading files(txt,csv) to form numpy arrays
60+
61+
# 1. Using np.fromfile
62+
np.random.rand(2,10000).tofile("/Users/pavitragajanana/development/5. InternalFiles/randomtext")
63+
# Reading the file using fromfile
64+
randtext = np.fromfile("/Users/pavitragajanana/development/5. InternalFiles/randomtext")
65+
66+
67+
# 2. Using np.genfromtxt
68+
datafile_genfromtxt = np.genfromtxt("/Users/pavitragajanana/development/2. Data Files/CrudeOil_Daily_Cushing_OK_WTI_Spot_Price_FOB.csv",
69+
usecols=[1],
70+
delimiter=",",
71+
skip_header=1)
72+
73+
74+
# 3. Loading csv files into numpy array using np.loadtxt
75+
datafile_arr = np.loadtxt("/Users/pavitragajanana/development/2. Data Files/CrudeOil_Daily_Cushing_OK_WTI_Spot_Price_FOB.csv",
76+
delimiter=",",
77+
skiprows=1, # Header
78+
usecols=[1])
79+
80+
81+
# 4. Using csv library
82+
import csv
83+
from datetime import datetime
84+
85+
with open("/Users/pavitragajanana/development/2. Data Files/CrudeOil_Daily_Cushing_OK_WTI_Spot_Price_FOB.csv", 'r') as f:
86+
datafile = list(csv.reader(f, delimiter=","))
87+
88+
# Converts the list to an ndarray
89+
datafile = np.array(datafile)
90+
# Converts into default datetime format
91+
datearray = np.array([datetime.strptime(x, "%m/%d/%y").strftime('%Y-%m-%d') for x in datafile[1:,0]])
92+
nominal = datafile[1:,1]
93+
94+
# Recreates original array
95+
datafile = np.vstack([datearray,nominal]).T
96+
97+
98+
99+
100+
# Array Attributes (size,shape,ndim,nbytes,itemsize,dtype)
101+
102+
# We create a 1D, 2D and a 3D array to explore the attributes
103+
# One-Dimensional Array
104+
one_dim_array = np.random.randint(12, size=7)
105+
106+
# Two-Dimensional Array
107+
two_dim_array = np.array([["Cupcake","Donut"],
108+
["Eclair","Froyo"],
109+
["Gingerbread","Honeycomb"],
110+
["Ice Cream Sandwich","Jelly Bean"],
111+
["KitKat","Lollipop"],
112+
["Marshmallow","Nougat"],
113+
["Oreo","Pie"]])
114+
115+
# Three-Dimensional Array
116+
three_dim_array = np.array([[["Civic","Accord","Pilot","FR-V"],
117+
["Odyssey","Jazz","CR-V","NSX"]],
118+
[["Insight","Ridgeline","Legend","HR-V"],
119+
["Passport","S660","Clarity","Mobilio"]],
120+
[["Airwave","Avancier","Beat","Shuttle"],
121+
["Concerto","Element","Logo","Stream"]]])
122+
123+
# Array Attributes for 1D array
124+
print(f'Dimensions of 1D array : {one_dim_array.ndim}')
125+
print(f'Size of 1D array : {one_dim_array.size}')
126+
print(f'Shape of 1D array : {one_dim_array.shape}')
127+
print(f'Total Bytes consumed by 1D array : {one_dim_array.nbytes}')
128+
print(f'dtype of 1D array : {one_dim_array.dtype}')
129+
print(f'Itemsize of 1D array : {one_dim_array.itemsize}')
130+
131+
# Array Attributes for 2D array
132+
print(f'Dimensions of 2D array : {two_dim_array.ndim}')
133+
print(f'Size of 2D array : {two_dim_array.size}')
134+
print(f'Shape of 2D array : {two_dim_array.shape}')
135+
print(f'Total Bytes consumed by 2D array : {two_dim_array.nbytes}')
136+
print(f'dtype of 2D array : {two_dim_array.dtype}')
137+
print(f'Itemsize of 2D array : {two_dim_array.itemsize}')
138+
139+
# Array Attributes for 3D array
140+
print(f'Dimensions of 3D array : {three_dim_array.ndim}')
141+
print(f'Size of 3D array : {three_dim_array.size}')
142+
print(f'Shape of 3D array : {three_dim_array.shape}')
143+
print(f'Total Bytes consumed by 3D array : {three_dim_array.nbytes}')
144+
print(f'dtype of 3D array : {three_dim_array.dtype}')
145+
print(f'Itemsize of 3D array : {three_dim_array.itemsize}')

0 commit comments

Comments
 (0)