You can filter elements in a NumPy array based on a condition. For example, to get all positive numbers from an array, you use a condition like array_name[array_name > 0].
import numpy as np numbers = np.array([-5, 4, 0, 2, 3]) positives = numbers[numbers > 0] print(positives) # Output: array([4, 2, 3])
You can perform logical operations on arrays to get True or False values based on a condition. For instance, checking which numbers are greater than 0 will return an array of True or False.
import numpy as np numbers = np.array([-5, 4, 0, 2, 3]) is_positive = numbers > 0 print(is_positive) # Output: array([False, True, False, True, True], dtype=bool)
You can create NumPy arrays from data stored in files. For example, you can read data from a CSV file and convert it into a NumPy array.
import numpy as np imported_data = np.genfromtxt("filename.csv", delimiter=",")
You can create NumPy arrays from lists or other data sources. For example, you can turn a Python list into a NumPy array using np.array().
import numpy as np my_array = np.array([1, 2, 3, 4]) print(my_array) # Output: array([1, 2, 3, 4])
You can access elements in a NumPy array by their index. For example, to get a specific value or row from a 2D array, you use indexing.
import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(matrix[0, 0]) # Output: 1 print(matrix[1, :]) # Output: array([4, 5, 6])
NumPy allows you to perform arithmetic operations on arrays element-wise. This means each element in one array can be added, subtracted, or manipulated with the corresponding element in another array.
import numpy as np digits = np.array([1, 3, 5, 7, 9]) evens = digits + 1 print(evens) # Output: array([2, 4, 6, 8, 10])
Histograms are used to visualize the distribution of data. They show how often different ranges of values occur in your dataset.
import numpy as np import matplotlib.pyplot as plt # Generate data with normal distribution mu = 0 sigma = 0.1 data = np.random.normal(mu, sigma, 1000) plt.hist(data, bins=30) plt.show()
The shape of a histogram can tell you about the distribution of your data. It could be unimodal (one peak), bimodal (two peaks), or have multiple peaks.
import numpy as np import matplotlib.pyplot as plt # Example data x_axis = np.random.normal(0, 1, 1000) plt.hist(x_axis, bins=20, color='blue') plt.show()
The standard deviation measures how spread out the values in your data are from the average. A low standard deviation means values are close to the average, while a high standard deviation means values are spread out.
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) mean_per_row = np.mean(data, axis=1) print(mean_per_row) # Output: array([2., 5., 8.])
You can use NumPy to calculate statistics like the mean and percentile, and visualize the results using tools like Matplotlib.
import numpy as np import matplotlib.pyplot as plt # Example data heights = np.array([49.7, 46.9, 62, 47.2, 47, 48.3, 48.7]) mean_height = np.mean(heights) print(mean_height) # Output: 48.4
Percentiles help you understand the distribution of your data by showing the value below which a given percentage of data falls. For example, the 50th percentile is the median.
import numpy as np percentile_25 = np.percentile(data, 25) print(percentile_25) # Output: value at 25th percentile
Sorting helps to arrange data in a specific order. For example, you might sort data to see the smallest or largest values quickly.
import numpy as np heights = np.array([49.7, 46.9, 62, 47.2, 47, 48.3, 48.7]) sorted_heights = np.sort(heights) print(sorted_heights) # Output: array([46.9, 47. , 47.2, 48.3, 48.7, 49.7, 62. ])
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!