機械学習プログラムで使用される一般的な推奨アルゴリズムの例

機械学習プログラムで使用される一般的な推奨アルゴリズムの例

推奨アルゴリズムは、機械学習とデータマイニングの分野の重要な部分であり、ユーザーにパーソナライズされた推奨を提供するために使用されます。 .NET では、さまざまなアルゴリズムを使用して推奨システムを実装できます。この記事では、協調フィルタリング、コンテンツ フィルタリング、ディープラーニング推奨システムという 3 つの一般的な推奨アルゴリズムを紹介し、対応する .NET ソース コードの例を示します。

協調フィルタリング推奨アルゴリズム

協調フィルタリング アルゴリズムは、ユーザーの行動データに基づいており、ユーザー間の類似性を分析することでユーザーに推奨コンテンツを提供します。一般的な協調フィルタリング アルゴリズムには、ユーザーベースの協調フィルタリングとアイテムベースの協調フィルタリングがあります。以下は、ユーザーベースの協調フィルタリングの .NET の例です。

 using System; using System.Collections.Generic; class CollaborativeFiltering { static void Main() { // 用户-物品评分矩阵Dictionary<string, Dictionary<string, double>> userItemRatings = new Dictionary<string, Dictionary<string, double>> { { "User1", new Dictionary<string, double> { { "Item1", 5.0 }, { "Item2", 3.0 } } }, { "User2", new Dictionary<string, double> { { "Item1", 4.0 }, { "Item3", 1.0 } } }, { "User3", new Dictionary<string, double> { { "Item2", 4.5 }, { "Item4", 2.0 } } } }; string targetUser = "User2"; string targetItem = "Item2"; // 计算与目标用户相似的其他用户var similarUsers = FindSimilarUsers(userItemRatings, targetUser); // 基于相似用户的评分预测double predictedRating = PredictRating(userItemRatings, similarUsers, targetUser, targetItem); Console.WriteLine($"预测用户{targetUser} 对物品{targetItem} 的评分为: {predictedRating}"); } static Dictionary<string, double> FindSimilarUsers(Dictionary<string, Dictionary<string, double>> userItemRatings, string targetUser) { Dictionary<string, double> similarUsers = new Dictionary<string, double>(); foreach (var user in userItemRatings.Keys) { if (user != targetUser) { double similarity = CalculateSimilarity(userItemRatings[targetUser], userItemRatings[user]); similarUsers.Add(user, similarity); } } return similarUsers; } static double CalculateSimilarity(Dictionary<string, double> ratings1, Dictionary<string, double> ratings2) { // 计算两个用户之间的相似性,可以使用不同的方法,如皮尔逊相关系数、余弦相似度等// 这里使用简单的欧氏距离作为示例double distance = 0.0; foreach (var item in ratings1.Keys) { if (ratings2.ContainsKey(item)) { distance += Math.Pow(ratings1[item] - ratings2[item], 2); } } return 1 / (1 + Math.Sqrt(distance)); } static double PredictRating(Dictionary<string, Dictionary<string, double>> userItemRatings, Dictionary<string, double> similarUsers, string targetUser, string targetItem) { double numerator = 0.0; double denominator = 0.0; foreach (var user in similarUsers.Keys) { if (userItemRatings[user].ContainsKey(targetItem)) { numerator += similarUsers[user] * userItemRatings[user][targetItem]; denominator += Math.Abs(similarUsers[user]); } } if (denominator == 0) { return 0; // 无法预测} return numerator / denominator; } }

この例では、ユーザーとアイテムの評価マトリックスを構築し、ユーザーベースの協調フィルタリング アルゴリズムを使用して、アイテムに対するユーザーの評価を予測します。まず、対象ユーザーに類似する他のユーザーを計算し、類似ユーザーの評価に基づいて予測を行います。

コンテンツフィルタリング推奨アルゴリズム

コンテンツ フィルタリング アルゴリズムは、アイテムの属性情報に基づいて、ユーザーの過去の好みに似たアイテムをユーザーに提供します。コンテンツ ベース フィルタリングの .NET の例を次に示します。

 using System; using System.Collections.Generic; class ContentFiltering { static void Main() { // 物品-属性矩阵Dictionary<string, Dictionary<string, double>> itemAttributes = new Dictionary<string, Dictionary<string, double>> { { "Item1", new Dictionary<string, double> { { "Genre", 1.0 }, { "Year", 2010.0 } } }, { "Item2", new Dictionary<string, double> { { "Genre", 2.0 }, { "Year", 2015.0 } } }, { "Item3", new Dictionary<string, double> { { "Genre", 1.5 }, { "Year", 2020.0 } } } }; string targetUser = "User1"; // 用户历史喜好List<string> userLikedItems = new List<string> { "Item1", "Item2" }; // 基于内容相似性的物品推荐var recommendedItems = RecommendItems(itemAttributes, userLikedItems, targetUser); Console.WriteLine($"为用户{targetUser} 推荐的物品是: {string.Join(", ", recommendedItems)}"); } static List<string> RecommendItems(Dictionary<string, Dictionary<string, double>> itemAttributes, List<string> userLikedItems, string targetUser) { Dictionary<string, double> itemScores = new Dictionary<string, double>(); foreach (var item in itemAttributes.Keys) { if (!userLikedItems.Contains(item)) { double similarity = CalculateItemSimilarity(itemAttributes, userLikedItems, item, targetUser); itemScores.Add(item, similarity); } } // 根据相似性得分排序物品var sortedItems = itemScores.OrderByDescending(x => x.Value).Select(x => x.Key).ToList(); return sortedItems; } static double CalculateItemSimilarity(Dictionary<string, Dictionary<string, double>> itemAttributes, List<string> userLikedItems, string item1, string targetUser) { double similarity = 0.0; foreach (var item2 in userLikedItems ) { similarity += CalculateJaccardSimilarity(itemAttributes[item1], itemAttributes[item2]); } return similarity; } static double CalculateJaccardSimilarity(Dictionary<string, double> attributes1, Dictionary<string, double> attributes2) { // 计算Jaccard相似性,可以根据属性值的相似性定义不同的相似性度量方法var intersection = attributes1.Keys.Intersect(attributes2.Keys).Count(); var union = attributes1.Keys.Union(attributes2.Keys).Count(); return intersection / (double)union; } }

この例では、アイテム属性マトリックスを構築し、コンテンツベースのフィルタリング アルゴリズムを使用してユーザーにアイテムを推奨します。アイテム間の類似性を計算し、ユーザーの過去の嗜​​好に基づいて類似アイテムを推奨しました。

ディープラーニングレコメンデーションシステム

ディープラーニング推奨システムは、ニューラル ネットワーク モデルを使用して、ユーザーとアイテム間の複雑な関係を学習し、より正確なパーソナライズされた推奨を提供します。以下は、PyTorch ライブラリを使用してシンプルなディープラーニング推奨システムを構築する方法を示した .NET の例です。

 // 请注意,此示例需要安装PyTorch.NET库using System; using System.Linq; using Python.Runtime; using torch = Python.Runtime.Torch; class DeepLearningRecommendation { static void Main() { // 启动Python运行时using (Py.GIL()) { // 创建一个简单的神经网络模型var model = CreateRecommendationModel(); // 模拟用户和物品的数据var userFeatures = torch.tensor(new double[,] { { 0.1, 0.2 }, { 0.4, 0.5 } }); var itemFeatures = torch.tensor(new double[,] { { 0.6, 0.7 }, { 0.8, 0.9 } }); // 计算用户和物品之间的交互var interaction = torch.mm(userFeatures, itemFeatures.T); // 使用模型进行推荐var recommendations = model.forward(interaction); Console.WriteLine("推荐得分:"); Console.WriteLine(recommendations); } } static dynamic CreateRecommendationModel() { using (Py.GIL()) { dynamic model = torch.nn.Sequential( torch.nn.Linear(2, 2), torch.nn.ReLU(), torch.nn.Linear(2, 1), torch.nn.Sigmoid() ); return model; } } }

この例では、PyTorch.NET ライブラリを使用して、推奨用のシンプルなニューラル ネットワーク モデルを作成しました。ユーザーとアイテムの特徴データをシミュレートし、ユーザーとアイテム間の相互作用を計算しました。最後に、モデルを使用して推奨事項を作成します。

この記事では、協調フィルタリング、コンテンツ フィルタリング、ディープラーニング推奨システムなど、3 つの一般的な推奨アルゴリズムの例を示します。これらのアルゴリズムを .NET 環境に実装すると、開発者はさまざまな種類の推奨システムを理解し、ユーザーにパーソナライズされた推奨を提供できるようになります。これらのサンプル コードは、さまざまなアプリケーション シナリオのニーズを満たす、より複雑な推奨システムを構築するための出発点として役立ちます。これらの例が役に立つことを願っています。

<<:  DrivingDiffusion: 最初のサラウンドワールド モデル: BEV データとシミュレーションの新しいアイデア!

>>: 

ブログ    
ブログ    
ブログ    

推薦する

AIが臨床試験でスムーズな患者マッチングを実現する方法

新型コロナウイルス感染症のパンデミックは、がんとの戦いにおいて医療界に重要な教訓を浮き彫りにした。そ...

...

GPT-4+物理エンジンは拡散モデルをサポートし、現実的で一貫性のある合理的なビデオを生成します。

拡散モデルの出現により、テキスト生成ビデオ技術の開発が促進されましたが、このような方法は通常、計算コ...

Google は 1,000 以上の「ロングテール」言語に対応する機械翻訳システムを開発、すでにいくつかのニッチ言語をサポート

学術および商用の機械翻訳 (MT) システムの品質は、過去 10 年間で劇的に向上しました。これらの...

Facebookはライブ動画でユーザーを見えなくする匿名化システムを開発した

最近、ノルウェー科学技術大学の「DeepPrivacy: 顔の匿名化のための生成的敵対的ネットワーク...

GenAI 時代の 12 の新しい仕事

GenAI は人間に取って代わるのではなく、熟練労働者、つまり GenAI を管理し最大限に活用する...

GPT-4V でさえ解明できない未来推論の解決策があります!華中科技大学と上海理工大学出身

マルチモーダル大規模言語モデルは、強力な画像理解および推論機能を発揮します。しかし、現在の観察に基づ...

ガートナー:世界の AI PC と生成 AI スマートフォンの出荷台数は 2024 年に 2 億 9,500 万台に達すると予測

ガートナーの最新予測によると、人工知能(AI)パーソナルコンピュータ(PC)と生成型人工知能(ジェネ...

...

知識抽出についてお話ししましょう。学びましたか?

1. はじめに知識抽出とは通常、豊富な意味情報を持つタグやフレーズなどの非構造化テキストから構造化...

...

cnBeta は、開発者が AI アプリケーションを構築するのに役立つ 3 つの新しい機械学習ツールをリリースしました。

TechCrunchのウェブサイト、北京時間9月25日によると、多くの競合他社と同様に、Micro...

...

シリコンバレーの人工知能専門家:人類は20年以内に老化の束縛から解放されるかもしれない

現在、世界最高齢の人は、ギネス世界記録に認定された118歳の日本人老人、田中カネさんです。田中選手の...

デジタル変革、人工知能、そして生産性の問題

企業がデジタル変革を進める際に、生成 AI がいかにして企業の生産性を向上させることができるかについ...