0% found this document useful (0 votes)
25 views10 pages

21BECE30036 Prac 1

This document discusses NumPy and Pandas for machine learning. It introduces NumPy arrays including 0D, 1D, 2D and 3D arrays. It covers NumPy operations like accessing, slicing, reshaping arrays. It also introduces Pandas DataFrames for reading CSV files and displaying/accessing DataFrame contents. Key concepts covered include NumPy data types, copying vs viewing arrays, sorting arrays, and creating DataFrames from dictionaries.

Uploaded by

Nimisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

21BECE30036 Prac 1

This document discusses NumPy and Pandas for machine learning. It introduces NumPy arrays including 0D, 1D, 2D and 3D arrays. It covers NumPy operations like accessing, slicing, reshaping arrays. It also introduces Pandas DataFrames for reading CSV files and displaying/accessing DataFrame contents. Key concepts covered include NumPy data types, copying vs viewing arrays, sorting arrays, and creating DataFrames from dictionaries.

Uploaded by

Nimisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Machine learning 21BECE30048

Practical 1
Numpy python
import numpy as np
arr= np.array([l, 2, 3, 4, 5]) print(arr)
print(type(arr))

[1 2 3 4 5]
<class 'numpy.ndarray'>

import numpy as np
arr= np.array((l, 2, 3, 4, 5))
print(arr)

[1 2 3 4 5]

O Dimension array
import numpy as np arr= np.array(42) print(arr)
42

1 Dimension array
import numpy as np

arr= np.array([l, 2, 3, 4, 5]) print(arr)


[1 2 3 4 5]

2 Dimension array
import numpy as np
arr= np.array([[l, 2, 3], [4, 5, 6]]) print(arr)
[ [1 2 3]
[4 5 6]]

3 Dimension array which have 2 Dimension array has element


