Linear algebra, useful command for python

Useful command for python summary

Linear algebra

That is, we transform x⃗ by B and then transform that resulting vector by A much as we would with the nested function f(g(x⃗ )) .
A.dot(B).dot(a) / A.dot(B.dot(x))

def cosine(a,b): cos = np.dot(a,b)/(np.sqrt(np.dot(a,a)) * np.sqrt(np.dot(b,b)) ) return cos

a_norm = 1/np.sqrt(np.dot(a,a))*a

OLS Construction

u = x matrix
v = y matrix

N = U.shape[0]
X = np.column_stack((np.ones(N),U)) # Add a constant (column vector of ones)
B = la.inv(X.T.dot(X)).dot(X.T.dot(v)).round(2)

#Calculate the R Squared
v_hat = X.dot(B)
v_hat_centered = v_hat - v_hat.mean()
v_centered = v - v.mean()

r_squared = v_hat_centered.dot(v_hat_centered)/v_centered.dot(v_centered)
r_squared.round(2)

#sigma
n = X.shape[0]
p = X.shape[1]
sigma2 = e.T.dot(e)/(n-p)

cov_B = sigma2*la.inv(X.T.dot(X))

Eigenvector

#Draw out the relevant items.
items = anes[anes.columns[anes.columns.str.contains(‘V’)]]

#I am also going to rescale the variables just so they have the same mean and variance
#this makes the different thermometer metrics comparable.
items = items.apply(lambda x: (x-x.mean())/(x.std()),axis=0)
#Generate a correlation matrix
p = items.corr()
#Decompose
evals,evecs = la.eig§

variance_explained = evals/sum(evals)

print(‘Proportion of Variation Explained’)
for i,val in enumerate(variance_explained):
print(f’’’
Eigenvalue {i+1} accounts for {round(val*100,2)}% of the variance
‘’’)

#select first 6 eigenvalues
weights = evecs[:,[0,1,2,3,4,5]]

#values of the subsetted data
X = items.values

#reducing data by weighting the data with our chosen eigenvalues
reduced_data = X.dot(weights)

#Convert data pandas data frame
reduced_data = pd.DataFrame(reduced_data,columns=[f"comp_{i+1}" for i in range(6)])

#Bind onto original data
D = pd.concat([anes[[‘respondent_id’,‘party_id’]],reduced_data],axis=1).dropna()

#View the head of the data
D.head()

g = sns.pairplot(D[[‘comp_1’,‘comp_2’,‘comp_3’,‘comp_4’,‘comp_5’,‘comp_6’,‘party_id’]],hue=“party_id”,height=4)

f, axes = plt.subplots(1, 3,figsize=(15,5))
i= sns.boxplot(y=‘comp_1’, x=‘party_id’, data=D,ax=axes[0])
i= sns.boxplot(y=‘comp_2’, x=‘party_id’, data=D,ax=axes[1])
i= sns.boxplot(y=‘comp_3’, x=‘party_id’, data=D,ax=axes[2])

"linear algebra and optimization for machine learning" csdn 是关于机器学习中的线性代数和优化的主题的博客文章。 线性代数在机器学习中起着重要的作用。它提供了一种处理数据的有效方法,可以用于解决许多复杂的问题。线性代数的主要工具之一是矩阵,它可以用来表示数据和变换。在机器学习中,我们经常使用矩阵来表示特征和样本,进行数据的转换和降维。线性代数还为我们提供了诸如特征值和特征向量等重要概念,这些概念在机器学习中具有广泛的应用。 优化是机器学习中的另一个重要主题。机器学习算法通常涉及到最小化或最大化一个目标函数,以此来找到最优的模型参数。而优化算法可以帮助我们在复杂的参数空间中搜索最优解。通过使用优化算法,我们可以有效地求解机器学习问题,例如回归、分类和聚类等。 "linear algebra and optimization for machine learning" csdn 的文章会深入探讨线性代数和优化在机器学习中的应用。它会介绍线性代数的基本概念,如矩阵运算、特征值和特征向量等,并说明它们在机器学习中的具体应用。同时,它还会介绍一些常用的优化算法,如梯度下降法和牛顿法等,并解释它们在机器学习中的作用。通过阅读这篇文章,读者可以更好地理解线性代数和优化在机器学习中的重要性,以及如何应用它们来解决实际的机器学习问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值