Sarah111-AHM / Semsmah

2 stars 0 forks source link

Lap(6) برمجة عملي #8

Open Sarah111-AHM opened 1 year ago

Sarah111-AHM commented 1 year ago

Q1) reshape the given array to 5 rows and 3 columns in Fortran order and return it to vector in C order without make copy arr = np.arange(15)


import numpy as np

arr = np.arange(15)
reshaped_arr = arr.reshape((5, 3), order='F')
flattened_arr = reshaped_arr.flatten(order='C')

print(flattened_arr)

image Q2) concatenate the following arrays vertically (by the rows) the split it as shown first: [[1 2 3] [4 5 6]] second: [[7 8 9]] third: [[10 11 12]] arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]])


import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[7, 8, 9], [10, 11, 12]])

concatenated_arr = np.vstack((arr1, arr2))
first = concatenated_arr[:2, :]
second = concatenated_arr[2:3, :]
third = concatenated_arr[3:, :]

print("First array:\n", first)
print("Second array:\n", second)
print("Third array:\n", third)

image image Output: image Q3) for the following array, repeat the first element twice, the second element three times, and the third element four times (9, 3) then use tile method to get this shape (9, 6) of the array arr = np.random.randn(3,3)


import numpy as np

arr = np.random.randn(3, 3)
repeated_arr = np.repeat(arr, [2, 3, 4], axis=0)
tiled_arr = np.tile(repeated_arr, (3, 2))

print(tiled_arr)
print(tiled_arr.shape)

image Q4) Write a Python program to create a lambda function that adds 15 to a given number passed in as an argument, also create a lambda function that multiplies argument x with argument y and print the result

import nyumpy as np 
# Lambda function to add 15 to a given number
add_fifteen = lambda num: num + 15
# Lambda function to multiply two numbers
multiply = lambda x, y: x * y
# Example usage
num = 5
result = add_fifteen(num)
print("Adding 15 to", num, "gives", result)
x = 3
y = 7
result = multiply(x, y)
print("Multiplying", x, "and", y, "gives", result)

image image Q5) Write a Python program to create a function that takes one argument, and that argument will be multiplied with an unknown given number Double the number of 15 = 30 Triple the number of 15 = 45 Quadruple the number of 15 = 60 Quintuple the number 15 = 75

def multiply_with_unknown(num):
    unknown_num = 5  # The unknown given number
    return num * unknown_num
# Example usage
num = 15
double = multiply_with_unknown(num)
triple = multiply_with_unknown(num) * 1.5
quadruple = multiply_with_unknown(num) * 2
quintuple = multiply_with_unknown(num) * 2.5
print("Double the number of", num, "=", double)
print("Triple the number of", num, "=", triple)
print("Quadruple the number of", num, "=", quadruple)
print("Quintuple the number of", num, "=", quintuple)

image image