当前位置: 首页>>代码示例>>Python>>正文


Python Weather.getHourlyWeather方法代码示例

本文整理汇总了Python中weather.Weather.getHourlyWeather方法的典型用法代码示例。如果您正苦于以下问题:Python Weather.getHourlyWeather方法的具体用法?Python Weather.getHourlyWeather怎么用?Python Weather.getHourlyWeather使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weather.Weather的用法示例。


在下文中一共展示了Weather.getHourlyWeather方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FeatureExtractor

# 需要导入模块: from weather import Weather [as 别名]
# 或者: from weather.Weather import getHourlyWeather [as 别名]
class FeatureExtractor(object):

    def __init__(self, use_sparse):
        self.config = ConfigParser.RawConfigParser()
        self.config.read(util.FEATURES_FILE)
        self.vectorizer = DictVectorizer(sparse=use_sparse)

        if self.config.getboolean(FEATURE_SELECTION, 'Cluster'):
            # Vectorizer without cluster features.
            self.precluster_vectorizer = DictVectorizer(sparse=use_sparse)
            self.clusterer = MiniBatchKMeans(n_clusters=15, init='k-means++')
        if self.config.getboolean(FEATURE_SELECTION, 'DailyWeather') or \
                self.config.getboolean(FEATURE_SELECTION, 'HourlyWeather') or \
                self.config.getboolean(FEATURE_SELECTION, 'Zone_DayOfWeek_HourlyWeather'):
            self.weather_data = Weather()

        if util.VERBOSE:
            feature_list = self.getFeatureList()
            print 'Feature Template List:'
            for feature in feature_list:
                print '\t%s' % feature

    def getFeatureList(self):
        feature_list = []
        for feature in self.config.options(FEATURE_SELECTION):
            if self.config.getboolean(FEATURE_SELECTION, feature):
                feature_list.append(feature)
        return feature_list

    def getFeatureVectors(self, X, is_test=False):
        '''
        Transform input list of training examples from a list of dicts to a
        numpy array or scipy sparse matrix for input into an sklearn model.

        :param X: a list of training examples, represented as a list of dicts
                  where each dict maps column names to column values.

        :param use_sparse: boolean for whether the return value should be
                           represented as a sparse matrix.

        :return: scipy matrix representing the training data.
        '''
        feature_dicts = [self.getFeatureDict(x) for x in X]

        # If clustering is enabled, compute the centroids, then
        # append the nearest centroid ID to each feature vector.
        if self.config.getboolean(FEATURE_SELECTION, 'Cluster'):
            self._appendClusterFeatures(feature_dicts, is_test)
        transformed = self.vectorizer.transform(feature_dicts) \
            if is_test \
            else self.vectorizer.fit_transform(feature_dicts)

        return transformed

    def getFeatureNameIndices(self):
        """
        :return: dict mapping feature names to indices.
        """
        return self.vectorizer.vocabulary_

    def _extractZone(self, x, feature_dict):
        feature_dict['Zone'] = str(x['zone_id'])

    def _extractHourOfDay(self, x, feature_dict):
        # Pad hours < 10 with a leading zero.
        feature_dict['HourOfDay'] = '%02d' % x['start_datetime'].hour

    def _extractDayOfWeek(self, x, feature_dict):
        # Pad day of week with a leading zero.
        feature_dict['DayOfWeek'] = '%02d' % x['start_datetime'].weekday()

    def _extractZoneHourOfDay(self, x, feature_dict):
        feature_dict['Zone_HourOfDay'] = '%d_%02d' % (x['zone_id'], x['start_datetime'].hour)

    # Concatenate the zone, day of week, and hour of day.
    def _extractZoneDayHour(self, x, feature_dict):
        # Pad day of week with a leading zero.
        # Pad hours < 10 with a leading zero.
        feature_dict['Zone_DayOfWeek_Hour'] = '%d_%02d_%02d' % \
            (x['zone_id'], x['start_datetime'].weekday(), x['start_datetime'].hour)

    def _extractZoneDayOfWeekHourlyWeather(self, x, feature_dict):
        hourly_weather = self.weather_data.getHourlyWeather(x['start_datetime'])
        rainfallValue = self._getHourlyRainfallValue(hourly_weather['PRCP'], True)
        feature_dict['Zone_DayOfWeek_HourlyRainfall'] = '%d_%02d_%s' % \
            (x['zone_id'], x['start_datetime'].weekday(), rainfallValue)

    def _extractCluster(self, x, feature_dict):
        feature_dict['Cluster'] = str(x['cluster_id'])

    def _extractDailyWeather(self, x, feature_dict):
        daily_weather = self.weather_data.getWeather(x['start_datetime'])
        feature_dict['DailyRainfall'] = self._getDailyRainfallValue(daily_weather['PRCP'])

    def _getDailyRainfallValue(self, rainfall):
        if rainfall == 0:
            return 'No_rainfall'
        elif rainfall < 100:
            return 'Less_than_1_inch'
        else:
#.........这里部分代码省略.........
开发者ID:vivekchoksi,项目名称:taxi-pickups,代码行数:103,代码来源:feature_extractor.py


注:本文中的weather.Weather.getHourlyWeather方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。