[go: up one dir, main page]

0% found this document useful (0 votes)
21 views1 page

NumPy Guide

The document is a beginner's guide to NumPy, covering essential operations such as array creation, manipulation, and mathematical functions. It includes examples of using Python lists versus NumPy arrays, array indexing, reshaping, and file handling. Additionally, it discusses various array operations, including scalar multiplication, addition, and matrix multiplication.

Uploaded by

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

NumPy Guide

The document is a beginner's guide to NumPy, covering essential operations such as array creation, manipulation, and mathematical functions. It includes examples of using Python lists versus NumPy arrays, array indexing, reshaping, and file handling. Additionally, it discusses various array operations, including scalar multiplication, addition, and matrix multiplication.

Uploaded by

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

Beginner's Guide to NumPy

Numpy
# Python Lists
list1 = list(range(1000000))
list2 = list(range(1000000))

%time result = [x * y for x, y in zip(list1, list2)]

import numpy as np
arr1 = np.arange(1000000)
arr2 = np.arange(1000000)

%time result = arr1 * arr2

Syntax
# !pip install numpy

import numpy as np

pyl = [1,2,3,4,5,6]
print(type(pyl))
pyl = np.array([pyl,pyl])
print(type(pyl))

pyl

pyl.ndim

pyl.size

pyl.dtype = np.int8
pyl.dtype

pyl.dtype = np.int64

pyl

s = pyl.shape
type(s)

s[0] = 4

x = np.zeros(20)
x = x.reshape(2,10)
x = x.reshape(4,5)
print(x)
x = x.T
print(x)
x = x.flatten()

print(x[5:-1])

y = np.array([[1,2,3,4,5],
[6,7,8,9,0],
[1,2,3,4,5]])

z = np.array([[1,2,3,4],
[6,7,8,9],
[1,2,3,4]])

print(f"y = {y}, shape: {y.shape}")


print(f"z = {z}, shape: {z.shape}")

z = np.hstack((z,np.ones((3,1))))
z

y*z

x = np.eye(5)
x

q = np.ones((2,5)) * 1
# q = q*10
q

w = np.full((2,5), 10)
w

x = np.random.rand(20)
print(x)
np.round(x, 2)

import numpy as np

arr = np.round(np.random.rand(2,5) * 10, 2)


arr

arr = np.random.randint(0, 100, (5,5))


arr

np.arange(1, 22, 3)

np.linspace(1,22, 10)

arr.dtype

arr.size * 64

arr = arr.astype(np.int8)
arr

arr.dtype

25*16

arr = arr.astype(np.int16)
arr[3][4] = 67
arr

2**16 / 2

Array operations
# Scaler Multiplication
arr = arr * 2
# arr = arr.astype(np.int64)
arr

# Scaler Addition
arr = arr + 2
arr

# corresponding Entities Addition


arr1 = arr
print(arr)
print()
print(arr1)

arr1+arr

# corresponding Entities Multiplication


arr1*arr

arr

# Matrix Multiplication
arr = np.arange(10).reshape(2,5)
arr1 = np.arange(10,20).reshape(2,5)

print(arr)
print()
print(arr1)
print()
print(arr.T @ arr1)
print(np.dot(arr.T, arr1))

# Indexing
# print(arr[5:])
# arr = np.vstack((arr, arr))
print("[x1,x2,x3,x4,x5]")
print(arr)
X_train = arr[:14,:4]
X_test = arr[14:,:4]
Y_train = arr[:14,4:]
Y_test = arr[14:,4:]

print(X_train)
print(X_train.shape)

print(X_test)
X_test.shape

print(Y_train)
print(Y_train.shape)

print(Y_test)
print(Y_test.shape)

# Boolean Indexing
print(arr)
arr[arr%2 == 1]

# Filtering
print(arr)
np.where(arr%2 == 0, 1, 0)

# Aggregations
# arr = arr * 9
# print(arr)
# print(arr.sum())
# print(arr.min())
# print(arr.max())
# print(arr.mean())
# print(arr.std())
# print(arr.argmin())
# print(arr.argmax())
# arr = np.random.randint(0, 100, 10)
print(arr)
# print()
arr[arr.argsort()]

# File Handling in numpy


print(arr.dtype)
np.savetxt('numpy.txt', arr)
print("File saved Successfully")

arr1 = np.loadtxt('numpy.txt')
print("File Loaded Successfully")
print(f"File Content: {arr1}")
print(arr1.dtype)

m = np.nanmean(arr1)
m

# Homework
np.std()
np.var()

You might also like