Similar to Write a NumPy program to find the most frequent value in an array. Take two NumPy arrays having two dimensions. Concatenate the arrays on axis 1. (20)
How to Consolidate Subscription Billing in Odoo 18 SalesCeline George
Ad
Write a NumPy program to find the most frequent value in an array. Take two NumPy arrays having two dimensions. Concatenate the arrays on axis 1.
1. Practical of Python
welcome
Take two NumPy arrays having two dimensions. Concatenate the arrays
on axis 1
# Importing the NumPy library
import numpy as np
# Creating the first 2D NumPy array (2 rows, 3 columns)
array1 = np.array([[1, 2, 3],
[4, 5, 6]])
# Creating the second 2D NumPy array (2 rows, 2 columns)
array2 = np.array([[7, 8],
[9, 10]])
# Concatenating the arrays along axis 1 (columns)
# Both arrays must have the same number of rows (axis 0)
result = np.concatenate((array1, array2), axis=1)
# Printing the result
print("Concatenated Array (axis=1):")
print(result)
output –
Concatenated Array (axis=1):
[[ 1 2 3 7 8]
[ 4 5 6 9 10]]
2. Write a NumPy program to find the most frequent value in an
array.
# Importing NumPy
import numpy as np
# Create a 1D NumPy array with some repeated values
array = np.array([1, 3, 2, 3, 4, 3, 5, 2, 2, 2, 4, 5, 2])
# Use np.unique to find all unique values and their counts
values, counts = np.unique(array, return_counts=True)
# Find the index of the maximum count
max_count_index = np.argmax(counts)
# The most frequent value is the one with the highest count
most_frequent = values[max_count_index]
# Print the result
print("The most frequent value in the array is:", most_frequent)
output –
The most frequent value in the array is: 2
Write a program to replace ‘a’ with ‘b’, ‘b’ with ‘c’,….,’z’ with ‘a’ and
similarly for ‘A’ with ‘B’,’B’ with ‘C’, …., ‘Z’ with ‘A’ in a file. The other
characters should remain unchanged.
# Function to shift letters to the next character in the alphabet
def shift_letter(char):
3. if 'a' <= char <= 'z':
return 'a' if char == 'z' else chr(ord(char) + 1)
elif 'A' <= char <= 'Z':
return 'A' if char == 'Z' else chr(ord(char) + 1)
else:
return char # Leave other characters unchanged
# Read from input file
with open('input.txt', 'r') as file:
content = file.read()
# Apply the shift to each character
shifted_content = ''.join(shift_letter(c) for c in content)
# Write to output file
with open('output.txt', 'w') as file:
file.write(shifted_content)
print("File transformation complete. Check 'output.txt'.")
input - Hello, World! Zebra.
Output - Ifmmp, Xpsme! Afcsb.
Write a function that reads the contents of the file f3.txt and counts the number
of alphabets, blank spaces, lowercase letters, number of words starting with a
vowel and number of occurrences of a work “hello”.
def analyze_file(filename='f3.txt'):
# Open and read the file
with open(filename, 'r') as file:
content = file.read()
# Count alphabets
4. alphabet_count = sum(1 for char in content if char.isalpha())
# Count blank spaces
space_count = content.count(' ')
# Count lowercase letters
lowercase_count = sum(1 for char in content if char.islower())
# Split into words
words = content.split()
# Count words starting with a vowel
vowels = 'aeiouAEIOU'
vowel_start_count = sum(1 for word in words if word and word[0] in vowels)
# Count occurrences of the word "hello" (case-insensitive)
hello_count = sum(1 for word in words if word.lower() == 'hello')
# Print results
print("Alphabet characters:", alphabet_count)
print("Blank spaces:", space_count)
print("Lowercase letters:", lowercase_count)
print("Words starting with a vowel:", vowel_start_count)
print("Occurrences of the word 'hello':", hello_count)
# Call the function
analyze_file()
input - Hello there! How are you? hello Hello hi elephant awesome.
Output - Alphabet characters: 46
Blank spaces: 8
Lowercase letters: 34
Words starting with a vowel: 3
Occurrences of the word 'hello': 3