Building a Transition Matrix using words in Python/Numpy

后端 未结 6 1757
抹茶落季
抹茶落季 2020-12-09 22:32

Im trying to build a 3x3 transition matrix with this data

days=[\'rain\', \'rain\', \'rain\', \'clouds\', \'rain\', \'sun\', \'clouds\', \'clouds\', 
  \'rai         


        
6条回答
  •  鱼传尺愫
    2020-12-09 23:30

    1. Convert the reports from the days into index codes.
    2. Iterate through the array, grabbing the codes for yesterday's weather and today's.
    3. Use those indices to tally the combination in your 3x3 matrix.

    Here's the coding set-up to get you started.

    report = [
      'rain', 'rain', 'rain', 'clouds', 'rain', 'sun', 'clouds', 'clouds', 
      'rain', 'sun', 'rain', 'rain', 'clouds', 'clouds', 'sun', 'sun', 
      'clouds', 'clouds', 'rain', 'clouds', 'sun', 'rain', 'rain', 'sun',
      'sun', 'clouds', 'clouds', 'rain', 'rain', 'sun', 'sun', 'rain', 
      'rain', 'sun', 'clouds', 'clouds', 'sun', 'sun', 'clouds', 'rain', 
      'rain', 'rain', 'rain', 'sun', 'sun', 'sun', 'sun', 'clouds', 'sun', 
      'clouds', 'clouds', 'sun', 'clouds', 'rain', 'sun', 'sun', 'sun', 
      'clouds', 'sun', 'rain', 'sun', 'sun', 'sun', 'sun', 'clouds', 
      'rain', 'clouds', 'clouds', 'sun', 'sun', 'sun', 'sun', 'sun', 'sun', 
      'clouds', 'clouds', 'clouds', 'clouds', 'clouds', 'sun', 'rain', 
      'rain', 'rain', 'clouds', 'sun', 'clouds', 'clouds', 'clouds', 'rain', 
      'clouds', 'rain', 'sun', 'sun', 'clouds', 'sun', 'sun', 'sun', 'sun',
      'sun', 'sun', 'rain']
    
    weather_dict = {"sun":0, "clouds":1, "rain": 2}
    weather_code = [weather_dict[day] for day in report]
    print weather_code
    
    for n in range(1, len(weather_code)):
        yesterday_code = weather_code[n-1]
        today_code     = weather_code[n]
    
    # You now have the indicies you need for your 3x3 matrix.
    

提交回复
热议问题