You can filter elements in a NumPy array based on a condition. For example, you can get all positive numbers from an array using a condition.
import numpy as np
numbers = np.array([-5, 4, 0, 2, 3])
positives = numbers[numbers > 0]
print(positives)
# Output: [4, 2, 3]
NumPy allows you to perform logical operations on arrays. For example, you can create an array of True or False values based on a condition applied to another array.
import numpy as np
numbers = np.array([-5, 4, 0, 2, 3])
is_positive = numbers > 0
print(is_positive)
# Output: [False, True, False, True, True]
You can create NumPy arrays from data in files, like CSV files, using the `np.genfromtxt()` function.
import numpy as np
data = np.genfromtxt('data.csv', delimiter=',') # Correct for CSV
print(data)
NumPy arrays can be created from lists or other iterable data structures using `np.array()`.
import numpy as np
my_array = np.array([1, 2, 3, 4])
print(my_array)
# Output: [1 2 3 4]
You can access specific elements in a NumPy array by specifying their indices. For example, `array[0, 0]` accesses the first element of a 2D array.
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: [4, 5, 6]
NumPy allows you to perform arithmetic operations on arrays element-wise. For example, adding two arrays together adds corresponding elements.
import numpy as np
odds = np.array([1, 3, 5, 7, 9])
evens = odds + 1
print(evens)
# Output: [2, 4, 6, 8, 10]
array1 = np.array([1, 2, 3])
array2 = np.array([4, 3, 2])
new_array = array1 + array2
print(new_array)
# Output: [5, 5, 5]
Histograms are used to visualize the distribution of data. They show how frequently each range of values occurs in a dataset.
import numpy as np
import matplotlib.pyplot as plt
mu = 0 # mean
sigma = 0.1 # standard deviation
data = np.random.normal(mu, sigma, 1000)
plt.hist(data, bins=30)
plt.show()
A normal distribution is a common way to describe data that clusters around a mean value. You can visualize it using histograms.
import numpy as np
import matplotlib.pyplot as plt
mu = 0 # mean
sigma = 1 # standard deviation
data = np.random.normal(mu, sigma, 1000)
plt.hist(data, bins=30)
plt.show()
The standard deviation measures how spread out the numbers in a dataset are from the mean. A higher standard deviation means more spread.
import numpy as np
ring_toss = np.array([[1, 0, 0], [0, 0, 1], [1, 0, 1]])
avg_per_row = np.mean(ring_toss, axis=1)
print(avg_per_row)
# Output: [0.33333333 0.33333333 0.66666667]
Histograms help you see the spread and frequency of data points. For example, you can see how many times data points fall into certain ranges.
import numpy as np
import matplotlib.pyplot as plt
data = np.array([1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8])
plt.hist(data, bins=10, color='blue')
plt.show()
Percentiles tell you the value below which a given percentage of observations fall. For example, the 50th percentile is the median.
import numpy as np
data = np.array([1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8])
percentile_25 = np.percentile(data, 25)
percentile_75 = np.percentile(data, 75)
print(percentile_25)
# Output: 3.5
print(percentile_75)
# Output: 6.5
The mean is the average of all numbers in a dataset. NumPy provides a function to calculate the mean easily.
import numpy as np
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.6
NumPy provides functions to sort arrays. Sorting helps in organizing data and finding patterns.
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: [46.9 47. 47.2 48.3 48.7 49.7 62. ]
68% of the data in a normal distribution will fall within one standard deviation of the mean. This shows where most data points are clustered around the mean.
95% of data in a normal distribution will fall within two standard deviations of the mean, showing a wider range of data points.
99.7% of data in a normal distribution will fall within three standard deviations of the mean, covering almost all data points.
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!