hello guass

编写不易如果觉得不错,麻烦关注一下~

参考:

https://blog.csdn.net/weixin_39813574/article/details/111015907

https://www.cnblogs.com/bingjianing/p/9117330.html

https://www.cnblogs.com/yanshw/p/11898408.html

https://blog.csdn.net/daniaokuye/article/details/116272253

1.求均值与协方差,下面是二维正太分布

>>> time=[1,2,3,4,5,6]
>>> cost=[4,5,3,4,8,9]
>>> import numpy as np
>>> np.array([time,cost])
array([[1, 2, 3, 4, 5, 6],
       [4, 5, 3, 4, 8, 9]])
>>> np.array([time,cost]).T
array([[1, 4],
       [2, 5],
       [3, 3],
       [4, 4],
       [5, 8],
       [6, 9]])
>>> var_matrix=np.array([time,cost]).T
>>> var_matrix
array([[1, 4],
       [2, 5],
       [3, 3],
       [4, 4],
       [5, 8],
       [6, 9]])
>>> mean = np.mean(var_matrix,axis=0)
>>> mean
array([3.5, 5.5])
>>> var_matrix.shape
(6, 2)
>>> sigma = np.cov(var_matrix.T)
>>> sigma
array([[3.5, 3.5],
       [3.5, 5.9]])

2.三维截图(应该默认高维可以!)

下面搜寻有没有直接求取多元高斯或正态分布密度函数的函数!

多元高斯分布密度函数

上式为 x 服从 k 元正态分布,x 为 k 维向量;|Σ| 代表协方差矩阵的行列式

下面测试了(0,0,0)点,和均值点的密度。

X 为3维 6个数据样本, mean 为均值,D为样本个数,利用最后一个式子可以直接求出协方差矩阵这里使用了样本数-1 作为分母

>>> X
tensor([[1., 2., 3., 4., 5., 6.],
        [4., 5., 3., 4., 8., 9.],
        [2., 2., 2., 2., 3., 3.]])
>>> ttt
tensor([[3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
        [5.5000, 5.5000, 5.5000, 5.5000, 5.5000, 5.5000],
        [2.3330, 2.3330, 2.3330, 2.3330, 2.3330, 2.3330]])
>>> D
6
>>> 1/(D-1)*((X-ttt)@(X-ttt).t())
tensor([[3.5000, 3.5000, 0.8000],
        [3.5000, 5.9000, 1.2000],
        [0.8000, 1.2000, 0.2667]])

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream>#include <cstdlib>#include <ctime>#include <cstring>using namespace std;const int MAX_WRONG = 6; //最大错误次数string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"}; //单词库int main(){ srand(time(0)); //用当前时间作为随机数种子 char play; //玩家选择是否继续游戏 do { string word = WORDS[rand() % 5]; //从单词库中随机选择一个单词 int wrong = 0; //错误次数 string soFar(word.size(), '_'); //初始时,所有字母都用"_"代替 string used = ""; //已经猜过的字母 cout << "Welcome to Hangman. Good luck!\n"; //循环直到猜出单词或者错误次数达到最大值 while (wrong < MAX_WRONG && soFar != word) { cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char guess; //玩家猜测的字母 cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); //将小写字母转换成大写字母 while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; if (word.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < word.size(); ++i) { if (word[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } } if (wrong == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << word << endl; cout << "\nDo you want to play again? (y/n): "; cin >> play; play = toupper(play); } while (play == 'Y'); return 0;}优化这段代码
最新发布
05-25
以下是优化后的代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <algorithm> #include <cctype> using namespace std; const int MAX_WRONG = 6; const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"}; int main() { srand(time(nullptr)); // 使用nullptr代替0作为空指针常量 char play = 'y'; while (tolower(play) == 'y') { // 使用tolower将玩家输入转换成小写字母 string word = WORDS[rand() % 5]; int wrong = 0; string soFar(word.size(), '_'); string used; cout << "Welcome to Hangman. Good luck!\n\n"; while (wrong < MAX_WRONG && soFar != word) { cout << "You have " << MAX_WRONG - wrong << " incorrect guesses left.\n"; cout << "You've used the following letters:\n" << used << endl; cout << "So far, the word is:\n" << soFar << endl; char guess; cout << "\nEnter your guess: "; cin >> guess; guess = toupper(guess); if (used.find(guess) != string::npos) { // 使用find函数代替while循环 cout << "You've already guessed " << guess << endl; continue; } used += guess; if (word.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < word.size(); ++i) { if (word[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } } if (wrong == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << word << endl; cout << "Do you want to play again? (y/n): "; cin >> play; } return 0; } ``` 主要的改进点如下: 1. 使用nullptr代替0作为空指针常量,使用tolower将玩家输入转换成小写字母。 2. 使用find函数代替while循环,优化猜测重复字母的判断逻辑。 3. 化繁为简,去掉一些不必要的输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值