import numpy as np
arr= np.array([[[l, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
Machine learning 21BECE30048

print(arr)

[ [ [1 2 3]
[4 5 6]]

[ [1 2 3]
[4 5 6]]]

ndim attribute give Dimension of array


import numpy as np

a np.array(42)
b np.array([l, 2, 3, 4, 5])
c np.array([[l, 2, 3], [4, 5, 6]])
d np.array([[[l, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)

0
1
2
3

ndmin use for define dimension of array


import numpy as np
arr= np.array([l, 2, 3, 4], ndmin=5) print(arr)
print('number of dimensions ·•' ' arr.ndim)

[[[[[1 2 3 4]]]]]

number of dimensions 5

Access Array Elements


import numpy as np
arr= np.array([l, 2, 3, 4])
Machine learning 21BECE30048

print(arr[0])
1

import numpy as np
arr= np.array([15, 21, 35, 44])
print(arr[l])
21

access array and add them


import numpy as np
arr= np.array([l, 21, 31, 4])
print(arr[2] + arr[3])
35

Access 2-D Arrays


import numpy as np
arr= np.array([[2,1,5,7,4], [9,8,1,2,4]])
print('2nd element on 1st row: ', arr[0, 1])
2nd element on 1st row: 1

Access 3-D Arrays


import numpy as np
arr= np.array([[[ll,5,6],[4, 5, 6]], [[4, 3, 2], [10, 11, 12]]])
print(arr[0, 1, 2])

Negative Indexing
import numpy as np
arr= np.array([[l,2,3,4,5), [6,7,8,9,10]])
print('Second Last element from 2nd dim: arr[l, -2])

Last element from 2nd dim: 9

NumPy Array Slicing


arr= np.array([l, 2, 3, 4, 5, 6, 7])
print(arr[l:5])
Machine learning 21BECE30048

[2 3 4 5]

arr = np.array([l, 2, 3, 4, 5, 6, 7])


print(arr[4:])
[5 6 7]

import numpy as np
arr= np.array([l, 2, 3, 4, 5, 6, 7])
print(arr[:4])
[1 2 3 4]

arr= np.array([l, 2, 3, 4, 5, 6, 7])


print(arr[-3:-1])
[5 6]

STEP slicing
import numpy as np
arr= np.array([l, 2, 3, 4, 5, 6, 7])
print(arr[l:5:2])
[2 4]
import numpy as np

arr= np.array([l, 2, 3, 4, 5, 6, 7))

print(arr[:: 2])
print(arr[:: -1])
print(arr[:: -2])

[1 3 5 7]
[7 6 5 4 3 2 1]
Machine learning 21BECE30048

[7 5 3 1]

Slicing 2-D Arrays


import numpy as np
arr= np.array([[l, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0, 1:4])

[2 3 4]
import numpy as np

arr= np.array([[l, 2, 3, 4, 5], [6, 7, 8, 9, 10]])


print(arr[0:2, 2])

[3 8]

Numpy Data type


import numpy as np

arr= np.array([l, 2, 3, 4])


print(arr.dtype)

int64
arr = np.array(['apple', 'banana', 'cherry'])
print(arr.dtype)
<U6
arr= np.array([l, 2, 3, 4], dtype='S') print(arr)
print(arr.dtype)
[b'l' b'2' b'3' b'4']
1s1
arr= np.array([l.1, 2.1, 3.1]) newarr = arr.astype('i')
print(newarr) print(newarr.dtype)
[1 2 3]
int32
Machine learning 21BECE30048

COPY and VIEW


arr= np.array([l, 2, 3, 4, 5]) x = arr.copy()
arr[0] = 42

print(arr)
print(x)
[42 2 3 4 5]
[1 2 3 4 5]

arr= np.array([l, 2, 3, 4, 5]) x = arr.view()


arr[0] = 42

print(arr) print(x)
[42 2 3 4 5]
[42 2 3 4 5]

shape and reshape


arr= np.array([[l, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
(2, 4)
arr= np.array([l, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
[ [ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

arrl=np.array([l, 2, 3])

arr2 = np.array( [4, 5, 6])

arr= np.concatenate((arrl, arr2))


print(arr)
Machine learning 21BECE30048

[1 2 3 4 5 6]

arr = np. array( [·banana·, ·cherry·, 'apple'])


print(np.sort(arr))

['apple' 'banana' 'cherry']

List

this list = ["apple", "banana", "cherry"]


print(thislist)

this list = ["apple", "banana", "cherry"]


print(thislist[l])

this list = ["apple", "banana", "cherry"]


thislist[l] = "blackcurrant"
print(thislist)

this list = ["apple", "banana", "cherry"]


thislist.append("orange")
print(thislist)

thislist = ["apple", "banana", "cherry"]


thislist.remove("banana")
print(thislist)

this list = ["apple", "banana", "cherry"]


for x in thislist:
print(x)

thislist = ["apple", "banana", "cherry"]


i=0
while i < len(thislist):
Machine learning 21BECE30048

print(thislist[i])
i=i+1
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist. sort()
print(thislist)
['apple', 'banana', 'cherry'] banana
['apple', 'blackcurrant', 'cherry']
['apple', 'banana', 'cherry', 'orange']
['apple', 'cherry']
apple
banana
cherry
apple
banana
cherry
['banana', 'kiwi', 'mango', 'orange', 'pineapple']

DataFrames
import pandas as pd df=pd.read_csv('Advertising.csv')

print(df)
Unnamed: 0 TV Radio Newspaper Sales
0 1 230.1 37.8 69.2 22.1
1 2 44.5 39.3 45.1 10.4
2 3 17.2 45.9 69.3 9.3
3 4 151.5 41.3 58.5 18.5
4 5 180.8 10.8 58.4 12.9
195 196 38.2 3.7 13.8 7.6
196 197 94.2 4.9 8.1 9.7
197 198 177.0 9.3 6.4 12.8
198 199 283.6 42.0 66.2 25.5
199 200 232.1 8.6 8.7 13.4

type(df)
pandas.core.frame.DataFrame
print(df.head())
Unnamed: 0 TV Radio Newspaper Sales
0 1 230.1 37.8 69.2 22.1
1 2 44.5 39.3 45.1 10.4
2 3 17.2 45.9 69.3 9.3
Machine learning 21BECE30048

3 4 151.5 41.3 58.5 18.5


4 5 180.8 10.8 58.4 12.9

print(df['Sales'])

0 22.1
1 10.4
2 9.3
3 18.5
4 12.9
195 7.6
196 9.7
197 12.8
198 25.5
199 13.4
Name: Sales, Length: 200, dtype: float64
print(pd._version_)

1. 5. 3

data= {
"calories": [20, 380, 390],
"duration": [50, 40, 45]

df pd.DataFrame(data, index ("dayl", "day2", "day3"])


print(df)

calories duration
dayl 420 50
day2 380 40
day3 390 45

data= {
"calories": [421, 30, 90],
Machine learning 21BECE30048

"duration": [55, 46, 455]

print(df)

dayl calories
420 duration
50
day2 380 40
day3 390 45
print(df.loc["day2"])
calories 380
duration 40
Name: day2, dtype: int64

You might also like