Ausnahme gefangen: SSL certificate problem: certificate is not yet valid 📌 NumPy 101: A Beginner's Guide to Data Science with Python

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 NumPy 101: A Beginner's Guide to Data Science with Python


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Day 4 of 100 Days Data Science Bootcamp from noob to expert.

GitHub link: Complete-Data-Science-Bootcamp

Main Post: Complete-Data-Science-Bootcamp

Recap Day 3

Yesterday we have studied in detail about OOPs in Python.

Let's Start

NumPy is a library in Python that provides fast and efficient numerical operations on arrays. It is used extensively in data science and machine learning to perform mathematical operations on large datasets.

Python list vs NumPy arrays

NumPy arrays are similar to Python lists, but they are more efficient and can perform operations faster. NumPy arrays are homogenous, meaning they can only contain elements of the same data type, while Python lists can contain elements of different data types. NumPy arrays also have a fixed size, while Python lists do not.

Creating a NumPy Array

There are several ways to create a NumPy array:

  • Basic ndarray: To create a basic NumPy array, we can use the numpy.array function. For example:
import numpy as np

a = np.array([1, 2, 3, 4])
print(a)

[1 2 3 4]

  • Array of zeros: To create an array of zeros, we can use the numpy.zeros function. For example:
a = np.zeros(5)
print(a)

[0. 0. 0. 0. 0.]

  • Array of ones: To create an array of ones, we can use the numpy.ones function. For example:
a = np.ones(5)
print(a)

[1. 1. 1. 1. 1.]

  • Random numbers in ndarray: To create an array of random numbers, we can use the numpy.random.rand function. For example:
a = np.random.rand(5)
print(a)

[0.34150314 0.25977794 0.18726775 0.00209527 0.08221211]

  • An array of your choice: To create an array with specific values, we can use the numpy.arange function. For example:
a = np.arange(1, 11)
print(a)

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

  • Matrix in NumPy: To create a 2D array (matrix), we can use the numpy.matrix function. For example:
a = np.matrix([[1, 2, 3], [4, 5, 6]])
print(a)

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

  • Evenly spaced ndarray: To create an array with evenly spaced values, we can use the numpy.linspace function. For example:
a = np.linspace(1, 10, 5)
print(a)

[ 1. 3.25 5.5 7.75 10. ]

Shape and Reshaping of NumPy Array

  • Dimensions of NumPy array: To get the dimensions of a NumPy array, we can use the ndarray.ndim attribute. For example:
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ndim)

2

  • Shape of NumPy array: To get the shape of a NumPy array, we can use the ndarray.shape attribute. For example:
print(a.shape)

(2, 3)

  • Size of NumPy array: To get the total number of elements in a NumPy array, we can use the ndarray.size attribute. For example:
print(a.size)

6

  • Reshaping a NumPy array: To reshape a NumPy array, we can use the ndarray.reshape function. For example:
a = np.array([1, 2, 3, 4, 5, 6])
b = a.reshape(2, 3)
print(b)

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

  • Flattening a NumPy array: To flatten a NumPy array, we can use the ndarray.flatten function. For example:
a = np.array([[1, 2, 3], [4, 5, 6]])
b = a.flatten()
print(b)

[1 2 3 4 5 6]

  • Transpose of a NumPy array: To get the transpose of a NumPy array, we can use the ndarray.transpose function. For example:
a = np.array([[1, 2, 3], [4, 5, 6]])
b = a.transpose()
print(b)

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

Expanding and Squeezing a NumPy Array

  • To expand a NumPy array, we can use the numpy.expand_dims function. For example:
a = np.array([1, 2, 3])
b = np.expand_dims(a, axis=1)
print(b)

[[1] [2] [3]]

  • To squeeze a NumPy array, we can use the numpy.squeeze function. For example:
a = np.array([[1], [2], [3]])
b = np.squeeze(a)
print(b)

[1 2 3]

Indexing and Slicing of NumPy Array

  • Slicing 1-D NumPy arrays: To slice a 1-D NumPy array, we can use the ndarray[start:end:step] notation. For example:
a = np.array([1, 2, 3, 4, 5, 6])
b = a[1:4]
print(b)

