当前位置:网站首页>"Weilai Cup" 2022 Niuke summer multi school training camp 2 i.[let fat tension] matrix multiplication j.[link with arithmetic progression] linear regression
"Weilai Cup" 2022 Niuke summer multi school training camp 2 i.[let fat tension] matrix multiplication j.[link with arithmetic progression] linear regression
2022-07-26 01:30:00 【HeartFireY】
I. let fat tension
Topic analysis
Definition l e ( i , j ) le(i, j) le(i,j) by X i , X j X_i, X_j Xi,Xj Cosine similarity between :
l e ( i , j ) = X i ⋅ X j ∣ X i ∣ ∣ X j ∣ le(i, j) = \frac{X_i \cdot X_j}{|X_i||X_j|} le(i,j)=∣Xi∣∣Xj∣Xi⋅Xj
Ask for novelty Y ′ Y' Y′, Y i n e w = ∑ j = 1 n l e ( i , j ) × Y j Y_i^{new} = \sum^n_{j = 1}le(i, j) \times Y_j Yinew=∑j=1nle(i,j)×Yj.
It is found that violent calculation obviously does not work , But it can be transformed into matrix operation , Divide by the module length to normalize each row of the matrix , So the answer is :
X ′ X ′ T Y X'X'^TY X′X′TY
X ′ X' X′ Is the matrix normalized by rows .
Then you can get the operation process : ( n × k ) × ( k × n ) × ( n × d ) (n\times k) \times (k \times n) \times(n \times d) (n×k)×(k×n)×(n×d).
It is found that the multiplication between the latter two matrices is done first , Complexity O ( n k d ) O(nkd) O(nkd). Then calculate directly .
Code
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
//#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
double X[10010][100], XT[100][10010], Y[10010][100], ans1[100][100], ans2[10010][100];
inline void solve(){
int n, k, d; cin >> n >> k >> d;
double sum = 0;
for(int i = 1; i <= n; i++) for(int j = 1; j <= k; j++)
cin >> X[i][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
cin >> Y[i][j];
for(int i = 1; i <= n; i++){
sum = 0;
for(int j = 1; j <= k; j++) sum += X[i][j] * X[i][j];
sum = sqrt(sum);
for(int j = 1; j <= k; j++) X[i][j] = XT[j][i] = X[i][j] / sum;
}
for(int i = 1; i <= k; i++) for(int j = 1; j <= d; j++)
for(int t = 1; t <= n; t++) ans1[i][j] += XT[i][t] * Y[t][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
for(int t = 1; t <= k; t++) ans2[i][j] += X[i][t] * ans1[t][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
cout << ans2[i][j] << " \n"[j == d];
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
J.Link with Arithmetic Progression Linear regression
Topic analysis
Give a sequence a [ i ] a[i] a[i], structure b [ i ] b[i] b[i], bring ∑ ( a [ i ] − b [ i ] ) 2 \sum(a[i] - b[i])^2 ∑(a[i]−b[i])2 Minimum , For the minimum .
It is easy to find that it is a linear regression problem , So consider using machine learning High school math Knowledge solutions for .
According to Gauss - Markov Theorem : Given the assumption of classical linear regression , The least squares estimator is a linear unbiased estimator with minimum variance .
For the function f ( X ) = w 1 x 1 + w 2 x 2 + ⋯ + w m x m f(X) = w_1x_1 + w_2x_2 + \dots + w_m x_m f(X)=w1x1+w2x2+⋯+wmxm, Define its objective function as J ( w ) = ∑ i = 1 n [ y i − y i ^ ] 2 J(w) = \sum^{n}_{i = 1}[y_i - \hat{y_i}]^2 J(w)=∑i=1n[yi−yi^]2.
Bring in the linear regression formula :
b = n ∑ i = 1 n x i y i − ( ∑ i = 1 n x i ) ( ∑ i = 1 n y i ) n ∑ i = 1 n x i 2 − ∑ i = 1 n x i a = y ˉ − b x ˉ b = \frac{n \sum^{n}_{i = 1}x_iy_i - (\sum^{n}_{i = 1}x_i)(\sum^{n}_{i = 1}y_i)}{n \sum^{n}_{i = 1}x_i^2 - \sum^{n}_{i = 1}x_i}\\ a = \bar{y} - b\bar{x} b=n∑i=1nxi2−∑i=1nxin∑i=1nxiyi−(∑i=1nxi)(∑i=1nyi)a=yˉ−bxˉ
Then calculate the error output .
Code
#include <bits/stdc++.h>
#pragma gcc optimize(2)
#define int long long
#define double long double
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
inline int read(){
int f = 1, x = 0; char s = getchar();
while(s < '0'||s > '9'){
if(s == '-') f = -1; s = getchar(); }
while(s >= '0' && s <= '9'){
x = x * 10 + s - '0'; s = getchar();}
return x *= f;
}
int a[N];
inline void solve(){
int n = read();
for(int i = 1; i <= n; i++) a[i] = read();
double sumx = 0, sumy = 0, da = 0, dc = 0;
for(int i = 1; i <= n; i++){
sumx += i, sumy += a[i];
da += i * a[i], dc += i * i;
}
double db = sumx * sumy / n, dd = sumx * sumx / n, ans = 0;
double B = (da - db)/(dc - dd);
double A = sumy / n - B * sumx / n;
for(int i = 1; i <= n; i++) ans += (B * i + A - a[i]) * (B * i + A - a[i]);
cout << ans << endl;
}
signed main(){
//ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
cout << fixed << setprecision(12);
while(t--) solve();
return 0;
}
边栏推荐
- C语言中的整型数据类型(你真的了解吗)
- The second China rust developer conference is coming, and the complete agenda has been exposed!
- Tutorial on principles and applications of database system (056) -- MySQL query (18): usage of other types of functions
- zeromq浅析
- Oracle is nested at multiple levels, and the alias problem of the table cannot be found
- U++ learning notes ustruct, uenum declaration and function library simple function implementation
- Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
- “蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
- PTGui Pro12垂直线纠正
- MDK编译过程及ARM编译工具链
猜你喜欢

Redis数据结构详解,结合书本

Docker advanced -mysql master-slave replication

zeromq浅析

【数据挖掘】生成模型和判别模型的区别及优缺点

NIO简易示例

Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?

NLP introduction + practice: Chapter 3: gradient descent and back propagation

Sqli-labs Less7

Handler消息机制-FWK层

聚势|海泰方圆亮相第五届数字中国建设峰会
随机推荐
数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
2022年最新北京建筑八大员(材料员)模拟考试试题及答案
[go] III. The simplest restful API server
旅行 (拆点分层)
【Go】如何控制协程的最大并发数
MDK compilation process and arm compilation tool chain
Practice sharing of monorepo based on yarn1.x
I just test it
What should I do when my blog is attacked by hackers?
“元气可乐”不是终点,“中国可乐”才是
Iftnews | suppose this is what the metauniverse looks like 20 years later
[combinational logic circuit] - encoder
Web middleware log analysis script 3.0 (shell script)
U++学习笔记 UStruct、UEnum声明以及函数库简单函数实现
Basic version of Google browser debugging tool (I)
PyCharm在创建py文件时自动添加头部注释
销量连连夺冠,五菱的成功秘诀只有低价吗?
NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南
Failed to load DLL
Oracle - isupplier portal Invoicing error