# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np





sample_list = [1,2,3]

list_array = np.array(sample_list)

list_array

tup = (1,2,3)

my_tup_array = np.array(tup)

my_tup_array


def multiply_loops(A, B):
    c=np.zeros((A.shape[0], B.shape[1]))
    for i in range(A.shape[0]):
        for k in range(B.shape[1]):
            c[i,k] = 0
            for j in range(B.shape[0]):
                n = A[i,j] * B[j,k]
                c[i,k] += n
    return c


def multiply_vector(A, B):
     return A @ B


X = np.random.random((100, 100))
Y = np.random.random((100, 100))

# %timeit multiply_loops(X, Y)
# %timeit multiply_vector(X, Y)

tup_dim = (3,4)
my_zeros_array = np.zeros(tup_dim, dtype = np.int16)

my_zeros_array

tup_dim = (3,4)
my_ones_array = np.ones(tup_dim,dtype = np.int16)

my_ones_array

tup_dim = (3,4)
my_seven_array = np.full(tup_dim,7, dtype = np.int16)

my_seven_array

my_identity_array = np.identity(4)

my_identity_array

my_rand_array = np.random.rand(3,4)

my_rand_array

tup_dim = (3,4)

my_uninitialized_array = np.empty(tup_dim)

my_uninitialized_array

my_range_array = np.arange(10,30,5)

my_range_array

my_spaced_array = np.linspace(0,30,6)

my_spaced_array

array_A = np.array([ [3,4,6], [0,8,1]])

array_A.ndim

array_A

array_A.shape

array_A.size

array_A.dtype

array_A.itemsize

my_array = np.array([[1, 4, 5, 6], [7, 8, 9, 10], [11, 12, 14, 16]])

my_array.ndim

my_array.shape

my_array.dtype

my_array.size

import os

import pandas as pd
# defining housing.csv file path
HOUSING_PATH =  '/cxldata/datasets/project/housing'
# reading the large housing.csv file using pandas
housing_raw = pd.read_csv(os.path.join(HOUSING_PATH, "housing.csv"))
# extracting only a few rows (5 rows) of data from the pandas dataframe 'my_df'
my_df = housing_raw.iloc[ : 5]
# creating a new small csv file - 'housing_short.csv' - containing the above extracted 5 rows of data
my_df.to_csv('housing_short.csv', index=False)

# +
import numpy as np
import os

# using pandas to load a large csv file (housing.csv) and creating a new smaller csv file out of it after extracting only a few rows of data from it.
import pandas as pd
HOUSING_PATH =  '/cxldata/datasets/project/housing'
housing_raw = pd.read_csv(os.path.join(HOUSING_PATH, "housing.csv"))
my_df = housing_raw.iloc[ : 5]
my_df.to_csv('housing_short.csv', index=False)

# loading the smaller csv file housing_short.csv using NumPy's loadtxt() function

FILE = 'housing_short.csv'

# defining load_housing_data() function, which takes filename (FILE) as input and loads this file using NumPy's loadtxt() function

def load_housing_data(file =FILE ):
    return np.loadtxt(file, dtype={'names': ('longitude','latitude','housing_median_age','total_rooms','total_bedrooms','population','households','median_income','median_house_value','ocean_proximity'), 'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', '|S15')}, delimiter=',', skiprows=1, unpack=True)

# calling the above defined load_housing_data() function, which returns various column values as NumPy arrays

longitude_arr, latitude_arr, housing_median_age_arr, total_rooms_arr, total_bedrooms_arr, population_arr, households_arr, median_income_arr, median_house_value_arr, ocean_proximity_arr = load_housing_data()

# printing the values of NumPy array - median_house_value_arr - that we got above.  
# median_house_value_arr contains values of median_house_value column of the csv file - housing_short.csv

print(median_house_value_arr)
# -

HOUSING_PATH = '/cxldata/datasets/project/housing'

FILE = os.path.join(HOUSING_PATH, "housing.csv")


def load_housing_dataset(file=FILE):
    return np.genfromtxt(file, dtype={'names': ('longitude','latitude','housing_median_age','total_rooms','total_bedrooms','population','households','median_income','median_house_value','ocean_proximity'),'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', '|S15')}, delimiter=',', skip_header=1, filling_values = 99999999, unpack=False)


result_arr = load_housing_dataset()

print(len(result_arr))

# +
my_arr = np.array([ [0,1], [2,3] ])

my_arr.resize((3,4))
# -

print(my_arr)

# +
my_arr = np.array([[1,2,3,4], [5,6,7,8]])

my_arr.resize((3,4))
# -

my_arr

# +
import numpy as np
my_arr = np.arange(9)

my_new_arr = my_arr.reshape(-1,3)
# -

my_new_arr

# +
my_first_arr = np.array([1,2,3,4,5,6,7,8])

my_new_arr = my_first_arr.reshape((2,4))
# -

print(my_new_arr)

print(my_first_arr)

import numpy as np
my_second_arr = np.arange(9)
my_second_arr

my_updated_arr = my_second_arr.reshape(-1,3)
print(my_updated_arr)
print(my_second_arr)

import numpy as np
from sklearn.datasets import load_sample_image

china = load_sample_image("china.jpg")

china.shape

print(china)

image = china[150:220,130:250]

image.shape

height, width, channels = image.shape

image_grayscale = image.mean(axis=2).astype(np.float32)

image_grayscale.shape

images = image_grayscale.reshape(1, height, width, 1)

images.shape

# +
import numpy as np

my_orig_arr = np.array((1, 15, 3, 9, 26, 7, 89, 12))
# -

my_rev_arr = my_orig_array[::-1]

print(my_rev_arr)

my_orig_arr2 = np.array((2, 9, 17, 13, 1, 4, 20, 57))

my_orig_arr2[2:5] = -6

print(my_orig_arr2)

arr = [1, 3, 5, 10, 4, 15]

arr[1 : 4]  = -3

apple = np.array((1, 8, 23, 3, 18, 91, 7, 15))

apple_slice = apple[1:4]

print(apple_slice)

print(apple)

apple_slice[1] = 99999

print(apple_slice)

print(apple)

apple_slice_new = apple[2:5].copy()

apple_slice_new[1] = 222222

print(apple_slice_new)

print(apple)

from sklearn.datasets import load_sample_image

import numpy as np

china = load_sample_image("china.jpg")

portion = china[120:250,110:230]

portion.shape

portion

multi_arr = np.arange(12).reshape(3,4)

print(multi_arr)

rows_wanted = np.array( [True, False, True] )

multi_arr_portion = multi_arr[rows_wanted, : ]

print(multi_arr_portion)

import numpy as np

my_multi_arr = np.arange(20).reshape(4,5)

my_multi_arr_portion = my_multi_arr[2:4,2:5]

print(my_multi_arr_portion)

my_2d_arr = np.arange(20).reshape(4,5)

my_1d_arr = my_2d_arr.ravel()

my_1d_arr

a_arr = np.array([60,70,80,90])
b_arr = np.arange(4)

c_arr = a_arr + b_arr

print(c_arr)

d_arr = np.array([60,70,80,90])
e_arr = np.arange(4)

f_arr = d_arr - e_arr

print(f_arr)

A_arr = np.array([ [ 5,9], [4, 7] ])
B_arr = np.array( [ [2, 8], [1, 6] ] )

M_arr = A_arr * B_arr

print(M_arr)

C_arr = np.array([ [ 5,9], [4, 7] ])
D_arr = np.array( [ [2, 8], [1, 6] ] )

P_arr = np.dot(C_arr,D_arr)

print(P_arr)

R_div = np.array([ [ 25,65], [40, 14] ])
S_div = np.array([ [2, 10], [4, 5] ] )

T_div = R_div/S_div

print(T_div)

R_int_div = np.array([ [ 25,65], [40, 70] ])
S_int_div = np.array([ [2, 10], [8, 3] ] )

T_int_div = R_int_div//S_int_div

print(T_int_div)

R_mod = np.array([ [ 20,65], [40, 70] ])
S_mod = np.array([ [2, 10], [8, 3] ] )

T_mod = R_mod%S_mod

print(T_mod)

R_exp = np.array([ [ 4, 2], [3, 4 ] ] )
S_exp = np.array( [ [2, 10], [4, 5] ] )

T_exp = R_exp**S_exp

print(T_exp)

U = np.array([ [ 22, 45], [90, 4 ] ] )

U[U<45]

X_stat = np.array([ [ 1, 2, 3], [ 4, -5, 6] ])

print(f'Mean = {X_stat.mean()}')

print(f'Mean = {X_stat.var()}')

print(f'Mean = {X_stat.std()}')

print(f'Mean = {X_stat.min()}')

print(f'Mean = {X_stat.max()}')

print(f'Mean = {X_stat.sum()}')

print(f'Mean = {X_stat.prod()}')

Z = np.arange(18).reshape(2,3,3)

print(Z)

print(Z.sum(axis=2))

N = np.arange(6).reshape(3,2)

print(N)

print("Transpose = ", N.T)

X_broad = np.ones((3,3))

print(X_broad)

Y_broad = np.arange(3)

print(Y_broad)

Z_broad = X_broad+Y_broad

print(Z_broad)



StarcoderdataJupyterScriptsDedupFiltered