keithgalli / numpy Goto Github PK
View Code? Open in Web Editor NEWJupyter Notebook & Data Associated with my Tutorial video on the Python NumPy Library
Jupyter Notebook & Data Associated with my Tutorial video on the Python NumPy Library
It seems like there are a few issues in the provided code. I will point them out and provide corrected code where needed.
In the "Accessing/Changing specific elements, rows, columns, etc" section, there's an attempt to change elements of the array b
using a list of lists. This will result in a ValueError. To update elements of a NumPy array, you should use the correct syntax.
In the "Random Integer values" section, the function np.random.randint
should be used with the low
and high
parameters to specify the range of random integers. The code provided is not syntactically correct.
Here's the corrected code with explanations:
import numpy as np
# The Basics
a = np.array([1, 2, 3], dtype='int32')
print(a)
b = np.array([[9.0, 8.0, 7.0], [6.0, 5.0, 4.0]])
print(b)
# Get Dimension
a.ndim
# Get Shape
b.shape
# Get Type
a.dtype
# Get Size
a.itemsize
# Get total size
a.nbytes
# Get number of elements
a.size
# Accessing/Changing specific elements, rows, columns, etc
a = np.array([[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]])
print(a)
# Get a specific element [r, c]
a[1, 5]
# Get a specific row
a[0, :]
# Get a specific column
a[:, 2]
# Getting a little more fancy [startindex:endindex:stepsize]
a[0, 1:-1:2]
a[1, 5] = 20
# Corrected way to change elements of a NumPy array
a[:, 2] = 1 # Change the entire column to 1
print(a)
# Initializing Different Types of Arrays
np.zeros((2, 3))
np.ones((4, 2, 2), dtype='int32')
np.full((2, 2), 99)
np.random.rand(4, 2)
np.random.randint(-4, 8, size=(3, 3))
np.identity(5)
# Repeat an array
arr = np.array([[1, 2, 3]])
r1 = np.repeat(arr, 3, axis=0)
print(r1)
output = np.ones((5, 5))
print(output)
z = np.zeros((3, 3))
z[1, 1] = 9
output[1:-1, 1:-1] = z
print(output)
# Be careful when copying arrays!!!
a = np.array([1, 2, 3])
b = a.copy()
b[0] = 100
print(a)
# Mathematics
a = np.array([1, 2, 3, 4])
print(a)
a + 2
a - 2
a * 2
a / 2
b = np.array([1, 0, 1, 0])
a + b
a ** 2
np.cos(a)
# Linear Algebra
a = np.ones((2, 3))
print(a)
b = np.full((3, 2), 2)
print(b)
np.matmul(a, b)
# Statistics
stats = np.array([[1, 2, 3], [4, 5, 6]])
print(stats)
np.min(stats)
np.max(stats, axis=1)
np.sum(stats, axis=0)
I've corrected the issues in the code related to updating elements in the array and the usage of np.random.randint
. The corrected code should work as expected.
In [34]
Instead of :
b[:,1,:] = [[9,9,9],[8,8]]
We use :
b[:,1,:] = [[9,9],[8,8]]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.