[2 3 4]

  • Slicing 2-D NumPy arrays: To slice a 2-D NumPy array, we can use the ndarray[row_start:row_end:row_step, col_start:col_end:col_step] notation. For example:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = a[0:2, 1:3]
print(b)

[[2 3] [5 6]]

  • Slicing 3-D NumPy arrays: To slice a 3-D NumPy array, we can use the ndarray[dim1_start:dim1_end:dim1_step, dim2_start:dim2_end:dim2_step, dim3_start:dim3_end:dim3_step] notation. For example:
a = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
b = a[0:2, 0:2, 0:2]
print(b)

[[[ 1 2] [ 4 5]]

[[ 7 8] [10 11]]]

  • Negative slicing of NumPy arrays: To slice a NumPy array with negative indices, we can use the ndarray[-end:-start:-step] notation. For example:
a = np.array([1, 2, 3, 4, 5, 6])
b = a[-4:-1]
print(b)

[3 4 5]

Stacking and Concatenating Numpy Arrays

  • Stacking ndarrays: To stack NumPy arrays vertically or horizontally, we can use the numpy.vstack or numpy.hstack functions. For example:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.vstack((a, b))
d = np.hstack((a, b))
print(c)
print(d)

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

  • Concatenating ndarrays: To concatenate NumPy arrays along a specific axis, we can use the numpy.concatenate function. For example:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.concatenate((a, b), axis=1)
print(c)

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

Broadcasting in Numpy Arrays

Broadcasting is a powerful feature in NumPy that allows us to perform arithmetic operations on arrays of different shapes. NumPy automatically broadcasts smaller arrays to match the shape of larger arrays in order to perform operations.

For example:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)

[5 7 9]

NumPy Ufuncs

NumPy provides a wide range of universal functions (ufuncs) that can be applied to arrays to perform mathematical operations. Some common ufuncs include:

  • np.add: Adds two arrays element-wise

  • np.subtract: Subtracts two arrays element-wise

  • np.multiply: Multiplies two arrays element-wise

  • np.divide: Divides two arrays element-wise

  • np.abs: Calculates the absolute value of an array

  • np.sin: Calculates the sine of an array

  • np.cos: Calculates the cosine of an array

  • np.exp: Calculates the exponent of an array

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.add(a, b)
d = np.subtract(a, b)
e = np.multiply(a, b)
f = np.divide(a, b)
g = np.abs(a)
h = np.sin(a)
i = np.cos(a)
j = np.exp(a)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
print(j)

[5 7 9] [-3 -3 -3] [ 4 10 18] [0.25 0.4 0.5 ] [1 2 3] [0.84147098 0.90929743 0.14112001] [ 0.54030231 -0.41614684 -0.9899925 ] [ 2.71828183 7.3890561 20.08553692]

Maths with NumPy Arrays

NumPy provides many functions to perform mathematical operations on arrays. Some common functions include:

  • np.mean: Calculates the mean of an array

  • np.median: Calculates the median of an array

  • np.std: Calculates the standard deviation of an array

  • np.min: Calculates the minimum value of an array

  • np.max: Calculates the maximum value of an array

  • np.argmin: Calculates the index of the minimum value of an array

  • np.argmax: Calculates the index of the maximum value of an array

  • np.sort: Sorts an array in ascending order

a = np.array([1, 2, 3, 4, 5])
mean = np.mean(a)
median = np.median(a)
std = np.std(a)
min_val = np.min(a)
max_val = np.max(a)
min_index = np.argmin(a)
max_index = np.argmax(a)
sorted_a = np.sort(a)
print(mean)
print(median)
print(std)
print(min_val)
print(max_val)
print(min_index)
print(max_index)
print(sorted_a)

3.0 3.0 1.4142135623730951 1 5 0 4 [1 2 3 4 5]

NumPy Arrays and Images

NumPy arrays can be used to represent images. To read an image into a NumPy array, we can use the scipy.misc.imread function. To write a NumPy array as an image, we can use the scipy.misc.imsave function.

import numpy as np
import imageio

# Read image into NumPy array
img = imageio.imread('image.jpeg')

# Perform some operations on the image
img = img * 2

# Save the modified image
imageio.imsave('modified_image.jpeg', img)

