Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numpy: An Introduction to Array Operations and Manipulation, Study Guides, Projects, Research of Advanced Computer Programming

An introduction to NumPy, a powerful library for numerical computations in Python. It covers the basics of creating arrays, indexing and slicing, vector operations, and matrix operations using NumPy. The document also includes examples of creating arrays of different shapes and sizes, as well as various ways of indexing and slicing arrays. Additionally, it covers scalar operations, dot product, and functions applied to elements of arrays.

What you will learn

  • What are the different ways of indexing and slicing arrays in NumPy?
  • What is NumPy and what are its features?
  • How to create arrays in NumPy?

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 09/27/2022

johnatan
johnatan 🇺🇸

4

(29)

280 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Numpy
May 29, 2019
Numpy Basics
In [1]: import numpy as np
#Create a rank 1 array
a=np.array([1,2,3])
print(type(a))
# check the object created
print(a.dtype)
# data type of the elements of the array
print(a.ndim)
# check the dimention of the array
print(a.shape)
# check the (row,col) of the array, shape returns a tuple
print(a[0], a[1], a[2])
a[0]= 5
# Change an element of the array
print(a)
# Create a rank 2 array
b=np.array([[1,2,3],[4,5,6]])
print(b.shape)
print(b[0,0], b[0,1], b[1,0])
<class
'
numpy.ndarray
'
>
int64
1
(3,)
1 2 3
[5 2 3]
(2, 3)
1 2 4
In [2]:
#different ways of creating arrays
import numpy as np
# Create an array of all zeros
a=np.zeros((2,2))
print(a)
# Create an array of all ones
b=np.ones((1,2))
1
pf3
pf4
pf5

Partial preview of the text

Download Numpy: An Introduction to Array Operations and Manipulation and more Study Guides, Projects, Research Advanced Computer Programming in PDF only on Docsity!

Numpy

May 29, 2019

Numpy Basics

In [1]: import numpy as np

#Create a rank 1 array a = np.array([1, 2, 3]) print(type(a))# check the object created print(a.dtype)# data type of the elements of the array print(a.ndim)# check the dimention of the array print(a.shape)# check the (row,col) of the array, shape returns a tuple print(a[0], a[1], a[2]) a[0] = 5 # Change an element of the array print(a)

Create a rank 2 array

b = np.array([[1,2,3],[4,5,6]]) print(b.shape) print(b[0, 0], b[0, 1], b[1, 0])

<class 'numpy.ndarray'> int 1 (3,) 1 2 3 [5 2 3] (2, 3) 1 2 4

In [2]: #different ways of creating arrays import numpy as np

Create an array of all zeros

a = np.zeros((2,2)) print(a)

Create an array of all ones

b = np.ones((1,2))

print(b)

Create a constant array

c = np.full((2,2), 7) print(c)

Create a 2x2 identity matrix

d = np.eye(2) print(d)

Create an array filled with random values

e = np.random.random((2,2)) print(e)

#using arange and reshape methods f = np.arange(12).reshape(4,3) print(f)

[[0. 0.] [0. 0.]] [[1. 1.]] [[7 7] [7 7]] [[1. 0.] [0. 1.]] [[0.85243703 0.07421425] [0.35034596 0.0979054 ]] [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]

In [3]: #Indexing & Slicing import numpy as np a = np.arange(10) print(a) print(a[0])#prints the element at the given index

s = slice(0,7,2) # a part of array with starting index and ending index #with steps given is sliced print (a[s]) b = a[2:9:2]# another way of slicing print(b)

[0 1 2 3 4 5 6 7 8 9] 0 [0 2 4 6]

print(b*2)

#functions applied to elements of arrays print(a.sum()) print(a.min()) print(np.log(a)) print('\n') print(np.sqrt(b))

[ 4 13 17 11] [ 3 40 72 18] 133 [ 4 6 9 10]

[ 2 16 18 4]

[1.09861229 1.60943791 2.07944154 2.19722458]

[1. 2.82842712 3. 1.41421356]

In [6]: ar = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]]) print(ar) print('\n') #transpose ar_trans= ar.T print(ar_trans) print('\n') #determinant print(np.linalg.det(ar)) #rank print(np.linalg.matrix_rank(ar)) #inverse print(np.linalg.inv(ar)) print('\n') #power iteration print(np.linalg.matrix_power(ar, 3))

[[ 6 1 1] [ 4 -2 5] [ 2 8 7]]

[[ 6 4 2]

[ 1 -2 8]

[ 1 5 7]]

[[ 0.17647059 -0.00326797 -0.02287582]

[ 0.05882353 -0.13071895 0.08496732]

[-0.11764706 0.1503268 0.05228758]]

[[336 162 228]

[406 162 469]

[698 702 905]]