Round to two decimals

Python Practice › NumPy Programs › Program 96

Round to two decimals

EasyNumPy Programs · 96 of 100

Write a NumPy program to round elements to two decimal places.

Input
[4.8757, 2.4578]
Output
[4.88 2.46]

Approach

  1. np.round(a, 2).

Solution

Python
import numpy as npprint(np.round(np.array([4.8757, 2.4578]), 2))
Output
[4.88 2.46]
Note

Rounding gives 4.88 and 2.46; the original sample (4.86, 2.45) was not rounded correctly.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Tangent of angles

Python Practice › NumPy Programs › Program 95

Tangent of angles

EasyNumPy Programs · 95 of 100

Write a NumPy program to calculate the tangent of angles.

Input
0, 45 degrees
Output
[0. 1.]

Approach

  1. np.tan(np.radians(...)).

Solution

Python
import numpy as npprint(np.tan(np.radians([0, 45])).round(3))
Output
[0. 1.]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Cosine of angles

Python Practice › NumPy Programs › Program 94

Cosine of angles

EasyNumPy Programs · 94 of 100

Write a NumPy program to calculate the cosine of angles.

Input
0, 60, 90 degrees
Output
[1.  0.5 0. ]

Approach

  1. np.cos(np.radians(...)).

Solution

Python
import numpy as npprint(np.cos(np.radians([0, 60, 90])).round(3))
Output
[1.  0.5 0. ]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Variance

Python Practice › NumPy Programs › Program 93

Variance

EasyNumPy Programs · 93 of 100

Write a NumPy program to calculate the variance of an array.

Input
[2, 4, 4, 4, 5, 5, 7, 9]
Output
4.0

Approach

  1. np.var.

Solution

Python
import numpy as npprint(np.var([2, 4, 4, 4, 5, 5, 7, 9]))
Output
4.0

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Standard deviation

Python Practice › NumPy Programs › Program 92

Standard deviation

EasyNumPy Programs · 92 of 100

Write a NumPy program to calculate the standard deviation of an array.

Input
[2, 4, 4, 4, 5, 5, 7, 9]
Output
2.0 2.138

Approach

  1. np.std (population); ddof=1 for a sample.

Solution

Python
import numpy as npa = np.array([2, 4, 4, 4, 5, 5, 7, 9])print(np.std(a), round(float(np.std(a, ddof=1)), 3))
Output
2.0 2.138

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sine of angles

Python Practice › NumPy Programs › Program 91

Sine of angles

EasyNumPy Programs · 91 of 100

Write a NumPy program to calculate the sine of angles.

Input
0, 30, 90 degrees
Output
[0.  0.5 1. ]

Approach

  1. Convert degrees to radians, then np.sin.

Solution

Python
import numpy as npprint(np.sin(np.radians([0, 30, 90])).round(3))
Output
[0.  0.5 1. ]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Do two arrays have the same shape?

Python Practice › NumPy Programs › Program 90

Do two arrays have the same shape?

EasyNumPy Programs · 90 of 100

Write a NumPy program to check whether two arrays have the same dimensions.

Output
True False

Approach

  1. Compare .shape.

Solution

Python
import numpy as npa, b, c = np.zeros((2, 3)), np.ones((2, 3)), np.ones((3, 2))print(a.shape == b.shape, a.shape == c.shape)
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Create and reshape

Python Practice › NumPy Programs › Program 89

Create and reshape

EasyNumPy Programs · 89 of 100

Write a NumPy program to create an array and reshape it.

Output
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(2, 6)

Approach

  1. reshape(-1, n) lets NumPy work out one dimension.

Solution

Python
import numpy as npa = np.arange(12)print(a.reshape(3, 4))print(a.reshape(-1, 6).shape)
Output
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(2, 6)

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Array to a DataFrame with headers

Python Practice › NumPy Programs › Program 88

Array to a DataFrame with headers

EasyNumPy Programs · 88 of 100

Write a NumPy program to convert an array into a pandas DataFrame with column headers.

Output
   maths  science
0     90       85
1     72       64
2     88       91

Approach

  1. pd.DataFrame(array, columns=[...]).

Solution

Python
import numpy as npimport pandas as pd a = np.array([[90, 85], [72, 64], [88, 91]])df = pd.DataFrame(a, columns=["maths", "science"])print(df)
Output
   maths  science
0     90       85
1     72       64
2     88       91

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Merge arrays of the same shape

Python Practice › NumPy Programs › Program 87

Merge arrays of the same shape

EasyNumPy Programs · 87 of 100

Write a NumPy program to merge two arrays of identical shape.

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

Approach

  1. np.vstack stacks rows; np.hstack joins side by side.

Solution

Python
import numpy as npa = np.array([[1, 2], [3, 4]])b = np.array([[5, 6], [7, 8]])print(np.vstack((a, b)))print(np.hstack((a, b)))
Output
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[1 2 5 6]
 [3 4 7 8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs