# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.8.12 64-bit
#     name: python3
# ---

# ## Introdução e prelinares 
# Até a pg 61

import mxnet as mx
from mxnet import np, npx

npx.set_np()

x=np.arange(12)
x,type(x)

x.shape

x.size

x=x.reshape(3,4)
x

X=np.arange(12).reshape(3,4)
Y=np.array([[2,1,4,3], [1,2,3,4], [4,3,2,1]])
X

Y

np.concatenate([X, Y], axis=0), np.concatenate([X, Y], axis=1)

X==Y

x=np.array([1,2,3]).reshape(3,1)
x

y=np.array([1,2]).reshape(1,2)
y

x+y

X,Y

X[-1]

X[1:3]

X[0:3, 1:3]=13
X

X>Y,X,Y

T1 = np.arange(12).reshape(1, 6, 2)
T1,np.arange(12)

T2 = np.ones(shape=(6,6,1))
T2

# ## Data  Preprocessing 
#
# pg 51

import os

os.makedirs(os.path.join('../../data','tmp'), exist_ok=True)

data_file=os.path.join('../../data','tmp','house_tiny.csv')

with open(data_file,'w') as f:
    f.write('NumRooms,Alley,Price\n') # Column names
    f.write('NA,Pave,127500\n') # Each row represents a data example
    f.write('2,NA,106000\n')
    f.write('4,NA,178100\n')
    f.write('NA,NA,140000\n')

import pandas as pd
data=pd.read_csv(data_file)
print(data)

inputs, outputs=data.iloc[:,0:2], data.iloc[:,2]
print(inputs)
inputs=inputs.fillna(inputs.mean())
print(inputs)

inputs=pd.get_dummies(inputs, dummy_na=True)
print(inputs)

X, y=np.array(inputs.values), np.array(outputs.values)
X, y

# ### fazer exercicios da pg 53

t3=np.random.choice(np.array([1,2,np.nan]), (20, 5),p=np.array([0.4,0.4, 0.3]))
t3

df=pd.DataFrame(t3.asnumpy(),columns=['a','b','c','e','f'])
print(df)

df.isnull().mean()

df.columns[df.isnull().mean() < 0.25]

res1=df[df.columns[df.isnull().mean() < 0.25]]
print(res1)

resInput=np.array(res1.values)
resOutput=np.random.rand(20)
resInput,resOutput



# ## Linear Algebra
# pg 53

A=np.arange(20).reshape(5,4)
A,A.mean(axis=0),A.mean(axis=1)

A.sum(),A.sum(axis=1),A.sum(axis=1,keepdims=True)

A/A.sum(axis=1,keepdims=True),A.cumsum(axis=1)

# ### Produto Hadamard (Element-wise) e Produto de Matriz
#            *                               dot
#            

A1=np.array([1,2,3,4]).reshape(2,2)
B1=np.array([-1,3,4,2]).reshape(2,2)
A1,B1,A1*B1,np.dot(A1,B1),np.dot(B1,A1)

A1=np.array([1,2,3,4])
B1=np.array([-1,3,4,2])
A1,B1,A1*B1,np.dot(A1,B1)

y=np.ones(4)
x.reshape(1,3), y

# ### Norm 
# pg 63
#
# <https://www.ime.unicamp.br/~valle/Teaching/MS211/Aula05.pdf>   - norma vetorial
#
#

# ### Norm L2
# \[3,-4]
#
# sqrt{3² + -4²}=5
#   

u=np.array([3,-4])
u,np.linalg.norm(u)

# ###Norm L1
#
# \[3,-4]
#
# |3|+|-4|=7
#

np.abs(u).sum()

# ### Frobenius norm 
# Aplica a L2 sobre Matrizes
#

# ### Exercícios pg 65
#
# #### Confrontar com https://hy38.github.io/D2L-2-linear-algebra
#

# #### 1. Prove that the transpose of a matrix A’s transpose is A : (𝐀T)⊤=A(𝐀^T)^⊤ = A(AT)⊤=A

A=np.arange(25).reshape(5,5)
AT=A.T
TA=AT.T
res=(A==TA).reshape(A.size)
#type(res),type(res.asnumpy()),
#np.diff(res,prepend=False, append=False)
np.count_nonzero(res%2==1)==A.size


#
# #### 2. Given two matrices A and B, show that the sum of transposes is equal to the transpose of a sum: A⊤+B⊤= (A+B)⊤
#

A1=np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)
A2=A1.copy()
B1=np.array([2,4,6,8,10,12,14,16,18]).reshape(3,3)
B2=B1.copy()
A1,A2,B1,B2

R1=A1.T + B1.T
R2=(A1+B1).T
res=(R1==R2).reshape(R1.size)
np.count_nonzero(res%2==1)==R1.size

# #### 3. Given any square matrixA, is A+A⊤ always symmetric? Why?
#

t1=A1+A1.T
t2=B1+B1.T
res1=(t1==t1.T).reshape(t1.size)
res2=(t2==t2.T).reshape(t2.size)
res1,res2
np.count_nonzero(res1%2==1)==res1.size , np.count_nonzero(res2%2==1)==res2.size

# #### 4. We defined the tensorX 
# of shape (2, 3, 4) in this section. What is the output oflen(X)?

T=np.ones((2,3,4))
T,T.size,T.shape

# #### 5. For a tensorXof arbitrary shape, does len(X) always correspond to the length of a certain axis ofX? What is that axis?
# first

# #### 6.Run A / A.sum(axis=1) 
# and see what happens. Can you analyze the reason?

t=A.sum(axis=1)
A,t,A/t

# #### 8. Consider a tensor with shape (2, 3, 4). What are the shapes of the summation outputs alongaxis 0, 1, and 2?

A=mx.nd.arange(1,25).reshape(2,3,4)
A,A.sum(0),A.sum(1),A.sum(2)

# #### 9. Feed a tensor with 3 or more axes to the linalg.norm function and observe its output. Whatdoes this function compute for tensors of arbitrary shape?
# https://www.educative.io/edpresso/what-is-the-nplinalgnorm-method-in-numpy
#

A=(np.arange(24)-5).reshape(2,3,4)
A , np.linalg.norm(A),np.linalg.norm(A,axis=0),np.linalg.norm(A,axis=1),np.linalg.norm(A,axis=2)



StarcoderdataJupyterScriptsDedupFiltered