To display the image using matplotlib, you can use the same code as before:

import numpy as np
import matplotlib.pyplot as plt

# Read image into NumPy array
img = imageio.imread('image.jpeg')

# Display image
plt.imshow(img)
plt.show()

Image description

After Image

# Read image into NumPy array
img = imageio.imread('modified_image.jpeg')

# Display image
plt.imshow(img)
plt.show()

Image description

Exercise Question you will find in the exercise notebook of Day 4 on GitHub.

If you liked it then...

Buy Me A Coffee

...



📌 NumPy 101: A Beginner's Guide to Data Science with Python


📈 68.2 Punkte

📌 Numpy Tutorial #2 - Numpy Arrays vs Python Lists (Python für Data Science)


📈 64.23 Punkte

📌 Numpy Tutorial #1 - Arrays erstellen (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #3 - Datentypen (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #4 - Shape erklärt (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #5 - Shapes in komplexen Dtypes (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #8 - Operationen mit Vektoren und Matrizen (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #7 - Slicen und Indexierung (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #6 - benannte DTypes (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #10 - Reshape (Python für Data Science)


📈 38.73 Punkte

📌 Numpy Tutorial #9 - Ravel und Flatten (Python für Data Science)


📈 38.73 Punkte

📌 Numpy up to 1.13.1 numpy.pad denial of service


📈 37.97 Punkte

📌 Numpy bis 1.13.1 numpy.pad Denial of Service


📈 37.97 Punkte

📌 Python for Data Science – Most Important Contribution of Data Science in Cybersecurity


📈 32.97 Punkte

📌 Revolutionizing Data Science Workflows | Python Data Science Day


📈 32.97 Punkte

📌 Microsoft Fabric for Data Science | Python Data Science Day


📈 32.97 Punkte

📌 Supercharging your Data Science projects with GitHub tools | Python Data Science Day


📈 32.97 Punkte

📌 Data Science: The Bear* Necessities | Python Data Science Day


📈 32.97 Punkte

📌 Linear Algebra for Data Science: Understanding and Applying Vectors, Matrices and their Operations using Numpy


📈 32.21 Punkte

📌 A Beginner's Guide to Radix Sort: Step-by-Step Guide and Python Code


📈 31.13 Punkte

📌 Streamlining Data Preparation with Pydantic: A 25-Minute Guide | Python Data Science Day


📈 29.64 Punkte

📌 Getting started with Python using Data Wrangler in Microsoft Fabric | Python Data Science Day


📈 29.61 Punkte

📌 Cryptocurrency Creation 101: A Beginner's Guide to Launching Your Own Coin


📈 29.47 Punkte

📌 JavaScript Classes 101: A Beginner's Guide to Object-Oriented Programming


📈 29.47 Punkte

📌 SynthDiD 101: A Beginner’s Guide to Synthetic Difference-in-Differences


📈 29.47 Punkte

📌 INTRODUCTION TO CSS CASCADE, SELECTOR AND SPECIFICITY- A BEGINNER GUIDE 101


📈 29.47 Punkte

📌 Demo: Working with NumPy | Even More Python for Beginners - Data Tools [28 of 31]


📈 28.85 Punkte

📌 Working with NumPy | Even More Python for Beginners - Data Tools [27 of 31]


📈 28.85 Punkte

📌 NumPy Ninja: Unleashing the Power of Python for Data Wizards


📈 28.85 Punkte

📌 Beginner-Friendly Data Science Reads (That Advanced Data Scientists Will Enjoy, Too)


📈 28.1 Punkte

📌 Everything About Python Numeric Data Types: Beginner’s Guide


📈 27.93 Punkte

📌 [Data Science] Using Pandas for Better (and Worse) Data Science


📈 26.46 Punkte

📌 TIBCO Data Science for AWS/Spotfire Data Science up to 6.4.0 Application Server spoofing


📈 26.46 Punkte

📌 TIBCO Data Science for AWS/Spotfire Data Science up to 6.4.0 Application Server Persistent cross site scripting


📈 26.46 Punkte

📌 TIBCO Data Science for AWS/Spotfire Data Science up to 6.4.0 Application Server Persistent cross site scripting


📈 26.46 Punkte











matomo