Telegram bot + ML:ユニバーサルマッチングアルゴリズム

Kaggleケースのモデルを作成し、見知らぬ人を研究してインスピレーションを得ます。フロントエンドジュニア学生である私にとって、Webプロジェクトにそれらを実装する方法を説明するすべての記事は、複雑な情報のオーバーヘッドを提供しますが、クールなモデルを「借用」して、サービスにすばやく実装したいと思います。手がかゆくて普遍的なアルゴリズムを思いついたので、解決策がすぐに見つかりました。





始めましょう。ステップ1

ほとんどのモデルのフォーマットをKaggleから取得したいので、将来、他の誰かの複雑なコードを理解せずに簡単に借りることができます。ボートのためにカートはで書くのPython 3.9 liby使用して、pyTelegramBotAPIの.pyと.ipynb yuzaemの拡張機能の互換性に対処するために、ipynb





したがって、依存関係をインストールします。





pip install pyTelegramBotAPI
pip install ipynb
      
      



Kaggleに移動し、好きなモデルを選択します。古典的なタイタニックのケースである災害からの機械学習から始め、このソリューション(タイタニックのランダムフォレスト:82.78%)を借りて、ボットプロジェクトにドラッグします。





表示された依存関係をインストールします。





pip install <>
      
      



ボットを見ました。ステップ2

新しいファイルを作成し、ライブラリをそのファイルにインポートします。





import telebot
from ipynb.fs.defs.ml import is_user_alive
      
      



ipynb.fs.defs.ml



ml , , is_user_alive



, . , .





, ( @BotFather):





bot = telebot.TeleBot('token')
      
      



/start



, . , .





@bot.messagehandler(commands=['start'])
def welcome(message):    
  bot.sendmessage(message.chat.id, '!   ,  ( ), '
                  ' (male/female), ,      (1-, 0-), '
                  '  (1-, 0-),  ,     '
                  ', -   ,      !')
      
      



, . :





@bot.messagehandler(contenttypes=['text'])
def answer(message):    
  bot.sendmessage(message.chat.id, '…')    
  passengerdata = message.text.split()    
  passengerdata.insert(0, 0)    
  passengerdata.insert(9, ',')    
  passengerdata[2] = '"', passengerdata[2], '"'
      
      



, , 9 , .





. 3

cntrl+f



#%%



. . :





<>

def is_user_alive(user_data):
  <  >
      
      



- , :





with open(os.path.join('input', 'test.csv'), "a") as fp:    
  wr = csv.writer(fp, dialect='excel')    
  wr.writerow(user_data)
      
      



Predictions



( ). , (return



):





return predictions[len(predictions)-1:]['Survived']
      
      



. 4

answer



, 0 1… . , :





answer = is_user_alive(passenger_data)

if int(answer) == 1:    
  bot.sendmessage(message.chat.id, '! ,      .')
 elif int(answer) == 0:    
  bot.send_message(message.chat.id, ',    …   .')
      
      



他の誰かをチェックする提案を使用して関数を作成answer



し、message



引数を使用して呼び出します。





def doagain(message):    
  bot.sendmessage(message.chat.id, '  - ?')
      
      



ポーリングを開始します。





while True:    
  try:        
    bot.polling(none_stop=True)    
  except ():        
    time.sleep(5)
      
      



結果

すべて!とても簡単ですよね?

そうでない場合は、ビデオバージョンを見ることができます:





コード:https://github.com/freakssha/ml-bot-titanic





これはかゆみを伴う手のスピードランであり、最適化されておらず、大幅に改善することができますが、その方法はまだわかりません。シンプルさと汎用性を失うことなくこれを行う方法を理解している場合は、書いてください!





GitHubInstVK








All Articles