Python_for_AIML1
Python_for_AIML1
1
• dict = {‘a’: ‘alpha’, ‘o’: ‘omega’, ‘g’: ‘gamma’}
• print(dict)
print(dict)
print(dict['a'])
[4]: print(dict.values())
2
[6, 7, 8]])
a and b information
2 2 3
(1, 3) (3, 3) (2, 2, 3)
3 9 (2, 2, 3)
[[1 2 3]]
[[0 1 2]
[3 4 5]
[6 7 8]]
[[[256 25 155]
[211 12 210]]
[[ 0 0 12]
[145 12 100]]]
3
#MA=np.random.randint(12,size=(3,4))
print(MA)
DD = MA[:2,:3]
print(rMA)
print(cMA)
[[1 2 3]]
[[1 2 3]]
[2]
[[1 3]]
[[3 2 1]]
[[1 2 3]
[1 1 1]]
[[1 2 3 1]]
4
Example: Loading the dataset above, numbers.csv file using NumPy.genfromtxt()
import numpy as np
path = "./dataset/numbers.csv"
data = np.genfromtxt(path, dtype=None, names=None, delimiter=",", encoding=None)
5
Example: Loading the dataset above, Iris.csv file using NumPy.genfromtxt()
import pandas as pd
path = "./dataset/iris_w_header.csv"
types = ['f8', 'f8', 'f8', 'f8', 'U50']
data = np.genfromtxt(path, dtype=None, names=True, delimiter=",", encoding=None)
6
print("sample of 3 rows of data : ", data[:3])
1.5 Exercise 1:
Load the data from numbers_ex.csv file using NumPy libraries and numpy.genfromtxt() function.
1. Display the shape of data, type of data.
Expected output: - shape of data : (4, 4) - datatype of data : float64
2. Display area (column 2) and price (column 4) data by slicing the data.
Expected output: - [ 70 60 50 120] - [ 910 1000 890 800]
3. Calculate the cost (cost = price / area) and instert/append cost in existing data
Expected output: - [[1.00000000e+00 7.00000000e+01 8.00000000e+00 9.10500000e+02
1.30071429e+01] - [2.00000000e+00 6.00000000e+01 1.30000000e+01 1.00025000e+03
1.66708333e+01] - [3.00000000e+00 5.00000000e+01 1.80000000e+01 8.90500000e+02
1.78100000e+01] - [4.00000000e+00 1.20000000e+02 2.30000000e+01 8.00000000e+02
6.66666667e+00]]
[2]: import numpy as np
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
print()
# 3) Display area (column 2) and price (column 4) data by slicing the data.
7
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
# cost = _________________________________________________
print()
8
1.6.2 Pandas DataFrame
DataFrame is an analog of a two-dimensional array with both flexible row indices and flexible
column names. Just as you might think of a two-dimensional array as an ordered sequence of
aligned one-dimensional columns, you can think of a DataFrame as a sequence of aligned Series
objects. Here, by “aligned” we mean that they share the same index.
import numpy as np
import pandas as pd
data=pd.Series([0.25,0.5,0.75,1.0])
print(data)
print(data.values) #access the values of a pandas series
print(data.index) #acess the index of a pandas series
print(data[2]) #access individual value
print(data[1:3]) #access subset of a series
0 0.25
1 0.50
2 0.75
3 1.00
dtype: float64
[0.25 0.5 0.75 1. ]
RangeIndex(start=0, stop=4, step=1)
0.75
1 0.50
2 0.75
dtype: float64
9
print(pd.DataFrame(my_2darray))
0 1 2
0 1 2 3
1 4 5 6
print(my_2darray1)
A B C
0 1 2 3
1 4 5 6
print(df['A'])
print(df.columns[1])
print(df)
10
print(df.at[2,'E'])
0 1
1 4
2 7
Name: A, dtype: int32
B
A B C D E
0 1 2 3 14 14
1 4 5 6 15 60
2 7 8 9 16 112
A B C D E
1 4 5 6 15 60
2 7 8 9 16 112
14
112
print(df)
A B D E
0 1 2 14 14
1 4 5 15 60
2 7 8 16 112
1.7 Exercise 2:
Create a following data table using panda data frame.
Expected Output:
11
[15]: #1) Declare n-d array of 3x3 with data
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
import pandas as pd
import matplotlib.pyplot as plt
path = "./dataset/iris_w_header.csv"
df = pd.read_csv(path)
print("shape of data : ", df.shape)
print("\n datatype of data : " , df.dtypes)
print("\n sample of 3 rows of data : ", df[:3])
print("\n sample of 3 rows of data using head(): ", df.head(3)) # or you can
,→using the head() function
12
petal_width class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
1.8 Exercise 3:
Load the data from Income3.csv file using Pandas’s read_csv function Pandas’s read_csv function
can read csv files which contains different datatypes. Statisics of each column can be displayed
using describe() function of the dataframe
1. Load and display top 3 rows of the data.
Expected output:
13
Observation Years of Higher Education (x) Income (y)
min 1.00000 0.000000 31007.000000
25% 5.75000 2.000000 53608.000000
50% 10.50000 4.000000 68876.500000
75% 15.25000 6.000000 79491.250000
max 20.00000 6.000000 89617.000000
3. Append/add a new column named called “Predicted” and load with the default value 0.
Expected output:
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
#_______________________________________________________________________________________________
# 3) Append/add a new column named called "Predicted" and load with the default
,→value 0.
#_______________________________________________________________________________________________
14
#_______________________________________________________________________________________________
15