repo_name
stringlengths
6
112
path
stringlengths
4
204
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
714
810k
license
stringclasses
15 values
kedz/cuttsum
trec2015/sbin/apsal/make-2015-strat.py
1
4766
import cuttsum.events import cuttsum.corpora from cuttsum.pipeline import InputStreamResource from cuttsum.classifiers import NuggetRegressor import cuttsum.judgements import pandas as pd import numpy as np from datetime import datetime from cuttsum.misc import event2semsim from sklearn.cluster import AffinityPropagation from collections import defaultdict from sklearn.metrics.pairwise import cosine_similarity from cuttsum.misc import event2lm_name import math def sigmoid(x): return 1. / (1. + math.exp(-x)) lm2thr = { "accidents-lm": 0.65, "natural_disaster-lm": 0.10, "social_unrest-lm": 0.65, "terrorism-lm": 0.40, } matches_df = cuttsum.judgements.get_merged_dataframe() def get_input_stream(event, gold_probs, extractor="goose", thresh=.8, delay=None, topk=20): max_nuggets = 3 corpus = cuttsum.corpora.get_raw_corpus(event) res = InputStreamResource() df = pd.concat( res.get_dataframes(event, corpus, extractor, thresh, delay, topk)) selector = (df["n conf"] == 1) & (df["nugget probs"].apply(len) == 0) df.loc[selector, "nugget probs"] = df.loc[selector, "nuggets"].apply(lambda x: {n:1 for n in x}) df["true probs"] = df["nugget probs"].apply(lambda x: [val for key, val in x.items()] +[0]) df["true probs"] = df["true probs"].apply(lambda x: np.max(x)) df.loc[(df["n conf"] == 1) & (df["nuggets"].apply(len) == 0), "true probs"] = 0 if gold_probs is True: df["probs"] = df["true probs"] else: df["probs"] = NuggetRegressor().predict(event, df) df["nuggets"] = df["nugget probs"].apply( lambda x: set([key for key, val in x.items() if val > .9])) nid2time = {} nids = set(matches_df[matches_df["query id"] == event.query_id]["nugget id"].tolist()) for nid in nids: ts = matches_df[matches_df["nugget id"] == nid]["update id"].apply(lambda x: int(x.split("-")[0])).tolist() ts.sort() nid2time[nid] = ts[0] fltr_nuggets = [] for name, row in df.iterrows(): fltr_nuggets.append( set([nug for nug in row["nuggets"] if nid2time[nug] <= row["timestamp"]])) #print df[["nuggets", "timestamp"]].apply(lambda y: print y[0]) # datetime.utcfromtimestamp(int(y["timestamp"]))) #print nids df["nuggets"] = fltr_nuggets df["nuggets"] = df["nuggets"].apply(lambda x: x if len(x) <= max_nuggets else set([])) return df event2nuggets = defaultdict(set) event2size = {} all_results = [] data = [] with open("apsal.strat.tsv", "w") as o, open("apsal.strat.sum.tsv", "w") as sumo: for event in cuttsum.events.get_events(): if event.query_num < 26: continue istream = get_input_stream(event, False) with open("clusters-2015/{}.tsv".format(event.query_id), "r") as f: df = pd.read_csv(f, sep="\t", converters={"stems": eval, "nuggets": eval}) thresh = lm2thr[event2lm_name(event)] #thresh = .7 cache = None semsim = event2semsim(event) results = [] for ts, batch in df.groupby("timestamp"): X = semsim.transform(batch["stems"].apply(lambda x: ' '.join(x)).tolist()) for i, (_, row) in enumerate(batch.iterrows()): if cache is None: cache = X[i] results.append(row.to_dict()) all_results.append(row.to_dict()) else: K = cosine_similarity(cache, X[i]) if (K < thresh).all(): cache = np.vstack([cache, X[i]]) results.append(row.to_dict()) all_results.append(row.to_dict()) for result in results: print "{}\t{}\t{}\t{}\t{}\t{}\t{}".format( event.query_num, "cunlp", "4APSAL", "-".join(result["update id"].split("-")[:2]), result["update id"].split("-")[-1], result["timestamp"], sigmoid(result["probs"])) o.write("{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format( event.query_num, "cunlp", "4APSAL", "-".join(result["update id"].split("-")[:2]), result["update id"].split("-")[-1], result["timestamp"], sigmoid(result["probs"]))) sumo.write("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format( event.query_num, "cunlp", "4APSAL", "-".join(result["update id"].split("-")[:2]), result["update id"].split("-")[-1], result["timestamp"], sigmoid(result["probs"]), result["sent text"])) #pd.DataFrame(results, columns=["update id", "timestamp", "sent text", "probs"]) #df = pd.DataFrame(all_results, columns=["thr", "event", "size", "E[gain]", "Comp.", "F1"]) #with open("apsal-submission-2015.tsv", "w") as f: # df.to_csv(f, sep="\t", index=False)
apache-2.0
SPJ-AI/lesson
training_python/svm_text.py
1
3081
# -*- coding: utf-8 -*- #! /usr/bin/python import MeCab # TokenizerとしてMeCabを使用 mecab = MeCab.Tagger("-Ochasen") # MeCabのインスタンス化 f = open('text.tsv') # トレーニングファイルの読み込み lines = f.readlines() words = [] # 単語トークン表層一覧を保持するリスト count = 0 dict = {} # テキスト:カテゴリのペアを保持する辞書 for line in lines: count += 1 if count == 1: continue # ヘッダをSkip split = line.split("\t") if len(split) < 2: continue dict[split[0].strip()] = split[1].strip() # テキスト:カテゴリのペアを格納 tokens = mecab.parse(split[0].strip()) # テキストの形態素解析 token = tokens.split("\n") for ele in token: element = ele.split("\t") surface = element[0] #トークンの表層 if surface == "EOS": break if surface not in words: words.append(surface) #表層の属するカテゴリを格納 f.close() #print(words) # リストの中身の確認 data_array = [] # ベクトル化されたトレーニングデータをストアする配列 target_array = [] # ベクトル化された正解データをストアする配列 category_array = [] # 分類対象カテゴリ一覧をダブりなくストアする配列 for category in dict.values(): if category not in category_array: category_array.append(category) for text in dict.keys(): print(text) entry_array = [0] * len(words) # 初期値0の配列を、wordsの長さ分生成(空ベクトル) target_array.append(category_array.index(dict[text])) # カテゴリ配列のインデックス番号をストア tokens = mecab.parse(text) # テキストの形態素解析 token = tokens.split("\n") for ele in token: element = ele.split("\t") surface = element[0] #トークンの表層 if surface == "EOS": break try: index = words.index(surface) entry_array[index] += 1 except Exception as e: print(str(e)) continue data_array.append(entry_array) print(data_array) print(category_array) print(target_array) from sklearn import svm #アルゴリズムとしてsvmを使用 clf = svm.SVC(gamma=0.001, C=100.) clf.fit(data_array, target_array) #トレーニングデータを全て学習 query = "人工知能は人間を近々凌駕する" query_array = [0] * len(words) # ベクトル化したクエリを格納する配列 tokens = mecab.parse(query) # クエリの形態素解析 token = tokens.split("\n") for ele in token: element = ele.split("\t") surface = element[0] #トークンの表層 if surface == "EOS": break try: index = words.index(surface) query_array[index] += 1 except Exception as e: print(str(e)) continue print(query_array) res = clf.predict(query_array) #トレーニングデータの最後のエントリの値を予測 print(res) print(category_array[res[0]])
gpl-3.0
alisidd/tensorflow
tensorflow/contrib/metrics/python/kernel_tests/histogram_ops_test.py
130
9577
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for histogram_ops.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from tensorflow.contrib.metrics.python.ops import histogram_ops from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.ops import array_ops from tensorflow.python.ops import variables from tensorflow.python.platform import test class Strict1dCumsumTest(test.TestCase): """Test this private function.""" def test_empty_tensor_returns_empty(self): with self.test_session(): tensor = constant_op.constant([]) result = histogram_ops._strict_1d_cumsum(tensor, 0) expected = constant_op.constant([]) np.testing.assert_array_equal(expected.eval(), result.eval()) def test_length_1_tensor_works(self): with self.test_session(): tensor = constant_op.constant([3], dtype=dtypes.float32) result = histogram_ops._strict_1d_cumsum(tensor, 1) expected = constant_op.constant([3], dtype=dtypes.float32) np.testing.assert_array_equal(expected.eval(), result.eval()) def test_length_3_tensor_works(self): with self.test_session(): tensor = constant_op.constant([1, 2, 3], dtype=dtypes.float32) result = histogram_ops._strict_1d_cumsum(tensor, 3) expected = constant_op.constant([1, 3, 6], dtype=dtypes.float32) np.testing.assert_array_equal(expected.eval(), result.eval()) class AUCUsingHistogramTest(test.TestCase): def setUp(self): self.rng = np.random.RandomState(0) def test_empty_labels_and_scores_gives_nan_auc(self): with self.test_session(): labels = constant_op.constant([], shape=[0], dtype=dtypes.bool) scores = constant_op.constant([], shape=[0], dtype=dtypes.float32) score_range = [0, 1.] auc, update_op = histogram_ops.auc_using_histogram(labels, scores, score_range) variables.local_variables_initializer().run() update_op.run() self.assertTrue(np.isnan(auc.eval())) def test_perfect_scores_gives_auc_1(self): self._check_auc( nbins=100, desired_auc=1.0, score_range=[0, 1.], num_records=50, frac_true=0.5, atol=0.05, num_updates=1) def test_terrible_scores_gives_auc_0(self): self._check_auc( nbins=100, desired_auc=0.0, score_range=[0, 1.], num_records=50, frac_true=0.5, atol=0.05, num_updates=1) def test_many_common_conditions(self): for nbins in [50]: for desired_auc in [0.3, 0.5, 0.8]: for score_range in [[-1, 1], [-10, 0]]: for frac_true in [0.3, 0.8]: # Tests pass with atol = 0.03. Moved up to 0.05 to avoid flakes. self._check_auc( nbins=nbins, desired_auc=desired_auc, score_range=score_range, num_records=100, frac_true=frac_true, atol=0.05, num_updates=50) def test_large_class_imbalance_still_ok(self): # With probability frac_true ** num_records, each batch contains only True # records. In this case, ~ 95%. # Tests pass with atol = 0.02. Increased to 0.05 to avoid flakes. self._check_auc( nbins=100, desired_auc=0.8, score_range=[-1, 1.], num_records=10, frac_true=0.995, atol=0.05, num_updates=1000) def test_super_accuracy_with_many_bins_and_records(self): # Test passes with atol = 0.0005. Increased atol to avoid flakes. self._check_auc( nbins=1000, desired_auc=0.75, score_range=[0, 1.], num_records=1000, frac_true=0.5, atol=0.005, num_updates=100) def _check_auc(self, nbins=100, desired_auc=0.75, score_range=None, num_records=50, frac_true=0.5, atol=0.05, num_updates=10): """Check auc accuracy against synthetic data. Args: nbins: nbins arg from contrib.metrics.auc_using_histogram. desired_auc: Number in [0, 1]. The desired auc for synthetic data. score_range: 2-tuple, (low, high), giving the range of the resultant scores. Defaults to [0, 1.]. num_records: Positive integer. The number of records to return. frac_true: Number in (0, 1). Expected fraction of resultant labels that will be True. This is just in expectation...more or less may actually be True. atol: Absolute tolerance for final AUC estimate. num_updates: Update internal histograms this many times, each with a new batch of synthetic data, before computing final AUC. Raises: AssertionError: If resultant AUC is not within atol of theoretical AUC from synthetic data. """ score_range = [0, 1.] or score_range with self.test_session(): labels = array_ops.placeholder(dtypes.bool, shape=[num_records]) scores = array_ops.placeholder(dtypes.float32, shape=[num_records]) auc, update_op = histogram_ops.auc_using_histogram( labels, scores, score_range, nbins=nbins) variables.local_variables_initializer().run() # Updates, then extract auc. for _ in range(num_updates): labels_a, scores_a = synthetic_data(desired_auc, score_range, num_records, self.rng, frac_true) update_op.run(feed_dict={labels: labels_a, scores: scores_a}) labels_a, scores_a = synthetic_data(desired_auc, score_range, num_records, self.rng, frac_true) # Fetch current auc, and verify that fetching again doesn't change it. auc_eval = auc.eval() self.assertAlmostEqual(auc_eval, auc.eval(), places=5) msg = ('nbins: %s, desired_auc: %s, score_range: %s, ' 'num_records: %s, frac_true: %s, num_updates: %s') % (nbins, desired_auc, score_range, num_records, frac_true, num_updates) np.testing.assert_allclose(desired_auc, auc_eval, atol=atol, err_msg=msg) def synthetic_data(desired_auc, score_range, num_records, rng, frac_true): """Create synthetic boolean_labels and scores with adjustable auc. Args: desired_auc: Number in [0, 1], the theoretical AUC of resultant data. score_range: 2-tuple, (low, high), giving the range of the resultant scores num_records: Positive integer. The number of records to return. rng: Initialized np.random.RandomState random number generator frac_true: Number in (0, 1). Expected fraction of resultant labels that will be True. This is just in expectation...more or less may actually be True. Returns: boolean_labels: np.array, dtype=bool. scores: np.array, dtype=np.float32 """ # We prove here why the method (below) for computing AUC works. Of course we # also checked this against sklearn.metrics.roc_auc_curve. # # First do this for score_range = [0, 1], then rescale. # WLOG assume AUC >= 0.5, otherwise we will solve for AUC >= 0.5 then swap # the labels. # So for AUC in [0, 1] we create False and True labels # and corresponding scores drawn from: # F ~ U[0, 1], T ~ U[x, 1] # We have, # AUC # = P[T > F] # = P[T > F | F < x] P[F < x] + P[T > F | F > x] P[F > x] # = (1 * x) + (0.5 * (1 - x)). # Inverting, we have: # x = 2 * AUC - 1, when AUC >= 0.5. assert 0 <= desired_auc <= 1 assert 0 < frac_true < 1 if desired_auc < 0.5: flip_labels = True desired_auc = 1 - desired_auc frac_true = 1 - frac_true else: flip_labels = False x = 2 * desired_auc - 1 labels = rng.binomial(1, frac_true, size=num_records).astype(bool) num_true = labels.sum() num_false = num_records - labels.sum() # Draw F ~ U[0, 1], and T ~ U[x, 1] false_scores = rng.rand(num_false) true_scores = x + rng.rand(num_true) * (1 - x) # Reshape [0, 1] to score_range. def reshape(scores): return score_range[0] + scores * (score_range[1] - score_range[0]) false_scores = reshape(false_scores) true_scores = reshape(true_scores) # Place into one array corresponding with the labels. scores = np.nan * np.ones(num_records, dtype=np.float32) scores[labels] = true_scores scores[~labels] = false_scores if flip_labels: labels = ~labels return labels, scores if __name__ == '__main__': test.main()
apache-2.0
RamaneekGill/Twitter-Sentiment-Analysis
NaiveBayes/train_sklearn_NaiveBayes.py
2
11388
""" Author: Ramaneek Gill This program uses Multinomial Naive Bayes to predict tweet sentiments. By default this trains on only 10% of the available dataset so that old machines or laptops don't run into 12+ GB RAM usage. Also by default this program only uses the top 2000 most common words as features to save computation, setting this to a higher threshold should improve accuracy. For more ways on how to make this machine learning implementation more powerful take a look at the constants in this program. CLI Arguments allowed: give_me_the_data Uses the full training set display_graphs Displays graphs retrain Trains a new model cross-validate Runs cross validation to fine tune the model test-validation_set Tests the latest trained model against the validation set test-test_set Tests the latets trained model against the test set _____ _ _ _ ___ _ _ / ___| | | (_) | | / _ \ | | (_) \ `--. ___ _ __ | |_ _ _ __ ___ ___ _ __ | |_ / /_\ \_ __ __ _| |_ _ ___ _ ___ `--. \/ _ \ '_ \| __| | '_ ` _ \ / _ \ '_ \| __| | _ | '_ \ / _` | | | | / __| / __| /\__/ / __/ | | | |_| | | | | | | __/ | | | |_ | | | | | | | (_| | | |_| \__ \ \__ \\ \____/ \___|_| |_|\__|_|_| |_| |_|\___|_| |_|\__| \_| |_/_| |_|\__,_|_|\__, |___/_|___/ __/ | |___/ Machine learning has begun! """ import sys import pickle import os.path import operator import numpy as np import matplotlib.pyplot as plt from scipy.stats import uniform as sp_rand from sklearn.naive_bayes import MultinomialNB from sklearn.grid_search import RandomizedSearchCV from sklearn.metrics import precision_recall_curve from sklearn.metrics import average_precision_score ### Global variables vocabulary = {} # A dictionary of all the unique words in the corpus ### Change me to higher values for better accuracy! NUM_FEATURES = 2000 # The number of most common words in the corpus to use as features PERCENTAGE_DATA_SET_TO_USE = 0.1 # The percentage of the dataset to use N_CV_ITERS = 10 # The number of iterations to use in randomized cross validation def load_parsed_data(): """ Loads the train, test, and validation sets Returns: inputs_train the input train set targets_train the target train set inputs_valid the input validation set targets_valid the target validation set inputs_test the input test set targets_test the target test set """ print('loading parsed dataset') inputs_train = np.load('../parsed_data/inputs_train.npy') targets_train = np.load('../parsed_data/targets_train.npy') inputs_valid = np.load('../parsed_data/inputs_valid.npy') targets_valid = np.load('../parsed_data/targets_valid.npy') inputs_test = np.load('../parsed_data/inputs_test.npy') targets_test = np.load('../parsed_data/targets_test.npy') print('loaded parsed dataset') return inputs_train, targets_train, inputs_valid, targets_valid, inputs_test, targets_test def trained_model_exists(): """ Checks to see if the extracted features for the Naive Bayes models are saved. Returns: boolean True iff file 'data/model.pkl' exists """ return os.path.exists('data/model.pkl') def load_trained_model(): """Loads and returns the trained model""" print('loading trained model') with open('data/model.pkl', 'rb') as input: classifier = pickle.load(input) print('loaded trained model') input.close() return classifier def save_model(classifier, prefix=''): """Saves the model""" print('saving trained model') with open('data/'+prefix+'model.pkl', 'wb') as output: pickle.dump(classifier, output, pickle.HIGHEST_PROTOCOL) print('saved trained model') def load_features(): """ Loads the extracted features for each data set Returns: train_features a dictionary of the features in the train set valid_features a dictionary of the features in the validation set test_features a dictionary of the features in the test set """ print('loading extracted features') train_features = np.load('data/train_features.npy') valid_features = np.load('data/valid_features.npy') test_features = np.load('data/test_features.npy') print('loaded extracted features') return train_features, valid_features, test_features def save_features(train_features, valid_features, test_features): """Saves the extracted features for each dataset""" print('saving extracted features') np.save('data/train_features.npy', train_features) np.save('data/valid_features.npy', valid_features) np.save('data/test_features.npy', test_features) print('saved extracted features') def build_vocabulary(inputs): """ Builds a dictionary of unique words in the corpus Returns: vocabulary a dictionary of all the unique words in the corpus """ print('building vocabulary of words in the corpus') global vocabulary for tweet in inputs: for word in str(tweet).split(): if vocabulary.has_key(word): vocabulary[word] += 1 else: vocabulary[word] = 1 print('built vocabulary of words in the corpus') return vocabulary def build_features(document, i, vocabulary_words): if i % 10000 == 0: print('extracted features for {0} tweets'.format(i)) document_words = set(str(document).split()) features = np.zeros(len(vocabulary_words)) for i in range(len(vocabulary_words)): features[i] = (vocabulary_words[i] in document_words) return features def extract_features(inputs_train, targets_train, inputs_valid, targets_valid, inputs_test, targets_test): """ Extracts features for training the model. Returns: train_features a dictionary of word presence in the entire input dataset for each tweet {'contains(lol)': False, 'contains(jbiebs)': True, ...} valid_features a dictionary of word presence in the entire input dataset for each tweet {'contains(lol)': False, 'contains(jbiebs)': True, ...} test_features a dictionary of word presence in the entire input dataset for each tweet {'contains(lol)': False, 'contains(jbiebs)': True, ...} """ inputs = np.hstack((inputs_train, inputs_valid, inputs_test)) vocabulary = build_vocabulary(inputs) # Get most common words from vocabulary global NUM_FEATURES words = dict(sorted(vocabulary.iteritems(), key=operator.itemgetter(1), reverse=True)[:NUM_FEATURES]) words = words.keys() print('extracting features for all tweets') train_features = [(build_features(inputs_train[i], i, words)) for i in range(len(inputs_train))] valid_features = [(build_features(inputs_valid[i], i, words)) for i in range(len(inputs_valid))] test_features = [(build_features(inputs_test[i], i, words)) for i in range(len(inputs_test))] print('extracted features for all tweets') return np.array(train_features), np.array(valid_features), np.array(test_features) def train_model(features, targets, alpha=1): """ Trains a Naive Bayes classifier using the features passed in. Returns: classifier the trained model """ print('training model') classifier = MultinomialNB(alpha=alpha) classifier.fit(features, targets) print('trained model') return classifier def cross_validate(train_features, targets_train, iters): """ Runs randomized cross validation using adjustable MultinomialNB params. Returns: The model that is the most accurate """ print('starting cross validation') param_grid = {'alpha': sp_rand()} model = MultinomialNB() rsearch = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_iter=iters) rsearch.fit(train_features, targets_train) print('finished cross validation') print('best model has a score of {} using alpha={}'.format(rsearch.best_score_, rsearch.best_estimator_.alpha)) return rsearch.best_estimator_.alpha def plot_precision_and_recall(predictions, targets): """Calculates and displays the precision and recall graph""" # Compute Precision-Recall and plot curve precision = dict() recall = dict() average_precision = dict() average_precision = average_precision_score(targets, predictions) precision, recall, _ = precision_recall_curve(targets, predictions) # Plot Precision-Recall curve plt.clf() plt.plot(recall, precision, label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.title('Precision-Recall example: AUC={0:0.2f}'.format(average_precision)) plt.legend(loc="lower left") plt.show() def main(): """ CLI Arguments allowed: --display_graphs Displays graphs --retrain Trains a new model --cross-validate Runs cross validation to fine tune the model --test=validation_set Tests the latest trained model against the validation set --test=test_set Tests the latets trained model against the test set """ print(__doc__) inputs_train, targets_train, inputs_valid, targets_valid, inputs_test, targets_test = load_parsed_data() # Limit the data used to make it possible to run on old machines if 'give_me_the_data' not in sys.argv: inputs_train = inputs_train[:len(inputs_train)*PERCENTAGE_DATA_SET_TO_USE] targets_train = targets_train[:len(targets_train)*PERCENTAGE_DATA_SET_TO_USE] inputs_valid = inputs_valid[:len(inputs_valid)*PERCENTAGE_DATA_SET_TO_USE] targets_valid = targets_valid[:len(targets_valid)*PERCENTAGE_DATA_SET_TO_USE] inputs_test = inputs_test[:len(inputs_test)*PERCENTAGE_DATA_SET_TO_USE] targets_test = targets_test[:len(targets_test)*PERCENTAGE_DATA_SET_TO_USE] else: print('WARNING: You are using the entire data set, this will consume 12+ GB of RAM') if '--display_graphs' in sys.argv: display_graphs = True else: display_graphs = False print('using {} percent of all data in corpus'.format(PERCENTAGE_DATA_SET_TO_USE*100)) print('using {} most common words as features'.format(NUM_FEATURES)) if not trained_model_exists() or '--retrain' in sys.argv: train_features, valid_features, test_features = extract_features( inputs_train, targets_train, inputs_valid, targets_valid, inputs_test, targets_test ) save_features(train_features, valid_features, test_features) classifier = train_model(train_features, targets_train) save_model(classifier) else: train_features, valid_features, test_features = load_features() classifier = load_trained_model() if '--cross-validate' in sys.argv: alpha = cross_validate(train_features, targets_train, N_CV_ITERS) classifier = train_model(train_features, targets_train, alpha) save_model(classifier, 'cross_validated_') if '--test=validation_set' in sys.argv: score = classifier.score(valid_features, targets_valid) print('Accuracy against validation set is {} percent'.format(score*100)) if display_graphs == True: predictions = classifier.predict(valid_features) plot_precision_and_recall(predictions, targets_valid) if '--test=test_set' in sys.argv: score = classifier.score(test_features, targets_test) print('Accuracy against test set is {} percent'.format(score*100)) if display_graphs == True: predictions = classifier.predict(test_features) plot_precision_and_recall(predictions, targets_test) if __name__ == "__main__": main()
gpl-3.0
wiless/gocomm
tools/plot_scatter_udp.py
1
1171
import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation import socket import struct import array fig = plt.figure() ax = plt.axes(xlim=(-3, 3), ylim=(-3, 3)) scat = plt.scatter([], [], s=10) plt.grid() def init(): b=np.array([]) b=np.vstack((b,b)) scat.set_offsets(b) return scat, def animate(i): UDP_IP = '' UDP_PORT = 8080 BUFFER_SIZE = 4200 # Normally 1024, but we want fast response sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((UDP_IP, UDP_PORT)) head=struct.Struct('< 10s d d q') packet, addr = sock.recvfrom(BUFFER_SIZE) # buffer size is 1024 bytes pklen=len(packet) header=list(head.unpack(packet[:34])) Data_format=struct.Struct('<%dd' % header[-1]) data=array.array('d',packet[34:]) val=list(Data_format.unpack_from(data)) print '-'*100 #print 'Packet number=',i #print 'Packet length=',pklen print 'Header=',header real=np.array(val[0::2]) imag=np.array(val[1::2]) #print real,imag symbols=np.vstack((real,imag)) #print symbols scat.set_offsets(symbols) return scat, ani = animation.FuncAnimation(fig, animate, frames=100,interval=100,init_func=init,blit=True) plt.show()
gpl-3.0
terkkila/scikit-learn
sklearn/cross_decomposition/pls_.py
187
28507
""" The :mod:`sklearn.pls` module implements Partial Least Squares (PLS). """ # Author: Edouard Duchesnay <[email protected]> # License: BSD 3 clause from ..base import BaseEstimator, RegressorMixin, TransformerMixin from ..utils import check_array, check_consistent_length from ..externals import six import warnings from abc import ABCMeta, abstractmethod import numpy as np from scipy import linalg from ..utils import arpack from ..utils.validation import check_is_fitted __all__ = ['PLSCanonical', 'PLSRegression', 'PLSSVD'] def _nipals_twoblocks_inner_loop(X, Y, mode="A", max_iter=500, tol=1e-06, norm_y_weights=False): """Inner loop of the iterative NIPALS algorithm. Provides an alternative to the svd(X'Y); returns the first left and right singular vectors of X'Y. See PLS for the meaning of the parameters. It is similar to the Power method for determining the eigenvectors and eigenvalues of a X'Y. """ y_score = Y[:, [0]] x_weights_old = 0 ite = 1 X_pinv = Y_pinv = None eps = np.finfo(X.dtype).eps # Inner loop of the Wold algo. while True: # 1.1 Update u: the X weights if mode == "B": if X_pinv is None: X_pinv = linalg.pinv(X) # compute once pinv(X) x_weights = np.dot(X_pinv, y_score) else: # mode A # Mode A regress each X column on y_score x_weights = np.dot(X.T, y_score) / np.dot(y_score.T, y_score) # 1.2 Normalize u x_weights /= np.sqrt(np.dot(x_weights.T, x_weights)) + eps # 1.3 Update x_score: the X latent scores x_score = np.dot(X, x_weights) # 2.1 Update y_weights if mode == "B": if Y_pinv is None: Y_pinv = linalg.pinv(Y) # compute once pinv(Y) y_weights = np.dot(Y_pinv, x_score) else: # Mode A regress each Y column on x_score y_weights = np.dot(Y.T, x_score) / np.dot(x_score.T, x_score) # 2.2 Normalize y_weights if norm_y_weights: y_weights /= np.sqrt(np.dot(y_weights.T, y_weights)) + eps # 2.3 Update y_score: the Y latent scores y_score = np.dot(Y, y_weights) / (np.dot(y_weights.T, y_weights) + eps) # y_score = np.dot(Y, y_weights) / np.dot(y_score.T, y_score) ## BUG x_weights_diff = x_weights - x_weights_old if np.dot(x_weights_diff.T, x_weights_diff) < tol or Y.shape[1] == 1: break if ite == max_iter: warnings.warn('Maximum number of iterations reached') break x_weights_old = x_weights ite += 1 return x_weights, y_weights, ite def _svd_cross_product(X, Y): C = np.dot(X.T, Y) U, s, Vh = linalg.svd(C, full_matrices=False) u = U[:, [0]] v = Vh.T[:, [0]] return u, v def _center_scale_xy(X, Y, scale=True): """ Center X, Y and scale if the scale parameter==True Returns ------- X, Y, x_mean, y_mean, x_std, y_std """ # center x_mean = X.mean(axis=0) X -= x_mean y_mean = Y.mean(axis=0) Y -= y_mean # scale if scale: x_std = X.std(axis=0, ddof=1) x_std[x_std == 0.0] = 1.0 X /= x_std y_std = Y.std(axis=0, ddof=1) y_std[y_std == 0.0] = 1.0 Y /= y_std else: x_std = np.ones(X.shape[1]) y_std = np.ones(Y.shape[1]) return X, Y, x_mean, y_mean, x_std, y_std class _PLS(six.with_metaclass(ABCMeta), BaseEstimator, TransformerMixin, RegressorMixin): """Partial Least Squares (PLS) This class implements the generic PLS algorithm, constructors' parameters allow to obtain a specific implementation such as: - PLS2 regression, i.e., PLS 2 blocks, mode A, with asymmetric deflation and unnormalized y weights such as defined by [Tenenhaus 1998] p. 132. With univariate response it implements PLS1. - PLS canonical, i.e., PLS 2 blocks, mode A, with symmetric deflation and normalized y weights such as defined by [Tenenhaus 1998] (p. 132) and [Wegelin et al. 2000]. This parametrization implements the original Wold algorithm. We use the terminology defined by [Wegelin et al. 2000]. This implementation uses the PLS Wold 2 blocks algorithm based on two nested loops: (i) The outer loop iterate over components. (ii) The inner loop estimates the weights vectors. This can be done with two algo. (a) the inner loop of the original NIPALS algo. or (b) a SVD on residuals cross-covariance matrices. n_components : int, number of components to keep. (default 2). scale : boolean, scale data? (default True) deflation_mode : str, "canonical" or "regression". See notes. mode : "A" classical PLS and "B" CCA. See notes. norm_y_weights: boolean, normalize Y weights to one? (default False) algorithm : string, "nipals" or "svd" The algorithm used to estimate the weights. It will be called n_components times, i.e. once for each iteration of the outer loop. max_iter : an integer, the maximum number of iterations (default 500) of the NIPALS inner loop (used only if algorithm="nipals") tol : non-negative real, default 1e-06 The tolerance used in the iterative algorithm. copy : boolean, default True Whether the deflation should be done on a copy. Let the default value to True unless you don't care about side effects. Attributes ---------- x_weights_ : array, [p, n_components] X block weights vectors. y_weights_ : array, [q, n_components] Y block weights vectors. x_loadings_ : array, [p, n_components] X block loadings vectors. y_loadings_ : array, [q, n_components] Y block loadings vectors. x_scores_ : array, [n_samples, n_components] X scores. y_scores_ : array, [n_samples, n_components] Y scores. x_rotations_ : array, [p, n_components] X block to latents rotations. y_rotations_ : array, [q, n_components] Y block to latents rotations. coef_: array, [p, q] The coefficients of the linear model: ``Y = X coef_ + Err`` n_iter_ : array-like Number of iterations of the NIPALS inner loop for each component. Not useful if the algorithm given is "svd". References ---------- Jacob A. Wegelin. A survey of Partial Least Squares (PLS) methods, with emphasis on the two-block case. Technical Report 371, Department of Statistics, University of Washington, Seattle, 2000. In French but still a reference: Tenenhaus, M. (1998). La regression PLS: theorie et pratique. Paris: Editions Technic. See also -------- PLSCanonical PLSRegression CCA PLS_SVD """ @abstractmethod def __init__(self, n_components=2, scale=True, deflation_mode="regression", mode="A", algorithm="nipals", norm_y_weights=False, max_iter=500, tol=1e-06, copy=True): self.n_components = n_components self.deflation_mode = deflation_mode self.mode = mode self.norm_y_weights = norm_y_weights self.scale = scale self.algorithm = algorithm self.max_iter = max_iter self.tol = tol self.copy = copy def fit(self, X, Y): """Fit model to data. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vectors, where n_samples in the number of samples and n_features is the number of predictors. Y : array-like of response, shape = [n_samples, n_targets] Target vectors, where n_samples in the number of samples and n_targets is the number of response variables. """ # copy since this will contains the residuals (deflated) matrices check_consistent_length(X, Y) X = check_array(X, dtype=np.float64, copy=self.copy) Y = check_array(Y, dtype=np.float64, copy=self.copy, ensure_2d=False) if Y.ndim == 1: Y = Y.reshape(-1, 1) n = X.shape[0] p = X.shape[1] q = Y.shape[1] if self.n_components < 1 or self.n_components > p: raise ValueError('Invalid number of components: %d' % self.n_components) if self.algorithm not in ("svd", "nipals"): raise ValueError("Got algorithm %s when only 'svd' " "and 'nipals' are known" % self.algorithm) if self.algorithm == "svd" and self.mode == "B": raise ValueError('Incompatible configuration: mode B is not ' 'implemented with svd algorithm') if self.deflation_mode not in ["canonical", "regression"]: raise ValueError('The deflation mode is unknown') # Scale (in place) X, Y, self.x_mean_, self.y_mean_, self.x_std_, self.y_std_\ = _center_scale_xy(X, Y, self.scale) # Residuals (deflated) matrices Xk = X Yk = Y # Results matrices self.x_scores_ = np.zeros((n, self.n_components)) self.y_scores_ = np.zeros((n, self.n_components)) self.x_weights_ = np.zeros((p, self.n_components)) self.y_weights_ = np.zeros((q, self.n_components)) self.x_loadings_ = np.zeros((p, self.n_components)) self.y_loadings_ = np.zeros((q, self.n_components)) self.n_iter_ = [] # NIPALS algo: outer loop, over components for k in range(self.n_components): if np.all(np.dot(Yk.T, Yk) < np.finfo(np.double).eps): # Yk constant warnings.warn('Y residual constant at iteration %s' % k) break # 1) weights estimation (inner loop) # ----------------------------------- if self.algorithm == "nipals": x_weights, y_weights, n_iter_ = \ _nipals_twoblocks_inner_loop( X=Xk, Y=Yk, mode=self.mode, max_iter=self.max_iter, tol=self.tol, norm_y_weights=self.norm_y_weights) self.n_iter_.append(n_iter_) elif self.algorithm == "svd": x_weights, y_weights = _svd_cross_product(X=Xk, Y=Yk) # compute scores x_scores = np.dot(Xk, x_weights) if self.norm_y_weights: y_ss = 1 else: y_ss = np.dot(y_weights.T, y_weights) y_scores = np.dot(Yk, y_weights) / y_ss # test for null variance if np.dot(x_scores.T, x_scores) < np.finfo(np.double).eps: warnings.warn('X scores are null at iteration %s' % k) break # 2) Deflation (in place) # ---------------------- # Possible memory footprint reduction may done here: in order to # avoid the allocation of a data chunk for the rank-one # approximations matrix which is then subtracted to Xk, we suggest # to perform a column-wise deflation. # # - regress Xk's on x_score x_loadings = np.dot(Xk.T, x_scores) / np.dot(x_scores.T, x_scores) # - subtract rank-one approximations to obtain remainder matrix Xk -= np.dot(x_scores, x_loadings.T) if self.deflation_mode == "canonical": # - regress Yk's on y_score, then subtract rank-one approx. y_loadings = (np.dot(Yk.T, y_scores) / np.dot(y_scores.T, y_scores)) Yk -= np.dot(y_scores, y_loadings.T) if self.deflation_mode == "regression": # - regress Yk's on x_score, then subtract rank-one approx. y_loadings = (np.dot(Yk.T, x_scores) / np.dot(x_scores.T, x_scores)) Yk -= np.dot(x_scores, y_loadings.T) # 3) Store weights, scores and loadings # Notation: self.x_scores_[:, k] = x_scores.ravel() # T self.y_scores_[:, k] = y_scores.ravel() # U self.x_weights_[:, k] = x_weights.ravel() # W self.y_weights_[:, k] = y_weights.ravel() # C self.x_loadings_[:, k] = x_loadings.ravel() # P self.y_loadings_[:, k] = y_loadings.ravel() # Q # Such that: X = TP' + Err and Y = UQ' + Err # 4) rotations from input space to transformed space (scores) # T = X W(P'W)^-1 = XW* (W* : p x k matrix) # U = Y C(Q'C)^-1 = YC* (W* : q x k matrix) self.x_rotations_ = np.dot( self.x_weights_, linalg.pinv(np.dot(self.x_loadings_.T, self.x_weights_))) if Y.shape[1] > 1: self.y_rotations_ = np.dot( self.y_weights_, linalg.pinv(np.dot(self.y_loadings_.T, self.y_weights_))) else: self.y_rotations_ = np.ones(1) if True or self.deflation_mode == "regression": # FIXME what's with the if? # Estimate regression coefficient # Regress Y on T # Y = TQ' + Err, # Then express in function of X # Y = X W(P'W)^-1Q' + Err = XB + Err # => B = W*Q' (p x q) self.coef_ = np.dot(self.x_rotations_, self.y_loadings_.T) self.coef_ = (1. / self.x_std_.reshape((p, 1)) * self.coef_ * self.y_std_) return self def transform(self, X, Y=None, copy=True): """Apply the dimension reduction learned on the train data. Parameters ---------- X : array-like of predictors, shape = [n_samples, p] Training vectors, where n_samples in the number of samples and p is the number of predictors. Y : array-like of response, shape = [n_samples, q], optional Training vectors, where n_samples in the number of samples and q is the number of response variables. copy : boolean, default True Whether to copy X and Y, or perform in-place normalization. Returns ------- x_scores if Y is not given, (x_scores, y_scores) otherwise. """ check_is_fitted(self, 'x_mean_') X = check_array(X, copy=copy) # Normalize X -= self.x_mean_ X /= self.x_std_ # Apply rotation x_scores = np.dot(X, self.x_rotations_) if Y is not None: Y = check_array(Y, ensure_2d=False, copy=copy) if Y.ndim == 1: Y = Y.reshape(-1, 1) Y -= self.y_mean_ Y /= self.y_std_ y_scores = np.dot(Y, self.y_rotations_) return x_scores, y_scores return x_scores def predict(self, X, copy=True): """Apply the dimension reduction learned on the train data. Parameters ---------- X : array-like of predictors, shape = [n_samples, p] Training vectors, where n_samples in the number of samples and p is the number of predictors. copy : boolean, default True Whether to copy X and Y, or perform in-place normalization. Notes ----- This call requires the estimation of a p x q matrix, which may be an issue in high dimensional space. """ check_is_fitted(self, 'x_mean_') X = check_array(X, copy=copy) # Normalize X -= self.x_mean_ X /= self.x_std_ Ypred = np.dot(X, self.coef_) return Ypred + self.y_mean_ def fit_transform(self, X, y=None, **fit_params): """Learn and apply the dimension reduction on the train data. Parameters ---------- X : array-like of predictors, shape = [n_samples, p] Training vectors, where n_samples in the number of samples and p is the number of predictors. Y : array-like of response, shape = [n_samples, q], optional Training vectors, where n_samples in the number of samples and q is the number of response variables. copy : boolean, default True Whether to copy X and Y, or perform in-place normalization. Returns ------- x_scores if Y is not given, (x_scores, y_scores) otherwise. """ check_is_fitted(self, 'x_mean_') return self.fit(X, y, **fit_params).transform(X, y) class PLSRegression(_PLS): """PLS regression PLSRegression implements the PLS 2 blocks regression known as PLS2 or PLS1 in case of one dimensional response. This class inherits from _PLS with mode="A", deflation_mode="regression", norm_y_weights=False and algorithm="nipals". Read more in the :ref:`User Guide <cross_decomposition>`. Parameters ---------- n_components : int, (default 2) Number of components to keep. scale : boolean, (default True) whether to scale the data max_iter : an integer, (default 500) the maximum number of iterations of the NIPALS inner loop (used only if algorithm="nipals") tol : non-negative real Tolerance used in the iterative algorithm default 1e-06. copy : boolean, default True Whether the deflation should be done on a copy. Let the default value to True unless you don't care about side effect Attributes ---------- x_weights_ : array, [p, n_components] X block weights vectors. y_weights_ : array, [q, n_components] Y block weights vectors. x_loadings_ : array, [p, n_components] X block loadings vectors. y_loadings_ : array, [q, n_components] Y block loadings vectors. x_scores_ : array, [n_samples, n_components] X scores. y_scores_ : array, [n_samples, n_components] Y scores. x_rotations_ : array, [p, n_components] X block to latents rotations. y_rotations_ : array, [q, n_components] Y block to latents rotations. coef_: array, [p, q] The coefficients of the linear model: ``Y = X coef_ + Err`` n_iter_ : array-like Number of iterations of the NIPALS inner loop for each component. Notes ----- For each component k, find weights u, v that optimizes: ``max corr(Xk u, Yk v) * var(Xk u) var(Yk u)``, such that ``|u| = 1`` Note that it maximizes both the correlations between the scores and the intra-block variances. The residual matrix of X (Xk+1) block is obtained by the deflation on the current X score: x_score. The residual matrix of Y (Yk+1) block is obtained by deflation on the current X score. This performs the PLS regression known as PLS2. This mode is prediction oriented. This implementation provides the same results that 3 PLS packages provided in the R language (R-project): - "mixOmics" with function pls(X, Y, mode = "regression") - "plspm " with function plsreg2(X, Y) - "pls" with function oscorespls.fit(X, Y) Examples -------- >>> from sklearn.cross_decomposition import PLSRegression >>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]] >>> Y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]] >>> pls2 = PLSRegression(n_components=2) >>> pls2.fit(X, Y) ... # doctest: +NORMALIZE_WHITESPACE PLSRegression(copy=True, max_iter=500, n_components=2, scale=True, tol=1e-06) >>> Y_pred = pls2.predict(X) References ---------- Jacob A. Wegelin. A survey of Partial Least Squares (PLS) methods, with emphasis on the two-block case. Technical Report 371, Department of Statistics, University of Washington, Seattle, 2000. In french but still a reference: Tenenhaus, M. (1998). La regression PLS: theorie et pratique. Paris: Editions Technic. """ def __init__(self, n_components=2, scale=True, max_iter=500, tol=1e-06, copy=True): _PLS.__init__(self, n_components=n_components, scale=scale, deflation_mode="regression", mode="A", norm_y_weights=False, max_iter=max_iter, tol=tol, copy=copy) class PLSCanonical(_PLS): """ PLSCanonical implements the 2 blocks canonical PLS of the original Wold algorithm [Tenenhaus 1998] p.204, referred as PLS-C2A in [Wegelin 2000]. This class inherits from PLS with mode="A" and deflation_mode="canonical", norm_y_weights=True and algorithm="nipals", but svd should provide similar results up to numerical errors. Read more in the :ref:`User Guide <cross_decomposition>`. Parameters ---------- scale : boolean, scale data? (default True) algorithm : string, "nipals" or "svd" The algorithm used to estimate the weights. It will be called n_components times, i.e. once for each iteration of the outer loop. max_iter : an integer, (default 500) the maximum number of iterations of the NIPALS inner loop (used only if algorithm="nipals") tol : non-negative real, default 1e-06 the tolerance used in the iterative algorithm copy : boolean, default True Whether the deflation should be done on a copy. Let the default value to True unless you don't care about side effect n_components : int, number of components to keep. (default 2). Attributes ---------- x_weights_ : array, shape = [p, n_components] X block weights vectors. y_weights_ : array, shape = [q, n_components] Y block weights vectors. x_loadings_ : array, shape = [p, n_components] X block loadings vectors. y_loadings_ : array, shape = [q, n_components] Y block loadings vectors. x_scores_ : array, shape = [n_samples, n_components] X scores. y_scores_ : array, shape = [n_samples, n_components] Y scores. x_rotations_ : array, shape = [p, n_components] X block to latents rotations. y_rotations_ : array, shape = [q, n_components] Y block to latents rotations. n_iter_ : array-like Number of iterations of the NIPALS inner loop for each component. Not useful if the algorithm provided is "svd". Notes ----- For each component k, find weights u, v that optimize:: max corr(Xk u, Yk v) * var(Xk u) var(Yk u), such that ``|u| = |v| = 1`` Note that it maximizes both the correlations between the scores and the intra-block variances. The residual matrix of X (Xk+1) block is obtained by the deflation on the current X score: x_score. The residual matrix of Y (Yk+1) block is obtained by deflation on the current Y score. This performs a canonical symmetric version of the PLS regression. But slightly different than the CCA. This is mostly used for modeling. This implementation provides the same results that the "plspm" package provided in the R language (R-project), using the function plsca(X, Y). Results are equal or collinear with the function ``pls(..., mode = "canonical")`` of the "mixOmics" package. The difference relies in the fact that mixOmics implementation does not exactly implement the Wold algorithm since it does not normalize y_weights to one. Examples -------- >>> from sklearn.cross_decomposition import PLSCanonical >>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]] >>> Y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]] >>> plsca = PLSCanonical(n_components=2) >>> plsca.fit(X, Y) ... # doctest: +NORMALIZE_WHITESPACE PLSCanonical(algorithm='nipals', copy=True, max_iter=500, n_components=2, scale=True, tol=1e-06) >>> X_c, Y_c = plsca.transform(X, Y) References ---------- Jacob A. Wegelin. A survey of Partial Least Squares (PLS) methods, with emphasis on the two-block case. Technical Report 371, Department of Statistics, University of Washington, Seattle, 2000. Tenenhaus, M. (1998). La regression PLS: theorie et pratique. Paris: Editions Technic. See also -------- CCA PLSSVD """ def __init__(self, n_components=2, scale=True, algorithm="nipals", max_iter=500, tol=1e-06, copy=True): _PLS.__init__(self, n_components=n_components, scale=scale, deflation_mode="canonical", mode="A", norm_y_weights=True, algorithm=algorithm, max_iter=max_iter, tol=tol, copy=copy) class PLSSVD(BaseEstimator, TransformerMixin): """Partial Least Square SVD Simply perform a svd on the crosscovariance matrix: X'Y There are no iterative deflation here. Read more in the :ref:`User Guide <cross_decomposition>`. Parameters ---------- n_components : int, default 2 Number of components to keep. scale : boolean, default True Whether to scale X and Y. copy : boolean, default True Whether to copy X and Y, or perform in-place computations. Attributes ---------- x_weights_ : array, [p, n_components] X block weights vectors. y_weights_ : array, [q, n_components] Y block weights vectors. x_scores_ : array, [n_samples, n_components] X scores. y_scores_ : array, [n_samples, n_components] Y scores. See also -------- PLSCanonical CCA """ def __init__(self, n_components=2, scale=True, copy=True): self.n_components = n_components self.scale = scale self.copy = copy def fit(self, X, Y): # copy since this will contains the centered data check_consistent_length(X, Y) X = check_array(X, dtype=np.float64, copy=self.copy) Y = check_array(Y, dtype=np.float64, copy=self.copy, ensure_2d=False) if Y.ndim == 1: Y = Y.reshape(-1, 1) if self.n_components > max(Y.shape[1], X.shape[1]): raise ValueError("Invalid number of components n_components=%d" " with X of shape %s and Y of shape %s." % (self.n_components, str(X.shape), str(Y.shape))) # Scale (in place) X, Y, self.x_mean_, self.y_mean_, self.x_std_, self.y_std_ =\ _center_scale_xy(X, Y, self.scale) # svd(X'Y) C = np.dot(X.T, Y) # The arpack svds solver only works if the number of extracted # components is smaller than rank(X) - 1. Hence, if we want to extract # all the components (C.shape[1]), we have to use another one. Else, # let's use arpacks to compute only the interesting components. if self.n_components >= np.min(C.shape): U, s, V = linalg.svd(C, full_matrices=False) else: U, s, V = arpack.svds(C, k=self.n_components) V = V.T self.x_scores_ = np.dot(X, U) self.y_scores_ = np.dot(Y, V) self.x_weights_ = U self.y_weights_ = V return self def transform(self, X, Y=None): """Apply the dimension reduction learned on the train data.""" check_is_fitted(self, 'x_mean_') X = check_array(X, dtype=np.float64) Xr = (X - self.x_mean_) / self.x_std_ x_scores = np.dot(Xr, self.x_weights_) if Y is not None: if Y.ndim == 1: Y = Y.reshape(-1, 1) Yr = (Y - self.y_mean_) / self.y_std_ y_scores = np.dot(Yr, self.y_weights_) return x_scores, y_scores return x_scores def fit_transform(self, X, y=None, **fit_params): """Learn and apply the dimension reduction on the train data. Parameters ---------- X : array-like of predictors, shape = [n_samples, p] Training vectors, where n_samples in the number of samples and p is the number of predictors. Y : array-like of response, shape = [n_samples, q], optional Training vectors, where n_samples in the number of samples and q is the number of response variables. Returns ------- x_scores if Y is not given, (x_scores, y_scores) otherwise. """ return self.fit(X, y, **fit_params).transform(X, y)
bsd-3-clause
abhisg/scikit-learn
sklearn/__check_build/__init__.py
345
1671
""" Module to give helpful messages to the user that did not compile the scikit properly. """ import os INPLACE_MSG = """ It appears that you are importing a local scikit-learn source tree. For this, you need to have an inplace install. Maybe you are in the source directory and you need to try from another location.""" STANDARD_MSG = """ If you have used an installer, please check that it is suited for your Python version, your operating system and your platform.""" def raise_build_error(e): # Raise a comprehensible error and list the contents of the # directory to help debugging on the mailing list. local_dir = os.path.split(__file__)[0] msg = STANDARD_MSG if local_dir == "sklearn/__check_build": # Picking up the local install: this will work only if the # install is an 'inplace build' msg = INPLACE_MSG dir_content = list() for i, filename in enumerate(os.listdir(local_dir)): if ((i + 1) % 3): dir_content.append(filename.ljust(26)) else: dir_content.append(filename + '\n') raise ImportError("""%s ___________________________________________________________________________ Contents of %s: %s ___________________________________________________________________________ It seems that scikit-learn has not been built correctly. If you have installed scikit-learn from source, please do not forget to build the package before using it: run `python setup.py install` or `make` in the source directory. %s""" % (e, local_dir, ''.join(dir_content).strip(), msg)) try: from ._check_build import check_build except ImportError as e: raise_build_error(e)
bsd-3-clause
toastedcornflakes/scikit-learn
examples/svm/plot_oneclass.py
80
2338
""" ========================================== One-class SVM with non-linear kernel (RBF) ========================================== An example using a one-class SVM for novelty detection. :ref:`One-class SVM <svm_outlier_detection>` is an unsupervised algorithm that learns a decision function for novelty detection: classifying new data as similar or different to the training set. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager from sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * np.random.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) # fit the model clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) n_error_train = y_pred_train[y_pred_train == -1].size n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size # plot the line, the points, and the nearest vectors to the plane Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.title("Novelty Detection") plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred') plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred') s = 40 b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white', s=s) b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='blueviolet', s=s) c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='gold', s=s) plt.axis('tight') plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend([a.collections[0], b1, b2, c], ["learned frontier", "training observations", "new regular observations", "new abnormal observations"], loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11)) plt.xlabel( "error train: %d/200 ; errors novel regular: %d/40 ; " "errors novel abnormal: %d/40" % (n_error_train, n_error_test, n_error_outliers)) plt.show()
bsd-3-clause
edbaskerville/mc3kit
python/analyze_convergence.py
1
1960
#!/usr/bin/env python import numpy as np import sys import json from collections import OrderedDict from autohist import * from sqlighter import * from mc3kit import * import pymc import matplotlib.pyplot as pp def analyzeConvergence(dbFilename, db, pn, burnin): print 'ANALYZING {0}'.format(pn) vals = np.array(getParameter(db, pn, burnin=burnin)) convDict = OrderedDict() gw = pymc.geweke(vals) gwDict = OrderedDict() gwDict['scores'] = gw frac2SD = len([x for x in gw if x[1] > -2 and x[1] < 2]) / float(len(gw)) gwDict['frac_2sd'] = frac2SD convDict['geweke'] = gwDict rl = pymc.raftery_lewis(vals, 0.025, r=0.01) rlDict = OrderedDict() rlDict['iter_req_acc'] = rl[0] rlDict['thin_first_ord'] = rl[1] rlDict['burnin'] = rl[2] rlDict['iter_total'] = rl[3] rlDict['thin_ind'] = rl[4] convDict['raftery_lewis'] = rlDict print '' return convDict if __name__ == '__main__': dbFilename = sys.argv[1] burnin = int(sys.argv[2]) db = connectToDatabase(dbFilename) createIndexes(db) paramNames = getFloatParameterNames(db) convDict = OrderedDict() for i, pn in enumerate(paramNames): convDict[pn] = analyzeConvergence(dbFilename, db, pn, burnin) print('SUMMARY:') allFrac2SD = [x['geweke']['frac_2sd'] for x in convDict.values()] meanFrac2SD = np.mean(allFrac2SD) minFrac2SD = np.min(allFrac2SD) print 'fraction of geweke Z-scores within 2 SD: mean {0}, min {1}'.format(meanFrac2SD, minFrac2SD) allThins = [x['raftery_lewis']['thin_ind'] for x in convDict.values()] meanThin = np.mean(allThins) maxThin = np.max(allThins) print 'thin needed: mean {0}, max {1}'.format(meanThin, maxThin) allIters = [x['raftery_lewis']['iter_total'] for x in convDict.values()] meanIter = np.mean(allIters) maxIter = np.max(allIters) print 'iterations needed: mean {0}, max {1}'.format(meanIter, maxIter) jsonFile = open(dbFilename + '.convergence.json', 'w') json.dump(convDict, jsonFile, indent=2) jsonFile.write('\n')
agpl-3.0
a113n/bcbio-nextgen
bcbio/rnaseq/pizzly.py
4
5043
""" run the pizzly fusion caller for RNA-seq https://github.com/pmelsted/pizzly http://www.biorxiv.org/content/early/2017/07/20/166322 """ from __future__ import print_function import os from bcbio.log import logger from bcbio import utils import bcbio.pipeline.datadict as dd from bcbio.pipeline import config_utils from bcbio.distributed.transaction import file_transaction from bcbio.rnaseq import kallisto, sailfish, gtf from bcbio.provenance import do from bcbio.utils import file_exists, safe_makedir from bcbio.bam import fasta h5py = utils.LazyImport("h5py") import numpy as np import pandas as pd def get_fragment_length(data): """ lifted from https://github.com/pmelsted/pizzly/scripts/pizzly_get_fragment_length.py """ h5 = kallisto.get_kallisto_h5(data) cutoff = 0.95 with h5py.File(h5) as f: x = np.asarray(f['aux']['fld'], dtype='float64') y = np.cumsum(x)/np.sum(x) fraglen = np.argmax(y > cutoff) return(fraglen) def run_pizzly(data): samplename = dd.get_sample_name(data) work_dir = dd.get_work_dir(data) pizzlydir = os.path.join(work_dir, "pizzly") gtf = dd.get_transcriptome_gtf(data) if not gtf: gtf = dd.get_gtf_file(data) if dd.get_transcriptome_fasta(data): gtf_fa = dd.get_transcriptome_fasta(data) else: gtf_fa = sailfish.create_combined_fasta(data) stripped_fa = os.path.splitext(os.path.basename(gtf_fa))[0] + "-noversions.fa" stripped_fa = os.path.join(pizzlydir, stripped_fa) gtf_fa = fasta.strip_transcript_versions(gtf_fa, stripped_fa) fraglength = get_fragment_length(data) cachefile = os.path.join(pizzlydir, "pizzly.cache") fusions = kallisto.get_kallisto_fusions(data) pizzlypath = config_utils.get_program("pizzly", dd.get_config(data)) outdir = pizzly(pizzlypath, gtf, gtf_fa, fraglength, cachefile, pizzlydir, fusions, samplename, data) return outdir def pizzly(pizzly_path, gtf, gtf_fa, fraglength, cachefile, pizzlydir, fusions, samplename, data): outdir = os.path.join(pizzlydir, samplename) out_stem = os.path.join(outdir, samplename) pizzly_gtf = make_pizzly_gtf(gtf, os.path.join(pizzlydir, "pizzly.gtf"), data) sentinel = os.path.join(out_stem, "-flat-filtered.tsv") pizzlycalls = out_stem + ".json" if not file_exists(pizzlycalls): with file_transaction(data, outdir) as tx_out_dir: safe_makedir(tx_out_dir) tx_out_stem = os.path.join(tx_out_dir, samplename) with file_transaction(cachefile) as tx_cache_file: cmd = ("{pizzly_path} -k 31 --gtf {pizzly_gtf} --cache {tx_cache_file} " "--align-score 2 --insert-size {fraglength} --fasta {gtf_fa} " "--output {tx_out_stem} {fusions}") message = ("Running pizzly on %s." % fusions) do.run(cmd.format(**locals()), message) flatfile = out_stem + "-flat.tsv" filteredfile = out_stem + "-flat-filtered.tsv" flatten_pizzly(pizzlycalls, flatfile, data) filter_pizzly(flatfile, filteredfile, data) return outdir def make_pizzly_gtf(gtf_file, out_file, data): """ pizzly needs the GTF to be in gene -> transcript -> exon order for each gene. it also wants the gene biotype set as the source """ if file_exists(out_file): return out_file db = gtf.get_gtf_db(gtf_file) with file_transaction(data, out_file) as tx_out_file: with open(tx_out_file, "w") as out_handle: for gene in db.features_of_type("gene"): children = [x for x in db.children(id=gene)] for child in children: if child.attributes.get("gene_biotype", None): gene_biotype = child.attributes.get("gene_biotype") gene.attributes['gene_biotype'] = gene_biotype gene.source = gene_biotype[0] print(gene, file=out_handle) for child in children: child.source = gene_biotype[0] # gffread produces a version-less FASTA file child.attributes.pop("transcript_version", None) print(child, file=out_handle) return out_file def flatten_pizzly(in_file, out_file, data): pizzlyflatten = config_utils.get_program("pizzly_flatten_json.py", data) if file_exists(out_file): return out_file cmd = "{pizzlyflatten} {in_file} > {tx_out_file}" message = "Flattening {in_file} to {out_file}." with file_transaction(data, out_file) as tx_out_file: do.run(cmd.format(**locals()), message.format(**locals())) return out_file def filter_pizzly(in_file, out_file, data): df = pd.read_csv(in_file, header=0, sep="\t") df = df.query('paircount > 1 and splitcount > 1') if file_exists(out_file): return out_file with file_transaction(out_file) as tx_out_file: df.to_csv(tx_out_file, sep="\t", index=False) return out_file
mit
ephes/scikit-learn
examples/decomposition/plot_ica_vs_pca.py
306
3329
""" ========================== FastICA on 2D point clouds ========================== This example illustrates visually in the feature space a comparison by results using two different component analysis techniques. :ref:`ICA` vs :ref:`PCA`. Representing ICA in the feature space gives the view of 'geometric ICA': ICA is an algorithm that finds directions in the feature space corresponding to projections with high non-Gaussianity. These directions need not be orthogonal in the original feature space, but they are orthogonal in the whitened feature space, in which all directions correspond to the same variance. PCA, on the other hand, finds orthogonal directions in the raw feature space that correspond to directions accounting for maximum variance. Here we simulate independent sources using a highly non-Gaussian process, 2 student T with a low number of degrees of freedom (top left figure). We mix them to create observations (top right figure). In this raw observation space, directions identified by PCA are represented by orange vectors. We represent the signal in the PCA space, after whitening by the variance corresponding to the PCA vectors (lower left). Running ICA corresponds to finding a rotation in this space to identify the directions of largest non-Gaussianity (lower right). """ print(__doc__) # Authors: Alexandre Gramfort, Gael Varoquaux # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA, FastICA ############################################################################### # Generate sample data rng = np.random.RandomState(42) S = rng.standard_t(1.5, size=(20000, 2)) S[:, 0] *= 2. # Mix data A = np.array([[1, 1], [0, 2]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations pca = PCA() S_pca_ = pca.fit(X).transform(X) ica = FastICA(random_state=rng) S_ica_ = ica.fit(X).transform(X) # Estimate the sources S_ica_ /= S_ica_.std(axis=0) ############################################################################### # Plot results def plot_samples(S, axis_list=None): plt.scatter(S[:, 0], S[:, 1], s=2, marker='o', zorder=10, color='steelblue', alpha=0.5) if axis_list is not None: colors = ['orange', 'red'] for color, axis in zip(colors, axis_list): axis /= axis.std() x_axis, y_axis = axis # Trick to get legend to work plt.plot(0.1 * x_axis, 0.1 * y_axis, linewidth=2, color=color) plt.quiver(0, 0, x_axis, y_axis, zorder=11, width=0.01, scale=6, color=color) plt.hlines(0, -3, 3) plt.vlines(0, -3, 3) plt.xlim(-3, 3) plt.ylim(-3, 3) plt.xlabel('x') plt.ylabel('y') plt.figure() plt.subplot(2, 2, 1) plot_samples(S / S.std()) plt.title('True Independent Sources') axis_list = [pca.components_.T, ica.mixing_] plt.subplot(2, 2, 2) plot_samples(X / np.std(X), axis_list=axis_list) legend = plt.legend(['PCA', 'ICA'], loc='upper right') legend.set_zorder(100) plt.title('Observations') plt.subplot(2, 2, 3) plot_samples(S_pca_ / np.std(S_pca_, axis=0)) plt.title('PCA recovered signals') plt.subplot(2, 2, 4) plot_samples(S_ica_ / np.std(S_ica_)) plt.title('ICA recovered signals') plt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.36) plt.show()
bsd-3-clause
elsuizo/Redes_fuzzy
Guia4/read_values_image.py
1
1328
#! /usr/bin/env python #-*- coding utf-8 -*- #*********************************************************************** # Imports import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg #*********************************************************************** class Cursor: def __init__(self, ax): self.ax = ax self.lx = ax.axhline(color='r') # the horiz line self.ly = ax.axvline(color='r') # the vert line self.x = 0 self.y = 0 self.pixel = [] # text location in axes coords self.txt = ax.text( 0.7, 0.9, '', transform=ax.transAxes) def mouse_move(self, event): if not event.inaxes: return self.x, self.y = event.xdata, event.ydata # update the line positions self.lx.set_ydata(self.y ) self.ly.set_xdata(self.x ) self.pixel.append(self.x) self.txt.set_text( 'x=%1.2f, y=%1.2f'%(self.x,self.y) ) plt.draw() fig, ax = plt.subplots() #fig = plt.figure() #ax = fig.add_subplot(111) cur = Cursor(ax) img = mpimg.imread('T1.png') cid = fig.canvas.mpl_connect('button_press_event', cur.mouse_move) print 'pixel (%s,%s)' % (cur.x,cur.y) plt.imshow(img) plt.show() print cur.pixel
gpl-3.0
mxlei01/healthcareai-py
example_advanced.py
4
6817
"""This file showcases some ways an advanced user can leverage the tools in healthcare.ai. Please use this example to learn about ways advanced users can utilize healthcareai If you have not installed healthcare.ai, refer to the instructions here: http://healthcareai-py.readthedocs.io To run this example: python3 example_advanced.py This code uses the diabetes sample data in datasets/data/diabetes.csv. """ import pandas as pd from sklearn.pipeline import Pipeline import healthcareai import healthcareai.common.filters as hcai_filters import healthcareai.common.transformers as hcai_transformers import healthcareai.trained_models.trained_supervised_model as hcai_tsm import healthcareai.pipelines.data_preparation as hcai_pipelines def main(): """Template script for ADVANCED USERS using healthcareai.""" # Load the included diabetes sample data dataframe = healthcareai.load_diabetes() # ...or load your own data from a .csv file: Uncomment to pull data from your CSV # dataframe = healthcareai.load_csv('path/to/your.csv') # ...or load data from a MSSQL server: Uncomment to pull data from MSSQL server # server = 'localhost' # database = 'SAM' # query = """SELECT * # FROM [SAM].[dbo].[DiabetesClincialSampleData] # -- In this step, just grab rows that have a target # WHERE ThirtyDayReadmitFLG is not null""" # # engine = hcai_db.build_mssql_engine_using_trusted_connections(server=server, database=database) # dataframe = pd.read_sql(query, engine) # Peek at the first 5 rows of data print(dataframe.head(5)) # Drop columns that won't help machine learning dataframe.drop(['PatientID'], axis=1, inplace=True) # Step 1: Prepare the data using optional imputation. There are two options for this: # ## Option 1: Use built in data prep pipeline that does enocding, imputation, null filtering, dummification clean_training_dataframe = hcai_pipelines.full_pipeline( 'classification', 'ThirtyDayReadmitFLG', 'PatientEncounterID', impute=True).fit_transform(dataframe) # ## Option 2: Build your own pipeline using healthcare.ai methods, your own, or a combination of either. # - Please note this is intentionally spartan, so we don't hinder your creativity. :) # - Also note that many of the healthcare.ai transformers intentionally return dataframes, compared to scikit that # return numpy arrays # custom_pipeline = Pipeline([ # ('remove_grain_column', hcai_filters.DataframeColumnRemover(columns_to_remove=['PatientEncounterID', 'PatientID'])), # ('imputation', hcai_transformers.DataFrameImputer(impute=True)), # ('convert_target_to_binary', hcai_transformers.DataFrameConvertTargetToBinary('classification', 'ThirtyDayReadmitFLG')), # # ('prediction_to_numeric', hcai_transformers.DataFrameConvertColumnToNumeric('ThirtyDayReadmitFLG')), # # ('create_dummy_variables', hcai_transformers.DataFrameCreateDummyVariables(excluded_columns=['ThirtyDayReadmitFLG'])), # ]) # # clean_training_dataframe = custom_pipeline.fit_transform(dataframe) # Step 2: Instantiate an Advanced Trainer class with your clean and prepared training data classification_trainer = healthcareai.AdvancedSupervisedModelTrainer( dataframe=clean_training_dataframe, model_type='classification', predicted_column='ThirtyDayReadmitFLG', grain_column='PatientEncounterID', verbose=False) # Step 3: split the data into train and test classification_trainer.train_test_split() # Step 4: Train some models # ## Train a KNN classifier with a randomized search over custom hyperparameters knn_hyperparameters = { 'algorithm': ['ball_tree', 'kd_tree'], 'n_neighbors': [1, 4, 6, 8, 10, 15, 20, 30, 50, 100, 200], 'weights': ['uniform', 'distance']} trained_knn = classification_trainer.knn( scoring_metric='accuracy', hyperparameter_grid=knn_hyperparameters, randomized_search=True, # Set this relative to the size of your hyperparameter space. Higher will train more models and be slower # Lower will be faster and possibly less performant number_iteration_samples=10 ) # ## Train a random forest classifier with a randomized search over custom hyperparameters # TODO these are bogus hyperparams for random forest random_forest_hyperparameters = { 'n_estimators': [50, 100, 200, 300], 'max_features': [1, 2, 3, 4], 'max_leaf_nodes': [None, 30, 400]} trained_random_forest = classification_trainer.random_forest_classifier( scoring_metric='accuracy', hyperparameter_grid=random_forest_hyperparameters, randomized_search=True, # Set this relative to the size of your hyperparameter space. Higher will train more models and be slower # Lower will be faster and possibly less performant number_iteration_samples=10 ) # Show the random forest feature importance graph hcai_tsm.plot_rf_features_from_tsm( trained_random_forest, classification_trainer.x_train, feature_limit=20, save=False) # ## Train a custom ensemble of models # The ensemble methods take a dictionary of TrainedSupervisedModels by a name of your choice custom_ensemble = { 'KNN': classification_trainer.knn( hyperparameter_grid=knn_hyperparameters, randomized_search=False, scoring_metric='roc_auc'), 'Logistic Regression': classification_trainer.logistic_regression(), 'Random Forest Classifier': classification_trainer.random_forest_classifier( randomized_search=False, scoring_metric='roc_auc')} trained_ensemble = classification_trainer.ensemble_classification( scoring_metric='roc_auc', trained_model_by_name=custom_ensemble) # Step 5: Evaluate and compare the models # Create a list of all the models you just trained that you want to compare models_to_compare = [trained_knn, trained_random_forest, trained_ensemble] # Create a ROC plot that compares all the them. hcai_tsm.tsm_classification_comparison_plots( trained_supervised_models=models_to_compare, plot_type='ROC', save=False) # Create a PR plot that compares all the them. hcai_tsm.tsm_classification_comparison_plots( trained_supervised_models=models_to_compare, plot_type='PR', save=False) # Inspect the raw ROC or PR cutoffs print(trained_random_forest.roc(print_output=False)) print(trained_random_forest.pr(print_output=False)) if __name__ == "__main__": main()
mit
terkkila/scikit-learn
sklearn/decomposition/__init__.py
99
1331
""" The :mod:`sklearn.decomposition` module includes matrix decomposition algorithms, including among others PCA, NMF or ICA. Most of the algorithms of this module can be regarded as dimensionality reduction techniques. """ from .nmf import NMF, ProjectedGradientNMF from .pca import PCA, RandomizedPCA from .incremental_pca import IncrementalPCA from .kernel_pca import KernelPCA from .sparse_pca import SparsePCA, MiniBatchSparsePCA from .truncated_svd import TruncatedSVD from .fastica_ import FastICA, fastica from .dict_learning import (dict_learning, dict_learning_online, sparse_encode, DictionaryLearning, MiniBatchDictionaryLearning, SparseCoder) from .factor_analysis import FactorAnalysis from ..utils.extmath import randomized_svd __all__ = ['DictionaryLearning', 'FastICA', 'IncrementalPCA', 'KernelPCA', 'MiniBatchDictionaryLearning', 'MiniBatchSparsePCA', 'NMF', 'PCA', 'ProjectedGradientNMF', 'RandomizedPCA', 'SparseCoder', 'SparsePCA', 'dict_learning', 'dict_learning_online', 'fastica', 'randomized_svd', 'sparse_encode', 'FactorAnalysis', 'TruncatedSVD']
bsd-3-clause
keflavich/APEX_CMZ_H2CO
analysis/dendro_glue_sm.py
2
3176
""" Load dendrograms into a Glue session """ from glue.core.data_factories import load_data,load_dendro from glue.core import DataCollection from glue.core.link_helpers import LinkSame from glue.qt.glue_application import GlueApplication from glue.core import Data, DataCollection, Component from glue.core.data_factories import astropy_tabular_data, load_data from glue.core.link_helpers import LinkSame, LinkTwoWay from glue.qt.glue_application import GlueApplication from glue.qt.widgets import ScatterWidget, ImageWidget from glue.qt.widgets.dendro_widget import DendroWidget from glue.qt.widgets.image_widget import StandaloneImageWidget from glue import qglue import matplotlib import numpy as np from astropy import units as u from astropy import coordinates from astropy import wcs from astropy.table import Table from astropy.io import ascii try: from paths import mpath,apath,fpath,molpath,hpath except ImportError: hpath = lambda x:x #load 2 datasets from files dendrogram = load_dendro(hpath('DendroMask_H2CO303202_smooth.hdf5')) dendro,sncube = dendrogram sncube.label='S/N Cube' cube = load_data(hpath('APEX_H2CO_303_202_smooth_bl.fits')) table = ascii.read(hpath('PPV_H2CO_Temperature_smooth.ipac'), format='ipac') table['glon'] = table['lon'] - 360*(table['lon'] > 180) table['xpix'] = table['x_cen'] # Glue "eats" these table['ypix'] = table['y_cen'] # Glue "eats" these catalog=Data(parent=table['parent'], label='Fitted Catalog') #catalog=Data() for column_name in table.columns: cc = table[column_name] uu = cc.unit if hasattr(cc, 'unit') else cc.units if cc.name == 'parent': cc.name = 'cat_parent' column_name = 'cat_parent' elif cc.name == 'height': cc.name = 'cat_height' column_name = 'cat_height' elif cc.name == 'peak': cc.name = 'cat_peak' column_name = 'cat_peak' nc = Component.autotyped(cc, units=uu) catalog.add_component(nc, column_name) # if column_name != 'parent' else '_flarent_' catalog.join_on_key(dendro, '_idx', dendro.pixel_component_ids[0]) dc = DataCollection(dendrogram) #dc = DataCollection([cube, dendrogram, catalog]) #dc.merge(cube,sncube) #sncube.join_on_key(dendro, 'structure', dendro.pixel_component_ids[0]) #dc.merge(catalog, dendro) # UNCOMMENT THIS LINE TO BREAK THE VIEWER dc.append(catalog) app = GlueApplication(dc) cube_viewer = app.new_data_viewer(ImageWidget) cube_viewer.add_data(sncube) # link positional information dc.add_link(LinkSame(sncube.id['structure'], catalog.id['_idx'])) #dc.add_link(LinkSame(image.id['World y: DEC--TAN'], catalog.id['DEJ2000'])) dc.add_link(LinkSame(cube.id['Galactic Longitude'], catalog.id['x_cen'])) dc.add_link(LinkSame(cube.id['Galactic Latitude'], catalog.id['y_cen'])) def ms_to_kms(x): return x/1e3 def kms_to_ms(x): return x*1e3 dc.add_link(LinkTwoWay(cube.id['Vrad'], catalog.id['v_cen'], ms_to_kms, kms_to_ms)) scatter = app.new_data_viewer(ScatterWidget) scatter.add_data(catalog) scatter.yatt = catalog.id['temperature_chi2'] scatter.xatt = catalog.id['area_exact'] dendview = app.new_data_viewer(DendroWidget) dendview.add_data(dendro) #start Glue app.start()
bsd-3-clause
Andrew-McNab-UK/DIRAC
Core/Utilities/Graphs/PieGraph.py
14
5528
######################################################################## # $HeadURL$ ######################################################################## """ PieGraph represents a pie graph The DIRAC Graphs package is derived from the GraphTool plotting package of the CMS/Phedex Project by ... <to be added> """ __RCSID__ = "$Id$" import numpy, math, time from matplotlib.patches import Wedge, Shadow from matplotlib.cbook import is_string_like from DIRAC.Core.Utilities.Graphs.PlotBase import PlotBase from DIRAC.Core.Utilities.Graphs.GraphData import GraphData from DIRAC.Core.Utilities.Graphs.GraphUtilities import * class PieGraph( PlotBase ): def __init__( self, data, ax, prefs, *args, **kw ): PlotBase.__init__( self, data, ax, prefs, *args, **kw ) self.pdata = data def pie( self, explode = None, colors = None, autopct = None, pctdistance = 0.6, shadow = False ): start = time.time() labels = self.pdata.getLabels() if labels[0][0] == "NoLabels": try: self.pdata.initialize(key_type='string') self.pdata.sortLabels() labels = self.pdata.getLabels() nLabels = self.pdata.getNumberOfLabels() explode = [0.] * nLabels if nLabels > 0: explode[0] = 0.1 except Exception,x: print "PieGraph Error: can not interpret data for the plot" #labels.reverse() values = [l[1] for l in labels] x = numpy.array( values, numpy.float64 ) self.legendData = labels sx = float( numpy.sum( x ) ) if sx > 1: x = numpy.divide( x, sx ) labels = [l[0] for l in labels] if explode is None: explode = [0] * len( x ) assert( len( x ) == len( labels ) ) assert( len( x ) == len( explode ) ) plot_axis_labels = self.prefs.get( 'plot_axis_labels', True ) center = 0, 0 radius = 1.1 theta1 = 0 i = 0 texts = [] slices = [] autotexts = [] for frac, label, expl in zip( x, labels, explode ): x, y = center theta2 = theta1 + frac thetam = 2 * math.pi * 0.5 * ( theta1 + theta2 ) x += expl * math.cos( thetam ) y += expl * math.sin( thetam ) color = self.palette.getColor( label ) w = Wedge( ( x, y ), radius, 360. * theta1, 360. * theta2, facecolor = color, lw = pixelToPoint( 0.5, self.dpi ), edgecolor = '#999999' ) slices.append( w ) self.ax.add_patch( w ) w.set_label( label ) if shadow: # make sure to add a shadow after the call to # add_patch so the figure and transform props will be # set shad = Shadow( w, -0.02, -0.02, #props={'facecolor':w.get_facecolor()} ) shad.set_zorder( 0.9 * w.get_zorder() ) self.ax.add_patch( shad ) if plot_axis_labels: if frac > 0.03: xt = x + 1.05 * radius * math.cos( thetam ) yt = y + 1.05 * radius * math.sin( thetam ) thetam %= 2 * math.pi if 0 < thetam and thetam < math.pi: valign = 'bottom' elif thetam == 0 or thetam == math.pi: valign = 'center' else: valign = 'top' if thetam > math.pi / 2.0 and thetam < 3.0 * math.pi / 2.0: halign = 'right' elif thetam == math.pi / 2.0 or thetam == 3.0 * math.pi / 2.0: halign = 'center' else: halign = 'left' t = self.ax.text( xt, yt, label, size = pixelToPoint( self.prefs['subtitle_size'], self.dpi ), horizontalalignment = halign, verticalalignment = valign ) t.set_family( self.prefs['font_family'] ) t.set_fontname( self.prefs['font'] ) t.set_size( pixelToPoint( self.prefs['text_size'], self.dpi ) ) texts.append( t ) if autopct is not None: xt = x + pctdistance * radius * math.cos( thetam ) yt = y + pctdistance * radius * math.sin( thetam ) if is_string_like( autopct ): s = autopct % ( 100. * frac ) elif callable( autopct ): s = autopct( 100. * frac ) else: raise TypeError( 'autopct must be callable or a format string' ) t = self.ax.text( xt, yt, s, horizontalalignment = 'center', verticalalignment = 'center' ) t.set_family( self.prefs['font_family'] ) t.set_fontname( self.prefs['font'] ) t.set_size( pixelToPoint( self.prefs['text_size'], self.dpi ) ) autotexts.append( t ) theta1 = theta2 i += 1 self.legendData.reverse() self.ax.set_xlim( ( -1.25, 1.25 ) ) self.ax.set_ylim( ( -1.25, 1.25 ) ) self.ax.set_axis_off() if autopct is None: return slices, texts else: return slices, texts, autotexts min_amount = .1 def getLegendData( self ): return self.legendData def draw( self ): self.ylabel = '' self.prefs['square_axis'] = True PlotBase.draw( self ) def my_display( x ): if x > 100 * self.min_amount: return '%.1f' % x + '%' else: return "" nLabels = self.pdata.getNumberOfLabels() explode = [0.] * nLabels if nLabels > 0: explode[0] = 0.1 self.wedges, text_labels, percent = self.pie( explode = explode, autopct = my_display )
gpl-3.0
grlee77/scipy
doc/source/tutorial/stats/plots/kde_plot4.py
142
1457
from functools import partial import numpy as np from scipy import stats import matplotlib.pyplot as plt def my_kde_bandwidth(obj, fac=1./5): """We use Scott's Rule, multiplied by a constant factor.""" return np.power(obj.n, -1./(obj.d+4)) * fac loc1, scale1, size1 = (-2, 1, 175) loc2, scale2, size2 = (2, 0.2, 50) x2 = np.concatenate([np.random.normal(loc=loc1, scale=scale1, size=size1), np.random.normal(loc=loc2, scale=scale2, size=size2)]) x_eval = np.linspace(x2.min() - 1, x2.max() + 1, 500) kde = stats.gaussian_kde(x2) kde2 = stats.gaussian_kde(x2, bw_method='silverman') kde3 = stats.gaussian_kde(x2, bw_method=partial(my_kde_bandwidth, fac=0.2)) kde4 = stats.gaussian_kde(x2, bw_method=partial(my_kde_bandwidth, fac=0.5)) pdf = stats.norm.pdf bimodal_pdf = pdf(x_eval, loc=loc1, scale=scale1) * float(size1) / x2.size + \ pdf(x_eval, loc=loc2, scale=scale2) * float(size2) / x2.size fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111) ax.plot(x2, np.zeros(x2.shape), 'b+', ms=12) ax.plot(x_eval, kde(x_eval), 'k-', label="Scott's Rule") ax.plot(x_eval, kde2(x_eval), 'b-', label="Silverman's Rule") ax.plot(x_eval, kde3(x_eval), 'g-', label="Scott * 0.2") ax.plot(x_eval, kde4(x_eval), 'c-', label="Scott * 0.5") ax.plot(x_eval, bimodal_pdf, 'r--', label="Actual PDF") ax.set_xlim([x_eval.min(), x_eval.max()]) ax.legend(loc=2) ax.set_xlabel('x') ax.set_ylabel('Density') plt.show()
bsd-3-clause
ChinaQuants/zipline
tests/serialization_cases.py
10
4121
import datetime import pytz import nose.tools as nt import pandas.util.testing as tm import pandas as pd from zipline.finance.blotter import Blotter, Order from zipline.finance.commission import PerShare, PerTrade, PerDollar from zipline.finance.performance.period import PerformancePeriod from zipline.finance.performance.position import Position from zipline.finance.performance.tracker import PerformanceTracker from zipline.finance.performance.position_tracker import PositionTracker from zipline.finance.risk.cumulative import RiskMetricsCumulative from zipline.finance.risk.period import RiskMetricsPeriod from zipline.finance.risk.report import RiskReport from zipline.finance.slippage import ( FixedSlippage, Transaction, VolumeShareSlippage ) from zipline.protocol import Account from zipline.protocol import Portfolio from zipline.protocol import Position as ProtocolPosition from zipline.finance.trading import SimulationParameters, TradingEnvironment from zipline.utils import factory def stringify_cases(cases, func=None): # get better test case names results = [] if func is None: def func(case): return case[0].__name__ for case in cases: new_case = list(case) key = func(case) new_case.insert(0, key) results.append(new_case) return results cases_env = TradingEnvironment() sim_params_daily = SimulationParameters( datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC), datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC), 10000, emission_rate='daily', env=cases_env) sim_params_minute = SimulationParameters( datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC), datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC), 10000, emission_rate='minute', env=cases_env) returns = factory.create_returns_from_list( [1.0], sim_params_daily) def object_serialization_cases(skip_daily=False): # Wrapped in a function to recreate DI objects. cases = [ (Blotter, (), {}, 'repr'), (Order, (datetime.datetime(2013, 6, 19), 8554, 100), {}, 'dict'), (PerShare, (), {}, 'dict'), (PerTrade, (), {}, 'dict'), (PerDollar, (), {}, 'dict'), (PerformancePeriod, (10000, cases_env.asset_finder), {'position_tracker': PositionTracker(cases_env.asset_finder)}, 'to_dict'), (Position, (8554,), {}, 'dict'), (PositionTracker, (cases_env.asset_finder,), {}, 'dict'), (PerformanceTracker, (sim_params_minute, cases_env), {}, 'to_dict'), (RiskMetricsCumulative, (sim_params_minute, cases_env), {}, 'to_dict'), (RiskMetricsPeriod, (returns.index[0], returns.index[0], returns, cases_env), {}, 'to_dict'), (RiskReport, (returns, sim_params_minute, cases_env), {}, 'to_dict'), (FixedSlippage, (), {}, 'dict'), (Transaction, (8554, 10, datetime.datetime(2013, 6, 19), 100, "0000"), {}, 'dict'), (VolumeShareSlippage, (), {}, 'dict'), (Account, (), {}, 'dict'), (Portfolio, (), {}, 'dict'), (ProtocolPosition, (8554,), {}, 'dict') ] if not skip_daily: cases.extend([ (PerformanceTracker, (sim_params_daily, cases_env), {}, 'to_dict'), (RiskMetricsCumulative, (sim_params_daily, cases_env), {}, 'to_dict'), (RiskReport, (returns, sim_params_daily, cases_env), {}, 'to_dict'), ]) return stringify_cases(cases) def assert_dict_equal(d1, d2): # check keys nt.assert_is_instance(d1, dict) nt.assert_is_instance(d2, dict) nt.assert_set_equal(set(d1.keys()), set(d2.keys())) for k in d1: v1 = d1[k] v2 = d2[k] asserter = nt.assert_equal if isinstance(v1, pd.DataFrame): asserter = tm.assert_frame_equal if isinstance(v1, pd.Series): asserter = tm.assert_series_equal try: asserter(v1, v2) except AssertionError: raise AssertionError('{k} is not equal'.format(k=k))
apache-2.0
CVML/scikit-learn
sklearn/cluster/__init__.py
364
1228
""" The :mod:`sklearn.cluster` module gathers popular unsupervised clustering algorithms. """ from .spectral import spectral_clustering, SpectralClustering from .mean_shift_ import (mean_shift, MeanShift, estimate_bandwidth, get_bin_seeds) from .affinity_propagation_ import affinity_propagation, AffinityPropagation from .hierarchical import (ward_tree, AgglomerativeClustering, linkage_tree, FeatureAgglomeration) from .k_means_ import k_means, KMeans, MiniBatchKMeans from .dbscan_ import dbscan, DBSCAN from .bicluster import SpectralBiclustering, SpectralCoclustering from .birch import Birch __all__ = ['AffinityPropagation', 'AgglomerativeClustering', 'Birch', 'DBSCAN', 'KMeans', 'FeatureAgglomeration', 'MeanShift', 'MiniBatchKMeans', 'SpectralClustering', 'affinity_propagation', 'dbscan', 'estimate_bandwidth', 'get_bin_seeds', 'k_means', 'linkage_tree', 'mean_shift', 'spectral_clustering', 'ward_tree', 'SpectralBiclustering', 'SpectralCoclustering']
bsd-3-clause
stapiar/stock-daily-data-webapp
functions/SymbolsManager.py
1
1947
''' Class that manage the list of symbols, this is maintained in a csv file defined in app_config.SYMBOLS_CSV_FILE. ''' import app_config from pandas import read_csv, DataFrame class SymbolsManager(object): symbol_list = [] def __init__(self): ''' Read the symbols param from the csv file defined in app_config.SYMBOLS_CSV_FILE. param symbols: pandas.core.frame.DataFrame, Data columns: symbol. ''' self.symbols = read_csv(app_config.SYMBOLS_CSV_FILE) def get_symbols(self): ''' Get a list of strings (symbols in the system). :param None :return: list of string. ''' return [row[1]["symbol"] for row in self.symbols.iterrows()] def save_symbols_to_csv(self): ''' Save the symbols param to the csv file defined in app_config.SYMBOLS_CSV_FILE. param symbols: pandas.core.frame.DataFrame, Data columns: symbol. ''' with open(app_config.SYMBOLS_CSV_FILE, "w") as f: self.symbols.to_csv(f, header=True, index=False) def remove_symbol(self, symbol): ''' function that remove a symbol from the system. Return False if there a error. :param symbol -- str, name of the symbol. :return: (bool, str) ''' self.symbols = self.symbols[self.symbols["symbol"] != symbol] self.save_symbols_to_csv() #return return (True, "OK") def add_symbol(self, symbol): ''' function that add a symbol to the system. Return False if there a error. :param symbol -- str, name of the symbol. :return: (bool, str) ''' if symbol not in self.symbols: self.symbols = self.symbols.append(DataFrame([symbol], columns=["symbol"])).sort(columns="symbol") self.save_symbols_to_csv() #return return (True, "OK")
bsd-2-clause
BartSiwek/Neurotransmitter2D
MeshCreator/src/Program.py
1
39689
import sys import math import matplotlib matplotlib.use('GTK') from matplotlib.figure import Figure from matplotlib.axes import Subplot from matplotlib.backends.backend_gtk import FigureCanvasGTK, NavigationToolbar from matplotlib.numerix import arange, sin, pi try: import pygtk pygtk.require("2.0") except: pass try: import gtk import gtk.glade import cairo from gtk import gdk except: sys.exit(1) from Pslg import Pslg, GridPoint, Segment, Hole, Region import ElementAwarePslg import PslgIo class PSLGView: pointDrawingSize = 2 pointBorderSize = 4 crossSize = 5 crossBorderSize = 7 def __init__(self, drawingArea): self.drawingArea = drawingArea self.pslg = Pslg() self.viewport = [(0, 0, 1.0)] self.selected = None def getSegmentsAsLinesList(self): psglSegments = self.pslg.getSegmentsAsLinesList() canvasSegments = [] for psglSegment in psglSegments: canvasStartPoint = self.psglToCanvas(*psglSegment[0:2]) canvasEndPoint = self.psglToCanvas(*psglSegment[2:4]) canvasSegments.append((canvasStartPoint[0], canvasStartPoint[1], canvasEndPoint[0], canvasEndPoint[1], psglSegment[4])) return canvasSegments def getPointsAsList(self): psglPoints = self.pslg.getPointsAsList() canvasPoints = [] for psglPoint in psglPoints: canvasPoints.append(self.psglToCanvas(*psglPoint)) return canvasPoints def getSelected(self): return self.selected def getHolesAsList(self): psglHoles = self.pslg.getHolesAsList() canvasHoles = [] for psglHole in psglHoles: canvasHoles.append(self.psglToCanvas(*psglHole)) return canvasHoles def getRegionsAsList(self): psglRegions = self.pslg.getRegionsAsList() canvasRegions = [] for psglRegion in psglRegions: canvasRegions.append(self.psglToCanvas(*psglRegion)) return canvasRegions def canvasToPsgl(self, x, y): allocation = self.drawingArea.get_allocation() curretViewport = self.viewport[-1] uniformX = x / allocation.width uniformY = y / allocation.height returnX = curretViewport[0] + uniformX / curretViewport[2] returnY = curretViewport[1] + uniformY / curretViewport[2] return (returnX, 1.0 - returnY) def psglToCanvas(self, x, y): size = self.drawingArea.get_allocation() curretViewport = self.viewport[-1] invX = x invY = 1 - y returnX = int((invX - curretViewport[0]) * curretViewport[2] * size.width ) returnY = int((invY - curretViewport[1]) * curretViewport[2] * size.height) return (returnX, returnY) def calculateDelta(self, canvasDelta): size = self.drawingArea.get_allocation() curretViewport = self.viewport[-1] return canvasDelta / (curretViewport[2] * size.width) def getCurrentFactor(self): return self.viewport[-1][2] def tryToSelectPoint(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) psglDelta = self.calculateDelta(PSLGView.pointBorderSize) if self.selected is not None and self.selected.__class__ is GridPoint: selectedDist = math.sqrt((self.selected.x - psglX) ** 2 + (self.selected.y - psglY) ** 2) if(selectedDist < psglDelta): return for point in self.pslg.points: dist = math.sqrt((point.x - psglX) ** 2 + (point.y - psglY) ** 2) if(dist < psglDelta): self.selected = point self.invalidateDrawingArea() return if self.selected is None: return self.selected = None self.invalidateDrawingArea() return def tryToSelectSegment(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) if self.selected is not None and self.selected.__class__ is Segment: if self.isPointOnSegment(self.selected, (psglX, psglY)): return for segment in self.pslg.segments: if self.isPointOnSegment(segment, (psglX, psglY)): self.selected = segment self.invalidateDrawingArea() return if self.selected is None: return self.selected = None self.invalidateDrawingArea() return def isPointOnSegment(self, segment, point): factor = self.viewport[-1][2] startPoint = (segment.startpoint.x, segment.startpoint.y) endPoint = (segment.endpoint.x, segment.endpoint.y) startEndVector = (endPoint[0] - startPoint[0], endPoint[1] - startPoint[1]) endStartVector = (startPoint[0] - endPoint[0], startPoint[1] - endPoint[1]) startPointVector = (point[0] - startPoint[0], point[1] - startPoint[1]) endPointVector = (point[0] - endPoint[0], point[1] - endPoint[1]) crossProduct = startEndVector[0] * startPointVector[1] - startEndVector[1] * startPointVector[0] if abs(crossProduct) <= 1E-3 / factor: #startEndVector * startPointVector dot1 = startEndVector[0] * startPointVector[0] + startEndVector[1] * startPointVector[1] #endStartVector * endPointVector dot2 = endStartVector[0] * endPointVector[0] + endStartVector[1] * endPointVector[1] if dot1 > 0 and dot2 > 0: return True return False def tryToSelectHole(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) psglDelta = self.calculateDelta(PSLGView.crossBorderSize) if self.selected is not None and self.selected.__class__ is Hole: selectedDist = math.sqrt((self.selected.x - psglX) ** 2 + (self.selected.y - psglY) ** 2) if(selectedDist < psglDelta): return for hole in self.pslg.holes: dist = math.sqrt((hole.x - psglX) ** 2 + (hole.y - psglY) ** 2) if(dist < psglDelta): self.selected = hole self.invalidateDrawingArea() return if self.selected is None: return self.selected = None self.invalidateDrawingArea() return def tryToSelectRegion(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) psglDelta = self.calculateDelta(PSLGView.crossBorderSize) if self.selected is not None and self.selected.__class__ is Region: selectedDist = math.sqrt((self.selected.x - psglX) ** 2 + (self.selected.y - psglY) ** 2) if(selectedDist < psglDelta): return for region in self.pslg.regions: dist = math.sqrt((region.x - psglX) ** 2 + (region.y - psglY) ** 2) if(dist < psglDelta): self.selected = region self.invalidateDrawingArea() return if self.selected is None: return self.selected = None self.invalidateDrawingArea() return def forceDeselect(self): if self.selected is not None: self.selected = None self.invalidateDrawingArea() def zoomIn(self, x, y): if(len(self.viewport) > 4): return allocation = self.drawingArea.get_allocation() oldX = self.viewport[-1][0] oldY = self.viewport[-1][1] oldFactor = self.viewport[-1][2] uniformX = x / allocation.width uniformY = y / allocation.height newX = max(oldX + uniformX / oldFactor - 1 / (4 * oldFactor), 0) newY = max(oldY + uniformY / oldFactor - 1 / (4 * oldFactor), 0) newFactor = 2 * oldFactor if newX + 1/newFactor > 1: newX = 1 - 1/newFactor if newY + 1/newFactor > 1: newY = 1 - 1/newFactor self.viewport.append((newX, newY, newFactor)) self.invalidateDrawingArea() def zoomOut(self): if(len(self.viewport) > 1): self.viewport.pop() self.invalidateDrawingArea() def pan(self, dx, dy): allocation = self.drawingArea.get_allocation() currentViewport = self.viewport.pop() uniformDeltaX = dx / allocation.width * 0.2 uniformDeltaY = dy / allocation.height * 0.2 newX = max(currentViewport[0] + uniformDeltaX, 0) newY = max(currentViewport[1] + uniformDeltaY, 0) factor = currentViewport[2] if newX + 1/factor > 1: newX = 1 - 1/factor if newY + 1/factor > 1: newY = 1 - 1/factor self.viewport.append((newX, newY, factor)) self.invalidateDrawingArea() def invalidateDrawingArea(self): allocation = self.drawingArea.get_allocation() self.drawingArea.window.invalidate_rect(gdk.Rectangle(0, 0, allocation.width, allocation.height), False) def addPoint(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) if self.selected is None or self.selected.__class__ is not GridPoint: self.pslg.points.append(GridPoint(psglX, psglY)) self.invalidateDrawingArea() def removePoint(self, point): if point is None: return toBeRemoved = [] for segment in self.pslg.segments: if segment.startpoint is point or segment.endpoint is point: toBeRemoved.append(segment) for removedSegment in toBeRemoved: self.pslg.segments.remove(removedSegment) self.pslg.points.remove(point) self.invalidateDrawingArea() def assignBoundaryMarkerToPoint(self, point, boundaryMarker): if point is None: return point.boundaryMarker = boundaryMarker def removeBoundaryMarkerFromPoint(self, point): if point is None: return point.boundaryMarker = None def addSegment(self, startPoint, endPoint): if startPoint is None or endPoint is None: return newSegment = Segment(startPoint, endPoint) if self.pslg.segments.count(newSegment) == 0: self.pslg.segments.append(newSegment) self.invalidateDrawingArea() def removeSelectedSegment(self): self.pslg.segments.remove(self.selected) self.forceDeselect() def addHole(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) if self.selected is None or self.selected.__class__ is not Hole: self.pslg.holes.append(Hole(psglX, psglY)) self.invalidateDrawingArea() def removeSelectedHole(self): self.pslg.holes.remove(self.selected) self.forceDeselect() def addRegion(self, x, y): (psglX, psglY) = self.canvasToPsgl(x, y) if self.selected is None or self.selected.__class__ is not Region: self.pslg.regions.append(Region(psglX, psglY)) self.invalidateDrawingArea() def removeRegion(self, region): if region is None: return self.pslg.regions.remove(region) self.forceDeselect() def assignIdToRegion(self, region, regionId): if region is None: return region.id = regionId def removeIdFromRegion(self, region): if region is None: return region.id = None def new(self): self.pslg = Pslg() self.viewport = [(0, 0, 1.0)] self.selected = None self.invalidateDrawingArea() def save(self, filename): file = open(filename, "w") try: PslgIo.saveToFile(file, self.pslg) finally: file.close() def load(self, filename): self.pslg = Pslg() self.viewport = [(0, 0, 1.0)] self.selected = None file = open(filename, "r") try: PslgIo.readFromFile(file, self.pslg, filename) self.invalidateDrawingArea() finally: file.close() class DummyState: def __init__(self, view): self.view = view def leftDown(self, event): pass def rightDown(self, event): pass def leftUp(self, event): pass def rightUp(self, event): pass def mouseMove(self, event): self.view.tryToSelectPoint(event.x, event.y) def mouseExit(self): self.view.forceDeselect() class ZoomState: def __init__(self, view): self.view = view def leftDown(self, event): self.view.zoomIn(event.x, event.y) def rightDown(self, event): self.view.zoomOut() def leftUp(self, event): pass def rightUp(self, event): pass def mouseMove(self, event): pass def mouseExit(self): pass class PanState: def __init__(self, view): self.view = view self.prevPoint = None def leftDown(self, event): self.prevPoint = (event.x, event.y) def rightDown(self, event): pass def leftUp(self, event): if self.prevPoint is not None: self.view.pan(*self.getPanVector(event.x, event.y)) self.prevPoint = None def rightUp(self, event): pass def mouseMove(self, event): if self.prevPoint is not None: self.view.pan(*self.getPanVector(event.x, event.y)) self.prevPoint = (event.x, event.y) def mouseExit(self): pass def getPanVector(self, x, y): return (self.prevPoint[0] - x, self.prevPoint[1] - y) class PointsState: def __init__(self, view, popupTree, boundaryMarkerSpinbutton): #Assigne members self.view = view self.popup = popupTree.get_widget("pointsMenu") self.boundaryMarkerSpinbutton = boundaryMarkerSpinbutton self.selectedPoint = None #Get menu items removePointMenuItem = popupTree.get_widget("removePointMenuitem") assignBoundaryMenuItem = popupTree.get_widget("assignBoundaryMenuitem") removeMarkerAssignmentMenuitem = popupTree.get_widget("removeMarkerAssignmentMenuitem") #Connect signals self.popup.connect("deactivate", self.onPointsMenuDeactivate) removePointMenuItem.connect("activate", self.onRemovePointMenuItemActivate) assignBoundaryMenuItem.connect("activate", self.onAssignBoundaryMenuItemActivate) removeMarkerAssignmentMenuitem.connect("activate", self.onRemoveMarkerAssignmentMenuItemActivate) def leftDown(self, event): self.view.addPoint(event.x, event.y) self.view.tryToSelectPoint(event.x, event.y) def rightDown(self, event): if self.view.selected is not None and self.view.selected.__class__ is GridPoint: self.selectedPoint = self.view.selected self.popup.popup(None, None, None, event.button, event.time) def leftUp(self, event): pass def rightUp(self, event): pass def mouseMove(self, event): self.view.tryToSelectPoint(event.x, event.y) def mouseExit(self): self.view.forceDeselect() def onPointsMenuDeactivate(self, widget): self.view.forceDeselect() def onRemovePointMenuItemActivate(self, widget): self.view.removePoint(self.selectedPoint) self.selectedPoint = None def onAssignBoundaryMenuItemActivate(self, widget): boundaryMarker = self.boundaryMarkerSpinbutton.get_value_as_int() self.view.assignBoundaryMarkerToPoint(self.selectedPoint, boundaryMarker) self.selectedPoint = None def onRemoveMarkerAssignmentMenuItemActivate(self, widget): self.view.removeBoundaryMarkerFromPoint(self.selectedPoint) self.selectedPoint = None class SegmentsState: def __init__(self, view): self.view = view self.newSegmentStartPoint = None def leftDown(self, event): if self.view.selected is not None and self.view.selected.__class__ is GridPoint: self.newSegmentStartPoint = self.view.selected else: self.newSegmentStartPoint = None def rightDown(self, event): if self.view.selected is not None and self.view.selected.__class__ is Segment: self.view.removeSelectedSegment() self.tryToSelect(event) def leftUp(self, event): if (self.view.selected is not None) and (self.view.selected.__class__ is GridPoint): startPoint = self.newSegmentStartPoint endPoint = self.view.selected if startPoint is not endPoint: self.view.addSegment(startPoint, endPoint) self.newSegmentStartPoint = None def rightUp(self, event): pass def mouseMove(self, event): self.tryToSelect(event) def mouseExit(self): self.view.forceDeselect() def tryToSelect(self, event): self.view.tryToSelectPoint(event.x, event.y) if self.view.selected is None: self.view.tryToSelectSegment(event.x, event.y) class HolesState: def __init__(self, view): self.view = view def leftDown(self, event): self.view.addHole(event.x, event.y) self.view.tryToSelectHole(event.x, event.y) def rightDown(self, event): if self.view.selected is not None and self.view.selected.__class__ is Hole: self.view.removeSelectedHole(); self.view.tryToSelectHole(event.x, event.y) def leftUp(self, event): pass def rightUp(self, event): pass def mouseMove(self, event): self.view.tryToSelectHole(event.x, event.y) def mouseExit(self): self.view.forceDeselect() class RegionsState: def __init__(self, view, popupTree, regionSpinButton): #Assign members self.view = view self.popup = popupTree.get_widget("regionsMenu") self.regionSpinButton = regionSpinButton self.selectedRegion = None #Get menu items removeRegionMenuitem = popupTree.get_widget("removeRegionMenuitem") assignRegionMenuitem = popupTree.get_widget("assignRegionMenuitem") removeRegionAssignmentMenuitem = popupTree.get_widget("removeRegionAssignmentMenuitem") #Connect signals self.popup.connect("deactivate", self.onRegionsMenuDeactivate) removeRegionMenuitem.connect("activate", self.onRemoveRegionMenuitemActivate) assignRegionMenuitem.connect("activate", self.onAssignRegionMenuitemActivate) removeRegionAssignmentMenuitem.connect("activate", self.onRemoveRegionAssignmentMenuItemActivate) def leftDown(self, event): self.view.addRegion(event.x, event.y) self.view.tryToSelectRegion(event.x, event.y) def rightDown(self, event): if self.view.selected is not None and self.view.selected.__class__ is Region: self.selectedRegion = self.view.selected self.popup.popup(None, None, None, event.button, event.time) def leftUp(self, event): pass def rightUp(self, event): pass def mouseMove(self, event): self.view.tryToSelectRegion(event.x, event.y) def mouseExit(self): self.view.forceDeselect() def onRegionsMenuDeactivate(self, widget): self.view.forceDeselect() def onRemoveRegionMenuitemActivate(self, widget): self.view.removeRegion(self.selectedRegion) self.selectedRegion = None def onAssignRegionMenuitemActivate(self, widget): regionId = self.regionSpinButton.get_value_as_int() self.view.assignIdToRegion(self.selectedRegion, regionId) self.selectedRegion = None def onRemoveRegionAssignmentMenuItemActivate(self, widget): self.view.removeIdFromRegion(self.selectedRegion) self.selectedRegion = None class MeshCreatorGui: mousePositionContextId = "Mouse position" def __init__(self): gladefile = "../ui/MeshCreator/MeshCreator.glade" self.windowname = "MainWindow" self.wTree = gtk.glade.XML(gladefile, self.windowname) self.statusBar = self.wTree.get_widget("StatusBar") self.drawingArea = self.wTree.get_widget("MainDrawingArea") self.boundaryMarkerSpinButton = self.wTree.get_widget("boundaryMarkerSpinButton") self.regionSpinButton = self.wTree.get_widget("regionSpinButton") self.toolboxContainer = self.wTree.get_widget("toolboxContainer") self.mainWindow = self.wTree.get_widget("MainWindow") self.background = None; self.pointsPopoupMenuTree = gtk.glade.XML(gladefile, "pointsMenu") pointsPopoupMenu = self.pointsPopoupMenuTree.get_widget("pointsMenu") pointsPopoupMenu.attach_to_widget(self.drawingArea, None) self.regionsPopoupMenuTree = gtk.glade.XML(gladefile, "regionsMenu") regionsPopoupMenu = self.regionsPopoupMenuTree.get_widget("regionsMenu") regionsPopoupMenu.attach_to_widget(self.drawingArea, None) dic = {"on_MainWindow_destroy" : gtk.main_quit, "on_quit_activate" : gtk.main_quit, "on_MainDrawingArea_motion_notify" : self.onMainDrawingAreaMotion, "on_MainDrawingArea_leave_notify" : self.onMainDrawingAreaLeave, "on_MainDrawingArea_expose" : self.onMainDrawingAreaExpose, "on_MainDrawingArea_configure" : self.onMainDrawingAreaConfigure, "on_MainDrawingArea_button_press" : self.onMainDrawingAreaButtonPress, "on_MainDrawingArea_button_release" : self.onMainDrawingAreaButtonRelease, "on_zoomToggleButton_toggled" : self.onZoomToggleButtonToggled, "on_panToggleButton_toggled" : self.onPanToggleButtonToggled, "on_pointsToggleButton_toggled" : self.onPointsToggleButtonToggled, "on_segmentsToggleButton_toggled" : self.onSegmentsToggleButtonToggled, "on_holesToggleButton_toggled" : self.onHolesToggleButtonToggled, "on_regionsToggleButton_toggled" : self.onRegionsToggleButtonToggled, "on_new_activate" : self.onMenuNewActivate, "on_open_activate" : self.onMenuOpenActivate, "on_save_activate" : self.onMenuSaveActivate, "on_save_as_activate" : self.onMenuSaveAsActivate, "on_open_background_activate" : self.onMenuOpenBackgroundActivate, "on_close_background_activate" : self.onMenuCloseBackgroundActivate } self.wTree.signal_autoconnect(dic) self.titleBase = self.mainWindow.get_title() self.filename = None self.pslgView = PSLGView(self.drawingArea) self.state = DummyState(self.pslgView) def onMainDrawingAreaMotion(self, widget, event): self.updateStatusBar(event.x, event.y) self.state.mouseMove(event) return True def onMainDrawingAreaLeave(self, widget, event): self.updateStatusBar(-1, -1) self.state.mouseExit() return True def onZoomToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = ZoomState(self.pslgView) else: self.state = DummyState(self.pslgView) return True def onPanToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = PanState(self.pslgView) else: self.state = DummyState(self.pslgView) return True def onPointsToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = PointsState(self.pslgView, self.pointsPopoupMenuTree, self.boundaryMarkerSpinButton) else: self.state = DummyState(self.pslgView) return True def onSegmentsToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = SegmentsState(self.pslgView) else: self.state = DummyState(self.pslgView) return True def onHolesToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = HolesState(self.pslgView) else: self.state = DummyState(self.pslgView) return True def onRegionsToggleButtonToggled(self, widget): self.untoggleAllExcept(widget) if widget.get_active(): self.state = RegionsState(self.pslgView, self.regionsPopoupMenuTree, self.regionSpinButton) else: self.state = DummyState(self.pslgView) return True def onMainDrawingAreaExpose(self, widget, event): allocation = self.drawingArea.get_allocation() gc = self.drawingArea.window.cairo_create() gc.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) gc.clip() #Clear gc.set_line_width(1) gc.set_source_rgb(1,1,1) gc.rectangle(0, 0, allocation.width, allocation.height) gc.fill() gc.set_source_rgb(0,0,0) gc.rectangle(0, 0, allocation.width, allocation.height) gc.stroke() #Draw background if self.background is not None: zero = self.pslgView.psglToCanvas(0.0, 1.0) factor = self.pslgView.getCurrentFactor() newWidth = int(factor * allocation.width) newHeight = int(factor * allocation.height) gc.save() resizedPixbuf = self.background.scale_simple(newWidth, newHeight, gdk.INTERP_TILES) gc.set_source_pixbuf(resizedPixbuf, zero[0], zero[1]) gc.paint() gc.restore() #Draw lines gc.set_source_rgb(0,0,0) lines = self.pslgView.getSegmentsAsLinesList() for line in lines: if(line[4] == self.boundaryMarkerSpinButton.get_value_as_int()): gc.set_line_width(5) else: gc.set_line_width(1) gc.move_to(line[0], line[1]) gc.line_to(line[2], line[3]) gc.stroke() #Draw points gc.set_line_width(1) gc.set_source_rgb(0,0,0) points = self.pslgView.getPointsAsList() for point in points: gc.arc(point[0], point[1], PSLGView.pointDrawingSize, 0, 2 * pi) gc.fill() #Draw holes gc.set_line_width(2) gc.set_source_rgb(0,0,0) holes = self.pslgView.getHolesAsList() for hole in holes: gc.move_to(hole[0] - PSLGView.crossSize, hole[1] - PSLGView.crossSize) gc.line_to(hole[0] + PSLGView.crossSize, hole[1] + PSLGView.crossSize) gc.stroke() gc.move_to(hole[0] - PSLGView.crossSize, hole[1] + PSLGView.crossSize) gc.line_to(hole[0] + PSLGView.crossSize, hole[1] - PSLGView.crossSize) gc.stroke() #Draw regions gc.set_line_width(2) gc.set_source_rgb(1.0,0,0) regions = self.pslgView.getRegionsAsList() for region in regions: # gc.set_source_rgb(0.75,0.75,0.75) # gc.arc(region[0], region[1], # 2, # 0, 2 * pi) # gc.fill() gc.move_to(region[0] - PSLGView.crossSize, region[1] - PSLGView.crossSize) gc.line_to(region[0] + PSLGView.crossSize, region[1] + PSLGView.crossSize) gc.stroke() gc.move_to(region[0] - PSLGView.crossSize, region[1] + PSLGView.crossSize) gc.line_to(region[0] + PSLGView.crossSize, region[1] - PSLGView.crossSize) gc.stroke() #Draw selected self.drawSelected(gc) #Return return True def drawSelected(self, gc): #Draw slected point selected = self.pslgView.getSelected() if selected is not None and selected.__class__ is GridPoint: (cx, cy) = self.pslgView.psglToCanvas(selected.x, selected.y) gc.set_line_width(1) gc.set_source_rgb(1,0.6,0) gc.arc(cx, cy, PSLGView.pointDrawingSize, 0, 2 * pi) gc.fill() if selected.boundaryMarker is not None: self.draw_text(gc, cx + PSLGView.pointDrawingSize, cy + PSLGView.pointDrawingSize, str(selected.boundaryMarker)) #Draw selected segment if selected is not None and selected.__class__ is Segment: psglStartPoint = selected.startpoint psglEndPoint = selected.endpoint (startCanvasX, startCanvasY) = self.pslgView.psglToCanvas(psglStartPoint.x, psglStartPoint.y) (endCanvasX, endCanvasY) = self.pslgView.psglToCanvas(psglEndPoint.x, psglEndPoint.y) gc.set_line_width(1) gc.set_source_rgb(1,0.6,0) gc.move_to(startCanvasX, startCanvasY) gc.line_to(endCanvasX, endCanvasY) gc.stroke() #Draw selected hole if selected is not None and selected.__class__ is Hole: (cx, cy) = self.pslgView.psglToCanvas(selected.x, selected.y) gc.set_line_width(2) gc.set_source_rgb(0.6,1,0) gc.move_to(cx - PSLGView.crossSize, cy - PSLGView.crossSize) gc.line_to(cx + PSLGView.crossSize, cy + PSLGView.crossSize) gc.stroke() gc.move_to(cx - PSLGView.crossSize, cy + PSLGView.crossSize) gc.line_to(cx + PSLGView.crossSize, cy - PSLGView.crossSize) gc.stroke() #Draw selected region if selected is not None and selected.__class__ is Region: (cx, cy) = self.pslgView.psglToCanvas(selected.x, selected.y) gc.set_line_width(2) gc.set_source_rgb(0.6,1,0) gc.move_to(cx - PSLGView.crossSize, cy - PSLGView.crossSize) gc.line_to(cx + PSLGView.crossSize, cy + PSLGView.crossSize) gc.stroke() gc.move_to(cx - PSLGView.crossSize, cy + PSLGView.crossSize) gc.line_to(cx + PSLGView.crossSize, cy - PSLGView.crossSize) gc.stroke() if selected.id is not None: self.draw_text(gc, cx + PSLGView.crossSize, cy - PSLGView.crossSize, str(selected.id)) def draw_text(self, gc, x, y, text): size = 10 pad = 2 font = "Sans" gc.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) gc.set_font_size(size) gc.set_line_width(0.5) gc.set_source_rgb(0,0,0) gc.move_to(x + pad, y + pad) gc.text_path(text) gc.fill() def onMainDrawingAreaConfigure(self, widget, event): return True def onMainDrawingAreaButtonPress(self, widget, event): if(event.button == 1): self.state.leftDown(event) if(event.button == 3): self.state.rightDown(event) return True def onMainDrawingAreaButtonRelease(self, widget, event): if(event.button == 1): self.state.leftUp(event) if(event.button == 3): self.state.rightUp(event) return True def untoggleAllExcept(self, widget): if widget is not None and not widget.get_active(): return for child in self.toolboxContainer: if child.__class__ is gtk.ToggleButton and child != widget: child.set_active(0) def updateStatusBar(self, eventX, eventY): context_id = self.statusBar.get_context_id(MeshCreatorGui.mousePositionContextId); self.statusBar.pop(context_id) if eventX >= 0 and eventY >= 0: (x, y) = self.pslgView.canvasToPsgl(eventX, eventY) (xC, yC) = self.pslgView.psglToCanvas(x, y) message = "X: " + str(x) + " Y: " + str(y) + " (" + str(xC) + ", " + str(yC) + ")" self.statusBar.push(context_id, message) def onMenuNewActivate(self, widget): self.pslgView.new() self.set_filename(None) def set_filename(self, filename): if filename is None: self.mainWindow.set_title(self.titleBase + " [Untitled]") else: self.mainWindow.set_title(self.titleBase + " [" + filename + "]") self.filename = filename def onMenuOpenActivate(self, widget): openFileChooser = gtk.FileChooserDialog(title="Open File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK) ) filter = gtk.FileFilter() filter.set_name(".poly files") filter.add_pattern("*.poly") openFileChooser.add_filter(filter) filter = gtk.FileFilter() filter.set_name(".node files") filter.add_pattern("*.node") openFileChooser.add_filter(filter) filter = gtk.FileFilter() filter.set_name(".ele files") filter.add_pattern("*.ele") openFileChooser.add_filter(filter) if openFileChooser.run() == gtk.RESPONSE_OK: try: choosenFilename = openFileChooser.get_filename() self.pslgView.load(choosenFilename) self.set_filename(choosenFilename) except Exception, error: #Show error errorDialog = gtk.MessageDialog(buttons=gtk.BUTTONS_OK, type=gtk.MESSAGE_ERROR) errorDialog.set_title("Error") errorDialog.set_markup("Error has occured:") errorDialog.format_secondary_text(str(error)) errorDialog.run() errorDialog.destroy() #Unload file self.onMenuNewActivate(widget) raise openFileChooser.destroy() def onMenuSaveActivate(self, widget): if self.filename is None: self.onMenuSaveAsActivate(widget) else: self.pslgView.save(self.filename) def onMenuSaveAsActivate(self, widget): saveFileChooser = gtk.FileChooserDialog(title="Open File", action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK) ) filter = gtk.FileFilter() filter.set_name(".poly files") filter.add_pattern("*.poly") saveFileChooser.set_do_overwrite_confirmation(True) saveFileChooser.add_filter(filter) if saveFileChooser.run() == gtk.RESPONSE_OK: try: choosenFilename = saveFileChooser.get_filename() self.pslgView.save(choosenFilename) self.set_filename(choosenFilename) except Exception, error: #Show error errorDialog = gtk.MessageDialog(buttons=gtk.BUTTONS_OK, type=gtk.MESSAGE_ERROR) errorDialog.set_title("Error") errorDialog.set_markup("Error has occured:") errorDialog.format_secondary_text(str(error)) errorDialog.run() errorDialog.destroy() raise saveFileChooser.destroy() def onMenuOpenBackgroundActivate(self, widget): openFileChooser = gtk.FileChooserDialog(title="Open File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK) ) filter = gtk.FileFilter() filter.set_name("Image files") filter.add_pattern("*.bmp") filter.add_pattern("*.jpg") filter.add_pattern("*.jpeg") filter.add_pattern("*.gif") filter.add_pattern("*.png") openFileChooser.add_filter(filter) if openFileChooser.run() == gtk.RESPONSE_OK: try: #Read image choosenFilename = openFileChooser.get_filename() self.background = gdk.pixbuf_new_from_file(choosenFilename) #Force redraw self.pslgView.invalidateDrawingArea() except Exception, error: #Show error errorDialog = gtk.MessageDialog(buttons=gtk.BUTTONS_OK, type=gtk.MESSAGE_ERROR) errorDialog.set_title("Error") errorDialog.set_markup("Error has occured:") errorDialog.format_secondary_text(str(error)) errorDialog.run() errorDialog.destroy() #Unload file self.onMenuNewActivate(widget) raise openFileChooser.destroy() def onMenuCloseBackgroundActivate(self, widget): self.background = None self.pslgView.invalidateDrawingArea() if __name__ == "__main__": MeshCreatorGui() gtk.main()
mit
cdegroc/scikit-learn
examples/decomposition/plot_ica_vs_pca.py
2
3177
""" ========================== FastICA on 2D point clouds ========================== Illustrate visually the results of :ref:`ICA` vs :ref:`PCA` in the feature space. Representing ICA in the feature space gives the view of 'geometric ICA': ICA is an algorithm that finds directions in the feature space corresponding to projections with high non-Gaussianity. These directions need not be orthogonal in the original feature space, but they are orthogonal in the whitened feature space, in which all directions correspond to the same variance. PCA, on the other hand, finds orthogonal directions in the raw feature space that correspond to directions accounting for maximum variance. Here we simulate independent sources using a highly non-Gaussian process, 2 student T with a low number of degrees of freedom (top left figure). We mix them to create observations (top right figure). In this raw observation space, directions identified by PCA are represented by green vectors. We represent the signal in the PCA space, after whitening by the variance corresponding to the PCA vectors (lower left). Running ICA corresponds to finding a rotation in this space to identify the directions of largest non-Gaussianity (lower right). """ print __doc__ # Authors: Alexandre Gramfort, Gael Varoquaux # License: BSD import numpy as np import pylab as pl from sklearn.decomposition import PCA, FastICA ############################################################################### # Generate sample data S = np.random.standard_t(1.5, size=(10000, 2)) S[0] *= 2. # Mix data A = np.array([[1, 1], [0, 2]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations pca = PCA() S_pca_ = pca.fit(X).transform(X) ica = FastICA() S_ica_ = ica.fit(X).transform(X) # Estimate the sources S_ica_ /= S_ica_.std(axis=0) ############################################################################### # Plot results def plot_samples(S, axis_list=None): pl.scatter(S[:, 0], S[:, 1], s=2, marker='o', linewidths=0, zorder=10) if axis_list is not None: colors = [(0, 0.6, 0), (0.6, 0, 0)] for color, axis in zip(colors, axis_list): axis /= axis.std() x_axis, y_axis = axis # Trick to get legend to work pl.plot(0.1 * x_axis, 0.1 * y_axis, linewidth=2, color=color) # pl.quiver(x_axis, y_axis, x_axis, y_axis, zorder=11, width=0.01, pl.quiver(0, 0, x_axis, y_axis, zorder=11, width=0.01, scale=6, color=color) pl.hlines(0, -3, 3) pl.vlines(0, -3, 3) pl.xlim(-3, 3) pl.ylim(-3, 3) pl.xlabel('x') pl.ylabel('y') pl.subplot(2, 2, 1) plot_samples(S / S.std()) pl.title('True Independent Sources') axis_list = [pca.components_.T, ica.get_mixing_matrix()] pl.subplot(2, 2, 2) plot_samples(X / np.std(X), axis_list=axis_list) pl.legend(['PCA', 'ICA'], loc='upper left') pl.title('Observations') pl.subplot(2, 2, 3) plot_samples(S_pca_ / np.std(S_pca_, axis=0)) pl.title('PCA scores') pl.subplot(2, 2, 4) plot_samples(S_ica_ / np.std(S_ica_)) pl.title('ICA estimated sources') pl.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.26) pl.show()
bsd-3-clause
drewokane/seaborn
examples/pairgrid_dotplot.py
27
1056
""" Dot plot with several variables =============================== _thumb: .3, .3 """ import seaborn as sns sns.set(style="whitegrid") # Load the dataset crashes = sns.load_dataset("car_crashes") # Make the PairGrid g = sns.PairGrid(crashes.sort("total", ascending=False), x_vars=crashes.columns[:-3], y_vars=["abbrev"], size=10, aspect=.25) # Draw a dot plot using the stripplot function g.map(sns.stripplot, size=10, orient="h", palette="Reds_r", edgecolor="gray") # Use the same x axis limits on all columns and add better labels g.set(xlim=(0, 25), xlabel="Crashes", ylabel="") # Use semantically meaningful titles for the columns titles = ["Total crashes", "Speeding crashes", "Alcohol crashes", "Not distracted crashes", "No previous crashes"] for ax, title in zip(g.axes.flat, titles): # Set a different title for each axes ax.set(title=title) # Make the grid horizontal instead of vertical ax.xaxis.grid(False) ax.yaxis.grid(True) sns.despine(left=True, bottom=True)
bsd-3-clause
tmhm/scikit-learn
examples/model_selection/plot_underfitting_overfitting.py
230
2649
""" ============================ Underfitting vs. Overfitting ============================ This example demonstrates the problems of underfitting and overfitting and how we can use linear regression with polynomial features to approximate nonlinear functions. The plot shows the function that we want to approximate, which is a part of the cosine function. In addition, the samples from the real function and the approximations of different models are displayed. The models have polynomial features of different degrees. We can see that a linear function (polynomial with degree 1) is not sufficient to fit the training samples. This is called **underfitting**. A polynomial of degree 4 approximates the true function almost perfectly. However, for higher degrees the model will **overfit** the training data, i.e. it learns the noise of the training data. We evaluate quantitatively **overfitting** / **underfitting** by using cross-validation. We calculate the mean squared error (MSE) on the validation set, the higher, the less likely the model generalizes correctly from the training data. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn import cross_validation np.random.seed(0) n_samples = 30 degrees = [1, 4, 15] true_fun = lambda X: np.cos(1.5 * np.pi * X) X = np.sort(np.random.rand(n_samples)) y = true_fun(X) + np.random.randn(n_samples) * 0.1 plt.figure(figsize=(14, 5)) for i in range(len(degrees)): ax = plt.subplot(1, len(degrees), i + 1) plt.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) pipeline.fit(X[:, np.newaxis], y) # Evaluate the models using crossvalidation scores = cross_validation.cross_val_score(pipeline, X[:, np.newaxis], y, scoring="mean_squared_error", cv=10) X_test = np.linspace(0, 1, 100) plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model") plt.plot(X_test, true_fun(X_test), label="True function") plt.scatter(X, y, label="Samples") plt.xlabel("x") plt.ylabel("y") plt.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format( degrees[i], -scores.mean(), scores.std())) plt.show()
bsd-3-clause
Lab603/PicEncyclopedias
jni-build/jni-build/jni/include/tensorflow/contrib/learn/python/learn/estimators/_sklearn.py
153
6723
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """sklearn cross-support.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import os import numpy as np import six def _pprint(d): return ', '.join(['%s=%s' % (key, str(value)) for key, value in d.items()]) class _BaseEstimator(object): """This is a cross-import when sklearn is not available. Adopted from sklearn.BaseEstimator implementation. https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py """ def get_params(self, deep=True): """Get parameters for this estimator. Args: deep: boolean, optional If `True`, will return the parameters for this estimator and contained subobjects that are estimators. Returns: params : mapping of string to any Parameter names mapped to their values. """ out = dict() param_names = [name for name in self.__dict__ if not name.startswith('_')] for key in param_names: value = getattr(self, key, None) if isinstance(value, collections.Callable): continue # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Args: **params: Parameters. Returns: self Raises: ValueError: If params contain invalid names. """ if not params: # Simple optimisation to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) for key, value in six.iteritems(params): split = key.split('__', 1) if len(split) > 1: # nested objects case name, sub_name = split if name not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (name, self)) sub_object = valid_params[name] sub_object.set_params(**{sub_name: value}) else: # simple objects case if key not in valid_params: raise ValueError('Invalid parameter %s for estimator %s. ' 'Check the list of available parameters ' 'with `estimator.get_params().keys()`.' % (key, self.__class__.__name__)) setattr(self, key, value) return self def __repr__(self): class_name = self.__class__.__name__ return '%s(%s)' % (class_name, _pprint(self.get_params(deep=False)),) # pylint: disable=old-style-class class _ClassifierMixin(): """Mixin class for all classifiers.""" pass class _RegressorMixin(): """Mixin class for all regression estimators.""" pass class _TransformerMixin(): """Mixin class for all transformer estimators.""" class NotFittedError(ValueError, AttributeError): """Exception class to raise if estimator is used before fitting. This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility. Examples: >>> from sklearn.svm import LinearSVC >>> from sklearn.exceptions import NotFittedError >>> try: ... LinearSVC().predict([[1, 2], [2, 3], [3, 4]]) ... except NotFittedError as e: ... print(repr(e)) ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS NotFittedError('This LinearSVC instance is not fitted yet',) Copied from https://github.com/scikit-learn/scikit-learn/master/sklearn/exceptions.py """ # pylint: enable=old-style-class def _accuracy_score(y_true, y_pred): score = y_true == y_pred return np.average(score) def _mean_squared_error(y_true, y_pred): if len(y_true.shape) > 1: y_true = np.squeeze(y_true) if len(y_pred.shape) > 1: y_pred = np.squeeze(y_pred) return np.average((y_true - y_pred)**2) def _train_test_split(*args, **options): # pylint: disable=missing-docstring test_size = options.pop('test_size', None) train_size = options.pop('train_size', None) random_state = options.pop('random_state', None) if test_size is None and train_size is None: train_size = 0.75 elif train_size is None: train_size = 1 - test_size train_size = int(train_size * args[0].shape[0]) np.random.seed(random_state) indices = np.random.permutation(args[0].shape[0]) train_idx, test_idx = indices[:train_size], indices[train_size:] result = [] for x in args: result += [x.take(train_idx, axis=0), x.take(test_idx, axis=0)] return tuple(result) # If "TENSORFLOW_SKLEARN" flag is defined then try to import from sklearn. TRY_IMPORT_SKLEARN = os.environ.get('TENSORFLOW_SKLEARN', False) if TRY_IMPORT_SKLEARN: # pylint: disable=g-import-not-at-top,g-multiple-import,unused-import from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin, TransformerMixin from sklearn.metrics import accuracy_score, log_loss, mean_squared_error from sklearn.cross_validation import train_test_split try: from sklearn.exceptions import NotFittedError except ImportError: try: from sklearn.utils.validation import NotFittedError except ImportError: pass else: # Naive implementations of sklearn classes and functions. BaseEstimator = _BaseEstimator ClassifierMixin = _ClassifierMixin RegressorMixin = _RegressorMixin TransformerMixin = _TransformerMixin accuracy_score = _accuracy_score log_loss = None mean_squared_error = _mean_squared_error train_test_split = _train_test_split
mit
btabibian/scikit-learn
sklearn/feature_selection/tests/test_mutual_info.py
30
6881
from __future__ import division import numpy as np from numpy.testing import run_module_suite from scipy.sparse import csr_matrix from sklearn.utils.testing import (assert_array_equal, assert_almost_equal, assert_false, assert_raises, assert_equal, assert_allclose, assert_greater) from sklearn.feature_selection.mutual_info_ import ( mutual_info_regression, mutual_info_classif, _compute_mi) def test_compute_mi_dd(): # In discrete case computations are straightforward and can be done # by hand on given vectors. x = np.array([0, 1, 1, 0, 0]) y = np.array([1, 0, 0, 0, 1]) H_x = H_y = -(3/5) * np.log(3/5) - (2/5) * np.log(2/5) H_xy = -1/5 * np.log(1/5) - 2/5 * np.log(2/5) - 2/5 * np.log(2/5) I_xy = H_x + H_y - H_xy assert_almost_equal(_compute_mi(x, y, True, True), I_xy) def test_compute_mi_cc(): # For two continuous variables a good approach is to test on bivariate # normal distribution, where mutual information is known. # Mean of the distribution, irrelevant for mutual information. mean = np.zeros(2) # Setup covariance matrix with correlation coeff. equal 0.5. sigma_1 = 1 sigma_2 = 10 corr = 0.5 cov = np.array([ [sigma_1**2, corr * sigma_1 * sigma_2], [corr * sigma_1 * sigma_2, sigma_2**2] ]) # True theoretical mutual information. I_theory = (np.log(sigma_1) + np.log(sigma_2) - 0.5 * np.log(np.linalg.det(cov))) np.random.seed(0) Z = np.random.multivariate_normal(mean, cov, size=1000) x, y = Z[:, 0], Z[:, 1] # Theory and computed values won't be very close, assert that the # first figures after decimal point match. for n_neighbors in [3, 5, 7]: I_computed = _compute_mi(x, y, False, False, n_neighbors) assert_almost_equal(I_computed, I_theory, 1) def test_compute_mi_cd(): # To test define a joint distribution as follows: # p(x, y) = p(x) p(y | x) # X ~ Bernoulli(p) # (Y | x = 0) ~ Uniform(-1, 1) # (Y | x = 1) ~ Uniform(0, 2) # Use the following formula for mutual information: # I(X; Y) = H(Y) - H(Y | X) # Two entropies can be computed by hand: # H(Y) = -(1-p)/2 * ln((1-p)/2) - p/2*log(p/2) - 1/2*log(1/2) # H(Y | X) = ln(2) # Now we need to implement sampling from out distribution, which is # done easily using conditional distribution logic. n_samples = 1000 np.random.seed(0) for p in [0.3, 0.5, 0.7]: x = np.random.uniform(size=n_samples) > p y = np.empty(n_samples) mask = x == 0 y[mask] = np.random.uniform(-1, 1, size=np.sum(mask)) y[~mask] = np.random.uniform(0, 2, size=np.sum(~mask)) I_theory = -0.5 * ((1 - p) * np.log(0.5 * (1 - p)) + p * np.log(0.5 * p) + np.log(0.5)) - np.log(2) # Assert the same tolerance. for n_neighbors in [3, 5, 7]: I_computed = _compute_mi(x, y, True, False, n_neighbors) assert_almost_equal(I_computed, I_theory, 1) def test_compute_mi_cd_unique_label(): # Test that adding unique label doesn't change MI. n_samples = 100 x = np.random.uniform(size=n_samples) > 0.5 y = np.empty(n_samples) mask = x == 0 y[mask] = np.random.uniform(-1, 1, size=np.sum(mask)) y[~mask] = np.random.uniform(0, 2, size=np.sum(~mask)) mi_1 = _compute_mi(x, y, True, False) x = np.hstack((x, 2)) y = np.hstack((y, 10)) mi_2 = _compute_mi(x, y, True, False) assert_equal(mi_1, mi_2) # We are going test that feature ordering by MI matches our expectations. def test_mutual_info_classif_discrete(): X = np.array([[0, 0, 0], [1, 1, 0], [2, 0, 1], [2, 0, 1], [2, 0, 1]]) y = np.array([0, 1, 2, 2, 1]) # Here X[:, 0] is the most informative feature, and X[:, 1] is weakly # informative. mi = mutual_info_classif(X, y, discrete_features=True) assert_array_equal(np.argsort(-mi), np.array([0, 2, 1])) def test_mutual_info_regression(): # We generate sample from multivariate normal distribution, using # transformation from initially uncorrelated variables. The zero # variables after transformation is selected as the target vector, # it has the strongest correlation with the variable 2, and # the weakest correlation with the variable 1. T = np.array([ [1, 0.5, 2, 1], [0, 1, 0.1, 0.0], [0, 0.1, 1, 0.1], [0, 0.1, 0.1, 1] ]) cov = T.dot(T.T) mean = np.zeros(4) np.random.seed(0) Z = np.random.multivariate_normal(mean, cov, size=1000) X = Z[:, 1:] y = Z[:, 0] mi = mutual_info_regression(X, y, random_state=0) assert_array_equal(np.argsort(-mi), np.array([1, 2, 0])) def test_mutual_info_classif_mixed(): # Here the target is discrete and there are two continuous and one # discrete feature. The idea of this test is clear from the code. np.random.seed(0) X = np.random.rand(1000, 3) X[:, 1] += X[:, 0] y = ((0.5 * X[:, 0] + X[:, 2]) > 0.5).astype(int) X[:, 2] = X[:, 2] > 0.5 mi = mutual_info_classif(X, y, discrete_features=[2], n_neighbors=3, random_state=0) assert_array_equal(np.argsort(-mi), [2, 0, 1]) for n_neighbors in [5, 7, 9]: mi_nn = mutual_info_classif(X, y, discrete_features=[2], n_neighbors=n_neighbors, random_state=0) # Check that the continuous values have an higher MI with greater # n_neighbors assert_greater(mi_nn[0], mi[0]) assert_greater(mi_nn[1], mi[1]) # The n_neighbors should not have any effect on the discrete value # The MI should be the same assert_equal(mi_nn[2], mi[2]) def test_mutual_info_options(): X = np.array([[0, 0, 0], [1, 1, 0], [2, 0, 1], [2, 0, 1], [2, 0, 1]], dtype=float) y = np.array([0, 1, 2, 2, 1], dtype=float) X_csr = csr_matrix(X) for mutual_info in (mutual_info_regression, mutual_info_classif): assert_raises(ValueError, mutual_info_regression, X_csr, y, discrete_features=False) mi_1 = mutual_info(X, y, discrete_features='auto', random_state=0) mi_2 = mutual_info(X, y, discrete_features=False, random_state=0) mi_3 = mutual_info(X_csr, y, discrete_features='auto', random_state=0) mi_4 = mutual_info(X_csr, y, discrete_features=True, random_state=0) assert_array_equal(mi_1, mi_2) assert_array_equal(mi_3, mi_4) assert_false(np.allclose(mi_1, mi_3)) if __name__ == '__main__': run_module_suite()
bsd-3-clause
johndpope/tensorflow
tensorflow/examples/learn/iris.py
35
1654
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Example of DNNClassifier for Iris plant dataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from sklearn import datasets from sklearn import metrics from sklearn import model_selection import tensorflow as tf def main(unused_argv): # Load dataset. iris = datasets.load_iris() x_train, x_test, y_train, y_test = model_selection.train_test_split( iris.data, iris.target, test_size=0.2, random_state=42) # Build 3 layer DNN with 10, 20, 10 units respectively. feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input( x_train) classifier = tf.contrib.learn.DNNClassifier( feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3) # Fit and predict. classifier.fit(x_train, y_train, steps=200) predictions = list(classifier.predict(x_test, as_iterable=True)) score = metrics.accuracy_score(y_test, predictions) print('Accuracy: {0:f}'.format(score)) if __name__ == '__main__': tf.app.run()
apache-2.0
waterponey/scikit-learn
examples/linear_model/plot_sgd_iris.py
58
2202
""" ======================================== Plot multi-class SGD on the iris dataset ======================================== Plot decision surface of multi-class SGD on iris dataset. The hyperplanes corresponding to the three one-versus-all (OVA) classifiers are represented by the dashed lines. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.linear_model import SGDClassifier # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target colors = "bry" # shuffle idx = np.arange(X.shape[0]) np.random.seed(13) np.random.shuffle(idx) X = X[idx] y = y[idx] # standardize mean = X.mean(axis=0) std = X.std(axis=0) X = (X - mean) / std h = .02 # step size in the mesh clf = SGDClassifier(alpha=0.001, n_iter=100).fit(X, y) # create a mesh to plot in x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired) plt.axis('tight') # Plot also the training points for i, color in zip(clf.classes_, colors): idx = np.where(y == i) plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i], cmap=plt.cm.Paired) plt.title("Decision surface of multi-class SGD") plt.axis('tight') # Plot the three one-against-all classifiers xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() coef = clf.coef_ intercept = clf.intercept_ def plot_hyperplane(c, color): def line(x0): return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1] plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color) for i, color in zip(clf.classes_, colors): plot_hyperplane(i, color) plt.legend() plt.show()
bsd-3-clause
lefthandedroo/Cosmo-models
Models/da_distrib-maker_from_txt.py
1
3284
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Nov 19 18:06:28 2018 @author: usyd This script collects sampler.flatchain[:, :] for the relevant model in results_erro_vs_data and integrates the equations describing the model with every set of parameters from emcee steps stored inside the sampler. Integration is over a single z=1089 (z=0 is incerted as initial condition in zodesolve.py). This integration results in mag and angular diameter distance at recombination. The distribution of angular diameter distances is saved in current directory. """ from pathlib import Path import pickle import numpy as np import datasim zpicks = np.array([1089]) data_dic = {'zpicks':zpicks} timed = False if timed: import cProfile, pstats, io pr = cProfile.Profile() pr.enable() models = None, 'LCDM' noise_options = 0.001, None #0.001, 0.01, 0.07, 0.14, 0.2 npoints_options = None, 1048000 #1048, 10480, 104800, 1048000 for npoints in npoints_options: if not npoints: # skipping None to allow iteratng over a list of 1 continue for noise in noise_options: if not noise: continue for test_key in models: if test_key: file_path = f'results_error_vs_data/{test_key}/{test_key}_sigma{noise}_npoints{npoints}.txt' my_file = Path(file_path) if my_file.is_file(): # results of error_vs_data runs flatchain = np.loadtxt(file_path) file_open_indicator = 1 print(f'{file_path} opened successfully') else: print(f"Couldn't open {file_path}") file_open_indicator = 0 # assert ensures only flatchain from intended file_path is used assert file_open_indicator > 0, f"{file_path} didn't have a flatchain" da_distrib = [] for i in range(0, len(flatchain)): values = flatchain[i, :] # not parsing real names as not needed names = np.zeros(len(values)).tolist() mag, da = datasim.magn(names, values, data_dic, test_key, plot_key=False) da_distrib.append(da[-1]) # adding da at z[-1]=1089 print(f'last da = {da[-1]}, z = {zpicks}') filename = f'da_distribs/txt_da_distrib_{test_key}_{noise}_{npoints}.p' pickle.dump(da_distrib, open(filename, 'wb')) import matplotlib.pyplot as plt import matplotlib as mpl mpl.style.use('default') # has to be switched on to set figure size mpl.style.use('fivethirtyeight') plt.rcParams['axes.facecolor'] = 'white' plt.rcParams['figure.facecolor'] = 'white' plt.rcParams['grid.color'] = 'white' for i in range(flatchain.ndim): plt.figure() plt.hist(flatchain[:,i], 50, color="C{}".format(i)) if timed: pr.disable() s = io.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print (s.getvalue())
mit
andrewcmyers/tensorflow
tensorflow/contrib/learn/python/learn/learn_io/data_feeder_test.py
71
12923
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests for `DataFeeder`.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import six from six.moves import xrange # pylint: disable=redefined-builtin # pylint: disable=wildcard-import from tensorflow.contrib.learn.python.learn.learn_io import * from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.platform import test # pylint: enable=wildcard-import class DataFeederTest(test.TestCase): # pylint: disable=undefined-variable """Tests for `DataFeeder`.""" def _wrap_dict(self, data, prepend=''): return {prepend + '1': data, prepend + '2': data} def _assert_raises(self, input_data): with self.assertRaisesRegexp(TypeError, 'annot convert'): data_feeder.DataFeeder(input_data, None, n_classes=0, batch_size=1) def test_input_uint32(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.uint32) self._assert_raises(data) self._assert_raises(self._wrap_dict(data)) def test_input_uint64(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.uint64) self._assert_raises(data) self._assert_raises(self._wrap_dict(data)) def _assert_dtype(self, expected_np_dtype, expected_tf_dtype, input_data): feeder = data_feeder.DataFeeder(input_data, None, n_classes=0, batch_size=1) if isinstance(input_data, dict): for k, v in list(feeder.input_dtype.items()): self.assertEqual(expected_np_dtype, v) else: self.assertEqual(expected_np_dtype, feeder.input_dtype) with ops.Graph().as_default() as g, self.test_session(g): inp, _ = feeder.input_builder() if isinstance(inp, dict): for k, v in list(inp.items()): self.assertEqual(expected_tf_dtype, v.dtype) else: self.assertEqual(expected_tf_dtype, inp.dtype) def test_input_int8(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.int8) self._assert_dtype(np.int8, dtypes.int8, data) self._assert_dtype(np.int8, dtypes.int8, self._wrap_dict(data)) def test_input_int16(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.int16) self._assert_dtype(np.int16, dtypes.int16, data) self._assert_dtype(np.int16, dtypes.int16, self._wrap_dict(data)) def test_input_int32(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.int32) self._assert_dtype(np.int32, dtypes.int32, data) self._assert_dtype(np.int32, dtypes.int32, self._wrap_dict(data)) def test_input_int64(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.int64) self._assert_dtype(np.int64, dtypes.int64, data) self._assert_dtype(np.int64, dtypes.int64, self._wrap_dict(data)) def test_input_uint8(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.uint8) self._assert_dtype(np.uint8, dtypes.uint8, data) self._assert_dtype(np.uint8, dtypes.uint8, self._wrap_dict(data)) def test_input_uint16(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.uint16) self._assert_dtype(np.uint16, dtypes.uint16, data) self._assert_dtype(np.uint16, dtypes.uint16, self._wrap_dict(data)) def test_input_float16(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.float16) self._assert_dtype(np.float16, dtypes.float16, data) self._assert_dtype(np.float16, dtypes.float16, self._wrap_dict(data)) def test_input_float32(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.float32) self._assert_dtype(np.float32, dtypes.float32, data) self._assert_dtype(np.float32, dtypes.float32, self._wrap_dict(data)) def test_input_float64(self): data = np.matrix([[1, 2], [3, 4]], dtype=np.float64) self._assert_dtype(np.float64, dtypes.float64, data) self._assert_dtype(np.float64, dtypes.float64, self._wrap_dict(data)) def test_input_bool(self): data = np.array([[False for _ in xrange(2)] for _ in xrange(2)]) self._assert_dtype(np.bool, dtypes.bool, data) self._assert_dtype(np.bool, dtypes.bool, self._wrap_dict(data)) def test_input_string(self): input_data = np.array([['str%d' % i for i in xrange(2)] for _ in xrange(2)]) self._assert_dtype(input_data.dtype, dtypes.string, input_data) self._assert_dtype(input_data.dtype, dtypes.string, self._wrap_dict(input_data)) def _assertAllClose(self, src, dest, src_key_of=None, src_prop=None): def func(x): val = getattr(x, src_prop) if src_prop else x return val if src_key_of is None else src_key_of[val] if isinstance(src, dict): for k in list(src.keys()): self.assertAllClose(func(src[k]), dest) else: self.assertAllClose(func(src), dest) def test_unsupervised(self): def func(feeder): with self.test_session(): inp, _ = feeder.input_builder() feed_dict_fn = feeder.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[1, 2]], feed_dict, 'name') data = np.matrix([[1, 2], [2, 3], [3, 4]]) func(data_feeder.DataFeeder(data, None, n_classes=0, batch_size=1)) func( data_feeder.DataFeeder( self._wrap_dict(data), None, n_classes=0, batch_size=1)) def test_data_feeder_regression(self): def func(df): inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[3, 4], [1, 2]], feed_dict, 'name') self._assertAllClose(out, [2, 1], feed_dict, 'name') x = np.matrix([[1, 2], [3, 4]]) y = np.array([1, 2]) func(data_feeder.DataFeeder(x, y, n_classes=0, batch_size=3)) func( data_feeder.DataFeeder( self._wrap_dict(x, 'in'), self._wrap_dict(y, 'out'), n_classes=self._wrap_dict(0, 'out'), batch_size=3)) def test_epoch(self): def func(feeder): with self.test_session(): feeder.input_builder() epoch = feeder.make_epoch_variable() feed_dict_fn = feeder.get_feed_dict_fn() # First input feed_dict = feed_dict_fn() self.assertAllClose(feed_dict[epoch.name], [0]) # Second input feed_dict = feed_dict_fn() self.assertAllClose(feed_dict[epoch.name], [0]) # Third input feed_dict = feed_dict_fn() self.assertAllClose(feed_dict[epoch.name], [0]) # Back to the first input again, so new epoch. feed_dict = feed_dict_fn() self.assertAllClose(feed_dict[epoch.name], [1]) data = np.matrix([[1, 2], [2, 3], [3, 4]]) labels = np.array([0, 0, 1]) func(data_feeder.DataFeeder(data, labels, n_classes=0, batch_size=1)) func( data_feeder.DataFeeder( self._wrap_dict(data, 'in'), self._wrap_dict(labels, 'out'), n_classes=self._wrap_dict(0, 'out'), batch_size=1)) def test_data_feeder_multioutput_regression(self): def func(df): inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[3, 4], [1, 2]], feed_dict, 'name') self._assertAllClose(out, [[3, 4], [1, 2]], feed_dict, 'name') x = np.matrix([[1, 2], [3, 4]]) y = np.array([[1, 2], [3, 4]]) func(data_feeder.DataFeeder(x, y, n_classes=0, batch_size=2)) func( data_feeder.DataFeeder( self._wrap_dict(x, 'in'), self._wrap_dict(y, 'out'), n_classes=self._wrap_dict(0, 'out'), batch_size=2)) def test_data_feeder_multioutput_classification(self): def func(df): inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[3, 4], [1, 2]], feed_dict, 'name') self._assertAllClose( out, [[[0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]], feed_dict, 'name') x = np.matrix([[1, 2], [3, 4]]) y = np.array([[0, 1, 2], [2, 3, 4]]) func(data_feeder.DataFeeder(x, y, n_classes=5, batch_size=2)) func( data_feeder.DataFeeder( self._wrap_dict(x, 'in'), self._wrap_dict(y, 'out'), n_classes=self._wrap_dict(5, 'out'), batch_size=2)) def test_streaming_data_feeder(self): def func(df): inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[[1, 2]], [[3, 4]]], feed_dict, 'name') self._assertAllClose(out, [[[1], [2]], [[2], [2]]], feed_dict, 'name') def x_iter(wrap_dict=False): yield np.array([[1, 2]]) if not wrap_dict else self._wrap_dict( np.array([[1, 2]]), 'in') yield np.array([[3, 4]]) if not wrap_dict else self._wrap_dict( np.array([[3, 4]]), 'in') def y_iter(wrap_dict=False): yield np.array([[1], [2]]) if not wrap_dict else self._wrap_dict( np.array([[1], [2]]), 'out') yield np.array([[2], [2]]) if not wrap_dict else self._wrap_dict( np.array([[2], [2]]), 'out') func( data_feeder.StreamingDataFeeder( x_iter(), y_iter(), n_classes=0, batch_size=2)) func( data_feeder.StreamingDataFeeder( x_iter(True), y_iter(True), n_classes=self._wrap_dict(0, 'out'), batch_size=2)) # Test non-full batches. func( data_feeder.StreamingDataFeeder( x_iter(), y_iter(), n_classes=0, batch_size=10)) func( data_feeder.StreamingDataFeeder( x_iter(True), y_iter(True), n_classes=self._wrap_dict(0, 'out'), batch_size=10)) def test_dask_data_feeder(self): if HAS_PANDAS and HAS_DASK: x = pd.DataFrame( dict( a=np.array([.1, .3, .4, .6, .2, .1, .6]), b=np.array([.7, .8, .1, .2, .5, .3, .9]))) x = dd.from_pandas(x, npartitions=2) y = pd.DataFrame(dict(labels=np.array([1, 0, 2, 1, 0, 1, 2]))) y = dd.from_pandas(y, npartitions=2) # TODO(ipolosukhin): Remove or restore this. # x = extract_dask_data(x) # y = extract_dask_labels(y) df = data_feeder.DaskDataFeeder(x, y, n_classes=2, batch_size=2) inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self.assertAllClose(feed_dict[inp.name], [[0.40000001, 0.1], [0.60000002, 0.2]]) self.assertAllClose(feed_dict[out.name], [[0., 0., 1.], [0., 1., 0.]]) def test_hdf5_data_feeder(self): def func(df): inp, out = df.input_builder() feed_dict_fn = df.get_feed_dict_fn() feed_dict = feed_dict_fn() self._assertAllClose(inp, [[3, 4], [1, 2]], feed_dict, 'name') self.assertAllClose(out, [2, 1], feed_dict, 'name') try: import h5py # pylint: disable=g-import-not-at-top x = np.matrix([[1, 2], [3, 4]]) y = np.array([1, 2]) h5f = h5py.File('test_hdf5.h5', 'w') h5f.create_dataset('x', data=x) h5f.create_dataset('y', data=y) h5f.close() h5f = h5py.File('test_hdf5.h5', 'r') x = h5f['x'] y = h5f['y'] func(data_feeder.DataFeeder(x, y, n_classes=0, batch_size=3)) func( data_feeder.DataFeeder( self._wrap_dict(x, 'in'), self._wrap_dict(y, 'out'), n_classes=self._wrap_dict(0, 'out'), batch_size=3)) except ImportError: print("Skipped test for hdf5 since it's not installed.") class SetupPredictDataFeederTest(DataFeederTest): """Tests for `DataFeeder.setup_predict_data_feeder`.""" def test_iterable_data(self): # pylint: disable=undefined-variable def func(df): self._assertAllClose(six.next(df), [[1, 2], [3, 4]]) self._assertAllClose(six.next(df), [[5, 6]]) data = [[1, 2], [3, 4], [5, 6]] x = iter(data) x_dict = iter([self._wrap_dict(v) for v in iter(data)]) func(data_feeder.setup_predict_data_feeder(x, batch_size=2)) func(data_feeder.setup_predict_data_feeder(x_dict, batch_size=2)) if __name__ == '__main__': test.main()
apache-2.0
etkirsch/scikit-learn
sklearn/utils/setup.py
296
2884
import os from os.path import join from sklearn._build_utils import get_blas_info def configuration(parent_package='', top_path=None): import numpy from numpy.distutils.misc_util import Configuration config = Configuration('utils', parent_package, top_path) config.add_subpackage('sparsetools') cblas_libs, blas_info = get_blas_info() cblas_compile_args = blas_info.pop('extra_compile_args', []) cblas_includes = [join('..', 'src', 'cblas'), numpy.get_include(), blas_info.pop('include_dirs', [])] libraries = [] if os.name == 'posix': libraries.append('m') cblas_libs.append('m') config.add_extension('sparsefuncs_fast', sources=['sparsefuncs_fast.c'], libraries=libraries) config.add_extension('arrayfuncs', sources=['arrayfuncs.c'], depends=[join('src', 'cholesky_delete.h')], libraries=cblas_libs, include_dirs=cblas_includes, extra_compile_args=cblas_compile_args, **blas_info ) config.add_extension( 'murmurhash', sources=['murmurhash.c', join('src', 'MurmurHash3.cpp')], include_dirs=['src']) config.add_extension('lgamma', sources=['lgamma.c', join('src', 'gamma.c')], include_dirs=['src'], libraries=libraries) config.add_extension('graph_shortest_path', sources=['graph_shortest_path.c'], include_dirs=[numpy.get_include()]) config.add_extension('fast_dict', sources=['fast_dict.cpp'], language="c++", include_dirs=[numpy.get_include()], libraries=libraries) config.add_extension('seq_dataset', sources=['seq_dataset.c'], include_dirs=[numpy.get_include()]) config.add_extension('weight_vector', sources=['weight_vector.c'], include_dirs=cblas_includes, libraries=cblas_libs, **blas_info) config.add_extension("_random", sources=["_random.c"], include_dirs=[numpy.get_include()], libraries=libraries) config.add_extension("_logistic_sigmoid", sources=["_logistic_sigmoid.c"], include_dirs=[numpy.get_include()], libraries=libraries) return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration(top_path='').todict())
bsd-3-clause
liyu1990/sklearn
sklearn/tests/test_kernel_approximation.py
244
7588
import numpy as np from scipy.sparse import csr_matrix from sklearn.utils.testing import assert_array_equal, assert_equal, assert_true from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_array_almost_equal, assert_raises from sklearn.utils.testing import assert_less_equal from sklearn.metrics.pairwise import kernel_metrics from sklearn.kernel_approximation import RBFSampler from sklearn.kernel_approximation import AdditiveChi2Sampler from sklearn.kernel_approximation import SkewedChi2Sampler from sklearn.kernel_approximation import Nystroem from sklearn.metrics.pairwise import polynomial_kernel, rbf_kernel # generate data rng = np.random.RandomState(0) X = rng.random_sample(size=(300, 50)) Y = rng.random_sample(size=(300, 50)) X /= X.sum(axis=1)[:, np.newaxis] Y /= Y.sum(axis=1)[:, np.newaxis] def test_additive_chi2_sampler(): # test that AdditiveChi2Sampler approximates kernel on random data # compute exact kernel # appreviations for easier formular X_ = X[:, np.newaxis, :] Y_ = Y[np.newaxis, :, :] large_kernel = 2 * X_ * Y_ / (X_ + Y_) # reduce to n_samples_x x n_samples_y by summing over features kernel = (large_kernel.sum(axis=2)) # approximate kernel mapping transform = AdditiveChi2Sampler(sample_steps=3) X_trans = transform.fit_transform(X) Y_trans = transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) assert_array_almost_equal(kernel, kernel_approx, 1) X_sp_trans = transform.fit_transform(csr_matrix(X)) Y_sp_trans = transform.transform(csr_matrix(Y)) assert_array_equal(X_trans, X_sp_trans.A) assert_array_equal(Y_trans, Y_sp_trans.A) # test error is raised on negative input Y_neg = Y.copy() Y_neg[0, 0] = -1 assert_raises(ValueError, transform.transform, Y_neg) # test error on invalid sample_steps transform = AdditiveChi2Sampler(sample_steps=4) assert_raises(ValueError, transform.fit, X) # test that the sample interval is set correctly sample_steps_available = [1, 2, 3] for sample_steps in sample_steps_available: # test that the sample_interval is initialized correctly transform = AdditiveChi2Sampler(sample_steps=sample_steps) assert_equal(transform.sample_interval, None) # test that the sample_interval is changed in the fit method transform.fit(X) assert_not_equal(transform.sample_interval_, None) # test that the sample_interval is set correctly sample_interval = 0.3 transform = AdditiveChi2Sampler(sample_steps=4, sample_interval=sample_interval) assert_equal(transform.sample_interval, sample_interval) transform.fit(X) assert_equal(transform.sample_interval_, sample_interval) def test_skewed_chi2_sampler(): # test that RBFSampler approximates kernel on random data # compute exact kernel c = 0.03 # appreviations for easier formular X_c = (X + c)[:, np.newaxis, :] Y_c = (Y + c)[np.newaxis, :, :] # we do it in log-space in the hope that it's more stable # this array is n_samples_x x n_samples_y big x n_features log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) + np.log(2.) - np.log(X_c + Y_c)) # reduce to n_samples_x x n_samples_y by summing over features in log-space kernel = np.exp(log_kernel.sum(axis=2)) # approximate kernel mapping transform = SkewedChi2Sampler(skewedness=c, n_components=1000, random_state=42) X_trans = transform.fit_transform(X) Y_trans = transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) assert_array_almost_equal(kernel, kernel_approx, 1) # test error is raised on negative input Y_neg = Y.copy() Y_neg[0, 0] = -1 assert_raises(ValueError, transform.transform, Y_neg) def test_rbf_sampler(): # test that RBFSampler approximates kernel on random data # compute exact kernel gamma = 10. kernel = rbf_kernel(X, Y, gamma=gamma) # approximate kernel mapping rbf_transform = RBFSampler(gamma=gamma, n_components=1000, random_state=42) X_trans = rbf_transform.fit_transform(X) Y_trans = rbf_transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) error = kernel - kernel_approx assert_less_equal(np.abs(np.mean(error)), 0.01) # close to unbiased np.abs(error, out=error) assert_less_equal(np.max(error), 0.1) # nothing too far off assert_less_equal(np.mean(error), 0.05) # mean is fairly close def test_input_validation(): # Regression test: kernel approx. transformers should work on lists # No assertions; the old versions would simply crash X = [[1, 2], [3, 4], [5, 6]] AdditiveChi2Sampler().fit(X).transform(X) SkewedChi2Sampler().fit(X).transform(X) RBFSampler().fit(X).transform(X) X = csr_matrix(X) RBFSampler().fit(X).transform(X) def test_nystroem_approximation(): # some basic tests rnd = np.random.RandomState(0) X = rnd.uniform(size=(10, 4)) # With n_components = n_samples this is exact X_transformed = Nystroem(n_components=X.shape[0]).fit_transform(X) K = rbf_kernel(X) assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K) trans = Nystroem(n_components=2, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) # test callable kernel linear_kernel = lambda X, Y: np.dot(X, Y.T) trans = Nystroem(n_components=2, kernel=linear_kernel, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) # test that available kernels fit and transform kernels_available = kernel_metrics() for kern in kernels_available: trans = Nystroem(n_components=2, kernel=kern, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) def test_nystroem_singular_kernel(): # test that nystroem works with singular kernel matrix rng = np.random.RandomState(0) X = rng.rand(10, 20) X = np.vstack([X] * 2) # duplicate samples gamma = 100 N = Nystroem(gamma=gamma, n_components=X.shape[0]).fit(X) X_transformed = N.transform(X) K = rbf_kernel(X, gamma=gamma) assert_array_almost_equal(K, np.dot(X_transformed, X_transformed.T)) assert_true(np.all(np.isfinite(Y))) def test_nystroem_poly_kernel_params(): # Non-regression: Nystroem should pass other parameters beside gamma. rnd = np.random.RandomState(37) X = rnd.uniform(size=(10, 4)) K = polynomial_kernel(X, degree=3.1, coef0=.1) nystroem = Nystroem(kernel="polynomial", n_components=X.shape[0], degree=3.1, coef0=.1) X_transformed = nystroem.fit_transform(X) assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K) def test_nystroem_callable(): # Test Nystroem on a callable. rnd = np.random.RandomState(42) n_samples = 10 X = rnd.uniform(size=(n_samples, 4)) def logging_histogram_kernel(x, y, log): """Histogram kernel that writes to a log.""" log.append(1) return np.minimum(x, y).sum() kernel_log = [] X = list(X) # test input validation Nystroem(kernel=logging_histogram_kernel, n_components=(n_samples - 1), kernel_params={'log': kernel_log}).fit(X) assert_equal(len(kernel_log), n_samples * (n_samples - 1) / 2)
bsd-3-clause
Myasuka/scikit-learn
doc/tutorial/text_analytics/skeletons/exercise_01_language_train_model.py
254
2005
"""Build a language detector model The goal of this exercise is to train a linear classifier on text features that represent sequences of up to 3 consecutive characters so as to be recognize natural languages by using the frequencies of short character sequences as 'fingerprints'. """ # Author: Olivier Grisel <[email protected]> # License: Simplified BSD import sys from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import Perceptron from sklearn.pipeline import Pipeline from sklearn.datasets import load_files from sklearn.cross_validation import train_test_split from sklearn import metrics # The training data folder must be passed as first argument languages_data_folder = sys.argv[1] dataset = load_files(languages_data_folder) # Split the dataset in training and test set: docs_train, docs_test, y_train, y_test = train_test_split( dataset.data, dataset.target, test_size=0.5) # TASK: Build a an vectorizer that splits strings into sequence of 1 to 3 # characters instead of word tokens # TASK: Build a vectorizer / classifier pipeline using the previous analyzer # the pipeline instance should stored in a variable named clf # TASK: Fit the pipeline on the training set # TASK: Predict the outcome on the testing set in a variable named y_predicted # Print the classification report print(metrics.classification_report(y_test, y_predicted, target_names=dataset.target_names)) # Plot the confusion matrix cm = metrics.confusion_matrix(y_test, y_predicted) print(cm) #import pylab as pl #pl.matshow(cm, cmap=pl.cm.jet) #pl.show() # Predict the result on some short new sentences: sentences = [ u'This is a language detection test.', u'Ceci est un test de d\xe9tection de la langue.', u'Dies ist ein Test, um die Sprache zu erkennen.', ] predicted = clf.predict(sentences) for s, p in zip(sentences, predicted): print(u'The language of "%s" is "%s"' % (s, dataset.target_names[p]))
bsd-3-clause
googleinterns/cabby
cabby/model/landmark_recognition/dataset_bert.py
1
7721
# coding=utf-8 # Copyright 2020 Google LLC # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from absl import logging from nltk.tokenize import word_tokenize import pandas as pd import torch from transformers import AutoTokenizer from typing import Dict, Tuple, Any, List from cabby.model import datasets tokenizer = AutoTokenizer.from_pretrained("bert-base-cased", padding=True, truncation=True) EXTRACT_ALL_PIVOTS = "all" class EntityRecognitionSplit(torch.utils.data.Dataset): """A split of the Entity Recognition dataset .""" def __init__(self, data: pd.DataFrame, pivot_type: str): # Tokenize instructions and corresponding labels. self.ds = data if pivot_type != EXTRACT_ALL_PIVOTS: self.ds['pivot_span'] = self.ds.apply(get_pivot_span_by_name, args=(pivot_type,), axis=1) labels = self.ds.pivot_span else: labels = data.entity_span basic_tokenization = [ basic_tokenize_and_align_labels(sent, labs) for sent, labs in zip(self.ds.instructions.tolist(), labels) ] self.inputs = [bert_tokenize_and_align_labels(sent, labs) for sent, labs in basic_tokenization] self.sent = data.instructions.tolist() def __getitem__(self, idx: int): '''Supports indexing such that TextGeoDataset[i] can be used to get i-th sample. Arguments: idx: The index for which a sample from the dataset will be returned. Returns: A single sample including text, the tokenization of the text and the corresponding labels. ''' input = {k: torch.tensor(v) for k, v in self.inputs[idx].items()} input['instructions'] = self.sent[idx] return input def __len__(self): return len(self.inputs) def create_dataset( data_dir: str, region: str, s2level: int, pivot_type: str = EXTRACT_ALL_PIVOTS ) -> Tuple[EntityRecognitionSplit, EntityRecognitionSplit, EntityRecognitionSplit]: '''Loads data and creates datasets and train, validate and test sets. Arguments: data_dir: The directory of the data. region: The region of the data. s2level: The s2level of the cells. pivot_type: name of the pivot to be extracted. Returns: The train, validate and test sets. ''' rvs_dataset = datasets.RVSDataset(data_dir, s2level, region) train_dataset = EntityRecognitionSplit(rvs_dataset.train, pivot_type) logging.info( f"Finished to create the train-set with {len(train_dataset)} samples") val_dataset = EntityRecognitionSplit(rvs_dataset.valid, pivot_type) logging.info( f"Finished to create the valid-set with {len(val_dataset)} samples") test_dataset = EntityRecognitionSplit(rvs_dataset.test, pivot_type) logging.info( f"Finished to create the test-set with {len(test_dataset)} samples") return train_dataset, val_dataset, test_dataset def basic_tokenize_and_align_labels( sentence: str, text_labels: Dict[str,Tuple[int, int]] ) : '''Tokenize sentence and preserve the labels, such that for each token there is a label. Arguments: sentence: the sentence to tokenize. text_labels: the dictionary containing the landmarks (keys) and span (values). Returns: A List of the tokens and their corresponding labels. ''' # Basic tokenization of the sentence. sentence_words = word_tokenize(sentence) # Create labels for each token according to the dictionary of entities and spans. labels = [] spans = sorted(list(text_labels.values())) cur_index = 0 span = spans.pop(0) start, end = span[0], span[1] for word in sentence_words: # If the word is in the span of landmark then add the # corresponding label 1 indicating it is a landmark. if cur_index >= start: labels.append(1) else: # Label 0 for non-landmark token. labels.append(0) # Change the start of the current position. cur_index += len(word) if cur_index < len(sentence) and sentence[cur_index] == ' ': cur_index += 1 # If the landmark span is finished remove the landmark span. if cur_index >= end: # If the list of entity spans is not empty, then remove the next span and # set it as the current span (start and end). # If the list of entity spans are done the set the current # entity span to a position beyond the sentence length. if len(spans) > 0: span = spans.pop(0) start, end = span[0], span[1] else: start = len(sentence) + 1 return sentence_words, labels def get_pivot_span_by_name(sample: pd.Series, pivot_type: str ) -> Dict[str, List[int]]: '''Get the entity span for a specific sample and a specific type of entity. Arguments: sample: the sample from which the span should be extracted. pivot_type: the type of the pivot. Returns: A span of an entity includes a start and end of the span positions. ''' pivot_name = sample[pivot_type][2] if pivot_name: return {pivot_type: sample.entity_span[pivot_name]} return {pivot_type: [0, 0]} # The pivot doesn't appear in the instructions. def bert_tokenize_and_align_labels( tokenized_sentence: List[int], tags: List[int] ) -> Dict[str, torch.Tensor]: '''Bert-tokenization of sentence and preserve the labels, such that for each token there is a label. Arguments: tokenized_sentence: the tokenized sentence. tags: the labels corresponding to the tokens. Returns: A dictionary of Bert-tokenized sentences and corresponding labels. ''' tokenized_inputs = tokenizer(tokenized_sentence, truncation=True, is_split_into_words=True) word_ids = tokenized_inputs.word_ids(batch_index=0) previous_word_idx = None label_ids = [] for word_idx in word_ids: # Special tokens have a word id that is None. # We set the label to -100 so they are automatically # ignored in the loss function. if word_idx is None: label_ids.append(-100) # We set the label for the first token of each word. elif word_idx != previous_word_idx: label_ids.append(tags[word_idx]) # For the other tokens in a word, we set the label to the current label, else: label_ids.append(tags[word_idx]) previous_word_idx = word_idx assert len(label_ids) == len(tokenized_inputs['input_ids']) tokenized_inputs["labels"] = label_ids return tokenized_inputs class PadSequence: def __call__(self, batch): sorted_batch = sorted(batch, key=lambda x: x['input_ids'].shape[0], reverse=True) # Get each sequence and pad it. input_ids = [x['input_ids'] for x in sorted_batch] input_ids_padded = torch.nn.utils.rnn.pad_sequence(input_ids, batch_first=True) labels = [torch.tensor(x['labels']).clone().detach() for x in sorted_batch] labels_padded = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True) attention_masks = torch.tensor( [[float(i != 0.0) for i in ii] for ii in input_ids_padded]) sample = {'labels': labels_padded, 'input_ids': input_ids_padded, 'attention_mask': attention_masks, } return sample
apache-2.0
shenzebang/scikit-learn
benchmarks/bench_isotonic.py
268
3046
""" Benchmarks of isotonic regression performance. We generate a synthetic dataset of size 10^n, for n in [min, max], and examine the time taken to run isotonic regression over the dataset. The timings are then output to stdout, or visualized on a log-log scale with matplotlib. This alows the scaling of the algorithm with the problem size to be visualized and understood. """ from __future__ import print_function import numpy as np import gc from datetime import datetime from sklearn.isotonic import isotonic_regression from sklearn.utils.bench import total_seconds import matplotlib.pyplot as plt import argparse def generate_perturbed_logarithm_dataset(size): return np.random.randint(-50, 50, size=n) \ + 50. * np.log(1 + np.arange(n)) def generate_logistic_dataset(size): X = np.sort(np.random.normal(size=size)) return np.random.random(size=size) < 1.0 / (1.0 + np.exp(-X)) DATASET_GENERATORS = { 'perturbed_logarithm': generate_perturbed_logarithm_dataset, 'logistic': generate_logistic_dataset } def bench_isotonic_regression(Y): """ Runs a single iteration of isotonic regression on the input data, and reports the total time taken (in seconds). """ gc.collect() tstart = datetime.now() isotonic_regression(Y) delta = datetime.now() - tstart return total_seconds(delta) if __name__ == '__main__': parser = argparse.ArgumentParser( description="Isotonic Regression benchmark tool") parser.add_argument('--iterations', type=int, required=True, help="Number of iterations to average timings over " "for each problem size") parser.add_argument('--log_min_problem_size', type=int, required=True, help="Base 10 logarithm of the minimum problem size") parser.add_argument('--log_max_problem_size', type=int, required=True, help="Base 10 logarithm of the maximum problem size") parser.add_argument('--show_plot', action='store_true', help="Plot timing output with matplotlib") parser.add_argument('--dataset', choices=DATASET_GENERATORS.keys(), required=True) args = parser.parse_args() timings = [] for exponent in range(args.log_min_problem_size, args.log_max_problem_size): n = 10 ** exponent Y = DATASET_GENERATORS[args.dataset](n) time_per_iteration = \ [bench_isotonic_regression(Y) for i in range(args.iterations)] timing = (n, np.mean(time_per_iteration)) timings.append(timing) # If we're not plotting, dump the timing to stdout if not args.show_plot: print(n, np.mean(time_per_iteration)) if args.show_plot: plt.plot(*zip(*timings)) plt.title("Average time taken running isotonic regression") plt.xlabel('Number of observations') plt.ylabel('Time (s)') plt.axis('tight') plt.loglog() plt.show()
bsd-3-clause
boztalay/LappyLogger
Analysis/dailyHistogram.py
1
1301
import sys import csv import datetime import numpy as np import matplotlib.pyplot as plt if len(sys.argv) != 2: print "Please specify a file to read" sys.exit(1) fileToPlot = open(sys.argv[1], "r") csvReader = csv.DictReader(fileToPlot) timestamps = [] data = [] for row in csvReader: timestamps.append(datetime.datetime.strptime(row["timestamp"], "%Y-%m-%d %H:%M:%S")) data.append(int(row["data"])) numBins = 48 binLengthInMinutes = (24 * 60) / numBins binLowerBoundaries = [] for i in range(0, numBins + 1): binLowerBoundaries.append(i * binLengthInMinutes) bins = [0 for i in range(0, len(binLowerBoundaries))] for i in range(0, len(timestamps)): timestampOfData = timestamps[i] bindex = int(((timestampOfData.hour * 60) + timestampOfData.minute) / binLengthInMinutes) bins[bindex] += data[i] xTickLabels = [] for lowerBoundary in binLowerBoundaries: hour = int(lowerBoundary / 60) minute = lowerBoundary % 60 minuteZero = "0" if minute < 10 else "" xTickLabels.append(str(hour) + ":" + minuteZero + str(minute)) plt.bar(binLowerBoundaries, bins, width=binLengthInMinutes) plt.xticks(binLowerBoundaries[0::4], xTickLabels[0::4], horizontalalignment="right", rotation=25) plt.xlim(0, 1440) plt.grid(True) plt.suptitle(sys.argv[1]) plt.show()
mit
tpsatish95/OCR-on-Indus-Seals
code/Test/binaryimage.py
1
6453
import matplotlib.patches as mpatches import matplotlib.pyplot as plt from scipy import ndimage from skimage.filters import threshold_otsu, gaussian_filter import skimage.io import skimage.color import skimage.morphology import numpy as np import os candidates = set() refined = set() final = set() final_extended = set() ref = set() def extend_rect(l): return (min([i[0] for i in l]), min([i[1] for i in l]), max([i[0]+i[2] for i in l]) - min([i[0] for i in l]), max([i[1]+i[3] for i in l]) - min([i[1] for i in l])) def extend_superbox(): global width, height thresh = ((width+height)/2)*(0.22) tempc = set() for x, y, w, h in final: if (x, y, w, h) in tempc: continue temp = set() temp.add((x, y, w, h)) for x1, y1, w1, h1 in final: # if abs(x1-x) <= thresh and abs(w1-w) <= thresh: if x1 >= x and (w1+x1) <= w+x: temp.add((x1, y1, w1, h1)) tempc.add((x1, y1, w1, h1)) final_extended.add(extend_rect(temp)) contains_remove(final_extended) def draw_superbox(finals=[]): noover = [] refinedT = [] global final final = set() # (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size if finals != []: refinedT = finals else: refinedT = refined remp = set(refinedT) ref = list(refinedT) while len(ref) > 0: x1, y1, w1, h1 = ref[0] if len(ref) == 1: # final box final.add((x1, y1, w1, h1)) ref.remove((x1, y1, w1, h1)) remp.remove((x1, y1, w1, h1)) else: ref.remove((x1, y1, w1, h1)) remp.remove((x1, y1, w1, h1)) over = set() for x2, y2, w2, h2 in remp: A = {'x1': x1, 'y1': y1, 'x2': x1+w1, 'y2': y1+h1, 'w': w1, 'h': h1} B = {'x1': x2, 'y1': y2, 'x2': x2+w2, 'y2': y2+h2, 'w': w2, 'h': h2} # overlap between A and B SA = A['w']*A['h'] SB = B['w']*B['h'] SI = np.max([ 0, np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ]) SU = SA + SB - SI overlap_AB = float(SI) / float(SU) overlap_A = float(SI) / float(SA) overlap_B = float(SI) / float(SB) # print(overlap_AB) # if overlap_A >= 0.15 or overlap_B >= 0.15: over.add((B['x1'],B['y1'],B['w'],B['h'])) # print(len(over)) if len(over) != 0: #Overlap remp = remp - over for i in over: ref.remove(i) over.add((A['x1'],A['y1'],A['w'],A['h'])) # print(over) final.add((min([i[0] for i in over]), min([i[1] for i in over]), max([i[0]+i[2] for i in over]) - min([i[0] for i in over]), max([i[1]+i[3] for i in over]) - min([i[1] for i in over]))) # final.add((np.mean([i[0] for i in over]), np.mean([i[1] for i in over]), np.mean([i[2] for i in over]), np.mean([i[3] for i in over]))) noover.append(False) else: #No overlap final.add((x1,y1,w1,h1)) noover.append(True) if all(noover): return else: draw_superbox(final) return def contains_remove(param = []): if param != []: can = set(param) else: can = set(candidates) for x, y, w, h in can: temp = set(can) temp.remove((x, y, w, h)) test = [] for x1, y1, w1, h1 in temp: A = {'x1': x, 'y1': y, 'x2': x+w, 'y2': y+h, 'w': w, 'h': h} B = {'x1': x1, 'y1': y1, 'x2': x1+w1, 'y2': y1+h1, 'w': w1, 'h': h1} # overlap between A and B SA = A['w']*A['h'] SB = B['w']*B['h'] SI = np.max([ 0, np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ]) SU = SA + SB - SI overlap_AB = float(SI) / float(SU) if overlap_AB > 0.0: # if x1>=x and y1 >= y and x1+w1 <= x+w and y1+h1 <= y+h: if x1<=x and y1 <= y and x1+w1 >= x+w and y1+h1 >= y+h: test.append(False) else: test.append(True) else: test.append(True) if all(test) and param == []: refined.add((x, y, w, h)) if all(test) and param != []: ref.add((x, y, w, h)) for d in os.listdir("Regions"): for name in os.listdir("Regions/"+d): print(d) if "_text" in name: candidates = set() refined = set() final = set() final_extended = set() ref = set() image = skimage.io.imread("Regions/"+d+"/"+name) image = skimage.color.rgb2gray(image) thresh = threshold_otsu(image) binary = image <= thresh skimage.io.imsave("temp/binary_"+d+".png", binary) bin = skimage.io.imread("temp/binary_"+d+".png") im = gaussian_filter(bin, sigma=3.5) blobs = im > im.mean() labels = skimage.morphology.label(blobs, neighbors = 4) blobs = ndimage.find_objects(labels) plt.imsave("temp/blobs_"+d+".png", im) image1 = skimage.io.imread("Regions/"+d+"/"+name) width = len(image1[0]) height = len(image1) for c1, c2 in blobs: if (c2.stop - c2.start) * c1.stop - c1.start > (image1.shape[0]*image1.shape[1])*(0.026): if (c2.stop - c2.start) * c1.stop - c1.start < (image1.shape[0]*image1.shape[1])*(0.90): candidates.add((c2.start, c1.start,c2.stop - c2.start, c1.stop - c1.start)) print(candidates) contains_remove() print(refined) draw_superbox() print(final) extend_superbox() print(final_extended) print(ref) image1 = skimage.io.imread("Regions/"+d+"/"+name) # draw rectangles on the original image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(image1) for x, y, w, h in ref: rect = mpatches.Rectangle( (x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect) plt.savefig("Regions/"+d+"/"+"patch_"+d+".png") plt.savefig("patches/"+"patch_"+d+".png") plt.close('all') ###MISC # from skimage.viewer import ImageViewer # viewer = ImageViewer(blobs) # viewer.show()
apache-2.0
amolkahat/pandas
pandas/tests/groupby/test_transform.py
3
28075
""" test with the .transform """ import pytest import numpy as np import pandas as pd from pandas.util import testing as tm from pandas import Series, DataFrame, Timestamp, MultiIndex, concat, date_range from pandas.core.dtypes.common import ( ensure_platform_int, is_timedelta64_dtype) from pandas.compat import StringIO from pandas._libs import groupby from pandas.util.testing import assert_frame_equal, assert_series_equal from pandas.core.groupby.groupby import DataError from pandas.core.config import option_context def assert_fp_equal(a, b): assert (np.abs(a - b) < 1e-12).all() def test_transform(): data = Series(np.arange(9) // 3, index=np.arange(9)) index = np.arange(9) np.random.shuffle(index) data = data.reindex(index) grouped = data.groupby(lambda x: x // 3) transformed = grouped.transform(lambda x: x * x.sum()) assert transformed[7] == 12 # GH 8046 # make sure that we preserve the input order df = DataFrame( np.arange(6, dtype='int64').reshape( 3, 2), columns=["a", "b"], index=[0, 2, 1]) key = [0, 0, 1] expected = df.sort_index().groupby(key).transform( lambda x: x - x.mean()).groupby(key).mean() result = df.groupby(key).transform(lambda x: x - x.mean()).groupby( key).mean() assert_frame_equal(result, expected) def demean(arr): return arr - arr.mean() people = DataFrame(np.random.randn(5, 5), columns=['a', 'b', 'c', 'd', 'e'], index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis']) key = ['one', 'two', 'one', 'two', 'one'] result = people.groupby(key).transform(demean).groupby(key).mean() expected = people.groupby(key).apply(demean).groupby(key).mean() assert_frame_equal(result, expected) # GH 8430 df = tm.makeTimeDataFrame() g = df.groupby(pd.Grouper(freq='M')) g.transform(lambda x: x - 1) # GH 9700 df = DataFrame({'a': range(5, 10), 'b': range(5)}) result = df.groupby('a').transform(max) expected = DataFrame({'b': range(5)}) tm.assert_frame_equal(result, expected) def test_transform_fast(): df = DataFrame({'id': np.arange(100000) / 3, 'val': np.random.randn(100000)}) grp = df.groupby('id')['val'] values = np.repeat(grp.mean().values, ensure_platform_int(grp.count().values)) expected = pd.Series(values, index=df.index, name='val') result = grp.transform(np.mean) assert_series_equal(result, expected) result = grp.transform('mean') assert_series_equal(result, expected) # GH 12737 df = pd.DataFrame({'grouping': [0, 1, 1, 3], 'f': [1.1, 2.1, 3.1, 4.5], 'd': pd.date_range('2014-1-1', '2014-1-4'), 'i': [1, 2, 3, 4]}, columns=['grouping', 'f', 'i', 'd']) result = df.groupby('grouping').transform('first') dates = [pd.Timestamp('2014-1-1'), pd.Timestamp('2014-1-2'), pd.Timestamp('2014-1-2'), pd.Timestamp('2014-1-4')] expected = pd.DataFrame({'f': [1.1, 2.1, 2.1, 4.5], 'd': dates, 'i': [1, 2, 2, 4]}, columns=['f', 'i', 'd']) assert_frame_equal(result, expected) # selection result = df.groupby('grouping')[['f', 'i']].transform('first') expected = expected[['f', 'i']] assert_frame_equal(result, expected) # dup columns df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['g', 'a', 'a']) result = df.groupby('g').transform('first') expected = df.drop('g', axis=1) assert_frame_equal(result, expected) def test_transform_broadcast(tsframe, ts): grouped = ts.groupby(lambda x: x.month) result = grouped.transform(np.mean) tm.assert_index_equal(result.index, ts.index) for _, gp in grouped: assert_fp_equal(result.reindex(gp.index), gp.mean()) grouped = tsframe.groupby(lambda x: x.month) result = grouped.transform(np.mean) tm.assert_index_equal(result.index, tsframe.index) for _, gp in grouped: agged = gp.mean() res = result.reindex(gp.index) for col in tsframe: assert_fp_equal(res[col], agged[col]) # group columns grouped = tsframe.groupby({'A': 0, 'B': 0, 'C': 1, 'D': 1}, axis=1) result = grouped.transform(np.mean) tm.assert_index_equal(result.index, tsframe.index) tm.assert_index_equal(result.columns, tsframe.columns) for _, gp in grouped: agged = gp.mean(1) res = result.reindex(columns=gp.columns) for idx in gp.index: assert_fp_equal(res.xs(idx), agged[idx]) def test_transform_axis(tsframe): # make sure that we are setting the axes # correctly when on axis=0 or 1 # in the presence of a non-monotonic indexer # GH12713 base = tsframe.iloc[0:5] r = len(base.index) c = len(base.columns) tso = DataFrame(np.random.randn(r, c), index=base.index, columns=base.columns, dtype='float64') # monotonic ts = tso grouped = ts.groupby(lambda x: x.weekday()) result = ts - grouped.transform('mean') expected = grouped.apply(lambda x: x - x.mean()) assert_frame_equal(result, expected) ts = ts.T grouped = ts.groupby(lambda x: x.weekday(), axis=1) result = ts - grouped.transform('mean') expected = grouped.apply(lambda x: (x.T - x.mean(1)).T) assert_frame_equal(result, expected) # non-monotonic ts = tso.iloc[[1, 0] + list(range(2, len(base)))] grouped = ts.groupby(lambda x: x.weekday()) result = ts - grouped.transform('mean') expected = grouped.apply(lambda x: x - x.mean()) assert_frame_equal(result, expected) ts = ts.T grouped = ts.groupby(lambda x: x.weekday(), axis=1) result = ts - grouped.transform('mean') expected = grouped.apply(lambda x: (x.T - x.mean(1)).T) assert_frame_equal(result, expected) def test_transform_dtype(): # GH 9807 # Check transform dtype output is preserved df = DataFrame([[1, 3], [2, 3]]) result = df.groupby(1).transform('mean') expected = DataFrame([[1.5], [1.5]]) assert_frame_equal(result, expected) def test_transform_bug(): # GH 5712 # transforming on a datetime column df = DataFrame(dict(A=Timestamp('20130101'), B=np.arange(5))) result = df.groupby('A')['B'].transform( lambda x: x.rank(ascending=False)) expected = Series(np.arange(5, 0, step=-1), name='B') assert_series_equal(result, expected) def test_transform_numeric_to_boolean(): # GH 16875 # inconsistency in transforming boolean values expected = pd.Series([True, True], name='A') df = pd.DataFrame({'A': [1.1, 2.2], 'B': [1, 2]}) result = df.groupby('B').A.transform(lambda x: True) assert_series_equal(result, expected) df = pd.DataFrame({'A': [1, 2], 'B': [1, 2]}) result = df.groupby('B').A.transform(lambda x: True) assert_series_equal(result, expected) def test_transform_datetime_to_timedelta(): # GH 15429 # transforming a datetime to timedelta df = DataFrame(dict(A=Timestamp('20130101'), B=np.arange(5))) expected = pd.Series([ Timestamp('20130101') - Timestamp('20130101')] * 5, name='A') # this does date math without changing result type in transform base_time = df['A'][0] result = df.groupby('A')['A'].transform( lambda x: x.max() - x.min() + base_time) - base_time assert_series_equal(result, expected) # this does date math and causes the transform to return timedelta result = df.groupby('A')['A'].transform(lambda x: x.max() - x.min()) assert_series_equal(result, expected) def test_transform_datetime_to_numeric(): # GH 10972 # convert dt to float df = DataFrame({ 'a': 1, 'b': date_range('2015-01-01', periods=2, freq='D')}) result = df.groupby('a').b.transform( lambda x: x.dt.dayofweek - x.dt.dayofweek.mean()) expected = Series([-0.5, 0.5], name='b') assert_series_equal(result, expected) # convert dt to int df = DataFrame({ 'a': 1, 'b': date_range('2015-01-01', periods=2, freq='D')}) result = df.groupby('a').b.transform( lambda x: x.dt.dayofweek - x.dt.dayofweek.min()) expected = Series([0, 1], name='b') assert_series_equal(result, expected) def test_transform_casting(): # 13046 data = """ idx A ID3 DATETIME 0 B-028 b76cd912ff "2014-10-08 13:43:27" 1 B-054 4a57ed0b02 "2014-10-08 14:26:19" 2 B-076 1a682034f8 "2014-10-08 14:29:01" 3 B-023 b76cd912ff "2014-10-08 18:39:34" 4 B-023 f88g8d7sds "2014-10-08 18:40:18" 5 B-033 b76cd912ff "2014-10-08 18:44:30" 6 B-032 b76cd912ff "2014-10-08 18:46:00" 7 B-037 b76cd912ff "2014-10-08 18:52:15" 8 B-046 db959faf02 "2014-10-08 18:59:59" 9 B-053 b76cd912ff "2014-10-08 19:17:48" 10 B-065 b76cd912ff "2014-10-08 19:21:38" """ df = pd.read_csv(StringIO(data), sep=r'\s+', index_col=[0], parse_dates=['DATETIME']) result = df.groupby('ID3')['DATETIME'].transform(lambda x: x.diff()) assert is_timedelta64_dtype(result.dtype) result = df[['ID3', 'DATETIME']].groupby('ID3').transform( lambda x: x.diff()) assert is_timedelta64_dtype(result.DATETIME.dtype) def test_transform_multiple(ts): grouped = ts.groupby([lambda x: x.year, lambda x: x.month]) grouped.transform(lambda x: x * 2) grouped.transform(np.mean) def test_dispatch_transform(tsframe): df = tsframe[::5].reindex(tsframe.index) grouped = df.groupby(lambda x: x.month) filled = grouped.fillna(method='pad') fillit = lambda x: x.fillna(method='pad') expected = df.groupby(lambda x: x.month).transform(fillit) assert_frame_equal(filled, expected) def test_transform_select_columns(df): f = lambda x: x.mean() result = df.groupby('A')['C', 'D'].transform(f) selection = df[['C', 'D']] expected = selection.groupby(df['A']).transform(f) assert_frame_equal(result, expected) def test_transform_exclude_nuisance(df): # this also tests orderings in transform between # series/frame to make sure it's consistent expected = {} grouped = df.groupby('A') expected['C'] = grouped['C'].transform(np.mean) expected['D'] = grouped['D'].transform(np.mean) expected = DataFrame(expected) result = df.groupby('A').transform(np.mean) assert_frame_equal(result, expected) def test_transform_function_aliases(df): result = df.groupby('A').transform('mean') expected = df.groupby('A').transform(np.mean) assert_frame_equal(result, expected) result = df.groupby('A')['C'].transform('mean') expected = df.groupby('A')['C'].transform(np.mean) assert_series_equal(result, expected) def test_series_fast_transform_date(): # GH 13191 df = pd.DataFrame({'grouping': [np.nan, 1, 1, 3], 'd': pd.date_range('2014-1-1', '2014-1-4')}) result = df.groupby('grouping')['d'].transform('first') dates = [pd.NaT, pd.Timestamp('2014-1-2'), pd.Timestamp('2014-1-2'), pd.Timestamp('2014-1-4')] expected = pd.Series(dates, name='d') assert_series_equal(result, expected) def test_transform_length(): # GH 9697 df = pd.DataFrame({'col1': [1, 1, 2, 2], 'col2': [1, 2, 3, np.nan]}) expected = pd.Series([3.0] * 4) def nsum(x): return np.nansum(x) results = [df.groupby('col1').transform(sum)['col2'], df.groupby('col1')['col2'].transform(sum), df.groupby('col1').transform(nsum)['col2'], df.groupby('col1')['col2'].transform(nsum)] for result in results: assert_series_equal(result, expected, check_names=False) def test_transform_coercion(): # 14457 # when we are transforming be sure to not coerce # via assignment df = pd.DataFrame(dict(A=['a', 'a'], B=[0, 1])) g = df.groupby('A') expected = g.transform(np.mean) result = g.transform(lambda x: np.mean(x)) assert_frame_equal(result, expected) def test_groupby_transform_with_int(): # GH 3740, make sure that we might upcast on item-by-item transform # floats df = DataFrame(dict(A=[1, 1, 1, 2, 2, 2], B=Series(1, dtype='float64'), C=Series( [1, 2, 3, 1, 2, 3], dtype='float64'), D='foo')) with np.errstate(all='ignore'): result = df.groupby('A').transform( lambda x: (x - x.mean()) / x.std()) expected = DataFrame(dict(B=np.nan, C=Series( [-1, 0, 1, -1, 0, 1], dtype='float64'))) assert_frame_equal(result, expected) # int case df = DataFrame(dict(A=[1, 1, 1, 2, 2, 2], B=1, C=[1, 2, 3, 1, 2, 3], D='foo')) with np.errstate(all='ignore'): result = df.groupby('A').transform( lambda x: (x - x.mean()) / x.std()) expected = DataFrame(dict(B=np.nan, C=[-1, 0, 1, -1, 0, 1])) assert_frame_equal(result, expected) # int that needs float conversion s = Series([2, 3, 4, 10, 5, -1]) df = DataFrame(dict(A=[1, 1, 1, 2, 2, 2], B=1, C=s, D='foo')) with np.errstate(all='ignore'): result = df.groupby('A').transform( lambda x: (x - x.mean()) / x.std()) s1 = s.iloc[0:3] s1 = (s1 - s1.mean()) / s1.std() s2 = s.iloc[3:6] s2 = (s2 - s2.mean()) / s2.std() expected = DataFrame(dict(B=np.nan, C=concat([s1, s2]))) assert_frame_equal(result, expected) # int downcasting result = df.groupby('A').transform(lambda x: x * 2 / 2) expected = DataFrame(dict(B=1, C=[2, 3, 4, 10, 5, -1])) assert_frame_equal(result, expected) def test_groupby_transform_with_nan_group(): # GH 9941 df = pd.DataFrame({'a': range(10), 'b': [1, 1, 2, 3, np.nan, 4, 4, 5, 5, 5]}) result = df.groupby(df.b)['a'].transform(max) expected = pd.Series([1., 1., 2., 3., np.nan, 6., 6., 9., 9., 9.], name='a') assert_series_equal(result, expected) def test_transform_mixed_type(): index = MultiIndex.from_arrays([[0, 0, 0, 1, 1, 1], [1, 2, 3, 1, 2, 3] ]) df = DataFrame({'d': [1., 1., 1., 2., 2., 2.], 'c': np.tile(['a', 'b', 'c'], 2), 'v': np.arange(1., 7.)}, index=index) def f(group): group['g'] = group['d'] * 2 return group[:1] grouped = df.groupby('c') result = grouped.apply(f) assert result['d'].dtype == np.float64 # this is by definition a mutating operation! with option_context('mode.chained_assignment', None): for key, group in grouped: res = f(group) assert_frame_equal(res, result.loc[key]) def _check_cython_group_transform_cumulative(pd_op, np_op, dtype): """ Check a group transform that executes a cumulative function. Parameters ---------- pd_op : callable The pandas cumulative function. np_op : callable The analogous one in NumPy. dtype : type The specified dtype of the data. """ is_datetimelike = False data = np.array([[1], [2], [3], [4]], dtype=dtype) ans = np.zeros_like(data) labels = np.array([0, 0, 0, 0], dtype=np.int64) pd_op(ans, data, labels, is_datetimelike) tm.assert_numpy_array_equal(np_op(data), ans[:, 0], check_dtype=False) def test_cython_group_transform_cumsum(any_real_dtype): # see gh-4095 dtype = np.dtype(any_real_dtype).type pd_op, np_op = groupby.group_cumsum, np.cumsum _check_cython_group_transform_cumulative(pd_op, np_op, dtype) def test_cython_group_transform_cumprod(): # see gh-4095 dtype = np.float64 pd_op, np_op = groupby.group_cumprod_float64, np.cumproduct _check_cython_group_transform_cumulative(pd_op, np_op, dtype) def test_cython_group_transform_algos(): # see gh-4095 is_datetimelike = False # with nans labels = np.array([0, 0, 0, 0, 0], dtype=np.int64) data = np.array([[1], [2], [3], [np.nan], [4]], dtype='float64') actual = np.zeros_like(data) actual.fill(np.nan) groupby.group_cumprod_float64(actual, data, labels, is_datetimelike) expected = np.array([1, 2, 6, np.nan, 24], dtype='float64') tm.assert_numpy_array_equal(actual[:, 0], expected) actual = np.zeros_like(data) actual.fill(np.nan) groupby.group_cumsum(actual, data, labels, is_datetimelike) expected = np.array([1, 3, 6, np.nan, 10], dtype='float64') tm.assert_numpy_array_equal(actual[:, 0], expected) # timedelta is_datetimelike = True data = np.array([np.timedelta64(1, 'ns')] * 5, dtype='m8[ns]')[:, None] actual = np.zeros_like(data, dtype='int64') groupby.group_cumsum(actual, data.view('int64'), labels, is_datetimelike) expected = np.array([np.timedelta64(1, 'ns'), np.timedelta64( 2, 'ns'), np.timedelta64(3, 'ns'), np.timedelta64(4, 'ns'), np.timedelta64(5, 'ns')]) tm.assert_numpy_array_equal(actual[:, 0].view('m8[ns]'), expected) @pytest.mark.parametrize( "op, args, targop", [('cumprod', (), lambda x: x.cumprod()), ('cumsum', (), lambda x: x.cumsum()), ('shift', (-1, ), lambda x: x.shift(-1)), ('shift', (1, ), lambda x: x.shift())]) def test_cython_transform_series(op, args, targop): # GH 4095 s = Series(np.random.randn(1000)) s_missing = s.copy() s_missing.iloc[2:10] = np.nan labels = np.random.randint(0, 50, size=1000).astype(float) # series for data in [s, s_missing]: # print(data.head()) expected = data.groupby(labels).transform(targop) tm.assert_series_equal( expected, data.groupby(labels).transform(op, *args)) tm.assert_series_equal(expected, getattr( data.groupby(labels), op)(*args)) @pytest.mark.parametrize("op", ['cumprod', 'cumsum']) @pytest.mark.parametrize("skipna", [False, True]) @pytest.mark.parametrize('input, exp', [ # When everything is NaN ({'key': ['b'] * 10, 'value': np.nan}, pd.Series([np.nan] * 10, name='value')), # When there is a single NaN ({'key': ['b'] * 10 + ['a'] * 2, 'value': [3] * 3 + [np.nan] + [3] * 8}, {('cumprod', False): [3.0, 9.0, 27.0] + [np.nan] * 7 + [3.0, 9.0], ('cumprod', True): [3.0, 9.0, 27.0, np.nan, 81., 243., 729., 2187., 6561., 19683., 3.0, 9.0], ('cumsum', False): [3.0, 6.0, 9.0] + [np.nan] * 7 + [3.0, 6.0], ('cumsum', True): [3.0, 6.0, 9.0, np.nan, 12., 15., 18., 21., 24., 27., 3.0, 6.0]})]) def test_groupby_cum_skipna(op, skipna, input, exp): df = pd.DataFrame(input) result = df.groupby('key')['value'].transform(op, skipna=skipna) if isinstance(exp, dict): expected = exp[(op, skipna)] else: expected = exp expected = pd.Series(expected, name='value') tm.assert_series_equal(expected, result) @pytest.mark.parametrize( "op, args, targop", [('cumprod', (), lambda x: x.cumprod()), ('cumsum', (), lambda x: x.cumsum()), ('shift', (-1, ), lambda x: x.shift(-1)), ('shift', (1, ), lambda x: x.shift())]) def test_cython_transform_frame(op, args, targop): s = Series(np.random.randn(1000)) s_missing = s.copy() s_missing.iloc[2:10] = np.nan labels = np.random.randint(0, 50, size=1000).astype(float) strings = list('qwertyuiopasdfghjklz') strings_missing = strings[:] strings_missing[5] = np.nan df = DataFrame({'float': s, 'float_missing': s_missing, 'int': [1, 1, 1, 1, 2] * 200, 'datetime': pd.date_range('1990-1-1', periods=1000), 'timedelta': pd.timedelta_range(1, freq='s', periods=1000), 'string': strings * 50, 'string_missing': strings_missing * 50}, columns=['float', 'float_missing', 'int', 'datetime', 'timedelta', 'string', 'string_missing']) df['cat'] = df['string'].astype('category') df2 = df.copy() df2.index = pd.MultiIndex.from_product([range(100), range(10)]) # DataFrame - Single and MultiIndex, # group by values, index level, columns for df in [df, df2]: for gb_target in [dict(by=labels), dict(level=0), dict(by='string') ]: # dict(by='string_missing')]: # dict(by=['int','string'])]: gb = df.groupby(**gb_target) # whitelisted methods set the selection before applying # bit a of hack to make sure the cythonized shift # is equivalent to pre 0.17.1 behavior if op == 'shift': gb._set_group_selection() if op != 'shift' and 'int' not in gb_target: # numeric apply fastpath promotes dtype so have # to apply separately and concat i = gb[['int']].apply(targop) f = gb[['float', 'float_missing']].apply(targop) expected = pd.concat([f, i], axis=1) else: expected = gb.apply(targop) expected = expected.sort_index(axis=1) tm.assert_frame_equal(expected, gb.transform(op, *args).sort_index( axis=1)) tm.assert_frame_equal( expected, getattr(gb, op)(*args).sort_index(axis=1)) # individual columns for c in df: if c not in ['float', 'int', 'float_missing' ] and op != 'shift': pytest.raises(DataError, gb[c].transform, op) pytest.raises(DataError, getattr(gb[c], op)) else: expected = gb[c].apply(targop) expected.name = c tm.assert_series_equal(expected, gb[c].transform(op, *args)) tm.assert_series_equal(expected, getattr(gb[c], op)(*args)) def test_transform_with_non_scalar_group(): # GH 10165 cols = pd.MultiIndex.from_tuples([ ('syn', 'A'), ('mis', 'A'), ('non', 'A'), ('syn', 'C'), ('mis', 'C'), ('non', 'C'), ('syn', 'T'), ('mis', 'T'), ('non', 'T'), ('syn', 'G'), ('mis', 'G'), ('non', 'G')]) df = pd.DataFrame(np.random.randint(1, 10, (4, 12)), columns=cols, index=['A', 'C', 'G', 'T']) tm.assert_raises_regex(ValueError, 'transform must return ' 'a scalar value for each ' 'group.*', df.groupby(axis=1, level=1).transform, lambda z: z.div(z.sum(axis=1), axis=0)) @pytest.mark.parametrize('cols,exp,comp_func', [ ('a', pd.Series([1, 1, 1], name='a'), tm.assert_series_equal), (['a', 'c'], pd.DataFrame({'a': [1, 1, 1], 'c': [1, 1, 1]}), tm.assert_frame_equal) ]) @pytest.mark.parametrize('agg_func', [ 'count', 'rank', 'size']) def test_transform_numeric_ret(cols, exp, comp_func, agg_func): if agg_func == 'size' and isinstance(cols, list): pytest.xfail("'size' transformation not supported with " "NDFrameGroupy") # GH 19200 df = pd.DataFrame( {'a': pd.date_range('2018-01-01', periods=3), 'b': range(3), 'c': range(7, 10)}) result = df.groupby('b')[cols].transform(agg_func) if agg_func == 'rank': exp = exp.astype('float') comp_func(result, exp) @pytest.mark.parametrize("mix_groupings", [True, False]) @pytest.mark.parametrize("as_series", [True, False]) @pytest.mark.parametrize("val1,val2", [ ('foo', 'bar'), (1, 2), (1., 2.)]) @pytest.mark.parametrize("fill_method,limit,exp_vals", [ ("ffill", None, [np.nan, np.nan, 'val1', 'val1', 'val1', 'val2', 'val2', 'val2']), ("ffill", 1, [np.nan, np.nan, 'val1', 'val1', np.nan, 'val2', 'val2', np.nan]), ("bfill", None, ['val1', 'val1', 'val1', 'val2', 'val2', 'val2', np.nan, np.nan]), ("bfill", 1, [np.nan, 'val1', 'val1', np.nan, 'val2', 'val2', np.nan, np.nan]) ]) def test_group_fill_methods(mix_groupings, as_series, val1, val2, fill_method, limit, exp_vals): vals = [np.nan, np.nan, val1, np.nan, np.nan, val2, np.nan, np.nan] _exp_vals = list(exp_vals) # Overwrite placeholder values for index, exp_val in enumerate(_exp_vals): if exp_val == 'val1': _exp_vals[index] = val1 elif exp_val == 'val2': _exp_vals[index] = val2 # Need to modify values and expectations depending on the # Series / DataFrame that we ultimately want to generate if mix_groupings: # ['a', 'b', 'a, 'b', ...] keys = ['a', 'b'] * len(vals) def interweave(list_obj): temp = list() for x in list_obj: temp.extend([x, x]) return temp _exp_vals = interweave(_exp_vals) vals = interweave(vals) else: # ['a', 'a', 'a', ... 'b', 'b', 'b'] keys = ['a'] * len(vals) + ['b'] * len(vals) _exp_vals = _exp_vals * 2 vals = vals * 2 df = DataFrame({'key': keys, 'val': vals}) if as_series: result = getattr( df.groupby('key')['val'], fill_method)(limit=limit) exp = Series(_exp_vals, name='val') assert_series_equal(result, exp) else: result = getattr(df.groupby('key'), fill_method)(limit=limit) exp = DataFrame({'key': keys, 'val': _exp_vals}) assert_frame_equal(result, exp) @pytest.mark.parametrize("fill_method", ['ffill', 'bfill']) def test_pad_stable_sorting(fill_method): # GH 21207 x = [0] * 20 y = [np.nan] * 10 + [1] * 10 if fill_method == 'bfill': y = y[::-1] df = pd.DataFrame({'x': x, 'y': y}) expected = df.copy() result = getattr(df.groupby('x'), fill_method)() tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("test_series", [True, False]) @pytest.mark.parametrize("periods,fill_method,limit", [ (1, 'ffill', None), (1, 'ffill', 1), (1, 'bfill', None), (1, 'bfill', 1), (-1, 'ffill', None), (-1, 'ffill', 1), (-1, 'bfill', None), (-1, 'bfill', 1)]) def test_pct_change(test_series, periods, fill_method, limit): vals = [np.nan, np.nan, 1, 2, 4, 10, np.nan, np.nan] exp_vals = Series(vals).pct_change(periods=periods, fill_method=fill_method, limit=limit).tolist() df = DataFrame({'key': ['a'] * len(vals) + ['b'] * len(vals), 'vals': vals * 2}) grp = df.groupby('key') def get_result(grp_obj): return grp_obj.pct_change(periods=periods, fill_method=fill_method, limit=limit) if test_series: exp = pd.Series(exp_vals * 2) exp.name = 'vals' grp = grp['vals'] result = get_result(grp) tm.assert_series_equal(result, exp) else: exp = DataFrame({'vals': exp_vals * 2}) result = get_result(grp) tm.assert_frame_equal(result, exp) @pytest.mark.parametrize("func", [np.any, np.all]) def test_any_all_np_func(func): # GH 20653 df = pd.DataFrame([['foo', True], [np.nan, True], ['foo', True]], columns=['key', 'val']) exp = pd.Series([True, np.nan, True], name='val') res = df.groupby('key')['val'].transform(func) tm.assert_series_equal(res, exp)
bsd-3-clause
jmargeta/scikit-learn
sklearn/tests/test_grid_search.py
2
16592
""" Testing for grid search module (sklearn.grid_search) """ from collections import Iterable, Sized from sklearn.externals.six.moves import cStringIO as StringIO from sklearn.externals.six.moves import xrange from itertools import chain, product import pickle import sys import warnings import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from scipy.stats import distributions from sklearn.base import BaseEstimator from sklearn.datasets.samples_generator import make_classification, make_blobs from sklearn.grid_search import (GridSearchCV, RandomizedSearchCV, ParameterGrid, ParameterSampler) from sklearn.svm import LinearSVC, SVC from sklearn.cluster import KMeans, MeanShift from sklearn.metrics import f1_score from sklearn.metrics import Scorer from sklearn.cross_validation import KFold, StratifiedKFold # Neither of the following two estimators inherit from BaseEstimator, # to test hyperparameter search on user-defined classifiers. class MockClassifier(object): """Dummy classifier to test the cross-validation""" def __init__(self, foo_param=0): self.foo_param = foo_param def fit(self, X, Y): assert_true(len(X) == len(Y)) return self def predict(self, T): return T.shape[0] predict_proba = predict decision_function = predict transform = predict def score(self, X=None, Y=None): if self.foo_param > 1: score = 1. else: score = 0. return score def get_params(self, deep=False): return {'foo_param': self.foo_param} def set_params(self, **params): self.foo_param = params['foo_param'] return self class MockListClassifier(object): """Dummy classifier to test the cross-validation. Checks that GridSearchCV didn't convert X to array. """ def __init__(self, foo_param=0): self.foo_param = foo_param def fit(self, X, Y): assert_true(len(X) == len(Y)) assert_true(isinstance(X, list)) return self def predict(self, T): return T.shape[0] def score(self, X=None, Y=None): if self.foo_param > 1: score = 1. else: score = 0. return score def get_params(self, deep=False): return {'foo_param': self.foo_param} def set_params(self, **params): self.foo_param = params['foo_param'] return self X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) y = np.array([1, 1, 2, 2]) def test_parameter_grid(): """Test basic properties of ParameterGrid.""" params1 = {"foo": [1, 2, 3]} grid1 = ParameterGrid(params1) assert_true(isinstance(grid1, Iterable)) assert_true(isinstance(grid1, Sized)) assert_equal(len(grid1), 3) params2 = {"foo": [4, 2], "bar": ["ham", "spam", "eggs"]} grid2 = ParameterGrid(params2) assert_equal(len(grid2), 6) # loop to assert we can iterate over the grid multiple times for i in xrange(2): # tuple + chain transforms {"a": 1, "b": 2} to ("a", 1, "b", 2) points = set(tuple(chain(*p.items())) for p in grid2) assert_equal(points, set(("foo", x, "bar", y) for x, y in product(params2["foo"], params2["bar"]))) def test_grid_search(): """Test that the best estimator contains the right value for foo_param""" clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, verbose=3) # make sure it selects the smallest parameter in case of ties old_stdout = sys.stdout sys.stdout = StringIO() grid_search.fit(X, y) sys.stdout = old_stdout assert_equal(grid_search.best_estimator_.foo_param, 2) for i, foo_i in enumerate([1, 2, 3]): assert_true(grid_search.cv_scores_[i][0] == {'foo_param': foo_i}) # Smoke test the score etc: grid_search.score(X, y) grid_search.predict_proba(X) grid_search.decision_function(X) grid_search.transform(X) def test_trivial_cv_scores(): """Test search over a "grid" with only one point. Non-regression test: cv_scores_ wouldn't be set by GridSearchCV. """ clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1]}) grid_search.fit(X, y) assert_true(hasattr(grid_search, "cv_scores_")) random_search = RandomizedSearchCV(clf, {'foo_param': [0]}) random_search.fit(X, y) assert_true(hasattr(random_search, "cv_scores_")) def test_no_refit(): """Test that grid search can be used for model selection only""" clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=False) grid_search.fit(X, y) assert_true(hasattr(grid_search, "best_params_")) def test_grid_search_error(): """Test that grid search will capture errors on data with different length""" X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, X_[:180], y_) def test_grid_search_iid(): # test the iid parameter # noise-free simple 2d-data X, y = make_blobs(centers=[[0, 0], [1, 0], [0, 1], [1, 1]], random_state=0, cluster_std=0.1, shuffle=False, n_samples=80) # split dataset into two folds that are not iid # first one contains data of all 4 blobs, second only from two. mask = np.ones(X.shape[0], dtype=np.bool) mask[np.where(y == 1)[0][::2]] = 0 mask[np.where(y == 2)[0][::2]] = 0 # this leads to perfect classification on one fold and a score of 1/3 on # the other svm = SVC(kernel='linear') # create "cv" for splits cv = [[mask, ~mask], [~mask, mask]] # once with iid=True (default) grid_search = GridSearchCV(svm, param_grid={'C': [1, 10]}, cv=cv) grid_search.fit(X, y) _, average_score, scores = grid_search.cv_scores_[0] assert_array_almost_equal(scores, [1, 1. / 3.]) # for first split, 1/4 of dataset is in test, for second 3/4. # take weighted average assert_almost_equal(average_score, 1 * 1. / 4. + 1. / 3. * 3. / 4.) # once with iid=False (default) grid_search = GridSearchCV(svm, param_grid={'C': [1, 10]}, cv=cv, iid=False) grid_search.fit(X, y) _, average_score, scores = grid_search.cv_scores_[0] # scores are the same as above assert_array_almost_equal(scores, [1, 1. / 3.]) # averaged score is just mean of scores assert_almost_equal(average_score, np.mean(scores)) def test_grid_search_one_grid_point(): X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) param_dict = {"C": [1.0], "kernel": ["rbf"], "gamma": [0.1]} clf = SVC() cv = GridSearchCV(clf, param_dict) cv.fit(X_, y_) clf = SVC(C=1.0, kernel="rbf", gamma=0.1) clf.fit(X_, y_) assert_array_equal(clf.dual_coef_, cv.best_estimator_.dual_coef_) def test_grid_search_bad_param_grid(): param_dict = {"C": 1.0} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) param_dict = {"C": []} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) param_dict = {"C": np.ones(6).reshape(3, 2)} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) def test_grid_search_sparse(): """Test that grid search works with both dense and sparse matrices""" X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(X_[:180], y_[:180]) y_pred = cv.predict(X_[180:]) C = cv.best_estimator_.C X_ = sp.csr_matrix(X_) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(X_[:180].tocoo(), y_[:180]) y_pred2 = cv.predict(X_[180:]) C2 = cv.best_estimator_.C assert_true(np.mean(y_pred == y_pred2) >= .9) assert_equal(C, C2) def test_grid_search_sparse_scoring(): X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1") cv.fit(X_[:180], y_[:180]) y_pred = cv.predict(X_[180:]) C = cv.best_estimator_.C X_ = sp.csr_matrix(X_) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1") cv.fit(X_[:180], y_[:180]) y_pred2 = cv.predict(X_[180:]) C2 = cv.best_estimator_.C assert_array_equal(y_pred, y_pred2) assert_equal(C, C2) # Smoke test the score #np.testing.assert_allclose(f1_score(cv.predict(X_[:180]), y[:180]), # cv.score(X_[:180], y[:180])) # test loss where greater is worse def f1_loss(y_true_, y_pred_): return -f1_score(y_true_, y_pred_) F1Loss = Scorer(f1_loss, greater_is_better=False) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring=F1Loss) cv.fit(X_[:180], y_[:180]) y_pred3 = cv.predict(X_[180:]) C3 = cv.best_estimator_.C assert_equal(C, C3) assert_array_equal(y_pred, y_pred3) def test_deprecated_score_func(): # test that old deprecated way of passing a score / loss function is still # supported X, y = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC(random_state=0) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1") cv.fit(X[:180], y[:180]) y_pred = cv.predict(X[180:]) C = cv.best_estimator_.C clf = LinearSVC(random_state=0) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, score_func=f1_score) with warnings.catch_warnings(record=True): # catch deprecation warning cv.fit(X[:180], y[:180]) y_pred_func = cv.predict(X[180:]) C_func = cv.best_estimator_.C assert_array_equal(y_pred, y_pred_func) assert_equal(C, C_func) # test loss where greater is worse def f1_loss(y_true_, y_pred_): return -f1_score(y_true_, y_pred_) clf = LinearSVC(random_state=0) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, loss_func=f1_loss) with warnings.catch_warnings(record=True): # catch deprecation warning cv.fit(X[:180], y[:180]) y_pred_loss = cv.predict(X[180:]) C_loss = cv.best_estimator_.C assert_array_equal(y_pred, y_pred_loss) assert_equal(C, C_loss) def test_grid_search_precomputed_kernel(): """Test that grid search works when the input features are given in the form of a precomputed kernel matrix """ X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) # compute the training kernel matrix corresponding to the linear kernel K_train = np.dot(X_[:180], X_[:180].T) y_train = y_[:180] clf = SVC(kernel='precomputed') cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(K_train, y_train) assert_true(cv.best_score_ >= 0) # compute the test kernel matrix K_test = np.dot(X_[180:], X_[:180].T) y_test = y_[180:] y_pred = cv.predict(K_test) assert_true(np.mean(y_pred == y_test) >= 0) # test error is raised when the precomputed kernel is not array-like # or sparse assert_raises(ValueError, cv.fit, K_train.tolist(), y_train) def test_grid_search_precomputed_kernel_error_nonsquare(): """Test that grid search returns an error with a non-square precomputed training kernel matrix""" K_train = np.zeros((10, 20)) y_train = np.ones((10, )) clf = SVC(kernel='precomputed') cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, K_train, y_train) def test_grid_search_precomputed_kernel_error_kernel_function(): """Test that grid search returns an error when using a kernel_function""" X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) kernel_function = lambda x1, x2: np.dot(x1, x2.T) clf = SVC(kernel=kernel_function) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, X_, y_) class BrokenClassifier(BaseEstimator): """Broken classifier that cannot be fit twice""" def __init__(self, parameter=None): self.parameter = parameter def fit(self, X, y): assert_true(not hasattr(self, 'has_been_fit_')) self.has_been_fit_ = True def predict(self, X): return np.zeros(X.shape[0]) def test_refit(): """Regression test for bug in refitting Simulates re-fitting a broken estimator; this used to break with sparse SVMs. """ X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) clf = GridSearchCV(BrokenClassifier(), [{'parameter': [0, 1]}], scoring="precision", refit=True) clf.fit(X, y) def test_X_as_list(): """Pass X as list in GridSearchCV """ X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) clf = MockListClassifier() cv = KFold(n=len(X), n_folds=3) grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, cv=cv) grid_search.fit(X.tolist(), y).score(X, y) assert_true(hasattr(grid_search, "cv_scores_")) def test_unsupervised_grid_search(): # test grid-search with unsupervised estimator X, y = make_blobs(random_state=0) km = KMeans(random_state=0) grid_search = GridSearchCV(km, param_grid=dict(n_clusters=[2, 3, 4]), scoring='ari') grid_search.fit(X, y) # ARI can find the right number :) assert_equal(grid_search.best_params_["n_clusters"], 3) # Now without a score, and without y grid_search = GridSearchCV(km, param_grid=dict(n_clusters=[2, 3, 4])) grid_search.fit(X) assert_equal(grid_search.best_params_["n_clusters"], 4) def test_bad_estimator(): # test grid-search with unsupervised estimator ms = MeanShift() assert_raises(TypeError, GridSearchCV, ms, param_grid=dict(gamma=[.1, 1, 10]), scoring='ari') def test_param_sampler(): # test basic properties of param sampler param_distributions = {"kernel": ["rbf", "linear"], "C": distributions.uniform(0, 1)} sampler = ParameterSampler(param_distributions=param_distributions, n_iter=10, random_state=0) samples = [x for x in sampler] assert_equal(len(samples), 10) for sample in samples: assert_true(sample["kernel"] in ["rbf", "linear"]) assert_true(0 <= sample["C"] <= 1) def test_randomized_search(): # very basic smoke test X, y = make_classification(n_samples=200, n_features=100, random_state=0) params = dict(C=distributions.expon()) search = RandomizedSearchCV(LinearSVC(), param_distributions=params) search.fit(X, y) assert_equal(len(search.cv_scores_), 10) def test_grid_search_score_consistency(): # test that correct scores are used from sklearn.metrics import auc_score clf = LinearSVC(random_state=0) X, y = make_blobs(random_state=0, centers=2) Cs = [.1, 1, 10] for score in ['f1', 'roc_auc']: grid_search = GridSearchCV(clf, {'C': Cs}, scoring=score) grid_search.fit(X, y) cv = StratifiedKFold(n_folds=3, y=y) for C, scores in zip(Cs, grid_search.cv_scores_): clf.set_params(C=C) scores = scores[2] # get the separate runs from grid scores i = 0 for train, test in cv: clf.fit(X[train], y[train]) if score == "f1": correct_score = f1_score(y[test], clf.predict(X[test])) elif score == "roc_auc": correct_score = auc_score(y[test], clf.decision_function(X[test])) assert_almost_equal(correct_score, scores[i]) i += 1 def test_pickle(): """Test that a fit search can be pickled""" clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=True) grid_search.fit(X, y) pickle.dumps(grid_search) # smoke test random_search = RandomizedSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=True) random_search.fit(X, y) pickle.dumps(random_search) # smoke test
bsd-3-clause
liyu1990/sklearn
sklearn/ensemble/partial_dependence.py
251
15097
"""Partial dependence plots for tree ensembles. """ # Authors: Peter Prettenhofer # License: BSD 3 clause from itertools import count import numbers import numpy as np from scipy.stats.mstats import mquantiles from ..utils.extmath import cartesian from ..externals.joblib import Parallel, delayed from ..externals import six from ..externals.six.moves import map, range, zip from ..utils import check_array from ..tree._tree import DTYPE from ._gradient_boosting import _partial_dependence_tree from .gradient_boosting import BaseGradientBoosting def _grid_from_X(X, percentiles=(0.05, 0.95), grid_resolution=100): """Generate a grid of points based on the ``percentiles of ``X``. The grid is generated by placing ``grid_resolution`` equally spaced points between the ``percentiles`` of each column of ``X``. Parameters ---------- X : ndarray The data percentiles : tuple of floats The percentiles which are used to construct the extreme values of the grid axes. grid_resolution : int The number of equally spaced points that are placed on the grid. Returns ------- grid : ndarray All data points on the grid; ``grid.shape[1] == X.shape[1]`` and ``grid.shape[0] == grid_resolution * X.shape[1]``. axes : seq of ndarray The axes with which the grid has been created. """ if len(percentiles) != 2: raise ValueError('percentile must be tuple of len 2') if not all(0. <= x <= 1. for x in percentiles): raise ValueError('percentile values must be in [0, 1]') axes = [] for col in range(X.shape[1]): uniques = np.unique(X[:, col]) if uniques.shape[0] < grid_resolution: # feature has low resolution use unique vals axis = uniques else: emp_percentiles = mquantiles(X, prob=percentiles, axis=0) # create axis based on percentiles and grid resolution axis = np.linspace(emp_percentiles[0, col], emp_percentiles[1, col], num=grid_resolution, endpoint=True) axes.append(axis) return cartesian(axes), axes def partial_dependence(gbrt, target_variables, grid=None, X=None, percentiles=(0.05, 0.95), grid_resolution=100): """Partial dependence of ``target_variables``. Partial dependence plots show the dependence between the joint values of the ``target_variables`` and the function represented by the ``gbrt``. Read more in the :ref:`User Guide <partial_dependence>`. Parameters ---------- gbrt : BaseGradientBoosting A fitted gradient boosting model. target_variables : array-like, dtype=int The target features for which the partial dependecy should be computed (size should be smaller than 3 for visual renderings). grid : array-like, shape=(n_points, len(target_variables)) The grid of ``target_variables`` values for which the partial dependecy should be evaluated (either ``grid`` or ``X`` must be specified). X : array-like, shape=(n_samples, n_features) The data on which ``gbrt`` was trained. It is used to generate a ``grid`` for the ``target_variables``. The ``grid`` comprises ``grid_resolution`` equally spaced points between the two ``percentiles``. percentiles : (low, high), default=(0.05, 0.95) The lower and upper percentile used create the extreme values for the ``grid``. Only if ``X`` is not None. grid_resolution : int, default=100 The number of equally spaced points on the ``grid``. Returns ------- pdp : array, shape=(n_classes, n_points) The partial dependence function evaluated on the ``grid``. For regression and binary classification ``n_classes==1``. axes : seq of ndarray or None The axes with which the grid has been created or None if the grid has been given. Examples -------- >>> samples = [[0, 0, 2], [1, 0, 0]] >>> labels = [0, 1] >>> from sklearn.ensemble import GradientBoostingClassifier >>> gb = GradientBoostingClassifier(random_state=0).fit(samples, labels) >>> kwargs = dict(X=samples, percentiles=(0, 1), grid_resolution=2) >>> partial_dependence(gb, [0], **kwargs) # doctest: +SKIP (array([[-4.52..., 4.52...]]), [array([ 0., 1.])]) """ if not isinstance(gbrt, BaseGradientBoosting): raise ValueError('gbrt has to be an instance of BaseGradientBoosting') if gbrt.estimators_.shape[0] == 0: raise ValueError('Call %s.fit before partial_dependence' % gbrt.__class__.__name__) if (grid is None and X is None) or (grid is not None and X is not None): raise ValueError('Either grid or X must be specified') target_variables = np.asarray(target_variables, dtype=np.int32, order='C').ravel() if any([not (0 <= fx < gbrt.n_features) for fx in target_variables]): raise ValueError('target_variables must be in [0, %d]' % (gbrt.n_features - 1)) if X is not None: X = check_array(X, dtype=DTYPE, order='C') grid, axes = _grid_from_X(X[:, target_variables], percentiles, grid_resolution) else: assert grid is not None # dont return axes if grid is given axes = None # grid must be 2d if grid.ndim == 1: grid = grid[:, np.newaxis] if grid.ndim != 2: raise ValueError('grid must be 2d but is %dd' % grid.ndim) grid = np.asarray(grid, dtype=DTYPE, order='C') assert grid.shape[1] == target_variables.shape[0] n_trees_per_stage = gbrt.estimators_.shape[1] n_estimators = gbrt.estimators_.shape[0] pdp = np.zeros((n_trees_per_stage, grid.shape[0],), dtype=np.float64, order='C') for stage in range(n_estimators): for k in range(n_trees_per_stage): tree = gbrt.estimators_[stage, k].tree_ _partial_dependence_tree(tree, grid, target_variables, gbrt.learning_rate, pdp[k]) return pdp, axes def plot_partial_dependence(gbrt, X, features, feature_names=None, label=None, n_cols=3, grid_resolution=100, percentiles=(0.05, 0.95), n_jobs=1, verbose=0, ax=None, line_kw=None, contour_kw=None, **fig_kw): """Partial dependence plots for ``features``. The ``len(features)`` plots are arranged in a grid with ``n_cols`` columns. Two-way partial dependence plots are plotted as contour plots. Read more in the :ref:`User Guide <partial_dependence>`. Parameters ---------- gbrt : BaseGradientBoosting A fitted gradient boosting model. X : array-like, shape=(n_samples, n_features) The data on which ``gbrt`` was trained. features : seq of tuples or ints If seq[i] is an int or a tuple with one int value, a one-way PDP is created; if seq[i] is a tuple of two ints, a two-way PDP is created. feature_names : seq of str Name of each feature; feature_names[i] holds the name of the feature with index i. label : object The class label for which the PDPs should be computed. Only if gbrt is a multi-class model. Must be in ``gbrt.classes_``. n_cols : int The number of columns in the grid plot (default: 3). percentiles : (low, high), default=(0.05, 0.95) The lower and upper percentile used to create the extreme values for the PDP axes. grid_resolution : int, default=100 The number of equally spaced points on the axes. n_jobs : int The number of CPUs to use to compute the PDs. -1 means 'all CPUs'. Defaults to 1. verbose : int Verbose output during PD computations. Defaults to 0. ax : Matplotlib axis object, default None An axis object onto which the plots will be drawn. line_kw : dict Dict with keywords passed to the ``pylab.plot`` call. For one-way partial dependence plots. contour_kw : dict Dict with keywords passed to the ``pylab.plot`` call. For two-way partial dependence plots. fig_kw : dict Dict with keywords passed to the figure() call. Note that all keywords not recognized above will be automatically included here. Returns ------- fig : figure The Matplotlib Figure object. axs : seq of Axis objects A seq of Axis objects, one for each subplot. Examples -------- >>> from sklearn.datasets import make_friedman1 >>> from sklearn.ensemble import GradientBoostingRegressor >>> X, y = make_friedman1() >>> clf = GradientBoostingRegressor(n_estimators=10).fit(X, y) >>> fig, axs = plot_partial_dependence(clf, X, [0, (0, 1)]) #doctest: +SKIP ... """ import matplotlib.pyplot as plt from matplotlib import transforms from matplotlib.ticker import MaxNLocator from matplotlib.ticker import ScalarFormatter if not isinstance(gbrt, BaseGradientBoosting): raise ValueError('gbrt has to be an instance of BaseGradientBoosting') if gbrt.estimators_.shape[0] == 0: raise ValueError('Call %s.fit before partial_dependence' % gbrt.__class__.__name__) # set label_idx for multi-class GBRT if hasattr(gbrt, 'classes_') and np.size(gbrt.classes_) > 2: if label is None: raise ValueError('label is not given for multi-class PDP') label_idx = np.searchsorted(gbrt.classes_, label) if gbrt.classes_[label_idx] != label: raise ValueError('label %s not in ``gbrt.classes_``' % str(label)) else: # regression and binary classification label_idx = 0 X = check_array(X, dtype=DTYPE, order='C') if gbrt.n_features != X.shape[1]: raise ValueError('X.shape[1] does not match gbrt.n_features') if line_kw is None: line_kw = {'color': 'green'} if contour_kw is None: contour_kw = {} # convert feature_names to list if feature_names is None: # if not feature_names use fx indices as name feature_names = [str(i) for i in range(gbrt.n_features)] elif isinstance(feature_names, np.ndarray): feature_names = feature_names.tolist() def convert_feature(fx): if isinstance(fx, six.string_types): try: fx = feature_names.index(fx) except ValueError: raise ValueError('Feature %s not in feature_names' % fx) return fx # convert features into a seq of int tuples tmp_features = [] for fxs in features: if isinstance(fxs, (numbers.Integral,) + six.string_types): fxs = (fxs,) try: fxs = np.array([convert_feature(fx) for fx in fxs], dtype=np.int32) except TypeError: raise ValueError('features must be either int, str, or tuple ' 'of int/str') if not (1 <= np.size(fxs) <= 2): raise ValueError('target features must be either one or two') tmp_features.append(fxs) features = tmp_features names = [] try: for fxs in features: l = [] # explicit loop so "i" is bound for exception below for i in fxs: l.append(feature_names[i]) names.append(l) except IndexError: raise ValueError('features[i] must be in [0, n_features) ' 'but was %d' % i) # compute PD functions pd_result = Parallel(n_jobs=n_jobs, verbose=verbose)( delayed(partial_dependence)(gbrt, fxs, X=X, grid_resolution=grid_resolution, percentiles=percentiles) for fxs in features) # get global min and max values of PD grouped by plot type pdp_lim = {} for pdp, axes in pd_result: min_pd, max_pd = pdp[label_idx].min(), pdp[label_idx].max() n_fx = len(axes) old_min_pd, old_max_pd = pdp_lim.get(n_fx, (min_pd, max_pd)) min_pd = min(min_pd, old_min_pd) max_pd = max(max_pd, old_max_pd) pdp_lim[n_fx] = (min_pd, max_pd) # create contour levels for two-way plots if 2 in pdp_lim: Z_level = np.linspace(*pdp_lim[2], num=8) if ax is None: fig = plt.figure(**fig_kw) else: fig = ax.get_figure() fig.clear() n_cols = min(n_cols, len(features)) n_rows = int(np.ceil(len(features) / float(n_cols))) axs = [] for i, fx, name, (pdp, axes) in zip(count(), features, names, pd_result): ax = fig.add_subplot(n_rows, n_cols, i + 1) if len(axes) == 1: ax.plot(axes[0], pdp[label_idx].ravel(), **line_kw) else: # make contour plot assert len(axes) == 2 XX, YY = np.meshgrid(axes[0], axes[1]) Z = pdp[label_idx].reshape(list(map(np.size, axes))).T CS = ax.contour(XX, YY, Z, levels=Z_level, linewidths=0.5, colors='k') ax.contourf(XX, YY, Z, levels=Z_level, vmax=Z_level[-1], vmin=Z_level[0], alpha=0.75, **contour_kw) ax.clabel(CS, fmt='%2.2f', colors='k', fontsize=10, inline=True) # plot data deciles + axes labels deciles = mquantiles(X[:, fx[0]], prob=np.arange(0.1, 1.0, 0.1)) trans = transforms.blended_transform_factory(ax.transData, ax.transAxes) ylim = ax.get_ylim() ax.vlines(deciles, [0], 0.05, transform=trans, color='k') ax.set_xlabel(name[0]) ax.set_ylim(ylim) # prevent x-axis ticks from overlapping ax.xaxis.set_major_locator(MaxNLocator(nbins=6, prune='lower')) tick_formatter = ScalarFormatter() tick_formatter.set_powerlimits((-3, 4)) ax.xaxis.set_major_formatter(tick_formatter) if len(axes) > 1: # two-way PDP - y-axis deciles + labels deciles = mquantiles(X[:, fx[1]], prob=np.arange(0.1, 1.0, 0.1)) trans = transforms.blended_transform_factory(ax.transAxes, ax.transData) xlim = ax.get_xlim() ax.hlines(deciles, [0], 0.05, transform=trans, color='k') ax.set_ylabel(name[1]) # hline erases xlim ax.set_xlim(xlim) else: ax.set_ylabel('Partial dependence') if len(axes) == 1: ax.set_ylim(pdp_lim[1]) axs.append(ax) fig.subplots_adjust(bottom=0.15, top=0.7, left=0.1, right=0.95, wspace=0.4, hspace=0.3) return fig, axs
bsd-3-clause
jingyany/gradient-descent
comparison.py
1
4645
""" This module implements gradient descent algorithm of L2-regularized logistic regression. The method is launched on a real-world dataset, the "Spam" data, which can be downloaded from https://statweb.stanford.edu/~tibs/ElemStatLearn/datasets/spam.data. It is also compared with L2-regularized logistic regression in sklearn """ import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn import preprocessing from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split def convert_label(indicator): if indicator == 1: return +1 else: return -1 def objective(beta, lamda, x, y): n = len(y) yx = y[:, None]*x obj = 1/n*(np.sum(np.log(np.exp(-yx.dot(beta))+1))) + lamda*np.linalg.norm(beta)**2 return obj def computegrad(beta, lamda, x, y): n = len(y) yx = y[:, None] * x upper = yx * np.exp(-yx.dot(beta[:, None])) bottom = np.exp(-yx.dot(beta)) + 1 gradient = -1 / n * np.sum(upper / bottom[:, None], axis=0) + 2 * lamda * beta return gradient def backtracking(beta, lamda, x, y, t=1, alpha=0.5, beta_s=0.8, max_iter=100): grad_beta = computegrad(beta, lamda, x=x, y=y) norm_grad_beta = np.linalg.norm(grad_beta) found_t = 0 iter = 0 while found_t == 0 and iter < max_iter: if (objective(beta - t*grad_beta, lamda, x=x, y=y)) < (objective(beta, lamda, x=x, y=y)-alpha*t*(norm_grad_beta)**2): found_t = 1 elif(iter == max_iter): print ("Maximum number of iterations reached") else: t = t*beta_s iter = iter + 1 return t def graddescent(beta_init, lamda, x, y, max_iter=1000): beta = beta_init grad_beta = computegrad(beta, lamda, x=x, y=y) beta_vals = beta iter = 0 while (iter < max_iter): t = backtracking(beta, lamda, x=x, y=y) beta = beta - t * grad_beta beta_vals = np.vstack((beta_vals, beta)) grad_beta = computegrad(beta, lamda, x=x, y=y) iter = iter + 1 return beta_vals def objective_plot(betas_gd, lamda, x, y): num = np.size(betas_gd, 0) objs_gd = np.zeros(num) for i in range(0, num): objs_gd[i] = objective(betas_gd[i], lamda, x=x, y=y) plt.plot(range(1, num + 1), objs_gd, label='gradient descent') plt.xlabel('Iteration') plt.ylabel('Objective value') plt.show() plt.interactive(False) def load_data(): df = pd.read_csv('spam.csv', sep=' ',header=None) data = df[df.columns[0:57]] data_scaled = preprocessing.scale(data) data_scaled = pd.DataFrame(data_scaled) target = df[df.columns[57]] target = target.apply(convert_label) x_train, x_test, y_train, y_test = train_test_split(data_scaled, target, test_size=0.2, random_state=1) y_train = np.asarray(y_train) x_train = np.asarray(x_train) return x_train, x_test, y_train, y_test def compute_misclassification_error(beta_opt, x, y): y_pred = 1/(1+np.exp(-x.dot(beta_opt))) > 0.5 y_pred = y_pred*2 - 1 # Convert to +/- 1 return np.mean(y_pred != y) def plot_misclassification_error(betas_grad, x, y): niter = np.size(betas_grad, 0) error_grad = np.zeros(niter) for i in range(niter): error_grad[i] = compute_misclassification_error(betas_grad[i, :], x, y) plt.plot(range(1, niter + 1), error_grad, label='gradient descent') plt.xlabel('Iteration') plt.ylabel('Misclassification error') plt.show() plt.interactive(False) def run(): """ :return: Optimal coefficients found using gradient descent, Optimal coefficients found using sklearn, Objective value using gradient descent, Objective value using sklearn """ print("Loading data...") x_train, x_test, y_train, y_test = load_data() d = np.size(x_train, 1) beta = np.zeros(d) n_train = len(y_train) lamda = 0.1 sklearn_lr = LogisticRegression(penalty='l2', C=1 / (2 * lamda * n_train), fit_intercept=False, tol=10e-8, max_iter=1000) sklearn_lr.fit(x_train, y_train) print("Running gradient descent...") betas_gd = graddescent(beta_init=beta, lamda=0.1, x=x_train, y=y_train) print('Optimal coefficients found using gradient descent:', betas_gd[-1, :]) print('Optimal coefficients found using sklearn', sklearn_lr.coef_) print('Objective value using gradient descent:',objective(betas_gd[-1, :], lamda, x=x_train, y=y_train)) print('Objective value using sklearn:',objective(sklearn_lr.coef_.flatten(), lamda, x=x_train, y=y_train)) if __name__ == '__main__': run()
mit
yyjiang/scikit-learn
sklearn/feature_extraction/text.py
24
50103
# -*- coding: utf-8 -*- # Authors: Olivier Grisel <[email protected]> # Mathieu Blondel <[email protected]> # Lars Buitinck <[email protected]> # Robert Layton <[email protected]> # Jochen Wersdörfer <[email protected]> # Roman Sinayev <[email protected]> # # License: BSD 3 clause """ The :mod:`sklearn.feature_extraction.text` submodule gathers utilities to build feature vectors from text documents. """ from __future__ import unicode_literals import array from collections import Mapping, defaultdict import numbers from operator import itemgetter import re import unicodedata import numpy as np import scipy.sparse as sp from ..base import BaseEstimator, TransformerMixin from ..externals import six from ..externals.six.moves import xrange from ..preprocessing import normalize from .hashing import FeatureHasher from .stop_words import ENGLISH_STOP_WORDS from ..utils import deprecated from ..utils.fixes import frombuffer_empty, bincount from ..utils.validation import check_is_fitted __all__ = ['CountVectorizer', 'ENGLISH_STOP_WORDS', 'TfidfTransformer', 'TfidfVectorizer', 'strip_accents_ascii', 'strip_accents_unicode', 'strip_tags'] def strip_accents_unicode(s): """Transform accentuated unicode symbols into their simple counterpart Warning: the python-level loop and join operations make this implementation 20 times slower than the strip_accents_ascii basic normalization. See also -------- strip_accents_ascii Remove accentuated char for any unicode symbol that has a direct ASCII equivalent. """ return ''.join([c for c in unicodedata.normalize('NFKD', s) if not unicodedata.combining(c)]) def strip_accents_ascii(s): """Transform accentuated unicode symbols into ascii or nothing Warning: this solution is only suited for languages that have a direct transliteration to ASCII symbols. See also -------- strip_accents_unicode Remove accentuated char for any unicode symbol. """ nkfd_form = unicodedata.normalize('NFKD', s) return nkfd_form.encode('ASCII', 'ignore').decode('ASCII') def strip_tags(s): """Basic regexp based HTML / XML tag stripper function For serious HTML/XML preprocessing you should rather use an external library such as lxml or BeautifulSoup. """ return re.compile(r"<([^>]+)>", flags=re.UNICODE).sub(" ", s) def _check_stop_list(stop): if stop == "english": return ENGLISH_STOP_WORDS elif isinstance(stop, six.string_types): raise ValueError("not a built-in stop list: %s" % stop) else: # assume it's a collection return stop class VectorizerMixin(object): """Provides common code for text vectorizers (tokenization logic).""" _white_spaces = re.compile(r"\s\s+") def decode(self, doc): """Decode the input into a string of unicode symbols The decoding strategy depends on the vectorizer parameters. """ if self.input == 'filename': with open(doc, 'rb') as fh: doc = fh.read() elif self.input == 'file': doc = doc.read() if isinstance(doc, bytes): doc = doc.decode(self.encoding, self.decode_error) if doc is np.nan: raise ValueError("np.nan is an invalid document, expected byte or " "unicode string.") return doc def _word_ngrams(self, tokens, stop_words=None): """Turn tokens into a sequence of n-grams after stop words filtering""" # handle stop words if stop_words is not None: tokens = [w for w in tokens if w not in stop_words] # handle token n-grams min_n, max_n = self.ngram_range if max_n != 1: original_tokens = tokens tokens = [] n_original_tokens = len(original_tokens) for n in xrange(min_n, min(max_n + 1, n_original_tokens + 1)): for i in xrange(n_original_tokens - n + 1): tokens.append(" ".join(original_tokens[i: i + n])) return tokens def _char_ngrams(self, text_document): """Tokenize text_document into a sequence of character n-grams""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) text_len = len(text_document) ngrams = [] min_n, max_n = self.ngram_range for n in xrange(min_n, min(max_n + 1, text_len + 1)): for i in xrange(text_len - n + 1): ngrams.append(text_document[i: i + n]) return ngrams def _char_wb_ngrams(self, text_document): """Whitespace sensitive char-n-gram tokenization. Tokenize text_document into a sequence of character n-grams excluding any whitespace (operating only inside word boundaries)""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) min_n, max_n = self.ngram_range ngrams = [] for w in text_document.split(): w = ' ' + w + ' ' w_len = len(w) for n in xrange(min_n, max_n + 1): offset = 0 ngrams.append(w[offset:offset + n]) while offset + n < w_len: offset += 1 ngrams.append(w[offset:offset + n]) if offset == 0: # count a short word (w_len < n) only once break return ngrams def build_preprocessor(self): """Return a function to preprocess the text before tokenization""" if self.preprocessor is not None: return self.preprocessor # unfortunately python functools package does not have an efficient # `compose` function that would have allowed us to chain a dynamic # number of functions. However the cost of a lambda call is a few # hundreds of nanoseconds which is negligible when compared to the # cost of tokenizing a string of 1000 chars for instance. noop = lambda x: x # accent stripping if not self.strip_accents: strip_accents = noop elif callable(self.strip_accents): strip_accents = self.strip_accents elif self.strip_accents == 'ascii': strip_accents = strip_accents_ascii elif self.strip_accents == 'unicode': strip_accents = strip_accents_unicode else: raise ValueError('Invalid value for "strip_accents": %s' % self.strip_accents) if self.lowercase: return lambda x: strip_accents(x.lower()) else: return strip_accents def build_tokenizer(self): """Return a function that splits a string into a sequence of tokens""" if self.tokenizer is not None: return self.tokenizer token_pattern = re.compile(self.token_pattern) return lambda doc: token_pattern.findall(doc) def get_stop_words(self): """Build or fetch the effective stop words list""" return _check_stop_list(self.stop_words) def build_analyzer(self): """Return a callable that handles preprocessing and tokenization""" if callable(self.analyzer): return self.analyzer preprocess = self.build_preprocessor() if self.analyzer == 'char': return lambda doc: self._char_ngrams(preprocess(self.decode(doc))) elif self.analyzer == 'char_wb': return lambda doc: self._char_wb_ngrams( preprocess(self.decode(doc))) elif self.analyzer == 'word': stop_words = self.get_stop_words() tokenize = self.build_tokenizer() return lambda doc: self._word_ngrams( tokenize(preprocess(self.decode(doc))), stop_words) else: raise ValueError('%s is not a valid tokenization scheme/analyzer' % self.analyzer) def _validate_vocabulary(self): vocabulary = self.vocabulary if vocabulary is not None: if not isinstance(vocabulary, Mapping): vocab = {} for i, t in enumerate(vocabulary): if vocab.setdefault(t, i) != i: msg = "Duplicate term in vocabulary: %r" % t raise ValueError(msg) vocabulary = vocab else: indices = set(six.itervalues(vocabulary)) if len(indices) != len(vocabulary): raise ValueError("Vocabulary contains repeated indices.") for i in xrange(len(vocabulary)): if i not in indices: msg = ("Vocabulary of size %d doesn't contain index " "%d." % (len(vocabulary), i)) raise ValueError(msg) if not vocabulary: raise ValueError("empty vocabulary passed to fit") self.fixed_vocabulary_ = True self.vocabulary_ = dict(vocabulary) else: self.fixed_vocabulary_ = False def _check_vocabulary(self): """Check if vocabulary is empty or missing (not fit-ed)""" msg = "%(name)s - Vocabulary wasn't fitted." check_is_fitted(self, 'vocabulary_', msg=msg), if len(self.vocabulary_) == 0: raise ValueError("Vocabulary is empty") @property @deprecated("The `fixed_vocabulary` attribute is deprecated and will be " "removed in 0.18. Please use `fixed_vocabulary_` instead.") def fixed_vocabulary(self): return self.fixed_vocabulary_ class HashingVectorizer(BaseEstimator, VectorizerMixin): """Convert a collection of text documents to a matrix of token occurrences It turns a collection of text documents into a scipy.sparse matrix holding token occurrence counts (or binary occurrence information), possibly normalized as token frequencies if norm='l1' or projected on the euclidean unit sphere if norm='l2'. This text vectorizer implementation uses the hashing trick to find the token string name to feature integer index mapping. This strategy has several advantages: - it is very low memory scalable to large datasets as there is no need to store a vocabulary dictionary in memory - it is fast to pickle and un-pickle as it holds no state besides the constructor parameters - it can be used in a streaming (partial fit) or parallel pipeline as there is no state computed during fit. There are also a couple of cons (vs using a CountVectorizer with an in-memory vocabulary): - there is no way to compute the inverse transform (from feature indices to string feature names) which can be a problem when trying to introspect which features are most important to a model. - there can be collisions: distinct tokens can be mapped to the same feature index. However in practice this is rarely an issue if n_features is large enough (e.g. 2 ** 18 for text classification problems). - no IDF weighting as this would render the transformer stateful. The hash function employed is the signed 32-bit version of Murmurhash3. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, default='utf-8' If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n), default=(1, 1) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. lowercase : boolean, default=True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). n_features : integer, default=(2 ** 20) The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners. norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. binary: boolean, default=False. If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype: type, optional Type of the matrix returned by fit_transform() or transform(). non_negative : boolean, default=False Whether output matrices should contain non-negative values only; effectively calls abs on the matrix prior to returning it. When True, output values can be interpreted as frequencies. When False, output values will have expected value zero. See also -------- CountVectorizer, TfidfVectorizer """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', n_features=(2 ** 20), binary=False, norm='l2', non_negative=False, dtype=np.float64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.n_features = n_features self.ngram_range = ngram_range self.binary = binary self.norm = norm self.non_negative = non_negative self.dtype = dtype def partial_fit(self, X, y=None): """Does nothing: this transformer is stateless. This method is just there to mark the fact that this transformer can work in a streaming setup. """ return self def fit(self, X, y=None): """Does nothing: this transformer is stateless.""" # triggers a parameter validation self._get_hasher().fit(X, y=y) return self def transform(self, X, y=None): """Transform a sequence of documents to a document-term matrix. Parameters ---------- X : iterable over raw text documents, length = n_samples Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed. y : (ignored) Returns ------- X : scipy.sparse matrix, shape = (n_samples, self.n_features) Document-term matrix. """ analyzer = self.build_analyzer() X = self._get_hasher().transform(analyzer(doc) for doc in X) if self.binary: X.data.fill(1) if self.norm is not None: X = normalize(X, norm=self.norm, copy=False) return X # Alias transform to fit_transform for convenience fit_transform = transform def _get_hasher(self): return FeatureHasher(n_features=self.n_features, input_type='string', dtype=self.dtype, non_negative=self.non_negative) def _document_frequency(X): """Count the number of non-zero values for each feature in sparse X.""" if sp.isspmatrix_csr(X): return bincount(X.indices, minlength=X.shape[1]) else: return np.diff(sp.csc_matrix(X, copy=False).indptr) class CountVectorizer(BaseEstimator, VectorizerMixin): """Convert a collection of text documents to a matrix of token counts This implementation produces a sparse representation of the counts using scipy.sparse.coo_matrix. If you do not provide an a-priori dictionary and you do not use an analyzer that does some kind of feature selection then the number of features will be equal to the vocabulary size found by analyzing the data. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. Only applies if ``analyzer == 'word'``. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, True by default Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp select tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. Indices in the mapping should not be repeated and should not have any gap between 0 and the largest index. binary : boolean, default=False If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype : type, optional Type of the matrix returned by fit_transform() or transform(). Attributes ---------- vocabulary_ : dict A mapping of terms to feature indices. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- HashingVectorizer, TfidfVectorizer Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.max_df = max_df self.min_df = min_df if max_df < 0 or min_df < 0: raise ValueError("negative value for max_df of min_df") self.max_features = max_features if max_features is not None: if (not isinstance(max_features, numbers.Integral) or max_features <= 0): raise ValueError( "max_features=%r, neither a positive integer nor None" % max_features) self.ngram_range = ngram_range self.vocabulary = vocabulary self.binary = binary self.dtype = dtype def _sort_features(self, X, vocabulary): """Sort features by name Returns a reordered matrix and modifies the vocabulary in place """ sorted_features = sorted(six.iteritems(vocabulary)) map_index = np.empty(len(sorted_features), dtype=np.int32) for new_val, (term, old_val) in enumerate(sorted_features): map_index[new_val] = old_val vocabulary[term] = new_val return X[:, map_index] def _limit_features(self, X, vocabulary, high=None, low=None, limit=None): """Remove too rare or too common features. Prune features that are non zero in more samples than high or less documents than low, modifying the vocabulary, and restricting it to at most the limit most frequent. This does not prune samples with zero features. """ if high is None and low is None and limit is None: return X, set() # Calculate a mask based on document frequencies dfs = _document_frequency(X) tfs = np.asarray(X.sum(axis=0)).ravel() mask = np.ones(len(dfs), dtype=bool) if high is not None: mask &= dfs <= high if low is not None: mask &= dfs >= low if limit is not None and mask.sum() > limit: mask_inds = (-tfs[mask]).argsort()[:limit] new_mask = np.zeros(len(dfs), dtype=bool) new_mask[np.where(mask)[0][mask_inds]] = True mask = new_mask new_indices = np.cumsum(mask) - 1 # maps old indices to new removed_terms = set() for term, old_index in list(six.iteritems(vocabulary)): if mask[old_index]: vocabulary[term] = new_indices[old_index] else: del vocabulary[term] removed_terms.add(term) kept_indices = np.where(mask)[0] if len(kept_indices) == 0: raise ValueError("After pruning, no terms remain. Try a lower" " min_df or a higher max_df.") return X[:, kept_indices], removed_terms def _count_vocab(self, raw_documents, fixed_vocab): """Create sparse feature matrix, and vocabulary where fixed_vocab=False """ if fixed_vocab: vocabulary = self.vocabulary_ else: # Add a new value when a new vocabulary item is seen vocabulary = defaultdict() vocabulary.default_factory = vocabulary.__len__ analyze = self.build_analyzer() j_indices = _make_int_array() indptr = _make_int_array() indptr.append(0) for doc in raw_documents: for feature in analyze(doc): try: j_indices.append(vocabulary[feature]) except KeyError: # Ignore out-of-vocabulary items for fixed_vocab=True continue indptr.append(len(j_indices)) if not fixed_vocab: # disable defaultdict behaviour vocabulary = dict(vocabulary) if not vocabulary: raise ValueError("empty vocabulary; perhaps the documents only" " contain stop words") j_indices = frombuffer_empty(j_indices, dtype=np.intc) indptr = np.frombuffer(indptr, dtype=np.intc) values = np.ones(len(j_indices)) X = sp.csr_matrix((values, j_indices, indptr), shape=(len(indptr) - 1, len(vocabulary)), dtype=self.dtype) X.sum_duplicates() return vocabulary, X def fit(self, raw_documents, y=None): """Learn a vocabulary dictionary of all tokens in the raw documents. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- self """ self.fit_transform(raw_documents) return self def fit_transform(self, raw_documents, y=None): """Learn the vocabulary dictionary and return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : array, [n_samples, n_features] Document-term matrix. """ # We intentionally don't call the transform method to make # fit_transform overridable without unwanted side effects in # TfidfVectorizer. self._validate_vocabulary() max_df = self.max_df min_df = self.min_df max_features = self.max_features vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary_) if self.binary: X.data.fill(1) if not self.fixed_vocabulary_: X = self._sort_features(X, vocabulary) n_doc = X.shape[0] max_doc_count = (max_df if isinstance(max_df, numbers.Integral) else max_df * n_doc) min_doc_count = (min_df if isinstance(min_df, numbers.Integral) else min_df * n_doc) if max_doc_count < min_doc_count: raise ValueError( "max_df corresponds to < documents than min_df") X, self.stop_words_ = self._limit_features(X, vocabulary, max_doc_count, min_doc_count, max_features) self.vocabulary_ = vocabulary return X def transform(self, raw_documents): """Transform documents to document-term matrix. Extract token counts out of raw text documents using the vocabulary fitted with fit or the one provided to the constructor. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : sparse matrix, [n_samples, n_features] Document-term matrix. """ if not hasattr(self, 'vocabulary_'): self._validate_vocabulary() self._check_vocabulary() # use the same matrix-building strategy as fit_transform _, X = self._count_vocab(raw_documents, fixed_vocab=True) if self.binary: X.data.fill(1) return X def inverse_transform(self, X): """Return terms per document with nonzero entries in X. Parameters ---------- X : {array, sparse matrix}, shape = [n_samples, n_features] Returns ------- X_inv : list of arrays, len = n_samples List of arrays of terms. """ self._check_vocabulary() if sp.issparse(X): # We need CSR format for fast row manipulations. X = X.tocsr() else: # We need to convert X to a matrix, so that the indexing # returns 2D objects X = np.asmatrix(X) n_samples = X.shape[0] terms = np.array(list(self.vocabulary_.keys())) indices = np.array(list(self.vocabulary_.values())) inverse_vocabulary = terms[np.argsort(indices)] return [inverse_vocabulary[X[i, :].nonzero()[1]].ravel() for i in range(n_samples)] def get_feature_names(self): """Array mapping from feature integer indices to feature name""" self._check_vocabulary() return [t for t, i in sorted(six.iteritems(self.vocabulary_), key=itemgetter(1))] def _make_int_array(): """Construct an array.array of a type suitable for scipy.sparse indices.""" return array.array(str("i")) class TfidfTransformer(BaseEstimator, TransformerMixin): """Transform a count matrix to a normalized tf or tf-idf representation Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification. The goal of using tf-idf instead of the raw frequencies of occurrence of a token in a given document is to scale down the impact of tokens that occur very frequently in a given corpus and that are hence empirically less informative than features that occur in a small fraction of the training corpus. The actual formula used for tf-idf is tf * (idf + 1) = tf + tf * idf, instead of tf * idf. The effect of this is that terms with zero idf, i.e. that occur in all documents of a training set, will not be entirely ignored. The formulas used to compute tf and idf depend on parameter settings that correspond to the SMART notation used in IR, as follows: Tf is "n" (natural) by default, "l" (logarithmic) when sublinear_tf=True. Idf is "t" when use_idf is given, "n" (none) otherwise. Normalization is "c" (cosine) when norm='l2', "n" (none) when norm=None. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). References ---------- .. [Yates2011] `R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.` .. [MRS2008] `C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.` """ def __init__(self, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): self.norm = norm self.use_idf = use_idf self.smooth_idf = smooth_idf self.sublinear_tf = sublinear_tf def fit(self, X, y=None): """Learn the idf vector (global term weights) Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts """ if not sp.issparse(X): X = sp.csc_matrix(X) if self.use_idf: n_samples, n_features = X.shape df = _document_frequency(X) # perform idf smoothing if required df += int(self.smooth_idf) n_samples += int(self.smooth_idf) # log+1 instead of log makes sure terms with zero idf don't get # suppressed entirely. idf = np.log(float(n_samples) / df) + 1.0 self._idf_diag = sp.spdiags(idf, diags=0, m=n_features, n=n_features) return self def transform(self, X, copy=True): """Transform a count matrix to a tf or tf-idf representation Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- vectors : sparse matrix, [n_samples, n_features] """ if hasattr(X, 'dtype') and np.issubdtype(X.dtype, np.float): # preserve float family dtype X = sp.csr_matrix(X, copy=copy) else: # convert counts or binary occurrences to floats X = sp.csr_matrix(X, dtype=np.float64, copy=copy) n_samples, n_features = X.shape if self.sublinear_tf: np.log(X.data, X.data) X.data += 1 if self.use_idf: check_is_fitted(self, '_idf_diag', 'idf vector is not fitted') expected_n_features = self._idf_diag.shape[0] if n_features != expected_n_features: raise ValueError("Input has n_features=%d while the model" " has been trained with n_features=%d" % ( n_features, expected_n_features)) # *= doesn't work X = X * self._idf_diag if self.norm: X = normalize(X, norm=self.norm, copy=False) return X @property def idf_(self): if hasattr(self, "_idf_diag"): return np.ravel(self._idf_diag.sum(axis=0)) else: return None class TfidfVectorizer(CountVectorizer): """Convert a collection of raw documents to a matrix of TF-IDF features. Equivalent to CountVectorizer followed by TfidfTransformer. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char'} or callable Whether the feature should be made of word or character n-grams. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If a string, it is passed to _check_stop_list and the appropriate stop list is returned. 'english' is currently the only supported string value. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, default True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. binary : boolean, default=False If True, all non-zero term counts are set to 1. This does not mean outputs will have only 0/1 values, only that the tf term in tf-idf is binary. (Set idf and normalization to False to get 0/1 outputs.) dtype : type, optional Type of the matrix returned by fit_transform() or transform(). norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). Attributes ---------- idf_ : array, shape = [n_features], or None The learned idf vector (global term weights) when ``use_idf`` is set to True, None otherwise. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- CountVectorizer Tokenize the documents and count the occurrences of token and return them as a sparse matrix TfidfTransformer Apply Term Frequency Inverse Document Frequency normalization to a sparse matrix of occurrence counts. Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, analyzer='word', stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): super(TfidfVectorizer, self).__init__( input=input, encoding=encoding, decode_error=decode_error, strip_accents=strip_accents, lowercase=lowercase, preprocessor=preprocessor, tokenizer=tokenizer, analyzer=analyzer, stop_words=stop_words, token_pattern=token_pattern, ngram_range=ngram_range, max_df=max_df, min_df=min_df, max_features=max_features, vocabulary=vocabulary, binary=binary, dtype=dtype) self._tfidf = TfidfTransformer(norm=norm, use_idf=use_idf, smooth_idf=smooth_idf, sublinear_tf=sublinear_tf) # Broadcast the TF-IDF parameters to the underlying transformer instance # for easy grid search and repr @property def norm(self): return self._tfidf.norm @norm.setter def norm(self, value): self._tfidf.norm = value @property def use_idf(self): return self._tfidf.use_idf @use_idf.setter def use_idf(self, value): self._tfidf.use_idf = value @property def smooth_idf(self): return self._tfidf.smooth_idf @smooth_idf.setter def smooth_idf(self, value): self._tfidf.smooth_idf = value @property def sublinear_tf(self): return self._tfidf.sublinear_tf @sublinear_tf.setter def sublinear_tf(self, value): self._tfidf.sublinear_tf = value @property def idf_(self): return self._tfidf.idf_ def fit(self, raw_documents, y=None): """Learn vocabulary and idf from training set. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- self : TfidfVectorizer """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) return self def fit_transform(self, raw_documents, y=None): """Learn vocabulary and idf, return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) # X is already a transformed view of raw_documents so # we set copy to False return self._tfidf.transform(X, copy=False) def transform(self, raw_documents, copy=True): """Transform documents to document-term matrix. Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform). Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ check_is_fitted(self, '_tfidf', 'The tfidf vector is not fitted') X = super(TfidfVectorizer, self).transform(raw_documents) return self._tfidf.transform(X, copy=False)
bsd-3-clause
louispotok/pandas
pandas/tests/io/test_pytables.py
1
211635
import pytest import os import tempfile from contextlib import contextmanager from warnings import catch_warnings from distutils.version import LooseVersion import datetime from datetime import timedelta import numpy as np import pandas as pd from pandas import (Series, DataFrame, Panel, MultiIndex, Int64Index, RangeIndex, Categorical, bdate_range, date_range, timedelta_range, Index, DatetimeIndex, isna, compat, concat, Timestamp) import pandas.util.testing as tm import pandas.util._test_decorators as td from pandas.util.testing import (assert_panel_equal, assert_frame_equal, assert_series_equal, set_timezone) from pandas.compat import (is_platform_windows, is_platform_little_endian, PY35, PY36, BytesIO, text_type, range, lrange, u) from pandas.io.formats.printing import pprint_thing from pandas.core.dtypes.common import is_categorical_dtype tables = pytest.importorskip('tables') from pandas.io import pytables as pytables # noqa:E402 from pandas.io.pytables import (TableIterator, # noqa:E402 HDFStore, get_store, Term, read_hdf, PossibleDataLossError, ClosedFileError) _default_compressor = ('blosc' if LooseVersion(tables.__version__) >= LooseVersion('2.2') else 'zlib') # contextmanager to ensure the file cleanup def safe_remove(path): if path is not None: try: os.remove(path) except: pass def safe_close(store): try: if store is not None: store.close() except: pass def create_tempfile(path): """ create an unopened named temporary file """ return os.path.join(tempfile.gettempdir(), path) @contextmanager def ensure_clean_store(path, mode='a', complevel=None, complib=None, fletcher32=False): try: # put in the temporary path if we don't have one already if not len(os.path.dirname(path)): path = create_tempfile(path) store = HDFStore(path, mode=mode, complevel=complevel, complib=complib, fletcher32=False) yield store finally: safe_close(store) if mode == 'w' or mode == 'a': safe_remove(path) @contextmanager def ensure_clean_path(path): """ return essentially a named temporary file that is not opened and deleted on existing; if path is a list, then create and return list of filenames """ try: if isinstance(path, list): filenames = [create_tempfile(p) for p in path] yield filenames else: filenames = [create_tempfile(path)] yield filenames[0] finally: for f in filenames: safe_remove(f) # set these parameters so we don't have file sharing tables.parameters.MAX_NUMEXPR_THREADS = 1 tables.parameters.MAX_BLOSC_THREADS = 1 tables.parameters.MAX_THREADS = 1 def _maybe_remove(store, key): """For tests using tables, try removing the table to be sure there is no content from previous tests using the same table name.""" try: store.remove(key) except: pass class Base(object): @classmethod def setup_class(cls): # Pytables 3.0.0 deprecates lots of things tm.reset_testing_mode() @classmethod def teardown_class(cls): # Pytables 3.0.0 deprecates lots of things tm.set_testing_mode() def setup_method(self, method): self.path = 'tmp.__%s__.h5' % tm.rands(10) def teardown_method(self, method): pass @pytest.mark.single class TestHDFStore(Base): def test_factory_fun(self): path = create_tempfile(self.path) try: with catch_warnings(record=True): with get_store(path) as tbl: raise ValueError('blah') except ValueError: pass finally: safe_remove(path) try: with catch_warnings(record=True): with get_store(path) as tbl: tbl['a'] = tm.makeDataFrame() with catch_warnings(record=True): with get_store(path) as tbl: assert len(tbl) == 1 assert type(tbl['a']) == DataFrame finally: safe_remove(self.path) def test_context(self): path = create_tempfile(self.path) try: with HDFStore(path) as tbl: raise ValueError('blah') except ValueError: pass finally: safe_remove(path) try: with HDFStore(path) as tbl: tbl['a'] = tm.makeDataFrame() with HDFStore(path) as tbl: assert len(tbl) == 1 assert type(tbl['a']) == DataFrame finally: safe_remove(path) def test_conv_read_write(self): path = create_tempfile(self.path) try: def roundtrip(key, obj, **kwargs): obj.to_hdf(path, key, **kwargs) return read_hdf(path, key) o = tm.makeTimeSeries() assert_series_equal(o, roundtrip('series', o)) o = tm.makeStringSeries() assert_series_equal(o, roundtrip('string_series', o)) o = tm.makeDataFrame() assert_frame_equal(o, roundtrip('frame', o)) with catch_warnings(record=True): o = tm.makePanel() assert_panel_equal(o, roundtrip('panel', o)) # table df = DataFrame(dict(A=lrange(5), B=lrange(5))) df.to_hdf(path, 'table', append=True) result = read_hdf(path, 'table', where=['index>2']) assert_frame_equal(df[df.index > 2], result) finally: safe_remove(path) def test_long_strings(self): # GH6166 # unconversion of long strings was being chopped in earlier # versions of numpy < 1.7.2 df = DataFrame({'a': tm.rands_array(100, size=10)}, index=tm.rands_array(100, size=10)) with ensure_clean_store(self.path) as store: store.append('df', df, data_columns=['a']) result = store.select('df') assert_frame_equal(df, result) def test_api(self): # GH4584 # API issue when to_hdf doesn't acdept append AND format args with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() df.iloc[:10].to_hdf(path, 'df', append=True, format='table') df.iloc[10:].to_hdf(path, 'df', append=True, format='table') assert_frame_equal(read_hdf(path, 'df'), df) # append to False df.iloc[:10].to_hdf(path, 'df', append=False, format='table') df.iloc[10:].to_hdf(path, 'df', append=True, format='table') assert_frame_equal(read_hdf(path, 'df'), df) with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() df.iloc[:10].to_hdf(path, 'df', append=True) df.iloc[10:].to_hdf(path, 'df', append=True, format='table') assert_frame_equal(read_hdf(path, 'df'), df) # append to False df.iloc[:10].to_hdf(path, 'df', append=False, format='table') df.iloc[10:].to_hdf(path, 'df', append=True) assert_frame_equal(read_hdf(path, 'df'), df) with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() df.to_hdf(path, 'df', append=False, format='fixed') assert_frame_equal(read_hdf(path, 'df'), df) df.to_hdf(path, 'df', append=False, format='f') assert_frame_equal(read_hdf(path, 'df'), df) df.to_hdf(path, 'df', append=False) assert_frame_equal(read_hdf(path, 'df'), df) df.to_hdf(path, 'df') assert_frame_equal(read_hdf(path, 'df'), df) with ensure_clean_store(self.path) as store: path = store._path df = tm.makeDataFrame() _maybe_remove(store, 'df') store.append('df', df.iloc[:10], append=True, format='table') store.append('df', df.iloc[10:], append=True, format='table') assert_frame_equal(store.select('df'), df) # append to False _maybe_remove(store, 'df') store.append('df', df.iloc[:10], append=False, format='table') store.append('df', df.iloc[10:], append=True, format='table') assert_frame_equal(store.select('df'), df) # formats _maybe_remove(store, 'df') store.append('df', df.iloc[:10], append=False, format='table') store.append('df', df.iloc[10:], append=True, format='table') assert_frame_equal(store.select('df'), df) _maybe_remove(store, 'df') store.append('df', df.iloc[:10], append=False, format='table') store.append('df', df.iloc[10:], append=True, format=None) assert_frame_equal(store.select('df'), df) with ensure_clean_path(self.path) as path: # invalid df = tm.makeDataFrame() pytest.raises(ValueError, df.to_hdf, path, 'df', append=True, format='f') pytest.raises(ValueError, df.to_hdf, path, 'df', append=True, format='fixed') pytest.raises(TypeError, df.to_hdf, path, 'df', append=True, format='foo') pytest.raises(TypeError, df.to_hdf, path, 'df', append=False, format='bar') # File path doesn't exist path = "" pytest.raises(compat.FileNotFoundError, read_hdf, path, 'df') def test_api_default_format(self): # default_format option with ensure_clean_store(self.path) as store: df = tm.makeDataFrame() pd.set_option('io.hdf.default_format', 'fixed') _maybe_remove(store, 'df') store.put('df', df) assert not store.get_storer('df').is_table pytest.raises(ValueError, store.append, 'df2', df) pd.set_option('io.hdf.default_format', 'table') _maybe_remove(store, 'df') store.put('df', df) assert store.get_storer('df').is_table _maybe_remove(store, 'df2') store.append('df2', df) assert store.get_storer('df').is_table pd.set_option('io.hdf.default_format', None) with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() pd.set_option('io.hdf.default_format', 'fixed') df.to_hdf(path, 'df') with HDFStore(path) as store: assert not store.get_storer('df').is_table pytest.raises(ValueError, df.to_hdf, path, 'df2', append=True) pd.set_option('io.hdf.default_format', 'table') df.to_hdf(path, 'df3') with HDFStore(path) as store: assert store.get_storer('df3').is_table df.to_hdf(path, 'df4', append=True) with HDFStore(path) as store: assert store.get_storer('df4').is_table pd.set_option('io.hdf.default_format', None) def test_keys(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeSeries() store['b'] = tm.makeStringSeries() store['c'] = tm.makeDataFrame() with catch_warnings(record=True): store['d'] = tm.makePanel() store['foo/bar'] = tm.makePanel() assert len(store) == 5 expected = set(['/a', '/b', '/c', '/d', '/foo/bar']) assert set(store.keys()) == expected assert set(store) == expected def test_keys_ignore_hdf_softlink(self): # GH 20523 # Puts a softlink into HDF file and rereads with ensure_clean_store(self.path) as store: df = DataFrame(dict(A=lrange(5), B=lrange(5))) store.put("df", df) assert store.keys() == ["/df"] store._handle.create_soft_link(store._handle.root, "symlink", "df") # Should ignore the softlink assert store.keys() == ["/df"] def test_iter_empty(self): with ensure_clean_store(self.path) as store: # GH 12221 assert list(store) == [] def test_repr(self): with ensure_clean_store(self.path) as store: repr(store) store.info() store['a'] = tm.makeTimeSeries() store['b'] = tm.makeStringSeries() store['c'] = tm.makeDataFrame() with catch_warnings(record=True): store['d'] = tm.makePanel() store['foo/bar'] = tm.makePanel() store.append('e', tm.makePanel()) df = tm.makeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['bool1'] = df['A'] > 0 df['bool2'] = df['B'] > 0 df['bool3'] = True df['int1'] = 1 df['int2'] = 2 df['timestamp1'] = Timestamp('20010102') df['timestamp2'] = Timestamp('20010103') df['datetime1'] = datetime.datetime(2001, 1, 2, 0, 0) df['datetime2'] = datetime.datetime(2001, 1, 3, 0, 0) df.loc[3:6, ['obj1']] = np.nan df = df._consolidate()._convert(datetime=True) # PerformanceWarning with catch_warnings(record=True): store['df'] = df # make a random group in hdf space store._handle.create_group(store._handle.root, 'bah') assert store.filename in repr(store) assert store.filename in str(store) store.info() # storers with ensure_clean_store(self.path) as store: df = tm.makeDataFrame() store.append('df', df) s = store.get_storer('df') repr(s) str(s) def test_contains(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeSeries() store['b'] = tm.makeDataFrame() store['foo/bar'] = tm.makeDataFrame() assert 'a' in store assert 'b' in store assert 'c' not in store assert 'foo/bar' in store assert '/foo/bar' in store assert '/foo/b' not in store assert 'bar' not in store # gh-2694: tables.NaturalNameWarning with catch_warnings(record=True): store['node())'] = tm.makeDataFrame() assert 'node())' in store def test_versioning(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeSeries() store['b'] = tm.makeDataFrame() df = tm.makeTimeDataFrame() _maybe_remove(store, 'df1') store.append('df1', df[:10]) store.append('df1', df[10:]) assert store.root.a._v_attrs.pandas_version == '0.15.2' assert store.root.b._v_attrs.pandas_version == '0.15.2' assert store.root.df1._v_attrs.pandas_version == '0.15.2' # write a file and wipe its versioning _maybe_remove(store, 'df2') store.append('df2', df) # this is an error because its table_type is appendable, but no # version info store.get_node('df2')._v_attrs.pandas_version = None pytest.raises(Exception, store.select, 'df2') def test_mode(self): df = tm.makeTimeDataFrame() def check(mode): with ensure_clean_path(self.path) as path: # constructor if mode in ['r', 'r+']: pytest.raises(IOError, HDFStore, path, mode=mode) else: store = HDFStore(path, mode=mode) assert store._handle.mode == mode store.close() with ensure_clean_path(self.path) as path: # context if mode in ['r', 'r+']: def f(): with HDFStore(path, mode=mode) as store: # noqa pass pytest.raises(IOError, f) else: with HDFStore(path, mode=mode) as store: assert store._handle.mode == mode with ensure_clean_path(self.path) as path: # conv write if mode in ['r', 'r+']: pytest.raises(IOError, df.to_hdf, path, 'df', mode=mode) df.to_hdf(path, 'df', mode='w') else: df.to_hdf(path, 'df', mode=mode) # conv read if mode in ['w']: pytest.raises(ValueError, read_hdf, path, 'df', mode=mode) else: result = read_hdf(path, 'df', mode=mode) assert_frame_equal(result, df) def check_default_mode(): # read_hdf uses default mode with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='w') result = read_hdf(path, 'df') assert_frame_equal(result, df) check('r') check('r+') check('a') check('w') check_default_mode() def test_reopen_handle(self): with ensure_clean_path(self.path) as path: store = HDFStore(path, mode='a') store['a'] = tm.makeTimeSeries() # invalid mode change pytest.raises(PossibleDataLossError, store.open, 'w') store.close() assert not store.is_open # truncation ok here store.open('w') assert store.is_open assert len(store) == 0 store.close() assert not store.is_open store = HDFStore(path, mode='a') store['a'] = tm.makeTimeSeries() # reopen as read store.open('r') assert store.is_open assert len(store) == 1 assert store._mode == 'r' store.close() assert not store.is_open # reopen as append store.open('a') assert store.is_open assert len(store) == 1 assert store._mode == 'a' store.close() assert not store.is_open # reopen as append (again) store.open('a') assert store.is_open assert len(store) == 1 assert store._mode == 'a' store.close() assert not store.is_open def test_open_args(self): with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() # create an in memory store store = HDFStore(path, mode='a', driver='H5FD_CORE', driver_core_backing_store=0) store['df'] = df store.append('df2', df) tm.assert_frame_equal(store['df'], df) tm.assert_frame_equal(store['df2'], df) store.close() # the file should not have actually been written assert not os.path.exists(path) def test_flush(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeSeries() store.flush() store.flush(fsync=True) def test_get(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeSeries() left = store.get('a') right = store['a'] tm.assert_series_equal(left, right) left = store.get('/a') right = store['/a'] tm.assert_series_equal(left, right) pytest.raises(KeyError, store.get, 'b') def test_getattr(self): with ensure_clean_store(self.path) as store: s = tm.makeTimeSeries() store['a'] = s # test attribute access result = store.a tm.assert_series_equal(result, s) result = getattr(store, 'a') tm.assert_series_equal(result, s) df = tm.makeTimeDataFrame() store['df'] = df result = store.df tm.assert_frame_equal(result, df) # errors pytest.raises(AttributeError, getattr, store, 'd') for x in ['mode', 'path', 'handle', 'complib']: pytest.raises(AttributeError, getattr, store, x) # not stores for x in ['mode', 'path', 'handle', 'complib']: getattr(store, "_%s" % x) def test_put(self): with ensure_clean_store(self.path) as store: ts = tm.makeTimeSeries() df = tm.makeTimeDataFrame() store['a'] = ts store['b'] = df[:10] store['foo/bar/bah'] = df[:10] store['foo'] = df[:10] store['/foo'] = df[:10] store.put('c', df[:10], format='table') # not OK, not a table pytest.raises( ValueError, store.put, 'b', df[10:], append=True) # node does not currently exist, test _is_table_type returns False # in this case # _maybe_remove(store, 'f') # pytest.raises(ValueError, store.put, 'f', df[10:], # append=True) # can't put to a table (use append instead) pytest.raises(ValueError, store.put, 'c', df[10:], append=True) # overwrite table store.put('c', df[:10], format='table', append=False) tm.assert_frame_equal(df[:10], store['c']) def test_put_string_index(self): with ensure_clean_store(self.path) as store: index = Index( ["I am a very long string index: %s" % i for i in range(20)]) s = Series(np.arange(20), index=index) df = DataFrame({'A': s, 'B': s}) store['a'] = s tm.assert_series_equal(store['a'], s) store['b'] = df tm.assert_frame_equal(store['b'], df) # mixed length index = Index(['abcdefghijklmnopqrstuvwxyz1234567890'] + ["I am a very long string index: %s" % i for i in range(20)]) s = Series(np.arange(21), index=index) df = DataFrame({'A': s, 'B': s}) store['a'] = s tm.assert_series_equal(store['a'], s) store['b'] = df tm.assert_frame_equal(store['b'], df) def test_put_compression(self): with ensure_clean_store(self.path) as store: df = tm.makeTimeDataFrame() store.put('c', df, format='table', complib='zlib') tm.assert_frame_equal(store['c'], df) # can't compress if format='fixed' pytest.raises(ValueError, store.put, 'b', df, format='fixed', complib='zlib') @td.skip_if_windows_python_3 def test_put_compression_blosc(self): df = tm.makeTimeDataFrame() with ensure_clean_store(self.path) as store: # can't compress if format='fixed' pytest.raises(ValueError, store.put, 'b', df, format='fixed', complib='blosc') store.put('c', df, format='table', complib='blosc') tm.assert_frame_equal(store['c'], df) def test_complibs_default_settings(self): # GH15943 df = tm.makeDataFrame() # Set complevel and check if complib is automatically set to # default value with ensure_clean_path(self.path) as tmpfile: df.to_hdf(tmpfile, 'df', complevel=9) result = pd.read_hdf(tmpfile, 'df') tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode='r') as h5file: for node in h5file.walk_nodes(where='/df', classname='Leaf'): assert node.filters.complevel == 9 assert node.filters.complib == 'zlib' # Set complib and check to see if compression is disabled with ensure_clean_path(self.path) as tmpfile: df.to_hdf(tmpfile, 'df', complib='zlib') result = pd.read_hdf(tmpfile, 'df') tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode='r') as h5file: for node in h5file.walk_nodes(where='/df', classname='Leaf'): assert node.filters.complevel == 0 assert node.filters.complib is None # Check if not setting complib or complevel results in no compression with ensure_clean_path(self.path) as tmpfile: df.to_hdf(tmpfile, 'df') result = pd.read_hdf(tmpfile, 'df') tm.assert_frame_equal(result, df) with tables.open_file(tmpfile, mode='r') as h5file: for node in h5file.walk_nodes(where='/df', classname='Leaf'): assert node.filters.complevel == 0 assert node.filters.complib is None # Check if file-defaults can be overridden on a per table basis with ensure_clean_path(self.path) as tmpfile: store = pd.HDFStore(tmpfile) store.append('dfc', df, complevel=9, complib='blosc') store.append('df', df) store.close() with tables.open_file(tmpfile, mode='r') as h5file: for node in h5file.walk_nodes(where='/df', classname='Leaf'): assert node.filters.complevel == 0 assert node.filters.complib is None for node in h5file.walk_nodes(where='/dfc', classname='Leaf'): assert node.filters.complevel == 9 assert node.filters.complib == 'blosc' def test_complibs(self): # GH14478 df = tm.makeDataFrame() # Building list of all complibs and complevels tuples all_complibs = tables.filters.all_complibs # Remove lzo if its not available on this platform if not tables.which_lib_version('lzo'): all_complibs.remove('lzo') # Remove bzip2 if its not available on this platform if not tables.which_lib_version("bzip2"): all_complibs.remove("bzip2") all_levels = range(0, 10) all_tests = [(lib, lvl) for lib in all_complibs for lvl in all_levels] for (lib, lvl) in all_tests: with ensure_clean_path(self.path) as tmpfile: gname = 'foo' # Write and read file to see if data is consistent df.to_hdf(tmpfile, gname, complib=lib, complevel=lvl) result = pd.read_hdf(tmpfile, gname) tm.assert_frame_equal(result, df) # Open file and check metadata # for correct amount of compression h5table = tables.open_file(tmpfile, mode='r') for node in h5table.walk_nodes(where='/' + gname, classname='Leaf'): assert node.filters.complevel == lvl if lvl == 0: assert node.filters.complib is None else: assert node.filters.complib == lib h5table.close() def test_put_integer(self): # non-date, non-string index df = DataFrame(np.random.randn(50, 100)) self._check_roundtrip(df, tm.assert_frame_equal) def test_put_mixed_type(self): df = tm.makeTimeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['bool1'] = df['A'] > 0 df['bool2'] = df['B'] > 0 df['bool3'] = True df['int1'] = 1 df['int2'] = 2 df['timestamp1'] = Timestamp('20010102') df['timestamp2'] = Timestamp('20010103') df['datetime1'] = datetime.datetime(2001, 1, 2, 0, 0) df['datetime2'] = datetime.datetime(2001, 1, 3, 0, 0) df.loc[3:6, ['obj1']] = np.nan df = df._consolidate()._convert(datetime=True) with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df') # PerformanceWarning with catch_warnings(record=True): store.put('df', df) expected = store.get('df') tm.assert_frame_equal(expected, df) def test_append(self): with ensure_clean_store(self.path) as store: # this is allowed by almost always don't want to do it # tables.NaturalNameWarning): with catch_warnings(record=True): df = tm.makeTimeDataFrame() _maybe_remove(store, 'df1') store.append('df1', df[:10]) store.append('df1', df[10:]) tm.assert_frame_equal(store['df1'], df) _maybe_remove(store, 'df2') store.put('df2', df[:10], format='table') store.append('df2', df[10:]) tm.assert_frame_equal(store['df2'], df) _maybe_remove(store, 'df3') store.append('/df3', df[:10]) store.append('/df3', df[10:]) tm.assert_frame_equal(store['df3'], df) # this is allowed by almost always don't want to do it # tables.NaturalNameWarning _maybe_remove(store, '/df3 foo') store.append('/df3 foo', df[:10]) store.append('/df3 foo', df[10:]) tm.assert_frame_equal(store['df3 foo'], df) # panel wp = tm.makePanel() _maybe_remove(store, 'wp1') store.append('wp1', wp.iloc[:, :10, :]) store.append('wp1', wp.iloc[:, 10:, :]) assert_panel_equal(store['wp1'], wp) # test using differt order of items on the non-index axes _maybe_remove(store, 'wp1') wp_append1 = wp.iloc[:, :10, :] store.append('wp1', wp_append1) wp_append2 = wp.iloc[:, 10:, :].reindex(items=wp.items[::-1]) store.append('wp1', wp_append2) assert_panel_equal(store['wp1'], wp) # dtype issues - mizxed type in a single object column df = DataFrame(data=[[1, 2], [0, 1], [1, 2], [0, 0]]) df['mixed_column'] = 'testing' df.loc[2, 'mixed_column'] = np.nan _maybe_remove(store, 'df') store.append('df', df) tm.assert_frame_equal(store['df'], df) # uints - test storage of uints uint_data = DataFrame({ 'u08': Series(np.random.randint(0, high=255, size=5), dtype=np.uint8), 'u16': Series(np.random.randint(0, high=65535, size=5), dtype=np.uint16), 'u32': Series(np.random.randint(0, high=2**30, size=5), dtype=np.uint32), 'u64': Series([2**58, 2**59, 2**60, 2**61, 2**62], dtype=np.uint64)}, index=np.arange(5)) _maybe_remove(store, 'uints') store.append('uints', uint_data) tm.assert_frame_equal(store['uints'], uint_data) # uints - test storage of uints in indexable columns _maybe_remove(store, 'uints') # 64-bit indices not yet supported store.append('uints', uint_data, data_columns=[ 'u08', 'u16', 'u32']) tm.assert_frame_equal(store['uints'], uint_data) def test_append_series(self): with ensure_clean_store(self.path) as store: # basic ss = tm.makeStringSeries() ts = tm.makeTimeSeries() ns = Series(np.arange(100)) store.append('ss', ss) result = store['ss'] tm.assert_series_equal(result, ss) assert result.name is None store.append('ts', ts) result = store['ts'] tm.assert_series_equal(result, ts) assert result.name is None ns.name = 'foo' store.append('ns', ns) result = store['ns'] tm.assert_series_equal(result, ns) assert result.name == ns.name # select on the values expected = ns[ns > 60] result = store.select('ns', 'foo>60') tm.assert_series_equal(result, expected) # select on the index and values expected = ns[(ns > 70) & (ns.index < 90)] result = store.select('ns', 'foo>70 and index<90') tm.assert_series_equal(result, expected) # multi-index mi = DataFrame(np.random.randn(5, 1), columns=['A']) mi['B'] = np.arange(len(mi)) mi['C'] = 'foo' mi.loc[3:5, 'C'] = 'bar' mi.set_index(['C', 'B'], inplace=True) s = mi.stack() s.index = s.index.droplevel(2) store.append('mi', s) tm.assert_series_equal(store['mi'], s) def test_store_index_types(self): # GH5386 # test storing various index types with ensure_clean_store(self.path) as store: def check(format, index): df = DataFrame(np.random.randn(10, 2), columns=list('AB')) df.index = index(len(df)) _maybe_remove(store, 'df') store.put('df', df, format=format) assert_frame_equal(df, store['df']) for index in [tm.makeFloatIndex, tm.makeStringIndex, tm.makeIntIndex, tm.makeDateIndex]: check('table', index) check('fixed', index) # period index currently broken for table # seee GH7796 FIXME check('fixed', tm.makePeriodIndex) # check('table',tm.makePeriodIndex) # unicode index = tm.makeUnicodeIndex if compat.PY3: check('table', index) check('fixed', index) else: # only support for fixed types (and they have a perf warning) pytest.raises(TypeError, check, 'table', index) # PerformanceWarning with catch_warnings(record=True): check('fixed', index) @pytest.mark.skipif(not is_platform_little_endian(), reason="reason platform is not little endian") def test_encoding(self): with ensure_clean_store(self.path) as store: df = DataFrame(dict(A='foo', B='bar'), index=range(5)) df.loc[2, 'A'] = np.nan df.loc[3, 'B'] = np.nan _maybe_remove(store, 'df') store.append('df', df, encoding='ascii') tm.assert_frame_equal(store['df'], df) expected = df.reindex(columns=['A']) result = store.select('df', Term('columns=A', encoding='ascii')) tm.assert_frame_equal(result, expected) def test_latin_encoding(self): if compat.PY2: tm.assert_raises_regex( TypeError, r'\[unicode\] is not implemented as a table column') return values = [[b'E\xc9, 17', b'', b'a', b'b', b'c'], [b'E\xc9, 17', b'a', b'b', b'c'], [b'EE, 17', b'', b'a', b'b', b'c'], [b'E\xc9, 17', b'\xf8\xfc', b'a', b'b', b'c'], [b'', b'a', b'b', b'c'], [b'\xf8\xfc', b'a', b'b', b'c'], [b'A\xf8\xfc', b'', b'a', b'b', b'c'], [np.nan, b'', b'b', b'c'], [b'A\xf8\xfc', np.nan, b'', b'b', b'c']] def _try_decode(x, encoding='latin-1'): try: return x.decode(encoding) except AttributeError: return x # not sure how to remove latin-1 from code in python 2 and 3 values = [[_try_decode(x) for x in y] for y in values] examples = [] for dtype in ['category', object]: for val in values: examples.append(pd.Series(val, dtype=dtype)) def roundtrip(s, key='data', encoding='latin-1', nan_rep=''): with ensure_clean_path(self.path) as store: s.to_hdf(store, key, format='table', encoding=encoding, nan_rep=nan_rep) retr = read_hdf(store, key) s_nan = s.replace(nan_rep, np.nan) if is_categorical_dtype(s_nan): assert is_categorical_dtype(retr) assert_series_equal(s_nan, retr, check_dtype=False, check_categorical=False) else: assert_series_equal(s_nan, retr) for s in examples: roundtrip(s) # fails: # for x in examples: # roundtrip(s, nan_rep=b'\xf8\xfc') def test_append_some_nans(self): with ensure_clean_store(self.path) as store: df = DataFrame({'A': Series(np.random.randn(20)).astype('int32'), 'A1': np.random.randn(20), 'A2': np.random.randn(20), 'B': 'foo', 'C': 'bar', 'D': Timestamp("20010101"), 'E': datetime.datetime(2001, 1, 2, 0, 0)}, index=np.arange(20)) # some nans _maybe_remove(store, 'df1') df.loc[0:15, ['A1', 'B', 'D', 'E']] = np.nan store.append('df1', df[:10]) store.append('df1', df[10:]) tm.assert_frame_equal(store['df1'], df) # first column df1 = df.copy() df1.loc[:, 'A1'] = np.nan _maybe_remove(store, 'df1') store.append('df1', df1[:10]) store.append('df1', df1[10:]) tm.assert_frame_equal(store['df1'], df1) # 2nd column df2 = df.copy() df2.loc[:, 'A2'] = np.nan _maybe_remove(store, 'df2') store.append('df2', df2[:10]) store.append('df2', df2[10:]) tm.assert_frame_equal(store['df2'], df2) # datetimes df3 = df.copy() df3.loc[:, 'E'] = np.nan _maybe_remove(store, 'df3') store.append('df3', df3[:10]) store.append('df3', df3[10:]) tm.assert_frame_equal(store['df3'], df3) def test_append_all_nans(self): with ensure_clean_store(self.path) as store: df = DataFrame({'A1': np.random.randn(20), 'A2': np.random.randn(20)}, index=np.arange(20)) df.loc[0:15, :] = np.nan # nan some entire rows (dropna=True) _maybe_remove(store, 'df') store.append('df', df[:10], dropna=True) store.append('df', df[10:], dropna=True) tm.assert_frame_equal(store['df'], df[-4:]) # nan some entire rows (dropna=False) _maybe_remove(store, 'df2') store.append('df2', df[:10], dropna=False) store.append('df2', df[10:], dropna=False) tm.assert_frame_equal(store['df2'], df) # tests the option io.hdf.dropna_table pd.set_option('io.hdf.dropna_table', False) _maybe_remove(store, 'df3') store.append('df3', df[:10]) store.append('df3', df[10:]) tm.assert_frame_equal(store['df3'], df) pd.set_option('io.hdf.dropna_table', True) _maybe_remove(store, 'df4') store.append('df4', df[:10]) store.append('df4', df[10:]) tm.assert_frame_equal(store['df4'], df[-4:]) # nan some entire rows (string are still written!) df = DataFrame({'A1': np.random.randn(20), 'A2': np.random.randn(20), 'B': 'foo', 'C': 'bar'}, index=np.arange(20)) df.loc[0:15, :] = np.nan _maybe_remove(store, 'df') store.append('df', df[:10], dropna=True) store.append('df', df[10:], dropna=True) tm.assert_frame_equal(store['df'], df) _maybe_remove(store, 'df2') store.append('df2', df[:10], dropna=False) store.append('df2', df[10:], dropna=False) tm.assert_frame_equal(store['df2'], df) # nan some entire rows (but since we have dates they are still # written!) df = DataFrame({'A1': np.random.randn(20), 'A2': np.random.randn(20), 'B': 'foo', 'C': 'bar', 'D': Timestamp("20010101"), 'E': datetime.datetime(2001, 1, 2, 0, 0)}, index=np.arange(20)) df.loc[0:15, :] = np.nan _maybe_remove(store, 'df') store.append('df', df[:10], dropna=True) store.append('df', df[10:], dropna=True) tm.assert_frame_equal(store['df'], df) _maybe_remove(store, 'df2') store.append('df2', df[:10], dropna=False) store.append('df2', df[10:], dropna=False) tm.assert_frame_equal(store['df2'], df) # Test to make sure defaults are to not drop. # Corresponding to Issue 9382 df_with_missing = DataFrame( {'col1': [0, np.nan, 2], 'col2': [1, np.nan, np.nan]}) with ensure_clean_path(self.path) as path: df_with_missing.to_hdf(path, 'df_with_missing', format='table') reloaded = read_hdf(path, 'df_with_missing') tm.assert_frame_equal(df_with_missing, reloaded) matrix = [[[np.nan, np.nan, np.nan], [1, np.nan, np.nan]], [[np.nan, np.nan, np.nan], [np.nan, 5, 6]], [[np.nan, np.nan, np.nan], [np.nan, 3, np.nan]]] with catch_warnings(record=True): panel_with_missing = Panel(matrix, items=['Item1', 'Item2', 'Item3'], major_axis=[1, 2], minor_axis=['A', 'B', 'C']) with ensure_clean_path(self.path) as path: panel_with_missing.to_hdf( path, 'panel_with_missing', format='table') reloaded_panel = read_hdf(path, 'panel_with_missing') tm.assert_panel_equal(panel_with_missing, reloaded_panel) def test_append_frame_column_oriented(self): with ensure_clean_store(self.path) as store: # column oriented df = tm.makeTimeDataFrame() _maybe_remove(store, 'df1') store.append('df1', df.iloc[:, :2], axes=['columns']) store.append('df1', df.iloc[:, 2:]) tm.assert_frame_equal(store['df1'], df) result = store.select('df1', 'columns=A') expected = df.reindex(columns=['A']) tm.assert_frame_equal(expected, result) # selection on the non-indexable result = store.select( 'df1', ('columns=A', 'index=df.index[0:4]')) expected = df.reindex(columns=['A'], index=df.index[0:4]) tm.assert_frame_equal(expected, result) # this isn't supported with pytest.raises(TypeError): store.select('df1', 'columns=A and index>df.index[4]') def test_append_with_different_block_ordering(self): # GH 4096; using same frames, but different block orderings with ensure_clean_store(self.path) as store: for i in range(10): df = DataFrame(np.random.randn(10, 2), columns=list('AB')) df['index'] = range(10) df['index'] += i * 10 df['int64'] = Series([1] * len(df), dtype='int64') df['int16'] = Series([1] * len(df), dtype='int16') if i % 2 == 0: del df['int64'] df['int64'] = Series([1] * len(df), dtype='int64') if i % 3 == 0: a = df.pop('A') df['A'] = a df.set_index('index', inplace=True) store.append('df', df) # test a different ordering but with more fields (like invalid # combinate) with ensure_clean_store(self.path) as store: df = DataFrame(np.random.randn(10, 2), columns=list('AB'), dtype='float64') df['int64'] = Series([1] * len(df), dtype='int64') df['int16'] = Series([1] * len(df), dtype='int16') store.append('df', df) # store additional fields in different blocks df['int16_2'] = Series([1] * len(df), dtype='int16') pytest.raises(ValueError, store.append, 'df', df) # store multile additional fields in different blocks df['float_3'] = Series([1.] * len(df), dtype='float64') pytest.raises(ValueError, store.append, 'df', df) def test_append_with_strings(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel() wp2 = wp.rename_axis( {x: "%s_extra" % x for x in wp.minor_axis}, axis=2) def check_col(key, name, size): assert getattr(store.get_storer(key) .table.description, name).itemsize == size store.append('s1', wp, min_itemsize=20) store.append('s1', wp2) expected = concat([wp, wp2], axis=2) expected = expected.reindex( minor_axis=sorted(expected.minor_axis)) assert_panel_equal(store['s1'], expected) check_col('s1', 'minor_axis', 20) # test dict format store.append('s2', wp, min_itemsize={'minor_axis': 20}) store.append('s2', wp2) expected = concat([wp, wp2], axis=2) expected = expected.reindex( minor_axis=sorted(expected.minor_axis)) assert_panel_equal(store['s2'], expected) check_col('s2', 'minor_axis', 20) # apply the wrong field (similar to #1) store.append('s3', wp, min_itemsize={'major_axis': 20}) pytest.raises(ValueError, store.append, 's3', wp2) # test truncation of bigger strings store.append('s4', wp) pytest.raises(ValueError, store.append, 's4', wp2) # avoid truncation on elements df = DataFrame([[123, 'asdqwerty'], [345, 'dggnhebbsdfbdfb']]) store.append('df_big', df) tm.assert_frame_equal(store.select('df_big'), df) check_col('df_big', 'values_block_1', 15) # appending smaller string ok df2 = DataFrame([[124, 'asdqy'], [346, 'dggnhefbdfb']]) store.append('df_big', df2) expected = concat([df, df2]) tm.assert_frame_equal(store.select('df_big'), expected) check_col('df_big', 'values_block_1', 15) # avoid truncation on elements df = DataFrame([[123, 'asdqwerty'], [345, 'dggnhebbsdfbdfb']]) store.append('df_big2', df, min_itemsize={'values': 50}) tm.assert_frame_equal(store.select('df_big2'), df) check_col('df_big2', 'values_block_1', 50) # bigger string on next append store.append('df_new', df) df_new = DataFrame( [[124, 'abcdefqhij'], [346, 'abcdefghijklmnopqrtsuvwxyz']]) pytest.raises(ValueError, store.append, 'df_new', df_new) # min_itemsize on Series index (GH 11412) df = tm.makeMixedDataFrame().set_index('C') store.append('ss', df['B'], min_itemsize={'index': 4}) tm.assert_series_equal(store.select('ss'), df['B']) # same as above, with data_columns=True store.append('ss2', df['B'], data_columns=True, min_itemsize={'index': 4}) tm.assert_series_equal(store.select('ss2'), df['B']) # min_itemsize in index without appending (GH 10381) store.put('ss3', df, format='table', min_itemsize={'index': 6}) # just make sure there is a longer string: df2 = df.copy().reset_index().assign(C='longer').set_index('C') store.append('ss3', df2) tm.assert_frame_equal(store.select('ss3'), pd.concat([df, df2])) # same as above, with a Series store.put('ss4', df['B'], format='table', min_itemsize={'index': 6}) store.append('ss4', df2['B']) tm.assert_series_equal(store.select('ss4'), pd.concat([df['B'], df2['B']])) # with nans _maybe_remove(store, 'df') df = tm.makeTimeDataFrame() df['string'] = 'foo' df.loc[1:4, 'string'] = np.nan df['string2'] = 'bar' df.loc[4:8, 'string2'] = np.nan df['string3'] = 'bah' df.loc[1:, 'string3'] = np.nan store.append('df', df) result = store.select('df') tm.assert_frame_equal(result, df) with ensure_clean_store(self.path) as store: def check_col(key, name, size): assert getattr(store.get_storer(key) .table.description, name).itemsize, size df = DataFrame(dict(A='foo', B='bar'), index=range(10)) # a min_itemsize that creates a data_column _maybe_remove(store, 'df') store.append('df', df, min_itemsize={'A': 200}) check_col('df', 'A', 200) assert store.get_storer('df').data_columns == ['A'] # a min_itemsize that creates a data_column2 _maybe_remove(store, 'df') store.append('df', df, data_columns=['B'], min_itemsize={'A': 200}) check_col('df', 'A', 200) assert store.get_storer('df').data_columns == ['B', 'A'] # a min_itemsize that creates a data_column2 _maybe_remove(store, 'df') store.append('df', df, data_columns=[ 'B'], min_itemsize={'values': 200}) check_col('df', 'B', 200) check_col('df', 'values_block_0', 200) assert store.get_storer('df').data_columns == ['B'] # infer the .typ on subsequent appends _maybe_remove(store, 'df') store.append('df', df[:5], min_itemsize=200) store.append('df', df[5:], min_itemsize=200) tm.assert_frame_equal(store['df'], df) # invalid min_itemsize keys df = DataFrame(['foo', 'foo', 'foo', 'barh', 'barh', 'barh'], columns=['A']) _maybe_remove(store, 'df') pytest.raises(ValueError, store.append, 'df', df, min_itemsize={'foo': 20, 'foobar': 20}) def test_to_hdf_with_min_itemsize(self): with ensure_clean_path(self.path) as path: # min_itemsize in index with to_hdf (GH 10381) df = tm.makeMixedDataFrame().set_index('C') df.to_hdf(path, 'ss3', format='table', min_itemsize={'index': 6}) # just make sure there is a longer string: df2 = df.copy().reset_index().assign(C='longer').set_index('C') df2.to_hdf(path, 'ss3', append=True, format='table') tm.assert_frame_equal(pd.read_hdf(path, 'ss3'), pd.concat([df, df2])) # same as above, with a Series df['B'].to_hdf(path, 'ss4', format='table', min_itemsize={'index': 6}) df2['B'].to_hdf(path, 'ss4', append=True, format='table') tm.assert_series_equal(pd.read_hdf(path, 'ss4'), pd.concat([df['B'], df2['B']])) @pytest.mark.parametrize("format", ['fixed', 'table']) def test_to_hdf_errors(self, format): data = ['\ud800foo'] ser = pd.Series(data, index=pd.Index(data)) with ensure_clean_path(self.path) as path: # GH 20835 ser.to_hdf(path, 'table', format=format, errors='surrogatepass') result = pd.read_hdf(path, 'table', errors='surrogatepass') tm.assert_series_equal(result, ser) def test_append_with_data_columns(self): with ensure_clean_store(self.path) as store: df = tm.makeTimeDataFrame() df.iloc[0, df.columns.get_loc('B')] = 1. _maybe_remove(store, 'df') store.append('df', df[:2], data_columns=['B']) store.append('df', df[2:]) tm.assert_frame_equal(store['df'], df) # check that we have indices created assert(store._handle.root.df.table.cols.index.is_indexed is True) assert(store._handle.root.df.table.cols.B.is_indexed is True) # data column searching result = store.select('df', 'B>0') expected = df[df.B > 0] tm.assert_frame_equal(result, expected) # data column searching (with an indexable and a data_columns) result = store.select( 'df', 'B>0 and index>df.index[3]') df_new = df.reindex(index=df.index[4:]) expected = df_new[df_new.B > 0] tm.assert_frame_equal(result, expected) # data column selection with a string data_column df_new = df.copy() df_new['string'] = 'foo' df_new.loc[1:4, 'string'] = np.nan df_new.loc[5:6, 'string'] = 'bar' _maybe_remove(store, 'df') store.append('df', df_new, data_columns=['string']) result = store.select('df', "string='foo'") expected = df_new[df_new.string == 'foo'] tm.assert_frame_equal(result, expected) # using min_itemsize and a data column def check_col(key, name, size): assert getattr(store.get_storer(key) .table.description, name).itemsize == size with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df') store.append('df', df_new, data_columns=['string'], min_itemsize={'string': 30}) check_col('df', 'string', 30) _maybe_remove(store, 'df') store.append( 'df', df_new, data_columns=['string'], min_itemsize=30) check_col('df', 'string', 30) _maybe_remove(store, 'df') store.append('df', df_new, data_columns=['string'], min_itemsize={'values': 30}) check_col('df', 'string', 30) with ensure_clean_store(self.path) as store: df_new['string2'] = 'foobarbah' df_new['string_block1'] = 'foobarbah1' df_new['string_block2'] = 'foobarbah2' _maybe_remove(store, 'df') store.append('df', df_new, data_columns=['string', 'string2'], min_itemsize={'string': 30, 'string2': 40, 'values': 50}) check_col('df', 'string', 30) check_col('df', 'string2', 40) check_col('df', 'values_block_1', 50) with ensure_clean_store(self.path) as store: # multiple data columns df_new = df.copy() df_new.iloc[0, df_new.columns.get_loc('A')] = 1. df_new.iloc[0, df_new.columns.get_loc('B')] = -1. df_new['string'] = 'foo' sl = df_new.columns.get_loc('string') df_new.iloc[1:4, sl] = np.nan df_new.iloc[5:6, sl] = 'bar' df_new['string2'] = 'foo' sl = df_new.columns.get_loc('string2') df_new.iloc[2:5, sl] = np.nan df_new.iloc[7:8, sl] = 'bar' _maybe_remove(store, 'df') store.append( 'df', df_new, data_columns=['A', 'B', 'string', 'string2']) result = store.select('df', "string='foo' and string2='foo'" " and A>0 and B<0") expected = df_new[(df_new.string == 'foo') & ( df_new.string2 == 'foo') & (df_new.A > 0) & (df_new.B < 0)] tm.assert_frame_equal(result, expected, check_index_type=False) # yield an empty frame result = store.select('df', "string='foo' and string2='cool'") expected = df_new[(df_new.string == 'foo') & ( df_new.string2 == 'cool')] tm.assert_frame_equal(result, expected, check_index_type=False) with ensure_clean_store(self.path) as store: # doc example df_dc = df.copy() df_dc['string'] = 'foo' df_dc.loc[4:6, 'string'] = np.nan df_dc.loc[7:9, 'string'] = 'bar' df_dc['string2'] = 'cool' df_dc['datetime'] = Timestamp('20010102') df_dc = df_dc._convert(datetime=True) df_dc.loc[3:5, ['A', 'B', 'datetime']] = np.nan _maybe_remove(store, 'df_dc') store.append('df_dc', df_dc, data_columns=['B', 'C', 'string', 'string2', 'datetime']) result = store.select('df_dc', 'B>0') expected = df_dc[df_dc.B > 0] tm.assert_frame_equal(result, expected, check_index_type=False) result = store.select( 'df_dc', ['B > 0', 'C > 0', 'string == foo']) expected = df_dc[(df_dc.B > 0) & (df_dc.C > 0) & ( df_dc.string == 'foo')] tm.assert_frame_equal(result, expected, check_index_type=False) with ensure_clean_store(self.path) as store: # doc example part 2 np.random.seed(1234) index = date_range('1/1/2000', periods=8) df_dc = DataFrame(np.random.randn(8, 3), index=index, columns=['A', 'B', 'C']) df_dc['string'] = 'foo' df_dc.loc[4:6, 'string'] = np.nan df_dc.loc[7:9, 'string'] = 'bar' df_dc.loc[:, ['B', 'C']] = df_dc.loc[:, ['B', 'C']].abs() df_dc['string2'] = 'cool' # on-disk operations store.append('df_dc', df_dc, data_columns=[ 'B', 'C', 'string', 'string2']) result = store.select('df_dc', 'B>0') expected = df_dc[df_dc.B > 0] tm.assert_frame_equal(result, expected) result = store.select( 'df_dc', ['B > 0', 'C > 0', 'string == "foo"']) expected = df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == 'foo')] tm.assert_frame_equal(result, expected) with ensure_clean_store(self.path) as store: with catch_warnings(record=True): # panel # GH5717 not handling data_columns np.random.seed(1234) p = tm.makePanel() store.append('p1', p) tm.assert_panel_equal(store.select('p1'), p) store.append('p2', p, data_columns=True) tm.assert_panel_equal(store.select('p2'), p) result = store.select('p2', where='ItemA>0') expected = p.to_frame() expected = expected[expected['ItemA'] > 0] tm.assert_frame_equal(result.to_frame(), expected) result = store.select( 'p2', where='ItemA>0 & minor_axis=["A","B"]') expected = p.to_frame() expected = expected[expected['ItemA'] > 0] expected = expected[expected.reset_index( level=['major']).index.isin(['A', 'B'])] tm.assert_frame_equal(result.to_frame(), expected) def test_create_table_index(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): def col(t, column): return getattr(store.get_storer(t).table.cols, column) # index=False wp = tm.makePanel() store.append('p5', wp, index=False) store.create_table_index('p5', columns=['major_axis']) assert(col('p5', 'major_axis').is_indexed is True) assert(col('p5', 'minor_axis').is_indexed is False) # index=True store.append('p5i', wp, index=True) assert(col('p5i', 'major_axis').is_indexed is True) assert(col('p5i', 'minor_axis').is_indexed is True) # default optlevels store.get_storer('p5').create_index() assert(col('p5', 'major_axis').index.optlevel == 6) assert(col('p5', 'minor_axis').index.kind == 'medium') # let's change the indexing scheme store.create_table_index('p5') assert(col('p5', 'major_axis').index.optlevel == 6) assert(col('p5', 'minor_axis').index.kind == 'medium') store.create_table_index('p5', optlevel=9) assert(col('p5', 'major_axis').index.optlevel == 9) assert(col('p5', 'minor_axis').index.kind == 'medium') store.create_table_index('p5', kind='full') assert(col('p5', 'major_axis').index.optlevel == 9) assert(col('p5', 'minor_axis').index.kind == 'full') store.create_table_index('p5', optlevel=1, kind='light') assert(col('p5', 'major_axis').index.optlevel == 1) assert(col('p5', 'minor_axis').index.kind == 'light') # data columns df = tm.makeTimeDataFrame() df['string'] = 'foo' df['string2'] = 'bar' store.append('f', df, data_columns=['string', 'string2']) assert(col('f', 'index').is_indexed is True) assert(col('f', 'string').is_indexed is True) assert(col('f', 'string2').is_indexed is True) # specify index=columns store.append( 'f2', df, index=['string'], data_columns=['string', 'string2']) assert(col('f2', 'index').is_indexed is False) assert(col('f2', 'string').is_indexed is True) assert(col('f2', 'string2').is_indexed is False) # try to index a non-table _maybe_remove(store, 'f2') store.put('f2', df) pytest.raises(TypeError, store.create_table_index, 'f2') def test_append_diff_item_order(self): with catch_warnings(record=True): wp = tm.makePanel() wp1 = wp.iloc[:, :10, :] wp2 = wp.iloc[wp.items.get_indexer(['ItemC', 'ItemB', 'ItemA']), 10:, :] with ensure_clean_store(self.path) as store: store.put('panel', wp1, format='table') pytest.raises(ValueError, store.put, 'panel', wp2, append=True) def test_append_hierarchical(self): index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['foo', 'bar']) df = DataFrame(np.random.randn(10, 3), index=index, columns=['A', 'B', 'C']) with ensure_clean_store(self.path) as store: store.append('mi', df) result = store.select('mi') tm.assert_frame_equal(result, df) # GH 3748 result = store.select('mi', columns=['A', 'B']) expected = df.reindex(columns=['A', 'B']) tm.assert_frame_equal(result, expected) with ensure_clean_path('test.hdf') as path: df.to_hdf(path, 'df', format='table') result = read_hdf(path, 'df', columns=['A', 'B']) expected = df.reindex(columns=['A', 'B']) tm.assert_frame_equal(result, expected) def test_column_multiindex(self): # GH 4710 # recreate multi-indexes properly index = MultiIndex.from_tuples([('A', 'a'), ('A', 'b'), ('B', 'a'), ('B', 'b')], names=['first', 'second']) df = DataFrame(np.arange(12).reshape(3, 4), columns=index) expected = df.copy() if isinstance(expected.index, RangeIndex): expected.index = Int64Index(expected.index) with ensure_clean_store(self.path) as store: store.put('df', df) tm.assert_frame_equal(store['df'], expected, check_index_type=True, check_column_type=True) store.put('df1', df, format='table') tm.assert_frame_equal(store['df1'], expected, check_index_type=True, check_column_type=True) pytest.raises(ValueError, store.put, 'df2', df, format='table', data_columns=['A']) pytest.raises(ValueError, store.put, 'df3', df, format='table', data_columns=True) # appending multi-column on existing table (see GH 6167) with ensure_clean_store(self.path) as store: store.append('df2', df) store.append('df2', df) tm.assert_frame_equal(store['df2'], concat((df, df))) # non_index_axes name df = DataFrame(np.arange(12).reshape(3, 4), columns=Index(list('ABCD'), name='foo')) expected = df.copy() if isinstance(expected.index, RangeIndex): expected.index = Int64Index(expected.index) with ensure_clean_store(self.path) as store: store.put('df1', df, format='table') tm.assert_frame_equal(store['df1'], expected, check_index_type=True, check_column_type=True) def test_store_multiindex(self): # validate multi-index names # GH 5527 with ensure_clean_store(self.path) as store: def make_index(names=None): return MultiIndex.from_tuples([(datetime.datetime(2013, 12, d), s, t) for d in range(1, 3) for s in range(2) for t in range(3)], names=names) # no names _maybe_remove(store, 'df') df = DataFrame(np.zeros((12, 2)), columns=[ 'a', 'b'], index=make_index()) store.append('df', df) tm.assert_frame_equal(store.select('df'), df) # partial names _maybe_remove(store, 'df') df = DataFrame(np.zeros((12, 2)), columns=[ 'a', 'b'], index=make_index(['date', None, None])) store.append('df', df) tm.assert_frame_equal(store.select('df'), df) # series _maybe_remove(store, 's') s = Series(np.zeros(12), index=make_index(['date', None, None])) store.append('s', s) xp = Series(np.zeros(12), index=make_index( ['date', 'level_1', 'level_2'])) tm.assert_series_equal(store.select('s'), xp) # dup with column _maybe_remove(store, 'df') df = DataFrame(np.zeros((12, 2)), columns=[ 'a', 'b'], index=make_index(['date', 'a', 't'])) pytest.raises(ValueError, store.append, 'df', df) # fully names _maybe_remove(store, 'df') df = DataFrame(np.zeros((12, 2)), columns=[ 'a', 'b'], index=make_index(['date', 's', 't'])) store.append('df', df) tm.assert_frame_equal(store.select('df'), df) def test_select_columns_in_where(self): # GH 6169 # recreate multi-indexes when columns is passed # in the `where` argument index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['foo_name', 'bar_name']) # With a DataFrame df = DataFrame(np.random.randn(10, 3), index=index, columns=['A', 'B', 'C']) with ensure_clean_store(self.path) as store: store.put('df', df, format='table') expected = df[['A']] tm.assert_frame_equal(store.select('df', columns=['A']), expected) tm.assert_frame_equal(store.select( 'df', where="columns=['A']"), expected) # With a Series s = Series(np.random.randn(10), index=index, name='A') with ensure_clean_store(self.path) as store: store.put('s', s, format='table') tm.assert_series_equal(store.select('s', where="columns=['A']"), s) def test_mi_data_columns(self): # GH 14435 idx = pd.MultiIndex.from_arrays([date_range('2000-01-01', periods=5), range(5)], names=['date', 'id']) df = pd.DataFrame({'a': [1.1, 1.2, 1.3, 1.4, 1.5]}, index=idx) with ensure_clean_store(self.path) as store: store.append('df', df, data_columns=True) actual = store.select('df', where='id == 1') expected = df.iloc[[1], :] tm.assert_frame_equal(actual, expected) def test_pass_spec_to_storer(self): df = tm.makeDataFrame() with ensure_clean_store(self.path) as store: store.put('df', df) pytest.raises(TypeError, store.select, 'df', columns=['A']) pytest.raises(TypeError, store.select, 'df', where=[('columns=A')]) def test_append_misc(self): with ensure_clean_store(self.path) as store: df = tm.makeDataFrame() store.append('df', df, chunksize=1) result = store.select('df') tm.assert_frame_equal(result, df) store.append('df1', df, expectedrows=10) result = store.select('df1') tm.assert_frame_equal(result, df) # more chunksize in append tests def check(obj, comparator): for c in [10, 200, 1000]: with ensure_clean_store(self.path, mode='w') as store: store.append('obj', obj, chunksize=c) result = store.select('obj') comparator(result, obj) df = tm.makeDataFrame() df['string'] = 'foo' df['float322'] = 1. df['float322'] = df['float322'].astype('float32') df['bool'] = df['float322'] > 0 df['time1'] = Timestamp('20130101') df['time2'] = Timestamp('20130102') check(df, tm.assert_frame_equal) with catch_warnings(record=True): p = tm.makePanel() check(p, assert_panel_equal) # empty frame, GH4273 with ensure_clean_store(self.path) as store: # 0 len df_empty = DataFrame(columns=list('ABC')) store.append('df', df_empty) pytest.raises(KeyError, store.select, 'df') # repeated append of 0/non-zero frames df = DataFrame(np.random.rand(10, 3), columns=list('ABC')) store.append('df', df) assert_frame_equal(store.select('df'), df) store.append('df', df_empty) assert_frame_equal(store.select('df'), df) # store df = DataFrame(columns=list('ABC')) store.put('df2', df) assert_frame_equal(store.select('df2'), df) with catch_warnings(record=True): # 0 len p_empty = Panel(items=list('ABC')) store.append('p', p_empty) pytest.raises(KeyError, store.select, 'p') # repeated append of 0/non-zero frames p = Panel(np.random.randn(3, 4, 5), items=list('ABC')) store.append('p', p) assert_panel_equal(store.select('p'), p) store.append('p', p_empty) assert_panel_equal(store.select('p'), p) # store store.put('p2', p_empty) assert_panel_equal(store.select('p2'), p_empty) def test_append_raise(self): with ensure_clean_store(self.path) as store: # test append with invalid input to get good error messages # list in column df = tm.makeDataFrame() df['invalid'] = [['a']] * len(df) assert df.dtypes['invalid'] == np.object_ pytest.raises(TypeError, store.append, 'df', df) # multiple invalid columns df['invalid2'] = [['a']] * len(df) df['invalid3'] = [['a']] * len(df) pytest.raises(TypeError, store.append, 'df', df) # datetime with embedded nans as object df = tm.makeDataFrame() s = Series(datetime.datetime(2001, 1, 2), index=df.index) s = s.astype(object) s[0:5] = np.nan df['invalid'] = s assert df.dtypes['invalid'] == np.object_ pytest.raises(TypeError, store.append, 'df', df) # directly ndarray pytest.raises(TypeError, store.append, 'df', np.arange(10)) # series directly pytest.raises(TypeError, store.append, 'df', Series(np.arange(10))) # appending an incompatible table df = tm.makeDataFrame() store.append('df', df) df['foo'] = 'foo' pytest.raises(ValueError, store.append, 'df', df) def test_table_index_incompatible_dtypes(self): df1 = DataFrame({'a': [1, 2, 3]}) df2 = DataFrame({'a': [4, 5, 6]}, index=date_range('1/1/2000', periods=3)) with ensure_clean_store(self.path) as store: store.put('frame', df1, format='table') pytest.raises(TypeError, store.put, 'frame', df2, format='table', append=True) def test_table_values_dtypes_roundtrip(self): with ensure_clean_store(self.path) as store: df1 = DataFrame({'a': [1, 2, 3]}, dtype='f8') store.append('df_f8', df1) assert_series_equal(df1.dtypes, store['df_f8'].dtypes) df2 = DataFrame({'a': [1, 2, 3]}, dtype='i8') store.append('df_i8', df2) assert_series_equal(df2.dtypes, store['df_i8'].dtypes) # incompatible dtype pytest.raises(ValueError, store.append, 'df_i8', df1) # check creation/storage/retrieval of float32 (a bit hacky to # actually create them thought) df1 = DataFrame( np.array([[1], [2], [3]], dtype='f4'), columns=['A']) store.append('df_f4', df1) assert_series_equal(df1.dtypes, store['df_f4'].dtypes) assert df1.dtypes[0] == 'float32' # check with mixed dtypes df1 = DataFrame(dict((c, Series(np.random.randn(5), dtype=c)) for c in ['float32', 'float64', 'int32', 'int64', 'int16', 'int8'])) df1['string'] = 'foo' df1['float322'] = 1. df1['float322'] = df1['float322'].astype('float32') df1['bool'] = df1['float32'] > 0 df1['time1'] = Timestamp('20130101') df1['time2'] = Timestamp('20130102') store.append('df_mixed_dtypes1', df1) result = store.select('df_mixed_dtypes1').get_dtype_counts() expected = Series({'float32': 2, 'float64': 1, 'int32': 1, 'bool': 1, 'int16': 1, 'int8': 1, 'int64': 1, 'object': 1, 'datetime64[ns]': 2}) result = result.sort_index() expected = expected.sort_index() tm.assert_series_equal(result, expected) def test_table_mixed_dtypes(self): # frame df = tm.makeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['bool1'] = df['A'] > 0 df['bool2'] = df['B'] > 0 df['bool3'] = True df['int1'] = 1 df['int2'] = 2 df['timestamp1'] = Timestamp('20010102') df['timestamp2'] = Timestamp('20010103') df['datetime1'] = datetime.datetime(2001, 1, 2, 0, 0) df['datetime2'] = datetime.datetime(2001, 1, 3, 0, 0) df.loc[3:6, ['obj1']] = np.nan df = df._consolidate()._convert(datetime=True) with ensure_clean_store(self.path) as store: store.append('df1_mixed', df) tm.assert_frame_equal(store.select('df1_mixed'), df) with catch_warnings(record=True): # panel wp = tm.makePanel() wp['obj1'] = 'foo' wp['obj2'] = 'bar' wp['bool1'] = wp['ItemA'] > 0 wp['bool2'] = wp['ItemB'] > 0 wp['int1'] = 1 wp['int2'] = 2 wp = wp._consolidate() with catch_warnings(record=True): with ensure_clean_store(self.path) as store: store.append('p1_mixed', wp) assert_panel_equal(store.select('p1_mixed'), wp) def test_unimplemented_dtypes_table_columns(self): with ensure_clean_store(self.path) as store: l = [('date', datetime.date(2001, 1, 2))] # py3 ok for unicode if not compat.PY3: l.append(('unicode', u('\\u03c3'))) # currently not supported dtypes #### for n, f in l: df = tm.makeDataFrame() df[n] = f pytest.raises( TypeError, store.append, 'df1_%s' % n, df) # frame df = tm.makeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['datetime1'] = datetime.date(2001, 1, 2) df = df._consolidate()._convert(datetime=True) with ensure_clean_store(self.path) as store: # this fails because we have a date in the object block...... pytest.raises(TypeError, store.append, 'df_unimplemented', df) def test_calendar_roundtrip_issue(self): # 8591 # doc example from tseries holiday section weekmask_egypt = 'Sun Mon Tue Wed Thu' holidays = ['2012-05-01', datetime.datetime(2013, 5, 1), np.datetime64('2014-05-01')] bday_egypt = pd.offsets.CustomBusinessDay( holidays=holidays, weekmask=weekmask_egypt) dt = datetime.datetime(2013, 4, 30) dts = date_range(dt, periods=5, freq=bday_egypt) s = (Series(dts.weekday, dts).map( Series('Mon Tue Wed Thu Fri Sat Sun'.split()))) with ensure_clean_store(self.path) as store: store.put('fixed', s) result = store.select('fixed') assert_series_equal(result, s) store.append('table', s) result = store.select('table') assert_series_equal(result, s) def test_roundtrip_tz_aware_index(self): # GH 17618 time = pd.Timestamp('2000-01-01 01:00:00', tz='US/Eastern') df = pd.DataFrame(data=[0], index=[time]) with ensure_clean_store(self.path) as store: store.put('frame', df, format='fixed') recons = store['frame'] tm.assert_frame_equal(recons, df) assert recons.index[0].value == 946706400000000000 def test_append_with_timedelta(self): # GH 3577 # append timedelta df = DataFrame(dict(A=Timestamp('20130101'), B=[Timestamp( '20130101') + timedelta(days=i, seconds=10) for i in range(10)])) df['C'] = df['A'] - df['B'] df.loc[3:5, 'C'] = np.nan with ensure_clean_store(self.path) as store: # table _maybe_remove(store, 'df') store.append('df', df, data_columns=True) result = store.select('df') assert_frame_equal(result, df) result = store.select('df', where="C<100000") assert_frame_equal(result, df) result = store.select('df', where="C<pd.Timedelta('-3D')") assert_frame_equal(result, df.iloc[3:]) result = store.select('df', "C<'-3D'") assert_frame_equal(result, df.iloc[3:]) # a bit hacky here as we don't really deal with the NaT properly result = store.select('df', "C<'-500000s'") result = result.dropna(subset=['C']) assert_frame_equal(result, df.iloc[6:]) result = store.select('df', "C<'-3.5D'") result = result.iloc[1:] assert_frame_equal(result, df.iloc[4:]) # fixed _maybe_remove(store, 'df2') store.put('df2', df) result = store.select('df2') assert_frame_equal(result, df) def test_remove(self): with ensure_clean_store(self.path) as store: ts = tm.makeTimeSeries() df = tm.makeDataFrame() store['a'] = ts store['b'] = df _maybe_remove(store, 'a') assert len(store) == 1 tm.assert_frame_equal(df, store['b']) _maybe_remove(store, 'b') assert len(store) == 0 # nonexistence pytest.raises(KeyError, store.remove, 'a_nonexistent_store') # pathing store['a'] = ts store['b/foo'] = df _maybe_remove(store, 'foo') _maybe_remove(store, 'b/foo') assert len(store) == 1 store['a'] = ts store['b/foo'] = df _maybe_remove(store, 'b') assert len(store) == 1 # __delitem__ store['a'] = ts store['b'] = df del store['a'] del store['b'] assert len(store) == 0 def test_remove_where(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): # non-existance crit1 = 'index>foo' pytest.raises(KeyError, store.remove, 'a', [crit1]) # try to remove non-table (with crit) # non-table ok (where = None) wp = tm.makePanel(30) store.put('wp', wp, format='table') store.remove('wp', ["minor_axis=['A', 'D']"]) rs = store.select('wp') expected = wp.reindex(minor_axis=['B', 'C']) assert_panel_equal(rs, expected) # empty where _maybe_remove(store, 'wp') store.put('wp', wp, format='table') # deleted number (entire table) n = store.remove('wp', []) assert n == 120 # non - empty where _maybe_remove(store, 'wp') store.put('wp', wp, format='table') pytest.raises(ValueError, store.remove, 'wp', ['foo']) def test_remove_startstop(self): # GH #4835 and #6177 with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel(30) # start _maybe_remove(store, 'wp1') store.put('wp1', wp, format='t') n = store.remove('wp1', start=32) assert n == 120 - 32 result = store.select('wp1') expected = wp.reindex(major_axis=wp.major_axis[:32 // 4]) assert_panel_equal(result, expected) _maybe_remove(store, 'wp2') store.put('wp2', wp, format='t') n = store.remove('wp2', start=-32) assert n == 32 result = store.select('wp2') expected = wp.reindex(major_axis=wp.major_axis[:-32 // 4]) assert_panel_equal(result, expected) # stop _maybe_remove(store, 'wp3') store.put('wp3', wp, format='t') n = store.remove('wp3', stop=32) assert n == 32 result = store.select('wp3') expected = wp.reindex(major_axis=wp.major_axis[32 // 4:]) assert_panel_equal(result, expected) _maybe_remove(store, 'wp4') store.put('wp4', wp, format='t') n = store.remove('wp4', stop=-32) assert n == 120 - 32 result = store.select('wp4') expected = wp.reindex(major_axis=wp.major_axis[-32 // 4:]) assert_panel_equal(result, expected) # start n stop _maybe_remove(store, 'wp5') store.put('wp5', wp, format='t') n = store.remove('wp5', start=16, stop=-16) assert n == 120 - 32 result = store.select('wp5') expected = wp.reindex( major_axis=(wp.major_axis[:16 // 4] .union(wp.major_axis[-16 // 4:]))) assert_panel_equal(result, expected) _maybe_remove(store, 'wp6') store.put('wp6', wp, format='t') n = store.remove('wp6', start=16, stop=16) assert n == 0 result = store.select('wp6') expected = wp.reindex(major_axis=wp.major_axis) assert_panel_equal(result, expected) # with where _maybe_remove(store, 'wp7') # TODO: unused? date = wp.major_axis.take(np.arange(0, 30, 3)) # noqa crit = 'major_axis=date' store.put('wp7', wp, format='t') n = store.remove('wp7', where=[crit], stop=80) assert n == 28 result = store.select('wp7') expected = wp.reindex(major_axis=wp.major_axis.difference( wp.major_axis[np.arange(0, 20, 3)])) assert_panel_equal(result, expected) def test_remove_crit(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel(30) # group row removal _maybe_remove(store, 'wp3') date4 = wp.major_axis.take([0, 1, 2, 4, 5, 6, 8, 9, 10]) crit4 = 'major_axis=date4' store.put('wp3', wp, format='t') n = store.remove('wp3', where=[crit4]) assert n == 36 result = store.select('wp3') expected = wp.reindex( major_axis=wp.major_axis.difference(date4)) assert_panel_equal(result, expected) # upper half _maybe_remove(store, 'wp') store.put('wp', wp, format='table') date = wp.major_axis[len(wp.major_axis) // 2] crit1 = 'major_axis>date' crit2 = "minor_axis=['A', 'D']" n = store.remove('wp', where=[crit1]) assert n == 56 n = store.remove('wp', where=[crit2]) assert n == 32 result = store['wp'] expected = wp.truncate(after=date).reindex(minor=['B', 'C']) assert_panel_equal(result, expected) # individual row elements _maybe_remove(store, 'wp2') store.put('wp2', wp, format='table') date1 = wp.major_axis[1:3] crit1 = 'major_axis=date1' store.remove('wp2', where=[crit1]) result = store.select('wp2') expected = wp.reindex( major_axis=wp.major_axis.difference(date1)) assert_panel_equal(result, expected) date2 = wp.major_axis[5] crit2 = 'major_axis=date2' store.remove('wp2', where=[crit2]) result = store['wp2'] expected = wp.reindex( major_axis=(wp.major_axis .difference(date1) .difference(Index([date2])) )) assert_panel_equal(result, expected) date3 = [wp.major_axis[7], wp.major_axis[9]] crit3 = 'major_axis=date3' store.remove('wp2', where=[crit3]) result = store['wp2'] expected = wp.reindex(major_axis=wp.major_axis .difference(date1) .difference(Index([date2])) .difference(Index(date3))) assert_panel_equal(result, expected) # corners _maybe_remove(store, 'wp4') store.put('wp4', wp, format='table') n = store.remove( 'wp4', where="major_axis>wp.major_axis[-1]") result = store.select('wp4') assert_panel_equal(result, wp) def test_invalid_terms(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): df = tm.makeTimeDataFrame() df['string'] = 'foo' df.loc[0:4, 'string'] = 'bar' wp = tm.makePanel() store.put('df', df, format='table') store.put('wp', wp, format='table') # some invalid terms pytest.raises(ValueError, store.select, 'wp', "minor=['A', 'B']") pytest.raises(ValueError, store.select, 'wp', ["index=['20121114']"]) pytest.raises(ValueError, store.select, 'wp', [ "index=['20121114', '20121114']"]) pytest.raises(TypeError, Term) # more invalid pytest.raises( ValueError, store.select, 'df', 'df.index[3]') pytest.raises(SyntaxError, store.select, 'df', 'index>') pytest.raises( ValueError, store.select, 'wp', "major_axis<'20000108' & minor_axis['A', 'B']") # from the docs with ensure_clean_path(self.path) as path: dfq = DataFrame(np.random.randn(10, 4), columns=list( 'ABCD'), index=date_range('20130101', periods=10)) dfq.to_hdf(path, 'dfq', format='table', data_columns=True) # check ok read_hdf(path, 'dfq', where="index>Timestamp('20130104') & columns=['A', 'B']") read_hdf(path, 'dfq', where="A>0 or C>0") # catch the invalid reference with ensure_clean_path(self.path) as path: dfq = DataFrame(np.random.randn(10, 4), columns=list( 'ABCD'), index=date_range('20130101', periods=10)) dfq.to_hdf(path, 'dfq', format='table') pytest.raises(ValueError, read_hdf, path, 'dfq', where="A>0 or C>0") def test_terms(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel() wpneg = Panel.fromDict({-1: tm.makeDataFrame(), 0: tm.makeDataFrame(), 1: tm.makeDataFrame()}) store.put('wp', wp, format='table') store.put('wpneg', wpneg, format='table') # panel result = store.select( 'wp', "major_axis<'20000108' and minor_axis=['A', 'B']") expected = wp.truncate( after='20000108').reindex(minor=['A', 'B']) assert_panel_equal(result, expected) # with deprecation result = store.select( 'wp', where=("major_axis<'20000108' " "and minor_axis=['A', 'B']")) expected = wp.truncate( after='20000108').reindex(minor=['A', 'B']) tm.assert_panel_equal(result, expected) with catch_warnings(record=True): # valid terms terms = [('major_axis=20121114'), ('major_axis>20121114'), (("major_axis=['20121114', '20121114']"),), ('major_axis=datetime.datetime(2012, 11, 14)'), 'major_axis> 20121114', 'major_axis >20121114', 'major_axis > 20121114', (("minor_axis=['A', 'B']"),), (("minor_axis=['A', 'B']"),), ((("minor_axis==['A', 'B']"),),), (("items=['ItemA', 'ItemB']"),), ('items=ItemA'), ] for t in terms: store.select('wp', t) with tm.assert_raises_regex( TypeError, 'Only named functions are supported'): store.select( 'wp', 'major_axis == (lambda x: x)("20130101")') with catch_warnings(record=True): # check USub node parsing res = store.select('wpneg', 'items == -1') expected = Panel({-1: wpneg[-1]}) tm.assert_panel_equal(res, expected) with tm.assert_raises_regex(NotImplementedError, 'Unary addition ' 'not supported'): store.select('wpneg', 'items == +1') def test_term_compat(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) store.append('wp', wp) result = store.select( 'wp', where=("major_axis>20000102 " "and minor_axis=['A', 'B']")) expected = wp.loc[:, wp.major_axis > Timestamp('20000102'), ['A', 'B']] assert_panel_equal(result, expected) store.remove('wp', 'major_axis>20000103') result = store.select('wp') expected = wp.loc[:, wp.major_axis <= Timestamp('20000103'), :] assert_panel_equal(result, expected) with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B', 'C', 'D']) store.append('wp', wp) # stringified datetimes result = store.select( 'wp', 'major_axis>datetime.datetime(2000, 1, 2)') expected = wp.loc[:, wp.major_axis > Timestamp('20000102')] assert_panel_equal(result, expected) result = store.select( 'wp', 'major_axis>datetime.datetime(2000, 1, 2)') expected = wp.loc[:, wp.major_axis > Timestamp('20000102')] assert_panel_equal(result, expected) result = store.select( 'wp', "major_axis=[datetime.datetime(2000, 1, 2, 0, 0), " "datetime.datetime(2000, 1, 3, 0, 0)]") expected = wp.loc[:, [Timestamp('20000102'), Timestamp('20000103')]] assert_panel_equal(result, expected) result = store.select( 'wp', "minor_axis=['A', 'B']") expected = wp.loc[:, :, ['A', 'B']] assert_panel_equal(result, expected) def test_same_name_scoping(self): with ensure_clean_store(self.path) as store: import pandas as pd df = DataFrame(np.random.randn(20, 2), index=pd.date_range('20130101', periods=20)) store.put('df', df, format='table') expected = df[df.index > pd.Timestamp('20130105')] import datetime # noqa result = store.select('df', 'index>datetime.datetime(2013,1,5)') assert_frame_equal(result, expected) from datetime import datetime # noqa # technically an error, but allow it result = store.select('df', 'index>datetime.datetime(2013,1,5)') assert_frame_equal(result, expected) result = store.select('df', 'index>datetime(2013,1,5)') assert_frame_equal(result, expected) def test_series(self): s = tm.makeStringSeries() self._check_roundtrip(s, tm.assert_series_equal) ts = tm.makeTimeSeries() self._check_roundtrip(ts, tm.assert_series_equal) ts2 = Series(ts.index, Index(ts.index, dtype=object)) self._check_roundtrip(ts2, tm.assert_series_equal) ts3 = Series(ts.values, Index(np.asarray(ts.index, dtype=object), dtype=object)) self._check_roundtrip(ts3, tm.assert_series_equal, check_index_type=False) def test_sparse_series(self): s = tm.makeStringSeries() s.iloc[3:5] = np.nan ss = s.to_sparse() self._check_roundtrip(ss, tm.assert_series_equal, check_series_type=True) ss2 = s.to_sparse(kind='integer') self._check_roundtrip(ss2, tm.assert_series_equal, check_series_type=True) ss3 = s.to_sparse(fill_value=0) self._check_roundtrip(ss3, tm.assert_series_equal, check_series_type=True) def test_sparse_frame(self): s = tm.makeDataFrame() s.iloc[3:5, 1:3] = np.nan s.iloc[8:10, -2] = np.nan ss = s.to_sparse() self._check_double_roundtrip(ss, tm.assert_frame_equal, check_frame_type=True) ss2 = s.to_sparse(kind='integer') self._check_double_roundtrip(ss2, tm.assert_frame_equal, check_frame_type=True) ss3 = s.to_sparse(fill_value=0) self._check_double_roundtrip(ss3, tm.assert_frame_equal, check_frame_type=True) def test_float_index(self): # GH #454 index = np.random.randn(10) s = Series(np.random.randn(10), index=index) self._check_roundtrip(s, tm.assert_series_equal) def test_tuple_index(self): # GH #492 col = np.arange(10) idx = [(0., 1.), (2., 3.), (4., 5.)] data = np.random.randn(30).reshape((3, 10)) DF = DataFrame(data, index=idx, columns=col) with catch_warnings(record=True): self._check_roundtrip(DF, tm.assert_frame_equal) def test_index_types(self): with catch_warnings(record=True): values = np.random.randn(2) func = lambda l, r: tm.assert_series_equal(l, r, check_dtype=True, check_index_type=True, check_series_type=True) with catch_warnings(record=True): ser = Series(values, [0, 'y']) self._check_roundtrip(ser, func) with catch_warnings(record=True): ser = Series(values, [datetime.datetime.today(), 0]) self._check_roundtrip(ser, func) with catch_warnings(record=True): ser = Series(values, ['y', 0]) self._check_roundtrip(ser, func) with catch_warnings(record=True): ser = Series(values, [datetime.date.today(), 'a']) self._check_roundtrip(ser, func) with catch_warnings(record=True): ser = Series(values, [0, 'y']) self._check_roundtrip(ser, func) ser = Series(values, [datetime.datetime.today(), 0]) self._check_roundtrip(ser, func) ser = Series(values, ['y', 0]) self._check_roundtrip(ser, func) ser = Series(values, [datetime.date.today(), 'a']) self._check_roundtrip(ser, func) ser = Series(values, [1.23, 'b']) self._check_roundtrip(ser, func) ser = Series(values, [1, 1.53]) self._check_roundtrip(ser, func) ser = Series(values, [1, 5]) self._check_roundtrip(ser, func) ser = Series(values, [datetime.datetime( 2012, 1, 1), datetime.datetime(2012, 1, 2)]) self._check_roundtrip(ser, func) def test_timeseries_preepoch(self): dr = bdate_range('1/1/1940', '1/1/1960') ts = Series(np.random.randn(len(dr)), index=dr) try: self._check_roundtrip(ts, tm.assert_series_equal) except OverflowError: pytest.skip('known failer on some windows platforms') @pytest.mark.parametrize("compression", [ False, pytest.param(True, marks=td.skip_if_windows_python_3) ]) def test_frame(self, compression): df = tm.makeDataFrame() # put in some random NAs df.values[0, 0] = np.nan df.values[5, 3] = np.nan self._check_roundtrip_table(df, tm.assert_frame_equal, compression=compression) self._check_roundtrip(df, tm.assert_frame_equal, compression=compression) tdf = tm.makeTimeDataFrame() self._check_roundtrip(tdf, tm.assert_frame_equal, compression=compression) with ensure_clean_store(self.path) as store: # not consolidated df['foo'] = np.random.randn(len(df)) store['df'] = df recons = store['df'] assert recons._data.is_consolidated() # empty self._check_roundtrip(df[:0], tm.assert_frame_equal) def test_empty_series_frame(self): s0 = Series() s1 = Series(name='myseries') df0 = DataFrame() df1 = DataFrame(index=['a', 'b', 'c']) df2 = DataFrame(columns=['d', 'e', 'f']) self._check_roundtrip(s0, tm.assert_series_equal) self._check_roundtrip(s1, tm.assert_series_equal) self._check_roundtrip(df0, tm.assert_frame_equal) self._check_roundtrip(df1, tm.assert_frame_equal) self._check_roundtrip(df2, tm.assert_frame_equal) def test_empty_series(self): for dtype in [np.int64, np.float64, np.object, 'm8[ns]', 'M8[ns]']: s = Series(dtype=dtype) self._check_roundtrip(s, tm.assert_series_equal) def test_can_serialize_dates(self): rng = [x.date() for x in bdate_range('1/1/2000', '1/30/2000')] frame = DataFrame(np.random.randn(len(rng), 4), index=rng) self._check_roundtrip(frame, tm.assert_frame_equal) def test_store_hierarchical(self): index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=['foo', 'bar']) frame = DataFrame(np.random.randn(10, 3), index=index, columns=['A', 'B', 'C']) self._check_roundtrip(frame, tm.assert_frame_equal) self._check_roundtrip(frame.T, tm.assert_frame_equal) self._check_roundtrip(frame['A'], tm.assert_series_equal) # check that the names are stored with ensure_clean_store(self.path) as store: store['frame'] = frame recons = store['frame'] tm.assert_frame_equal(recons, frame) def test_store_index_name(self): df = tm.makeDataFrame() df.index.name = 'foo' with ensure_clean_store(self.path) as store: store['frame'] = df recons = store['frame'] tm.assert_frame_equal(recons, df) def test_store_index_name_with_tz(self): # GH 13884 df = pd.DataFrame({'A': [1, 2]}) df.index = pd.DatetimeIndex([1234567890123456787, 1234567890123456788]) df.index = df.index.tz_localize('UTC') df.index.name = 'foo' with ensure_clean_store(self.path) as store: store.put('frame', df, format='table') recons = store['frame'] tm.assert_frame_equal(recons, df) @pytest.mark.parametrize('table_format', ['table', 'fixed']) def test_store_index_name_numpy_str(self, table_format): # GH #13492 idx = pd.Index(pd.to_datetime([datetime.date(2000, 1, 1), datetime.date(2000, 1, 2)]), name=u('cols\u05d2')) idx1 = pd.Index(pd.to_datetime([datetime.date(2010, 1, 1), datetime.date(2010, 1, 2)]), name=u('rows\u05d0')) df = pd.DataFrame(np.arange(4).reshape(2, 2), columns=idx, index=idx1) # This used to fail, returning numpy strings instead of python strings. with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format=table_format) df2 = read_hdf(path, 'df') assert_frame_equal(df, df2, check_names=True) assert type(df2.index.name) == text_type assert type(df2.columns.name) == text_type def test_store_series_name(self): df = tm.makeDataFrame() series = df['A'] with ensure_clean_store(self.path) as store: store['series'] = series recons = store['series'] tm.assert_series_equal(recons, series) @pytest.mark.parametrize("compression", [ False, pytest.param(True, marks=td.skip_if_windows_python_3) ]) def test_store_mixed(self, compression): def _make_one(): df = tm.makeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['bool1'] = df['A'] > 0 df['bool2'] = df['B'] > 0 df['int1'] = 1 df['int2'] = 2 return df._consolidate() df1 = _make_one() df2 = _make_one() self._check_roundtrip(df1, tm.assert_frame_equal) self._check_roundtrip(df2, tm.assert_frame_equal) with ensure_clean_store(self.path) as store: store['obj'] = df1 tm.assert_frame_equal(store['obj'], df1) store['obj'] = df2 tm.assert_frame_equal(store['obj'], df2) # check that can store Series of all of these types self._check_roundtrip(df1['obj1'], tm.assert_series_equal, compression=compression) self._check_roundtrip(df1['bool1'], tm.assert_series_equal, compression=compression) self._check_roundtrip(df1['int1'], tm.assert_series_equal, compression=compression) def test_wide(self): with catch_warnings(record=True): wp = tm.makePanel() self._check_roundtrip(wp, assert_panel_equal) def test_select_with_dups(self): # single dtypes df = DataFrame(np.random.randn(10, 4), columns=['A', 'A', 'B', 'B']) df.index = date_range('20130101 9:30', periods=10, freq='T') with ensure_clean_store(self.path) as store: store.append('df', df) result = store.select('df') expected = df assert_frame_equal(result, expected, by_blocks=True) result = store.select('df', columns=df.columns) expected = df assert_frame_equal(result, expected, by_blocks=True) result = store.select('df', columns=['A']) expected = df.loc[:, ['A']] assert_frame_equal(result, expected) # dups across dtypes df = concat([DataFrame(np.random.randn(10, 4), columns=['A', 'A', 'B', 'B']), DataFrame(np.random.randint(0, 10, size=20) .reshape(10, 2), columns=['A', 'C'])], axis=1) df.index = date_range('20130101 9:30', periods=10, freq='T') with ensure_clean_store(self.path) as store: store.append('df', df) result = store.select('df') expected = df assert_frame_equal(result, expected, by_blocks=True) result = store.select('df', columns=df.columns) expected = df assert_frame_equal(result, expected, by_blocks=True) expected = df.loc[:, ['A']] result = store.select('df', columns=['A']) assert_frame_equal(result, expected, by_blocks=True) expected = df.loc[:, ['B', 'A']] result = store.select('df', columns=['B', 'A']) assert_frame_equal(result, expected, by_blocks=True) # duplicates on both index and columns with ensure_clean_store(self.path) as store: store.append('df', df) store.append('df', df) expected = df.loc[:, ['B', 'A']] expected = concat([expected, expected]) result = store.select('df', columns=['B', 'A']) assert_frame_equal(result, expected, by_blocks=True) def test_wide_table_dups(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel() store.put('panel', wp, format='table') store.put('panel', wp, format='table', append=True) recons = store['panel'] assert_panel_equal(recons, wp) def test_long(self): def _check(left, right): assert_panel_equal(left.to_panel(), right.to_panel()) with catch_warnings(record=True): wp = tm.makePanel() self._check_roundtrip(wp.to_frame(), _check) def test_overwrite_node(self): with ensure_clean_store(self.path) as store: store['a'] = tm.makeTimeDataFrame() ts = tm.makeTimeSeries() store['a'] = ts tm.assert_series_equal(store['a'], ts) def test_sparse_with_compression(self): # GH 2931 # make sparse dataframe arr = np.random.binomial(n=1, p=.01, size=(1000, 10)) df = DataFrame(arr).to_sparse(fill_value=0) # case 1: store uncompressed self._check_double_roundtrip(df, tm.assert_frame_equal, compression=False, check_frame_type=True) # case 2: store compressed (works) self._check_double_roundtrip(df, tm.assert_frame_equal, compression='zlib', check_frame_type=True) # set one series to be completely sparse df[0] = np.zeros(1000) # case 3: store df with completely sparse series uncompressed self._check_double_roundtrip(df, tm.assert_frame_equal, compression=False, check_frame_type=True) # case 4: try storing df with completely sparse series compressed # (fails) self._check_double_roundtrip(df, tm.assert_frame_equal, compression='zlib', check_frame_type=True) def test_select(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel() # put/select ok _maybe_remove(store, 'wp') store.put('wp', wp, format='table') store.select('wp') # non-table ok (where = None) _maybe_remove(store, 'wp') store.put('wp2', wp) store.select('wp2') # selection on the non-indexable with a large number of columns wp = Panel(np.random.randn(100, 100, 100), items=['Item%03d' % i for i in range(100)], major_axis=date_range('1/1/2000', periods=100), minor_axis=['E%03d' % i for i in range(100)]) _maybe_remove(store, 'wp') store.append('wp', wp) items = ['Item%03d' % i for i in range(80)] result = store.select('wp', 'items=items') expected = wp.reindex(items=items) assert_panel_equal(expected, result) # selectin non-table with a where # pytest.raises(ValueError, store.select, # 'wp2', ('column', ['A', 'D'])) # select with columns= df = tm.makeTimeDataFrame() _maybe_remove(store, 'df') store.append('df', df) result = store.select('df', columns=['A', 'B']) expected = df.reindex(columns=['A', 'B']) tm.assert_frame_equal(expected, result) # equivalentsly result = store.select('df', [("columns=['A', 'B']")]) expected = df.reindex(columns=['A', 'B']) tm.assert_frame_equal(expected, result) # with a data column _maybe_remove(store, 'df') store.append('df', df, data_columns=['A']) result = store.select('df', ['A > 0'], columns=['A', 'B']) expected = df[df.A > 0].reindex(columns=['A', 'B']) tm.assert_frame_equal(expected, result) # all a data columns _maybe_remove(store, 'df') store.append('df', df, data_columns=True) result = store.select('df', ['A > 0'], columns=['A', 'B']) expected = df[df.A > 0].reindex(columns=['A', 'B']) tm.assert_frame_equal(expected, result) # with a data column, but different columns _maybe_remove(store, 'df') store.append('df', df, data_columns=['A']) result = store.select('df', ['A > 0'], columns=['C', 'D']) expected = df[df.A > 0].reindex(columns=['C', 'D']) tm.assert_frame_equal(expected, result) def test_select_dtypes(self): with ensure_clean_store(self.path) as store: # with a Timestamp data column (GH #2637) df = DataFrame(dict( ts=bdate_range('2012-01-01', periods=300), A=np.random.randn(300))) _maybe_remove(store, 'df') store.append('df', df, data_columns=['ts', 'A']) result = store.select('df', "ts>=Timestamp('2012-02-01')") expected = df[df.ts >= Timestamp('2012-02-01')] tm.assert_frame_equal(expected, result) # bool columns (GH #2849) df = DataFrame(np.random.randn(5, 2), columns=['A', 'B']) df['object'] = 'foo' df.loc[4:5, 'object'] = 'bar' df['boolv'] = df['A'] > 0 _maybe_remove(store, 'df') store.append('df', df, data_columns=True) expected = (df[df.boolv == True] # noqa .reindex(columns=['A', 'boolv'])) for v in [True, 'true', 1]: result = store.select('df', 'boolv == %s' % str(v), columns=['A', 'boolv']) tm.assert_frame_equal(expected, result) expected = (df[df.boolv == False] # noqa .reindex(columns=['A', 'boolv'])) for v in [False, 'false', 0]: result = store.select( 'df', 'boolv == %s' % str(v), columns=['A', 'boolv']) tm.assert_frame_equal(expected, result) # integer index df = DataFrame(dict(A=np.random.rand(20), B=np.random.rand(20))) _maybe_remove(store, 'df_int') store.append('df_int', df) result = store.select( 'df_int', "index<10 and columns=['A']") expected = df.reindex(index=list(df.index)[0:10], columns=['A']) tm.assert_frame_equal(expected, result) # float index df = DataFrame(dict(A=np.random.rand( 20), B=np.random.rand(20), index=np.arange(20, dtype='f8'))) _maybe_remove(store, 'df_float') store.append('df_float', df) result = store.select( 'df_float', "index<10.0 and columns=['A']") expected = df.reindex(index=list(df.index)[0:10], columns=['A']) tm.assert_frame_equal(expected, result) with ensure_clean_store(self.path) as store: # floats w/o NaN df = DataFrame( dict(cols=range(11), values=range(11)), dtype='float64') df['cols'] = (df['cols'] + 10).apply(str) store.append('df1', df, data_columns=True) result = store.select( 'df1', where='values>2.0') expected = df[df['values'] > 2.0] tm.assert_frame_equal(expected, result) # floats with NaN df.iloc[0] = np.nan expected = df[df['values'] > 2.0] store.append('df2', df, data_columns=True, index=False) result = store.select( 'df2', where='values>2.0') tm.assert_frame_equal(expected, result) # https://github.com/PyTables/PyTables/issues/282 # bug in selection when 0th row has a np.nan and an index # store.append('df3',df,data_columns=True) # result = store.select( # 'df3', where='values>2.0') # tm.assert_frame_equal(expected, result) # not in first position float with NaN ok too df = DataFrame( dict(cols=range(11), values=range(11)), dtype='float64') df['cols'] = (df['cols'] + 10).apply(str) df.iloc[1] = np.nan expected = df[df['values'] > 2.0] store.append('df4', df, data_columns=True) result = store.select( 'df4', where='values>2.0') tm.assert_frame_equal(expected, result) # test selection with comparison against numpy scalar # GH 11283 with ensure_clean_store(self.path) as store: df = tm.makeDataFrame() expected = df[df['A'] > 0] store.append('df', df, data_columns=True) np_zero = np.float64(0) # noqa result = store.select('df', where=["A>np_zero"]) tm.assert_frame_equal(expected, result) def test_select_with_many_inputs(self): with ensure_clean_store(self.path) as store: df = DataFrame(dict(ts=bdate_range('2012-01-01', periods=300), A=np.random.randn(300), B=range(300), users=['a'] * 50 + ['b'] * 50 + ['c'] * 100 + ['a%03d' % i for i in range(100)])) _maybe_remove(store, 'df') store.append('df', df, data_columns=['ts', 'A', 'B', 'users']) # regular select result = store.select('df', "ts>=Timestamp('2012-02-01')") expected = df[df.ts >= Timestamp('2012-02-01')] tm.assert_frame_equal(expected, result) # small selector result = store.select( 'df', "ts>=Timestamp('2012-02-01') & users=['a','b','c']") expected = df[(df.ts >= Timestamp('2012-02-01')) & df.users.isin(['a', 'b', 'c'])] tm.assert_frame_equal(expected, result) # big selector along the columns selector = ['a', 'b', 'c'] + ['a%03d' % i for i in range(60)] result = store.select( 'df', "ts>=Timestamp('2012-02-01') and users=selector") expected = df[(df.ts >= Timestamp('2012-02-01')) & df.users.isin(selector)] tm.assert_frame_equal(expected, result) selector = range(100, 200) result = store.select('df', 'B=selector') expected = df[df.B.isin(selector)] tm.assert_frame_equal(expected, result) assert len(result) == 100 # big selector along the index selector = Index(df.ts[0:100].values) result = store.select('df', 'ts=selector') expected = df[df.ts.isin(selector.values)] tm.assert_frame_equal(expected, result) assert len(result) == 100 def test_select_iterator(self): # single table with ensure_clean_store(self.path) as store: df = tm.makeTimeDataFrame(500) _maybe_remove(store, 'df') store.append('df', df) expected = store.select('df') results = [s for s in store.select('df', iterator=True)] result = concat(results) tm.assert_frame_equal(expected, result) results = [s for s in store.select('df', chunksize=100)] assert len(results) == 5 result = concat(results) tm.assert_frame_equal(expected, result) results = [s for s in store.select('df', chunksize=150)] result = concat(results) tm.assert_frame_equal(result, expected) with ensure_clean_path(self.path) as path: df = tm.makeTimeDataFrame(500) df.to_hdf(path, 'df_non_table') pytest.raises(TypeError, read_hdf, path, 'df_non_table', chunksize=100) pytest.raises(TypeError, read_hdf, path, 'df_non_table', iterator=True) with ensure_clean_path(self.path) as path: df = tm.makeTimeDataFrame(500) df.to_hdf(path, 'df', format='table') results = [s for s in read_hdf(path, 'df', chunksize=100)] result = concat(results) assert len(results) == 5 tm.assert_frame_equal(result, df) tm.assert_frame_equal(result, read_hdf(path, 'df')) # multiple with ensure_clean_store(self.path) as store: df1 = tm.makeTimeDataFrame(500) store.append('df1', df1, data_columns=True) df2 = tm.makeTimeDataFrame(500).rename( columns=lambda x: "%s_2" % x) df2['foo'] = 'bar' store.append('df2', df2) df = concat([df1, df2], axis=1) # full selection expected = store.select_as_multiple( ['df1', 'df2'], selector='df1') results = [s for s in store.select_as_multiple( ['df1', 'df2'], selector='df1', chunksize=150)] result = concat(results) tm.assert_frame_equal(expected, result) def test_select_iterator_complete_8014(self): # GH 8014 # using iterator and where clause chunksize = 1e4 # no iterator with ensure_clean_store(self.path) as store: expected = tm.makeTimeDataFrame(100064, 'S') _maybe_remove(store, 'df') store.append('df', expected) beg_dt = expected.index[0] end_dt = expected.index[-1] # select w/o iteration and no where clause works result = store.select('df') tm.assert_frame_equal(expected, result) # select w/o iterator and where clause, single term, begin # of range, works where = "index >= '%s'" % beg_dt result = store.select('df', where=where) tm.assert_frame_equal(expected, result) # select w/o iterator and where clause, single term, end # of range, works where = "index <= '%s'" % end_dt result = store.select('df', where=where) tm.assert_frame_equal(expected, result) # select w/o iterator and where clause, inclusive range, # works where = "index >= '%s' & index <= '%s'" % (beg_dt, end_dt) result = store.select('df', where=where) tm.assert_frame_equal(expected, result) # with iterator, full range with ensure_clean_store(self.path) as store: expected = tm.makeTimeDataFrame(100064, 'S') _maybe_remove(store, 'df') store.append('df', expected) beg_dt = expected.index[0] end_dt = expected.index[-1] # select w/iterator and no where clause works results = [s for s in store.select('df', chunksize=chunksize)] result = concat(results) tm.assert_frame_equal(expected, result) # select w/iterator and where clause, single term, begin of range where = "index >= '%s'" % beg_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) tm.assert_frame_equal(expected, result) # select w/iterator and where clause, single term, end of range where = "index <= '%s'" % end_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) tm.assert_frame_equal(expected, result) # select w/iterator and where clause, inclusive range where = "index >= '%s' & index <= '%s'" % (beg_dt, end_dt) results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) tm.assert_frame_equal(expected, result) def test_select_iterator_non_complete_8014(self): # GH 8014 # using iterator and where clause chunksize = 1e4 # with iterator, non complete range with ensure_clean_store(self.path) as store: expected = tm.makeTimeDataFrame(100064, 'S') _maybe_remove(store, 'df') store.append('df', expected) beg_dt = expected.index[1] end_dt = expected.index[-2] # select w/iterator and where clause, single term, begin of range where = "index >= '%s'" % beg_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) rexpected = expected[expected.index >= beg_dt] tm.assert_frame_equal(rexpected, result) # select w/iterator and where clause, single term, end of range where = "index <= '%s'" % end_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) rexpected = expected[expected.index <= end_dt] tm.assert_frame_equal(rexpected, result) # select w/iterator and where clause, inclusive range where = "index >= '%s' & index <= '%s'" % (beg_dt, end_dt) results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) rexpected = expected[(expected.index >= beg_dt) & (expected.index <= end_dt)] tm.assert_frame_equal(rexpected, result) # with iterator, empty where with ensure_clean_store(self.path) as store: expected = tm.makeTimeDataFrame(100064, 'S') _maybe_remove(store, 'df') store.append('df', expected) end_dt = expected.index[-1] # select w/iterator and where clause, single term, begin of range where = "index > '%s'" % end_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] assert 0 == len(results) def test_select_iterator_many_empty_frames(self): # GH 8014 # using iterator and where clause can return many empty # frames. chunksize = int(1e4) # with iterator, range limited to the first chunk with ensure_clean_store(self.path) as store: expected = tm.makeTimeDataFrame(100000, 'S') _maybe_remove(store, 'df') store.append('df', expected) beg_dt = expected.index[0] end_dt = expected.index[chunksize - 1] # select w/iterator and where clause, single term, begin of range where = "index >= '%s'" % beg_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] result = concat(results) rexpected = expected[expected.index >= beg_dt] tm.assert_frame_equal(rexpected, result) # select w/iterator and where clause, single term, end of range where = "index <= '%s'" % end_dt results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] assert len(results) == 1 result = concat(results) rexpected = expected[expected.index <= end_dt] tm.assert_frame_equal(rexpected, result) # select w/iterator and where clause, inclusive range where = "index >= '%s' & index <= '%s'" % (beg_dt, end_dt) results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] # should be 1, is 10 assert len(results) == 1 result = concat(results) rexpected = expected[(expected.index >= beg_dt) & (expected.index <= end_dt)] tm.assert_frame_equal(rexpected, result) # select w/iterator and where clause which selects # *nothing*. # # To be consistent with Python idiom I suggest this should # return [] e.g. `for e in []: print True` never prints # True. where = "index <= '%s' & index >= '%s'" % (beg_dt, end_dt) results = [s for s in store.select( 'df', where=where, chunksize=chunksize)] # should be [] assert len(results) == 0 def test_retain_index_attributes(self): # GH 3499, losing frequency info on index recreation df = DataFrame(dict( A=Series(lrange(3), index=date_range('2000-1-1', periods=3, freq='H')))) with ensure_clean_store(self.path) as store: _maybe_remove(store, 'data') store.put('data', df, format='table') result = store.get('data') tm.assert_frame_equal(df, result) for attr in ['freq', 'tz', 'name']: for idx in ['index', 'columns']: assert (getattr(getattr(df, idx), attr, None) == getattr(getattr(result, idx), attr, None)) # try to append a table with a different frequency with catch_warnings(record=True): df2 = DataFrame(dict( A=Series(lrange(3), index=date_range('2002-1-1', periods=3, freq='D')))) store.append('data', df2) assert store.get_storer('data').info['index']['freq'] is None # this is ok _maybe_remove(store, 'df2') df2 = DataFrame(dict( A=Series(lrange(3), index=[Timestamp('20010101'), Timestamp('20010102'), Timestamp('20020101')]))) store.append('df2', df2) df3 = DataFrame(dict( A=Series(lrange(3), index=date_range('2002-1-1', periods=3, freq='D')))) store.append('df2', df3) def test_retain_index_attributes2(self): with ensure_clean_path(self.path) as path: with catch_warnings(record=True): df = DataFrame(dict( A=Series(lrange(3), index=date_range('2000-1-1', periods=3, freq='H')))) df.to_hdf(path, 'data', mode='w', append=True) df2 = DataFrame(dict( A=Series(lrange(3), index=date_range('2002-1-1', periods=3, freq='D')))) df2.to_hdf(path, 'data', append=True) idx = date_range('2000-1-1', periods=3, freq='H') idx.name = 'foo' df = DataFrame(dict(A=Series(lrange(3), index=idx))) df.to_hdf(path, 'data', mode='w', append=True) assert read_hdf(path, 'data').index.name == 'foo' with catch_warnings(record=True): idx2 = date_range('2001-1-1', periods=3, freq='H') idx2.name = 'bar' df2 = DataFrame(dict(A=Series(lrange(3), index=idx2))) df2.to_hdf(path, 'data', append=True) assert read_hdf(path, 'data').index.name is None def test_panel_select(self): with ensure_clean_store(self.path) as store: with catch_warnings(record=True): wp = tm.makePanel() store.put('wp', wp, format='table') date = wp.major_axis[len(wp.major_axis) // 2] crit1 = ('major_axis>=date') crit2 = ("minor_axis=['A', 'D']") result = store.select('wp', [crit1, crit2]) expected = wp.truncate(before=date).reindex(minor=['A', 'D']) assert_panel_equal(result, expected) result = store.select( 'wp', ['major_axis>="20000124"', ("minor_axis=['A', 'B']")]) expected = wp.truncate( before='20000124').reindex(minor=['A', 'B']) assert_panel_equal(result, expected) def test_frame_select(self): df = tm.makeTimeDataFrame() with ensure_clean_store(self.path) as store: store.put('frame', df, format='table') date = df.index[len(df) // 2] crit1 = Term('index>=date') assert crit1.env.scope['date'] == date crit2 = ("columns=['A', 'D']") crit3 = ('columns=A') result = store.select('frame', [crit1, crit2]) expected = df.loc[date:, ['A', 'D']] tm.assert_frame_equal(result, expected) result = store.select('frame', [crit3]) expected = df.loc[:, ['A']] tm.assert_frame_equal(result, expected) # invalid terms df = tm.makeTimeDataFrame() store.append('df_time', df) pytest.raises( ValueError, store.select, 'df_time', "index>0") # can't select if not written as table # store['frame'] = df # pytest.raises(ValueError, store.select, # 'frame', [crit1, crit2]) def test_frame_select_complex(self): # select via complex criteria df = tm.makeTimeDataFrame() df['string'] = 'foo' df.loc[df.index[0:4], 'string'] = 'bar' with ensure_clean_store(self.path) as store: store.put('df', df, format='table', data_columns=['string']) # empty result = store.select('df', 'index>df.index[3] & string="bar"') expected = df.loc[(df.index > df.index[3]) & (df.string == 'bar')] tm.assert_frame_equal(result, expected) result = store.select('df', 'index>df.index[3] & string="foo"') expected = df.loc[(df.index > df.index[3]) & (df.string == 'foo')] tm.assert_frame_equal(result, expected) # or result = store.select('df', 'index>df.index[3] | string="bar"') expected = df.loc[(df.index > df.index[3]) | (df.string == 'bar')] tm.assert_frame_equal(result, expected) result = store.select('df', '(index>df.index[3] & ' 'index<=df.index[6]) | string="bar"') expected = df.loc[((df.index > df.index[3]) & ( df.index <= df.index[6])) | (df.string == 'bar')] tm.assert_frame_equal(result, expected) # invert result = store.select('df', 'string!="bar"') expected = df.loc[df.string != 'bar'] tm.assert_frame_equal(result, expected) # invert not implemented in numexpr :( pytest.raises(NotImplementedError, store.select, 'df', '~(string="bar")') # invert ok for filters result = store.select('df', "~(columns=['A','B'])") expected = df.loc[:, df.columns.difference(['A', 'B'])] tm.assert_frame_equal(result, expected) # in result = store.select( 'df', "index>df.index[3] & columns in ['A','B']") expected = df.loc[df.index > df.index[3]].reindex(columns=[ 'A', 'B']) tm.assert_frame_equal(result, expected) def test_frame_select_complex2(self): with ensure_clean_path(['parms.hdf', 'hist.hdf']) as paths: pp, hh = paths # use non-trivial selection criteria parms = DataFrame({'A': [1, 1, 2, 2, 3]}) parms.to_hdf(pp, 'df', mode='w', format='table', data_columns=['A']) selection = read_hdf(pp, 'df', where='A=[2,3]') hist = DataFrame(np.random.randn(25, 1), columns=['data'], index=MultiIndex.from_tuples( [(i, j) for i in range(5) for j in range(5)], names=['l1', 'l2'])) hist.to_hdf(hh, 'df', mode='w', format='table') expected = read_hdf(hh, 'df', where='l1=[2, 3, 4]') # sccope with list like l = selection.index.tolist() # noqa store = HDFStore(hh) result = store.select('df', where='l1=l') assert_frame_equal(result, expected) store.close() result = read_hdf(hh, 'df', where='l1=l') assert_frame_equal(result, expected) # index index = selection.index # noqa result = read_hdf(hh, 'df', where='l1=index') assert_frame_equal(result, expected) result = read_hdf(hh, 'df', where='l1=selection.index') assert_frame_equal(result, expected) result = read_hdf(hh, 'df', where='l1=selection.index.tolist()') assert_frame_equal(result, expected) result = read_hdf(hh, 'df', where='l1=list(selection.index)') assert_frame_equal(result, expected) # sccope with index store = HDFStore(hh) result = store.select('df', where='l1=index') assert_frame_equal(result, expected) result = store.select('df', where='l1=selection.index') assert_frame_equal(result, expected) result = store.select('df', where='l1=selection.index.tolist()') assert_frame_equal(result, expected) result = store.select('df', where='l1=list(selection.index)') assert_frame_equal(result, expected) store.close() def test_invalid_filtering(self): # can't use more than one filter (atm) df = tm.makeTimeDataFrame() with ensure_clean_store(self.path) as store: store.put('df', df, format='table') # not implemented pytest.raises(NotImplementedError, store.select, 'df', "columns=['A'] | columns=['B']") # in theory we could deal with this pytest.raises(NotImplementedError, store.select, 'df', "columns=['A','B'] & columns=['C']") def test_string_select(self): # GH 2973 with ensure_clean_store(self.path) as store: df = tm.makeTimeDataFrame() # test string ==/!= df['x'] = 'none' df.loc[2:7, 'x'] = '' store.append('df', df, data_columns=['x']) result = store.select('df', 'x=none') expected = df[df.x == 'none'] assert_frame_equal(result, expected) try: result = store.select('df', 'x!=none') expected = df[df.x != 'none'] assert_frame_equal(result, expected) except Exception as detail: pprint_thing("[{0}]".format(detail)) pprint_thing(store) pprint_thing(expected) df2 = df.copy() df2.loc[df2.x == '', 'x'] = np.nan store.append('df2', df2, data_columns=['x']) result = store.select('df2', 'x!=none') expected = df2[isna(df2.x)] assert_frame_equal(result, expected) # int ==/!= df['int'] = 1 df.loc[2:7, 'int'] = 2 store.append('df3', df, data_columns=['int']) result = store.select('df3', 'int=2') expected = df[df.int == 2] assert_frame_equal(result, expected) result = store.select('df3', 'int!=2') expected = df[df.int != 2] assert_frame_equal(result, expected) def test_read_column(self): df = tm.makeTimeDataFrame() with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df') # GH 17912 # HDFStore.select_column should raise a KeyError # exception if the key is not a valid store with pytest.raises(KeyError, message='No object named index in the file'): store.select_column('df', 'index') store.append('df', df) # error pytest.raises(KeyError, store.select_column, 'df', 'foo') def f(): store.select_column('df', 'index', where=['index>5']) pytest.raises(Exception, f) # valid result = store.select_column('df', 'index') tm.assert_almost_equal(result.values, Series(df.index).values) assert isinstance(result, Series) # not a data indexable column pytest.raises( ValueError, store.select_column, 'df', 'values_block_0') # a data column df2 = df.copy() df2['string'] = 'foo' store.append('df2', df2, data_columns=['string']) result = store.select_column('df2', 'string') tm.assert_almost_equal(result.values, df2['string'].values) # a data column with NaNs, result excludes the NaNs df3 = df.copy() df3['string'] = 'foo' df3.loc[4:6, 'string'] = np.nan store.append('df3', df3, data_columns=['string']) result = store.select_column('df3', 'string') tm.assert_almost_equal(result.values, df3['string'].values) # start/stop result = store.select_column('df3', 'string', start=2) tm.assert_almost_equal(result.values, df3['string'].values[2:]) result = store.select_column('df3', 'string', start=-2) tm.assert_almost_equal(result.values, df3['string'].values[-2:]) result = store.select_column('df3', 'string', stop=2) tm.assert_almost_equal(result.values, df3['string'].values[:2]) result = store.select_column('df3', 'string', stop=-2) tm.assert_almost_equal(result.values, df3['string'].values[:-2]) result = store.select_column('df3', 'string', start=2, stop=-2) tm.assert_almost_equal(result.values, df3['string'].values[2:-2]) result = store.select_column('df3', 'string', start=-2, stop=2) tm.assert_almost_equal(result.values, df3['string'].values[-2:2]) # GH 10392 - make sure column name is preserved df4 = DataFrame({'A': np.random.randn(10), 'B': 'foo'}) store.append('df4', df4, data_columns=True) expected = df4['B'] result = store.select_column('df4', 'B') tm.assert_series_equal(result, expected) def test_coordinates(self): df = tm.makeTimeDataFrame() with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df') store.append('df', df) # all c = store.select_as_coordinates('df') assert((c.values == np.arange(len(df.index))).all()) # get coordinates back & test vs frame _maybe_remove(store, 'df') df = DataFrame(dict(A=lrange(5), B=lrange(5))) store.append('df', df) c = store.select_as_coordinates('df', ['index<3']) assert((c.values == np.arange(3)).all()) result = store.select('df', where=c) expected = df.loc[0:2, :] tm.assert_frame_equal(result, expected) c = store.select_as_coordinates('df', ['index>=3', 'index<=4']) assert((c.values == np.arange(2) + 3).all()) result = store.select('df', where=c) expected = df.loc[3:4, :] tm.assert_frame_equal(result, expected) assert isinstance(c, Index) # multiple tables _maybe_remove(store, 'df1') _maybe_remove(store, 'df2') df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns=lambda x: "%s_2" % x) store.append('df1', df1, data_columns=['A', 'B']) store.append('df2', df2) c = store.select_as_coordinates('df1', ['A>0', 'B>0']) df1_result = store.select('df1', c) df2_result = store.select('df2', c) result = concat([df1_result, df2_result], axis=1) expected = concat([df1, df2], axis=1) expected = expected[(expected.A > 0) & (expected.B > 0)] tm.assert_frame_equal(result, expected) # pass array/mask as the coordinates with ensure_clean_store(self.path) as store: df = DataFrame(np.random.randn(1000, 2), index=date_range('20000101', periods=1000)) store.append('df', df) c = store.select_column('df', 'index') where = c[DatetimeIndex(c).month == 5].index expected = df.iloc[where] # locations result = store.select('df', where=where) tm.assert_frame_equal(result, expected) # boolean result = store.select('df', where=where) tm.assert_frame_equal(result, expected) # invalid pytest.raises(ValueError, store.select, 'df', where=np.arange(len(df), dtype='float64')) pytest.raises(ValueError, store.select, 'df', where=np.arange(len(df) + 1)) pytest.raises(ValueError, store.select, 'df', where=np.arange(len(df)), start=5) pytest.raises(ValueError, store.select, 'df', where=np.arange(len(df)), start=5, stop=10) # selection with filter selection = date_range('20000101', periods=500) result = store.select('df', where='index in selection') expected = df[df.index.isin(selection)] tm.assert_frame_equal(result, expected) # list df = DataFrame(np.random.randn(10, 2)) store.append('df2', df) result = store.select('df2', where=[0, 3, 5]) expected = df.iloc[[0, 3, 5]] tm.assert_frame_equal(result, expected) # boolean where = [True] * 10 where[-2] = False result = store.select('df2', where=where) expected = df.loc[where] tm.assert_frame_equal(result, expected) # start/stop result = store.select('df2', start=5, stop=10) expected = df[5:10] tm.assert_frame_equal(result, expected) def test_append_to_multiple(self): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns=lambda x: "%s_2" % x) df2['foo'] = 'bar' df = concat([df1, df2], axis=1) with ensure_clean_store(self.path) as store: # exceptions pytest.raises(ValueError, store.append_to_multiple, {'df1': ['A', 'B'], 'df2': None}, df, selector='df3') pytest.raises(ValueError, store.append_to_multiple, {'df1': None, 'df2': None}, df, selector='df3') pytest.raises( ValueError, store.append_to_multiple, 'df1', df, 'df1') # regular operation store.append_to_multiple( {'df1': ['A', 'B'], 'df2': None}, df, selector='df1') result = store.select_as_multiple( ['df1', 'df2'], where=['A>0', 'B>0'], selector='df1') expected = df[(df.A > 0) & (df.B > 0)] tm.assert_frame_equal(result, expected) def test_append_to_multiple_dropna(self): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns=lambda x: "%s_2" % x) df1.iloc[1, df1.columns.get_indexer(['A', 'B'])] = np.nan df = concat([df1, df2], axis=1) with ensure_clean_store(self.path) as store: # dropna=True should guarantee rows are synchronized store.append_to_multiple( {'df1': ['A', 'B'], 'df2': None}, df, selector='df1', dropna=True) result = store.select_as_multiple(['df1', 'df2']) expected = df.dropna() tm.assert_frame_equal(result, expected) tm.assert_index_equal(store.select('df1').index, store.select('df2').index) @pytest.mark.xfail(run=False, reason="append_to_multiple_dropna_false " "is not raising as failed") def test_append_to_multiple_dropna_false(self): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns=lambda x: "%s_2" % x) df1.iloc[1, df1.columns.get_indexer(['A', 'B'])] = np.nan df = concat([df1, df2], axis=1) with ensure_clean_store(self.path) as store: # dropna=False shouldn't synchronize row indexes store.append_to_multiple( {'df1a': ['A', 'B'], 'df2a': None}, df, selector='df1a', dropna=False) with pytest.raises(ValueError): store.select_as_multiple(['df1a', 'df2a']) assert not store.select('df1a').index.equals( store.select('df2a').index) def test_select_as_multiple(self): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame().rename(columns=lambda x: "%s_2" % x) df2['foo'] = 'bar' with ensure_clean_store(self.path) as store: # no tables stored pytest.raises(Exception, store.select_as_multiple, None, where=['A>0', 'B>0'], selector='df1') store.append('df1', df1, data_columns=['A', 'B']) store.append('df2', df2) # exceptions pytest.raises(Exception, store.select_as_multiple, None, where=['A>0', 'B>0'], selector='df1') pytest.raises(Exception, store.select_as_multiple, [None], where=['A>0', 'B>0'], selector='df1') pytest.raises(KeyError, store.select_as_multiple, ['df1', 'df3'], where=['A>0', 'B>0'], selector='df1') pytest.raises(KeyError, store.select_as_multiple, ['df3'], where=['A>0', 'B>0'], selector='df1') pytest.raises(KeyError, store.select_as_multiple, ['df1', 'df2'], where=['A>0', 'B>0'], selector='df4') # default select result = store.select('df1', ['A>0', 'B>0']) expected = store.select_as_multiple( ['df1'], where=['A>0', 'B>0'], selector='df1') tm.assert_frame_equal(result, expected) expected = store.select_as_multiple( 'df1', where=['A>0', 'B>0'], selector='df1') tm.assert_frame_equal(result, expected) # multiple result = store.select_as_multiple( ['df1', 'df2'], where=['A>0', 'B>0'], selector='df1') expected = concat([df1, df2], axis=1) expected = expected[(expected.A > 0) & (expected.B > 0)] tm.assert_frame_equal(result, expected) # multiple (diff selector) result = store.select_as_multiple( ['df1', 'df2'], where='index>df2.index[4]', selector='df2') expected = concat([df1, df2], axis=1) expected = expected[5:] tm.assert_frame_equal(result, expected) # test excpection for diff rows store.append('df3', tm.makeTimeDataFrame(nper=50)) pytest.raises(ValueError, store.select_as_multiple, ['df1', 'df3'], where=['A>0', 'B>0'], selector='df1') @pytest.mark.skipif( LooseVersion(tables.__version__) < LooseVersion('3.1.0'), reason=("tables version does not support fix for nan selection " "bug: GH 4858")) def test_nan_selection_bug_4858(self): with ensure_clean_store(self.path) as store: df = DataFrame(dict(cols=range(6), values=range(6)), dtype='float64') df['cols'] = (df['cols'] + 10).apply(str) df.iloc[0] = np.nan expected = DataFrame(dict(cols=['13.0', '14.0', '15.0'], values=[ 3., 4., 5.]), index=[3, 4, 5]) # write w/o the index on that particular column store.append('df', df, data_columns=True, index=['cols']) result = store.select('df', where='values>2.0') assert_frame_equal(result, expected) def test_start_stop_table(self): with ensure_clean_store(self.path) as store: # table df = DataFrame(dict(A=np.random.rand(20), B=np.random.rand(20))) store.append('df', df) result = store.select( 'df', "columns=['A']", start=0, stop=5) expected = df.loc[0:4, ['A']] tm.assert_frame_equal(result, expected) # out of range result = store.select( 'df', "columns=['A']", start=30, stop=40) assert len(result) == 0 expected = df.loc[30:40, ['A']] tm.assert_frame_equal(result, expected) def test_start_stop_multiple(self): # GH 16209 with ensure_clean_store(self.path) as store: df = DataFrame({"foo": [1, 2], "bar": [1, 2]}) store.append_to_multiple({'selector': ['foo'], 'data': None}, df, selector='selector') result = store.select_as_multiple(['selector', 'data'], selector='selector', start=0, stop=1) expected = df.loc[[0], ['foo', 'bar']] tm.assert_frame_equal(result, expected) def test_start_stop_fixed(self): with ensure_clean_store(self.path) as store: # fixed, GH 8287 df = DataFrame(dict(A=np.random.rand(20), B=np.random.rand(20)), index=pd.date_range('20130101', periods=20)) store.put('df', df) result = store.select( 'df', start=0, stop=5) expected = df.iloc[0:5, :] tm.assert_frame_equal(result, expected) result = store.select( 'df', start=5, stop=10) expected = df.iloc[5:10, :] tm.assert_frame_equal(result, expected) # out of range result = store.select( 'df', start=30, stop=40) expected = df.iloc[30:40, :] tm.assert_frame_equal(result, expected) # series s = df.A store.put('s', s) result = store.select('s', start=0, stop=5) expected = s.iloc[0:5] tm.assert_series_equal(result, expected) result = store.select('s', start=5, stop=10) expected = s.iloc[5:10] tm.assert_series_equal(result, expected) # sparse; not implemented df = tm.makeDataFrame() df.iloc[3:5, 1:3] = np.nan df.iloc[8:10, -2] = np.nan dfs = df.to_sparse() store.put('dfs', dfs) with pytest.raises(NotImplementedError): store.select('dfs', start=0, stop=5) def test_select_filter_corner(self): df = DataFrame(np.random.randn(50, 100)) df.index = ['%.3d' % c for c in df.index] df.columns = ['%.3d' % c for c in df.columns] with ensure_clean_store(self.path) as store: store.put('frame', df, format='table') crit = 'columns=df.columns[:75]' result = store.select('frame', [crit]) tm.assert_frame_equal(result, df.loc[:, df.columns[:75]]) crit = 'columns=df.columns[:75:2]' result = store.select('frame', [crit]) tm.assert_frame_equal(result, df.loc[:, df.columns[:75:2]]) def test_path_pathlib(self): df = tm.makeDataFrame() result = tm.round_trip_pathlib( lambda p: df.to_hdf(p, 'df'), lambda p: pd.read_hdf(p, 'df')) tm.assert_frame_equal(df, result) @pytest.mark.parametrize('start, stop', [(0, 2), (1, 2), (None, None)]) def test_contiguous_mixed_data_table(self, start, stop): # GH 17021 # ValueError when reading a contiguous mixed-data table ft. VLArray df = DataFrame({'a': Series([20111010, 20111011, 20111012]), 'b': Series(['ab', 'cd', 'ab'])}) with ensure_clean_store(self.path) as store: store.append('test_dataset', df) result = store.select('test_dataset', start=start, stop=stop) assert_frame_equal(df[start:stop], result) def test_path_pathlib_hdfstore(self): df = tm.makeDataFrame() def writer(path): with pd.HDFStore(path) as store: df.to_hdf(store, 'df') def reader(path): with pd.HDFStore(path) as store: return pd.read_hdf(store, 'df') result = tm.round_trip_pathlib(writer, reader) tm.assert_frame_equal(df, result) def test_pickle_path_localpath(self): df = tm.makeDataFrame() result = tm.round_trip_pathlib( lambda p: df.to_hdf(p, 'df'), lambda p: pd.read_hdf(p, 'df')) tm.assert_frame_equal(df, result) def test_path_localpath_hdfstore(self): df = tm.makeDataFrame() def writer(path): with pd.HDFStore(path) as store: df.to_hdf(store, 'df') def reader(path): with pd.HDFStore(path) as store: return pd.read_hdf(store, 'df') result = tm.round_trip_localpath(writer, reader) tm.assert_frame_equal(df, result) def _check_roundtrip(self, obj, comparator, compression=False, **kwargs): options = {} if compression: options['complib'] = _default_compressor with ensure_clean_store(self.path, 'w', **options) as store: store['obj'] = obj retrieved = store['obj'] comparator(retrieved, obj, **kwargs) def _check_double_roundtrip(self, obj, comparator, compression=False, **kwargs): options = {} if compression: options['complib'] = compression or _default_compressor with ensure_clean_store(self.path, 'w', **options) as store: store['obj'] = obj retrieved = store['obj'] comparator(retrieved, obj, **kwargs) store['obj'] = retrieved again = store['obj'] comparator(again, obj, **kwargs) def _check_roundtrip_table(self, obj, comparator, compression=False): options = {} if compression: options['complib'] = _default_compressor with ensure_clean_store(self.path, 'w', **options) as store: store.put('obj', obj, format='table') retrieved = store['obj'] comparator(retrieved, obj) def test_multiple_open_close(self): # gh-4409: open & close multiple times with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() df.to_hdf(path, 'df', mode='w', format='table') # single store = HDFStore(path) assert 'CLOSED' not in store.info() assert store.is_open store.close() assert 'CLOSED' in store.info() assert not store.is_open with ensure_clean_path(self.path) as path: if pytables._table_file_open_policy_is_strict: # multiples store1 = HDFStore(path) def f(): HDFStore(path) pytest.raises(ValueError, f) store1.close() else: # multiples store1 = HDFStore(path) store2 = HDFStore(path) assert 'CLOSED' not in store1.info() assert 'CLOSED' not in store2.info() assert store1.is_open assert store2.is_open store1.close() assert 'CLOSED' in store1.info() assert not store1.is_open assert 'CLOSED' not in store2.info() assert store2.is_open store2.close() assert 'CLOSED' in store1.info() assert 'CLOSED' in store2.info() assert not store1.is_open assert not store2.is_open # nested close store = HDFStore(path, mode='w') store.append('df', df) store2 = HDFStore(path) store2.append('df2', df) store2.close() assert 'CLOSED' in store2.info() assert not store2.is_open store.close() assert 'CLOSED' in store.info() assert not store.is_open # double closing store = HDFStore(path, mode='w') store.append('df', df) store2 = HDFStore(path) store.close() assert 'CLOSED' in store.info() assert not store.is_open store2.close() assert 'CLOSED' in store2.info() assert not store2.is_open # ops on a closed store with ensure_clean_path(self.path) as path: df = tm.makeDataFrame() df.to_hdf(path, 'df', mode='w', format='table') store = HDFStore(path) store.close() pytest.raises(ClosedFileError, store.keys) pytest.raises(ClosedFileError, lambda: 'df' in store) pytest.raises(ClosedFileError, lambda: len(store)) pytest.raises(ClosedFileError, lambda: store['df']) pytest.raises(AttributeError, lambda: store.df) pytest.raises(ClosedFileError, store.select, 'df') pytest.raises(ClosedFileError, store.get, 'df') pytest.raises(ClosedFileError, store.append, 'df2', df) pytest.raises(ClosedFileError, store.put, 'df3', df) pytest.raises(ClosedFileError, store.get_storer, 'df2') pytest.raises(ClosedFileError, store.remove, 'df2') def f(): store.select('df') tm.assert_raises_regex(ClosedFileError, 'file is not open', f) def test_pytables_native_read(self): with ensure_clean_store( tm.get_data_path('legacy_hdf/pytables_native.h5'), mode='r') as store: d2 = store['detector/readout'] assert isinstance(d2, DataFrame) @pytest.mark.skipif(PY35 and is_platform_windows(), reason="native2 read fails oddly on windows / 3.5") def test_pytables_native2_read(self): with ensure_clean_store( tm.get_data_path('legacy_hdf/pytables_native2.h5'), mode='r') as store: str(store) d1 = store['detector'] assert isinstance(d1, DataFrame) def test_legacy_table_read(self): # legacy table types with ensure_clean_store( tm.get_data_path('legacy_hdf/legacy_table.h5'), mode='r') as store: with catch_warnings(record=True): store.select('df1') store.select('df2') store.select('wp1') # force the frame store.select('df2', typ='legacy_frame') # old version warning pytest.raises( Exception, store.select, 'wp1', 'minor_axis=B') df2 = store.select('df2') result = store.select('df2', 'index>df2.index[2]') expected = df2[df2.index > df2.index[2]] assert_frame_equal(expected, result) def test_copy(self): with catch_warnings(record=True): def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs): try: store = HDFStore(f, 'r') if new_f is None: import tempfile fd, new_f = tempfile.mkstemp() tstore = store.copy( new_f, keys=keys, propindexes=propindexes, **kwargs) # check keys if keys is None: keys = store.keys() assert set(keys) == set(tstore.keys()) # check indices & nrows for k in tstore.keys(): if tstore.get_storer(k).is_table: new_t = tstore.get_storer(k) orig_t = store.get_storer(k) assert orig_t.nrows == new_t.nrows # check propindixes if propindexes: for a in orig_t.axes: if a.is_indexed: assert new_t[a.name].is_indexed finally: safe_close(store) safe_close(tstore) try: os.close(fd) except: pass safe_remove(new_f) # new table df = tm.makeDataFrame() try: path = create_tempfile(self.path) st = HDFStore(path) st.append('df', df, data_columns=['A']) st.close() do_copy(f=path) do_copy(f=path, propindexes=False) finally: safe_remove(path) def test_store_datetime_fractional_secs(self): with ensure_clean_store(self.path) as store: dt = datetime.datetime(2012, 1, 2, 3, 4, 5, 123456) series = Series([0], [dt]) store['a'] = series assert store['a'].index[0] == dt def test_tseries_indices_series(self): with ensure_clean_store(self.path) as store: idx = tm.makeDateIndex(10) ser = Series(np.random.randn(len(idx)), idx) store['a'] = ser result = store['a'] tm.assert_series_equal(result, ser) assert result.index.freq == ser.index.freq tm.assert_class_equal(result.index, ser.index, obj="series index") idx = tm.makePeriodIndex(10) ser = Series(np.random.randn(len(idx)), idx) store['a'] = ser result = store['a'] tm.assert_series_equal(result, ser) assert result.index.freq == ser.index.freq tm.assert_class_equal(result.index, ser.index, obj="series index") def test_tseries_indices_frame(self): with ensure_clean_store(self.path) as store: idx = tm.makeDateIndex(10) df = DataFrame(np.random.randn(len(idx), 3), index=idx) store['a'] = df result = store['a'] assert_frame_equal(result, df) assert result.index.freq == df.index.freq tm.assert_class_equal(result.index, df.index, obj="dataframe index") idx = tm.makePeriodIndex(10) df = DataFrame(np.random.randn(len(idx), 3), idx) store['a'] = df result = store['a'] assert_frame_equal(result, df) assert result.index.freq == df.index.freq tm.assert_class_equal(result.index, df.index, obj="dataframe index") def test_unicode_index(self): unicode_values = [u('\u03c3'), u('\u03c3\u03c3')] # PerformanceWarning with catch_warnings(record=True): s = Series(np.random.randn(len(unicode_values)), unicode_values) self._check_roundtrip(s, tm.assert_series_equal) def test_unicode_longer_encoded(self): # GH 11234 char = '\u0394' df = pd.DataFrame({'A': [char]}) with ensure_clean_store(self.path) as store: store.put('df', df, format='table', encoding='utf-8') result = store.get('df') tm.assert_frame_equal(result, df) df = pd.DataFrame({'A': ['a', char], 'B': ['b', 'b']}) with ensure_clean_store(self.path) as store: store.put('df', df, format='table', encoding='utf-8') result = store.get('df') tm.assert_frame_equal(result, df) def test_store_datetime_mixed(self): df = DataFrame( {'a': [1, 2, 3], 'b': [1., 2., 3.], 'c': ['a', 'b', 'c']}) ts = tm.makeTimeSeries() df['d'] = ts.index[:3] self._check_roundtrip(df, tm.assert_frame_equal) # def test_cant_write_multiindex_table(self): # # for now, #1848 # df = DataFrame(np.random.randn(10, 4), # index=[np.arange(5).repeat(2), # np.tile(np.arange(2), 5)]) # pytest.raises(Exception, store.put, 'foo', df, format='table') def test_append_with_diff_col_name_types_raises_value_error(self): df = DataFrame(np.random.randn(10, 1)) df2 = DataFrame({'a': np.random.randn(10)}) df3 = DataFrame({(1, 2): np.random.randn(10)}) df4 = DataFrame({('1', 2): np.random.randn(10)}) df5 = DataFrame({('1', 2, object): np.random.randn(10)}) with ensure_clean_store(self.path) as store: name = 'df_%s' % tm.rands(10) store.append(name, df) for d in (df2, df3, df4, df5): with pytest.raises(ValueError): store.append(name, d) def test_query_with_nested_special_character(self): df = DataFrame({'a': ['a', 'a', 'c', 'b', 'test & test', 'c', 'b', 'e'], 'b': [1, 2, 3, 4, 5, 6, 7, 8]}) expected = df[df.a == 'test & test'] with ensure_clean_store(self.path) as store: store.append('test', df, format='table', data_columns=True) result = store.select('test', 'a = "test & test"') tm.assert_frame_equal(expected, result) def test_categorical(self): with ensure_clean_store(self.path) as store: # Basic _maybe_remove(store, 's') s = Series(Categorical(['a', 'b', 'b', 'a', 'a', 'c'], categories=[ 'a', 'b', 'c', 'd'], ordered=False)) store.append('s', s, format='table') result = store.select('s') tm.assert_series_equal(s, result) _maybe_remove(store, 's_ordered') s = Series(Categorical(['a', 'b', 'b', 'a', 'a', 'c'], categories=[ 'a', 'b', 'c', 'd'], ordered=True)) store.append('s_ordered', s, format='table') result = store.select('s_ordered') tm.assert_series_equal(s, result) _maybe_remove(store, 'df') df = DataFrame({"s": s, "vals": [1, 2, 3, 4, 5, 6]}) store.append('df', df, format='table') result = store.select('df') tm.assert_frame_equal(result, df) # Dtypes s = Series([1, 1, 2, 2, 3, 4, 5]).astype('category') store.append('si', s) result = store.select('si') tm.assert_series_equal(result, s) s = Series([1, 1, np.nan, 2, 3, 4, 5]).astype('category') store.append('si2', s) result = store.select('si2') tm.assert_series_equal(result, s) # Multiple df2 = df.copy() df2['s2'] = Series(list('abcdefg')).astype('category') store.append('df2', df2) result = store.select('df2') tm.assert_frame_equal(result, df2) # Make sure the metadata is OK info = store.info() assert '/df2 ' in info # assert '/df2/meta/values_block_0/meta' in info assert '/df2/meta/values_block_1/meta' in info # unordered s = Series(Categorical(['a', 'b', 'b', 'a', 'a', 'c'], categories=[ 'a', 'b', 'c', 'd'], ordered=False)) store.append('s2', s, format='table') result = store.select('s2') tm.assert_series_equal(result, s) # Query store.append('df3', df, data_columns=['s']) expected = df[df.s.isin(['b', 'c'])] result = store.select('df3', where=['s in ["b","c"]']) tm.assert_frame_equal(result, expected) expected = df[df.s.isin(['b', 'c'])] result = store.select('df3', where=['s = ["b","c"]']) tm.assert_frame_equal(result, expected) expected = df[df.s.isin(['d'])] result = store.select('df3', where=['s in ["d"]']) tm.assert_frame_equal(result, expected) expected = df[df.s.isin(['f'])] result = store.select('df3', where=['s in ["f"]']) tm.assert_frame_equal(result, expected) # Appending with same categories is ok store.append('df3', df) df = concat([df, df]) expected = df[df.s.isin(['b', 'c'])] result = store.select('df3', where=['s in ["b","c"]']) tm.assert_frame_equal(result, expected) # Appending must have the same categories df3 = df.copy() df3['s'].cat.remove_unused_categories(inplace=True) with pytest.raises(ValueError): store.append('df3', df3) # Remove, and make sure meta data is removed (its a recursive # removal so should be). result = store.select('df3/meta/s/meta') assert result is not None store.remove('df3') with pytest.raises(KeyError): store.select('df3/meta/s/meta') def test_categorical_conversion(self): # GH13322 # Check that read_hdf with categorical columns doesn't return rows if # where criteria isn't met. obsids = ['ESP_012345_6789', 'ESP_987654_3210'] imgids = ['APF00006np', 'APF0001imm'] data = [4.3, 9.8] # Test without categories df = DataFrame(dict(obsids=obsids, imgids=imgids, data=data)) # We are expecting an empty DataFrame matching types of df expected = df.iloc[[], :] with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table', data_columns=True) result = read_hdf(path, 'df', where='obsids=B') tm.assert_frame_equal(result, expected) # Test with categories df.obsids = df.obsids.astype('category') df.imgids = df.imgids.astype('category') # We are expecting an empty DataFrame matching types of df expected = df.iloc[[], :] with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table', data_columns=True) result = read_hdf(path, 'df', where='obsids=B') tm.assert_frame_equal(result, expected) def test_categorical_nan_only_columns(self): # GH18413 # Check that read_hdf with categorical columns with NaN-only values can # be read back. df = pd.DataFrame({ 'a': ['a', 'b', 'c', np.nan], 'b': [np.nan, np.nan, np.nan, np.nan], 'c': [1, 2, 3, 4], 'd': pd.Series([None] * 4, dtype=object) }) df['a'] = df.a.astype('category') df['b'] = df.b.astype('category') df['d'] = df.b.astype('category') expected = df with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table', data_columns=True) result = read_hdf(path, 'df') tm.assert_frame_equal(result, expected) def test_duplicate_column_name(self): df = DataFrame(columns=["a", "a"], data=[[0, 0]]) with ensure_clean_path(self.path) as path: pytest.raises(ValueError, df.to_hdf, path, 'df', format='fixed') df.to_hdf(path, 'df', format='table') other = read_hdf(path, 'df') tm.assert_frame_equal(df, other) assert df.equals(other) assert other.equals(df) def test_round_trip_equals(self): # GH 9330 df = DataFrame({"B": [1, 2], "A": ["x", "y"]}) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table') other = read_hdf(path, 'df') tm.assert_frame_equal(df, other) assert df.equals(other) assert other.equals(df) def test_preserve_timedeltaindex_type(self): # GH9635 # Storing TimedeltaIndexed DataFrames in fixed stores did not preserve # the type of the index. df = DataFrame(np.random.normal(size=(10, 5))) df.index = timedelta_range( start='0s', periods=10, freq='1s', name='example') with ensure_clean_store(self.path) as store: store['df'] = df assert_frame_equal(store['df'], df) def test_columns_multiindex_modified(self): # BUG: 7212 # read_hdf store.select modified the passed columns parameters # when multi-indexed. df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) df.index.name = 'letters' df = df.set_index(keys='E', append=True) data_columns = df.index.names + df.columns.tolist() with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='a', append=True, data_columns=data_columns, index=False) cols2load = list('BCD') cols2load_original = list(cols2load) df_loaded = read_hdf(path, 'df', columns=cols2load) # noqa assert cols2load_original == cols2load def test_to_hdf_with_object_column_names(self): # GH9057 # Writing HDF5 table format should only work for string-like # column types types_should_fail = [tm.makeIntIndex, tm.makeFloatIndex, tm.makeDateIndex, tm.makeTimedeltaIndex, tm.makePeriodIndex] types_should_run = [tm.makeStringIndex, tm.makeCategoricalIndex] if compat.PY3: types_should_run.append(tm.makeUnicodeIndex) else: # TODO: Add back to types_should_fail # https://github.com/pandas-dev/pandas/issues/20907 pass for index in types_should_fail: df = DataFrame(np.random.randn(10, 2), columns=index(2)) with ensure_clean_path(self.path) as path: with catch_warnings(record=True): with tm.assert_raises_regex( ValueError, ("cannot have non-object label " "DataIndexableCol")): df.to_hdf(path, 'df', format='table', data_columns=True) for index in types_should_run: df = DataFrame(np.random.randn(10, 2), columns=index(2)) with ensure_clean_path(self.path) as path: with catch_warnings(record=True): df.to_hdf(path, 'df', format='table', data_columns=True) result = pd.read_hdf( path, 'df', where="index = [{0}]".format(df.index[0])) assert(len(result)) def test_read_hdf_open_store(self): # GH10330 # No check for non-string path_or-buf, and no test of open store df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) df.index.name = 'letters' df = df.set_index(keys='E', append=True) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='w') direct = read_hdf(path, 'df') store = HDFStore(path, mode='r') indirect = read_hdf(store, 'df') tm.assert_frame_equal(direct, indirect) assert store.is_open store.close() def test_read_hdf_iterator(self): df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) df.index.name = 'letters' df = df.set_index(keys='E', append=True) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='w', format='t') direct = read_hdf(path, 'df') iterator = read_hdf(path, 'df', iterator=True) assert isinstance(iterator, TableIterator) indirect = next(iterator.__iter__()) tm.assert_frame_equal(direct, indirect) iterator.store.close() def test_read_hdf_errors(self): df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: pytest.raises(IOError, read_hdf, path, 'key') df.to_hdf(path, 'df') store = HDFStore(path, mode='r') store.close() pytest.raises(IOError, read_hdf, store, 'df') def test_read_hdf_generic_buffer_errors(self): pytest.raises(NotImplementedError, read_hdf, BytesIO(b''), 'df') def test_invalid_complib(self): df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: with pytest.raises(ValueError): df.to_hdf(path, 'df', complib='foolib') # GH10443 def test_read_nokey(self): df = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) # Categorical dtype not supported for "fixed" format. So no need # to test with that dtype in the dataframe here. with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='a') reread = read_hdf(path) assert_frame_equal(df, reread) df.to_hdf(path, 'df2', mode='a') pytest.raises(ValueError, read_hdf, path) def test_read_nokey_table(self): # GH13231 df = DataFrame({'i': range(5), 'c': Series(list('abacd'), dtype='category')}) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', mode='a', format='table') reread = read_hdf(path) assert_frame_equal(df, reread) df.to_hdf(path, 'df2', mode='a', format='table') pytest.raises(ValueError, read_hdf, path) def test_read_nokey_empty(self): with ensure_clean_path(self.path) as path: store = HDFStore(path) store.close() pytest.raises(ValueError, read_hdf, path) @td.skip_if_no('pathlib') def test_read_from_pathlib_path(self): # GH11773 from pathlib import Path expected = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as filename: path_obj = Path(filename) expected.to_hdf(path_obj, 'df', mode='a') actual = read_hdf(path_obj, 'df') tm.assert_frame_equal(expected, actual) @td.skip_if_no('py.path') def test_read_from_py_localpath(self): # GH11773 from py.path import local as LocalPath expected = DataFrame(np.random.rand(4, 5), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as filename: path_obj = LocalPath(filename) expected.to_hdf(path_obj, 'df', mode='a') actual = read_hdf(path_obj, 'df') tm.assert_frame_equal(expected, actual) def test_query_long_float_literal(self): # GH 14241 df = pd.DataFrame({'A': [1000000000.0009, 1000000000.0011, 1000000000.0015]}) with ensure_clean_store(self.path) as store: store.append('test', df, format='table', data_columns=True) cutoff = 1000000000.0006 result = store.select('test', "A < %.4f" % cutoff) assert result.empty cutoff = 1000000000.0010 result = store.select('test', "A > %.4f" % cutoff) expected = df.loc[[1, 2], :] tm.assert_frame_equal(expected, result) exact = 1000000000.0011 result = store.select('test', 'A == %.4f' % exact) expected = df.loc[[1], :] tm.assert_frame_equal(expected, result) def test_query_compare_column_type(self): # GH 15492 df = pd.DataFrame({'date': ['2014-01-01', '2014-01-02'], 'real_date': date_range('2014-01-01', periods=2), 'float': [1.1, 1.2], 'int': [1, 2]}, columns=['date', 'real_date', 'float', 'int']) with ensure_clean_store(self.path) as store: store.append('test', df, format='table', data_columns=True) ts = pd.Timestamp('2014-01-01') # noqa result = store.select('test', where='real_date > ts') expected = df.loc[[1], :] tm.assert_frame_equal(expected, result) for op in ['<', '>', '==']: # non strings to string column always fail for v in [2.1, True, pd.Timestamp('2014-01-01'), pd.Timedelta(1, 's')]: query = 'date {op} v'.format(op=op) with pytest.raises(TypeError): result = store.select('test', where=query) # strings to other columns must be convertible to type v = 'a' for col in ['int', 'float', 'real_date']: query = '{col} {op} v'.format(op=op, col=col) with pytest.raises(ValueError): result = store.select('test', where=query) for v, col in zip(['1', '1.1', '2014-01-01'], ['int', 'float', 'real_date']): query = '{col} {op} v'.format(op=op, col=col) result = store.select('test', where=query) if op == '==': expected = df.loc[[0], :] elif op == '>': expected = df.loc[[1], :] else: expected = df.loc[[], :] tm.assert_frame_equal(expected, result) @pytest.mark.parametrize('format', ['fixed', 'table']) def test_read_hdf_series_mode_r(self, format): # GH 16583 # Tests that reading a Series saved to an HDF file # still works if a mode='r' argument is supplied series = tm.makeFloatSeries() with ensure_clean_path(self.path) as path: series.to_hdf(path, key='data', format=format) result = pd.read_hdf(path, key='data', mode='r') tm.assert_series_equal(result, series) @pytest.mark.skipif(not PY36, reason="Need python 3.6") def test_fspath(self): with tm.ensure_clean('foo.h5') as path: with pd.HDFStore(path) as store: assert os.fspath(store) == str(path) def test_read_py2_hdf_file_in_py3(self): # GH 16781 # tests reading a PeriodIndex DataFrame written in Python2 in Python3 # the file was generated in Python 2.7 like so: # # df = pd.DataFrame([1.,2,3], index=pd.PeriodIndex( # ['2015-01-01', '2015-01-02', '2015-01-05'], freq='B')) # df.to_hdf('periodindex_0.20.1_x86_64_darwin_2.7.13.h5', 'p') expected = pd.DataFrame([1., 2, 3], index=pd.PeriodIndex( ['2015-01-01', '2015-01-02', '2015-01-05'], freq='B')) with ensure_clean_store( tm.get_data_path( 'legacy_hdf/periodindex_0.20.1_x86_64_darwin_2.7.13.h5'), mode='r') as store: result = store['p'] assert_frame_equal(result, expected) class TestHDFComplexValues(Base): # GH10447 def test_complex_fixed(self): df = DataFrame(np.random.rand(4, 5).astype(np.complex64), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) df = DataFrame(np.random.rand(4, 5).astype(np.complex128), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) def test_complex_table(self): df = DataFrame(np.random.rand(4, 5).astype(np.complex64), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) df = DataFrame(np.random.rand(4, 5).astype(np.complex128), index=list('abcd'), columns=list('ABCDE')) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table', mode='w') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) def test_complex_mixed_fixed(self): complex64 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64) complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128) df = DataFrame({'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'], 'C': complex64, 'D': complex128, 'E': [1.0, 2.0, 3.0, 4.0]}, index=list('abcd')) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) def test_complex_mixed_table(self): complex64 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64) complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128) df = DataFrame({'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'], 'C': complex64, 'D': complex128, 'E': [1.0, 2.0, 3.0, 4.0]}, index=list('abcd')) with ensure_clean_store(self.path) as store: store.append('df', df, data_columns=['A', 'B']) result = store.select('df', where='A>2') assert_frame_equal(df.loc[df.A > 2], result) with ensure_clean_path(self.path) as path: df.to_hdf(path, 'df', format='table') reread = read_hdf(path, 'df') assert_frame_equal(df, reread) def test_complex_across_dimensions_fixed(self): with catch_warnings(record=True): complex128 = np.array( [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list('abcd')) df = DataFrame({'A': s, 'B': s}) p = Panel({'One': df, 'Two': df}) objs = [s, df, p] comps = [tm.assert_series_equal, tm.assert_frame_equal, tm.assert_panel_equal] for obj, comp in zip(objs, comps): with ensure_clean_path(self.path) as path: obj.to_hdf(path, 'obj', format='fixed') reread = read_hdf(path, 'obj') comp(obj, reread) def test_complex_across_dimensions(self): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list('abcd')) df = DataFrame({'A': s, 'B': s}) with catch_warnings(record=True): p = Panel({'One': df, 'Two': df}) objs = [df, p] comps = [tm.assert_frame_equal, tm.assert_panel_equal] for obj, comp in zip(objs, comps): with ensure_clean_path(self.path) as path: obj.to_hdf(path, 'obj', format='table') reread = read_hdf(path, 'obj') comp(obj, reread) def test_complex_indexing_error(self): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128) df = DataFrame({'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd'], 'C': complex128}, index=list('abcd')) with ensure_clean_store(self.path) as store: pytest.raises(TypeError, store.append, 'df', df, data_columns=['C']) def test_complex_series_error(self): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list('abcd')) with ensure_clean_path(self.path) as path: pytest.raises(TypeError, s.to_hdf, path, 'obj', format='t') with ensure_clean_path(self.path) as path: s.to_hdf(path, 'obj', format='t', index=False) reread = read_hdf(path, 'obj') tm.assert_series_equal(s, reread) def test_complex_append(self): df = DataFrame({'a': np.random.randn(100).astype(np.complex128), 'b': np.random.randn(100)}) with ensure_clean_store(self.path) as store: store.append('df', df, data_columns=['b']) store.append('df', df) result = store.select('df') assert_frame_equal(pd.concat([df, df], 0), result) class TestTimezones(Base): def _compare_with_tz(self, a, b): tm.assert_frame_equal(a, b) # compare the zones on each element for c in a.columns: for i in a.index: a_e = a.loc[i, c] b_e = b.loc[i, c] if not (a_e == b_e and a_e.tz == b_e.tz): raise AssertionError( "invalid tz comparison [%s] [%s]" % (a_e, b_e)) def test_append_with_timezones_dateutil(self): from datetime import timedelta # use maybe_get_tz instead of dateutil.tz.gettz to handle the windows # filename issues. from pandas._libs.tslibs.timezones import maybe_get_tz gettz = lambda x: maybe_get_tz('dateutil/' + x) # as columns with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df_tz') df = DataFrame(dict(A=[Timestamp('20130102 2:00:00', tz=gettz( 'US/Eastern')) + timedelta(hours=1) * i for i in range(5)])) store.append('df_tz', df, data_columns=['A']) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) # select with tz aware expected = df[df.A >= df.A[3]] result = store.select('df_tz', where='A>=df.A[3]') self._compare_with_tz(result, expected) # ensure we include dates in DST and STD time here. _maybe_remove(store, 'df_tz') df = DataFrame(dict(A=Timestamp('20130102', tz=gettz('US/Eastern')), B=Timestamp('20130603', tz=gettz('US/Eastern'))), index=range(5)) store.append('df_tz', df) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) df = DataFrame(dict(A=Timestamp('20130102', tz=gettz('US/Eastern')), B=Timestamp('20130102', tz=gettz('EET'))), index=range(5)) pytest.raises(ValueError, store.append, 'df_tz', df) # this is ok _maybe_remove(store, 'df_tz') store.append('df_tz', df, data_columns=['A', 'B']) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) # can't append with diff timezone df = DataFrame(dict(A=Timestamp('20130102', tz=gettz('US/Eastern')), B=Timestamp('20130102', tz=gettz('CET'))), index=range(5)) pytest.raises(ValueError, store.append, 'df_tz', df) # as index with ensure_clean_store(self.path) as store: # GH 4098 example df = DataFrame(dict(A=Series(lrange(3), index=date_range( '2000-1-1', periods=3, freq='H', tz=gettz('US/Eastern'))))) _maybe_remove(store, 'df') store.put('df', df) result = store.select('df') assert_frame_equal(result, df) _maybe_remove(store, 'df') store.append('df', df) result = store.select('df') assert_frame_equal(result, df) def test_append_with_timezones_pytz(self): from datetime import timedelta # as columns with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df_tz') df = DataFrame(dict(A=[Timestamp('20130102 2:00:00', tz='US/Eastern') + timedelta(hours=1) * i for i in range(5)])) store.append('df_tz', df, data_columns=['A']) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) # select with tz aware self._compare_with_tz(store.select( 'df_tz', where='A>=df.A[3]'), df[df.A >= df.A[3]]) _maybe_remove(store, 'df_tz') # ensure we include dates in DST and STD time here. df = DataFrame(dict(A=Timestamp('20130102', tz='US/Eastern'), B=Timestamp('20130603', tz='US/Eastern')), index=range(5)) store.append('df_tz', df) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) df = DataFrame(dict(A=Timestamp('20130102', tz='US/Eastern'), B=Timestamp('20130102', tz='EET')), index=range(5)) pytest.raises(ValueError, store.append, 'df_tz', df) # this is ok _maybe_remove(store, 'df_tz') store.append('df_tz', df, data_columns=['A', 'B']) result = store['df_tz'] self._compare_with_tz(result, df) assert_frame_equal(result, df) # can't append with diff timezone df = DataFrame(dict(A=Timestamp('20130102', tz='US/Eastern'), B=Timestamp('20130102', tz='CET')), index=range(5)) pytest.raises(ValueError, store.append, 'df_tz', df) # as index with ensure_clean_store(self.path) as store: # GH 4098 example df = DataFrame(dict(A=Series(lrange(3), index=date_range( '2000-1-1', periods=3, freq='H', tz='US/Eastern')))) _maybe_remove(store, 'df') store.put('df', df) result = store.select('df') assert_frame_equal(result, df) _maybe_remove(store, 'df') store.append('df', df) result = store.select('df') assert_frame_equal(result, df) def test_tseries_select_index_column(self): # GH7777 # selecting a UTC datetimeindex column did # not preserve UTC tzinfo set before storing # check that no tz still works rng = date_range('1/1/2000', '1/30/2000') frame = DataFrame(np.random.randn(len(rng), 4), index=rng) with ensure_clean_store(self.path) as store: store.append('frame', frame) result = store.select_column('frame', 'index') assert rng.tz == DatetimeIndex(result.values).tz # check utc rng = date_range('1/1/2000', '1/30/2000', tz='UTC') frame = DataFrame(np.random.randn(len(rng), 4), index=rng) with ensure_clean_store(self.path) as store: store.append('frame', frame) result = store.select_column('frame', 'index') assert rng.tz == result.dt.tz # double check non-utc rng = date_range('1/1/2000', '1/30/2000', tz='US/Eastern') frame = DataFrame(np.random.randn(len(rng), 4), index=rng) with ensure_clean_store(self.path) as store: store.append('frame', frame) result = store.select_column('frame', 'index') assert rng.tz == result.dt.tz def test_timezones_fixed(self): with ensure_clean_store(self.path) as store: # index rng = date_range('1/1/2000', '1/30/2000', tz='US/Eastern') df = DataFrame(np.random.randn(len(rng), 4), index=rng) store['df'] = df result = store['df'] assert_frame_equal(result, df) # as data # GH11411 _maybe_remove(store, 'df') df = DataFrame({'A': rng, 'B': rng.tz_convert('UTC').tz_localize(None), 'C': rng.tz_convert('CET'), 'D': range(len(rng))}, index=rng) store['df'] = df result = store['df'] assert_frame_equal(result, df) def test_fixed_offset_tz(self): rng = date_range('1/1/2000 00:00:00-07:00', '1/30/2000 00:00:00-07:00') frame = DataFrame(np.random.randn(len(rng), 4), index=rng) with ensure_clean_store(self.path) as store: store['frame'] = frame recons = store['frame'] tm.assert_index_equal(recons.index, rng) assert rng.tz == recons.index.tz @td.skip_if_windows def test_store_timezone(self): # GH2852 # issue storing datetime.date with a timezone as it resets when read # back in a new timezone # original method with ensure_clean_store(self.path) as store: today = datetime.date(2013, 9, 10) df = DataFrame([1, 2, 3], index=[today, today, today]) store['obj1'] = df result = store['obj1'] assert_frame_equal(result, df) # with tz setting with ensure_clean_store(self.path) as store: with set_timezone('EST5EDT'): today = datetime.date(2013, 9, 10) df = DataFrame([1, 2, 3], index=[today, today, today]) store['obj1'] = df with set_timezone('CST6CDT'): result = store['obj1'] assert_frame_equal(result, df) def test_legacy_datetimetz_object(self): # legacy from < 0.17.0 # 8260 expected = DataFrame(dict(A=Timestamp('20130102', tz='US/Eastern'), B=Timestamp('20130603', tz='CET')), index=range(5)) with ensure_clean_store( tm.get_data_path('legacy_hdf/datetimetz_object.h5'), mode='r') as store: result = store['df'] assert_frame_equal(result, expected) def test_dst_transitions(self): # make sure we are not failing on transaitions with ensure_clean_store(self.path) as store: times = pd.date_range("2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", freq="H", ambiguous='infer') for i in [times, times + pd.Timedelta('10min')]: _maybe_remove(store, 'df') df = DataFrame({'A': range(len(i)), 'B': i}, index=i) store.append('df', df) result = store.select('df') assert_frame_equal(result, df)
bsd-3-clause
TaizoAyase/FSECplotter2
FSECplotter/pyqt/widgets/figurecanvas.py
1
5968
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' FSECplotter2 - The interactive plotting application for FSEC. Copyright 2015-2017, TaizoAyase, tikuta, biochem-fan This file is part of FSECplotter2. FSECplotter2 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ''' from PyQt5 import QtCore, QtGui, QtWidgets import numpy as np # force matplotlib to use PyQt5 backends import matplotlib matplotlib.use("Qt5Agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure # FigureCanvas inherits QWidget class Figurecanvas(FigureCanvas): """ This class receives the data from model, and plots the figure. The class does not have the model pointer. plot_fig method gets the dictionary of data from PlotArea widgets. """ SEABORN_STYLE = ['darkgrid', 'whitegrid', 'dark', 'white', 'ticks'] SEABORN_CONTEXT = [None, 'paper', 'notebook', 'talk', 'poster'] def __init__(self, parent=None, width=4, height=3, dpi=100, use_seaborn=False, style=0, context=0): self.seaborn = use_seaborn if use_seaborn: import seaborn as sns sns.set_style(self.SEABORN_STYLE[style]) sns.set_context(self.SEABORN_CONTEXT[context]) # set matplitlib figure object self.fig = Figure(figsize=(width, height), dpi=int(dpi)) self.axes = self.fig.add_subplot(111) self.axes.hold(True) # call constructor of FigureCanvas super(Figurecanvas, self).__init__(self.fig) self.setParent(parent) # expand plot area as large as possible FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) self.x_min = 0 self.x_max = 30 # set color map object self.__cm = matplotlib.cm.gist_rainbow def plot_fig(self, current_data, linewidth, volume_x): self.axes.clear() if not self.seaborn: # this is omitted when using seaborn self.axes.grid() # if current_data has no data, return if current_data['total_data'] == 0: return # iterate for the length of dataset( len(filename) ) num_data = len(current_data['filenames']) num_color = current_data['total_data'] self.axes.set_color_cycle([self.__cm(1.*i/num_color) for i in range(num_color)]) for i in range(num_data): x = current_data['data'][i][:, 0] y = current_data['data'][i][:, 1] if volume_x: x *= current_data['flowrate'][i] # set color col = current_data['color'][i] # if default, use default rainbow if col is None: col = self.__conv_to_hex(self.__cm(1.*i/num_color)) self.axes.plot(x, y, label=current_data['filenames'][i], visible=current_data['enable_flags'][i], linewidth=linewidth, color=col) self.axes.set_xlim(self.x_min, self.x_max) if self.y_min and self.y_max: self.axes.set_ylim(self.y_min, self.y_max) self.axes.legend(loc=3, mode="expand", borderaxespad=0., bbox_to_anchor=(0., 1.02, 1., .102), prop={'size': 'small'}) xlab = "Volume(ml)" if volume_x else "Time(min)" self.axes.set_xlabel(xlab) self.axes.set_ylabel("FL intensity(AU)") self.__adjust_scale(num_data) self.draw() def save_fig_to(self, filepath): self.fig.savefig(filepath, bbox_inches='tight') def set_xlim(self, x_min, x_max): if not x_min: self.x_min = 0. else: self.x_min = float(x_min) if not x_max: self.x_max = 30. else: self.x_max = float(x_max) # avoid the illegal range setting if self.x_min > self.x_max: self.x_max = self.x_min + 1.0 return self.x_min, self.x_max def set_ylim(self, y_min, y_max): # add 0.1 to raw value to avoid the False judge in if statement if not y_min: self.y_min = None elif y_min == "-": self.y_min = None else: self.y_min = float(y_min) + 0.1 if not y_max: self.y_max = None elif y_max == "-": self.y_max = None else: self.y_max = float(y_max) + 0.1 if not (self.y_min and self.y_max): return # avoid the illegal range setting if self.y_min > self.y_max: self.y_max = self.y_min + 100.0 return self.y_min, self.y_max # private def __adjust_scale(self, num_data): # in this scheme, at 16 sample, top~0.25, # which is the smallest size of graph-area if num_data < 16: adj = 0.9 - 0.04 * num_data else: adj = 0.25 self.fig.subplots_adjust(top=adj) def __conv_to_hex(self, rgba_col): red = int(rgba_col[0] * 255) green = int(rgba_col[1] * 255) blue = int(rgba_col[2] * 255) return '#{r:02x}{g:02x}{b:02x}'.format(r=red, g=green, b=blue)
gpl-2.0
belltailjp/scikit-learn
examples/cluster/plot_digits_agglomeration.py
377
1694
#!/usr/bin/python # -*- coding: utf-8 -*- """ ========================================================= Feature agglomeration ========================================================= These images how similar features are merged together using feature agglomeration. """ print(__doc__) # Code source: Gaël Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, cluster from sklearn.feature_extraction.image import grid_to_graph digits = datasets.load_digits() images = digits.images X = np.reshape(images, (len(images), -1)) connectivity = grid_to_graph(*images[0].shape) agglo = cluster.FeatureAgglomeration(connectivity=connectivity, n_clusters=32) agglo.fit(X) X_reduced = agglo.transform(X) X_restored = agglo.inverse_transform(X_reduced) images_restored = np.reshape(X_restored, images.shape) plt.figure(1, figsize=(4, 3.5)) plt.clf() plt.subplots_adjust(left=.01, right=.99, bottom=.01, top=.91) for i in range(4): plt.subplot(3, 4, i + 1) plt.imshow(images[i], cmap=plt.cm.gray, vmax=16, interpolation='nearest') plt.xticks(()) plt.yticks(()) if i == 1: plt.title('Original data') plt.subplot(3, 4, 4 + i + 1) plt.imshow(images_restored[i], cmap=plt.cm.gray, vmax=16, interpolation='nearest') if i == 1: plt.title('Agglomerated data') plt.xticks(()) plt.yticks(()) plt.subplot(3, 4, 10) plt.imshow(np.reshape(agglo.labels_, images[0].shape), interpolation='nearest', cmap=plt.cm.spectral) plt.xticks(()) plt.yticks(()) plt.title('Labels') plt.show()
bsd-3-clause
DSLituiev/scikit-learn
sklearn/gaussian_process/gaussian_process.py
16
34896
# -*- coding: utf-8 -*- # Author: Vincent Dubourg <[email protected]> # (mostly translation, see implementation details) # Licence: BSD 3 clause from __future__ import print_function import numpy as np from scipy import linalg, optimize from ..base import BaseEstimator, RegressorMixin from ..metrics.pairwise import manhattan_distances from ..utils import check_random_state, check_array, check_X_y from ..utils.validation import check_is_fitted from . import regression_models as regression from . import correlation_models as correlation from ..utils import deprecated MACHINE_EPSILON = np.finfo(np.double).eps @deprecated("l1_cross_distances is deprecated and will be removed in 0.20.") def l1_cross_distances(X): """ Computes the nonzero componentwise L1 cross-distances between the vectors in X. Parameters ---------- X: array_like An array with shape (n_samples, n_features) Returns ------- D: array with shape (n_samples * (n_samples - 1) / 2, n_features) The array of componentwise L1 cross-distances. ij: arrays with shape (n_samples * (n_samples - 1) / 2, 2) The indices i and j of the vectors in X associated to the cross- distances in D: D[k] = np.abs(X[ij[k, 0]] - Y[ij[k, 1]]). """ X = check_array(X) n_samples, n_features = X.shape n_nonzero_cross_dist = n_samples * (n_samples - 1) // 2 ij = np.zeros((n_nonzero_cross_dist, 2), dtype=np.int) D = np.zeros((n_nonzero_cross_dist, n_features)) ll_1 = 0 for k in range(n_samples - 1): ll_0 = ll_1 ll_1 = ll_0 + n_samples - k - 1 ij[ll_0:ll_1, 0] = k ij[ll_0:ll_1, 1] = np.arange(k + 1, n_samples) D[ll_0:ll_1] = np.abs(X[k] - X[(k + 1):n_samples]) return D, ij @deprecated("GaussianProcess is deprecated and will be removed in 0.20. " "Use the GaussianProcessRegressor instead.") class GaussianProcess(BaseEstimator, RegressorMixin): """The legacy Gaussian Process model class. Note that this class is deprecated and will be removed in 0.20. Use the GaussianProcessRegressor instead. Read more in the :ref:`User Guide <gaussian_process>`. Parameters ---------- regr : string or callable, optional A regression function returning an array of outputs of the linear regression functional basis. The number of observations n_samples should be greater than the size p of this basis. Default assumes a simple constant regression trend. Available built-in regression models are:: 'constant', 'linear', 'quadratic' corr : string or callable, optional A stationary autocorrelation function returning the autocorrelation between two points x and x'. Default assumes a squared-exponential autocorrelation model. Built-in correlation models are:: 'absolute_exponential', 'squared_exponential', 'generalized_exponential', 'cubic', 'linear' beta0 : double array_like, optional The regression weight vector to perform Ordinary Kriging (OK). Default assumes Universal Kriging (UK) so that the vector beta of regression weights is estimated using the maximum likelihood principle. storage_mode : string, optional A string specifying whether the Cholesky decomposition of the correlation matrix should be stored in the class (storage_mode = 'full') or not (storage_mode = 'light'). Default assumes storage_mode = 'full', so that the Cholesky decomposition of the correlation matrix is stored. This might be a useful parameter when one is not interested in the MSE and only plan to estimate the BLUP, for which the correlation matrix is not required. verbose : boolean, optional A boolean specifying the verbose level. Default is verbose = False. theta0 : double array_like, optional An array with shape (n_features, ) or (1, ). The parameters in the autocorrelation model. If thetaL and thetaU are also specified, theta0 is considered as the starting point for the maximum likelihood estimation of the best set of parameters. Default assumes isotropic autocorrelation model with theta0 = 1e-1. thetaL : double array_like, optional An array with shape matching theta0's. Lower bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. thetaU : double array_like, optional An array with shape matching theta0's. Upper bound on the autocorrelation parameters for maximum likelihood estimation. Default is None, so that it skips maximum likelihood estimation and it uses theta0. normalize : boolean, optional Input X and observations y are centered and reduced wrt means and standard deviations estimated from the n_samples observations provided. Default is normalize = True so that data is normalized to ease maximum likelihood estimation. nugget : double or ndarray, optional Introduce a nugget effect to allow smooth predictions from noisy data. If nugget is an ndarray, it must be the same length as the number of data points used for the fit. The nugget is added to the diagonal of the assumed training covariance; in this way it acts as a Tikhonov regularization in the problem. In the special case of the squared exponential correlation function, the nugget mathematically represents the variance of the input values. Default assumes a nugget close to machine precision for the sake of robustness (nugget = 10. * MACHINE_EPSILON). optimizer : string, optional A string specifying the optimization algorithm to be used. Default uses 'fmin_cobyla' algorithm from scipy.optimize. Available optimizers are:: 'fmin_cobyla', 'Welch' 'Welch' optimizer is dued to Welch et al., see reference [WBSWM1992]_. It consists in iterating over several one-dimensional optimizations instead of running one single multi-dimensional optimization. random_start : int, optional The number of times the Maximum Likelihood Estimation should be performed from a random starting point. The first MLE always uses the specified starting point (theta0), the next starting points are picked at random according to an exponential distribution (log-uniform on [thetaL, thetaU]). Default does not use random starting point (random_start = 1). random_state: integer or numpy.RandomState, optional The generator used to shuffle the sequence of coordinates of theta in the Welch optimizer. If an integer is given, it fixes the seed. Defaults to the global numpy random number generator. Attributes ---------- theta_ : array Specified theta OR the best set of autocorrelation parameters (the \ sought maximizer of the reduced likelihood function). reduced_likelihood_function_value_ : array The optimal reduced likelihood function value. Examples -------- >>> import numpy as np >>> from sklearn.gaussian_process import GaussianProcess >>> X = np.array([[1., 3., 5., 6., 7., 8.]]).T >>> y = (X * np.sin(X)).ravel() >>> gp = GaussianProcess(theta0=0.1, thetaL=.001, thetaU=1.) >>> gp.fit(X, y) # doctest: +ELLIPSIS GaussianProcess(beta0=None... ... Notes ----- The presentation implementation is based on a translation of the DACE Matlab toolbox, see reference [NLNS2002]_. References ---------- .. [NLNS2002] `H.B. Nielsen, S.N. Lophaven, H. B. Nielsen and J. Sondergaard. DACE - A MATLAB Kriging Toolbox.` (2002) http://www2.imm.dtu.dk/~hbn/dace/dace.pdf .. [WBSWM1992] `W.J. Welch, R.J. Buck, J. Sacks, H.P. Wynn, T.J. Mitchell, and M.D. Morris (1992). Screening, predicting, and computer experiments. Technometrics, 34(1) 15--25.` http://www.jstor.org/pss/1269548 """ _regression_types = { 'constant': regression.constant, 'linear': regression.linear, 'quadratic': regression.quadratic} _correlation_types = { 'absolute_exponential': correlation.absolute_exponential, 'squared_exponential': correlation.squared_exponential, 'generalized_exponential': correlation.generalized_exponential, 'cubic': correlation.cubic, 'linear': correlation.linear} _optimizer_types = [ 'fmin_cobyla', 'Welch'] def __init__(self, regr='constant', corr='squared_exponential', beta0=None, storage_mode='full', verbose=False, theta0=1e-1, thetaL=None, thetaU=None, optimizer='fmin_cobyla', random_start=1, normalize=True, nugget=10. * MACHINE_EPSILON, random_state=None): self.regr = regr self.corr = corr self.beta0 = beta0 self.storage_mode = storage_mode self.verbose = verbose self.theta0 = theta0 self.thetaL = thetaL self.thetaU = thetaU self.normalize = normalize self.nugget = nugget self.optimizer = optimizer self.random_start = random_start self.random_state = random_state def fit(self, X, y): """ The Gaussian Process model fitting method. Parameters ---------- X : double array_like An array with shape (n_samples, n_features) with the input at which observations were made. y : double array_like An array with shape (n_samples, ) or shape (n_samples, n_targets) with the observations of the output to be predicted. Returns ------- gp : self A fitted Gaussian Process model object awaiting data to perform predictions. """ # Run input checks self._check_params() self.random_state = check_random_state(self.random_state) # Force data to 2D numpy.array X, y = check_X_y(X, y, multi_output=True, y_numeric=True) self.y_ndim_ = y.ndim if y.ndim == 1: y = y[:, np.newaxis] # Check shapes of DOE & observations n_samples, n_features = X.shape _, n_targets = y.shape # Run input checks self._check_params(n_samples) # Normalize data or don't if self.normalize: X_mean = np.mean(X, axis=0) X_std = np.std(X, axis=0) y_mean = np.mean(y, axis=0) y_std = np.std(y, axis=0) X_std[X_std == 0.] = 1. y_std[y_std == 0.] = 1. # center and scale X if necessary X = (X - X_mean) / X_std y = (y - y_mean) / y_std else: X_mean = np.zeros(1) X_std = np.ones(1) y_mean = np.zeros(1) y_std = np.ones(1) # Calculate matrix of distances D between samples D, ij = l1_cross_distances(X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple input features cannot have the same" " target value.") # Regression matrix and parameters F = self.regr(X) n_samples_F = F.shape[0] if F.ndim > 1: p = F.shape[1] else: p = 1 if n_samples_F != n_samples: raise Exception("Number of rows in F and X do not match. Most " "likely something is going wrong with the " "regression model.") if p > n_samples_F: raise Exception(("Ordinary least squares problem is undetermined " "n_samples=%d must be greater than the " "regression model size p=%d.") % (n_samples, p)) if self.beta0 is not None: if self.beta0.shape[0] != p: raise Exception("Shapes of beta0 and F do not match.") # Set attributes self.X = X self.y = y self.D = D self.ij = ij self.F = F self.X_mean, self.X_std = X_mean, X_std self.y_mean, self.y_std = y_mean, y_std # Determine Gaussian Process model parameters if self.thetaL is not None and self.thetaU is not None: # Maximum Likelihood Estimation of the parameters if self.verbose: print("Performing Maximum Likelihood Estimation of the " "autocorrelation parameters...") self.theta_, self.reduced_likelihood_function_value_, par = \ self._arg_max_reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad parameter region. " "Try increasing upper bound") else: # Given parameters if self.verbose: print("Given autocorrelation parameters. " "Computing Gaussian Process model parameters...") self.theta_ = self.theta0 self.reduced_likelihood_function_value_, par = \ self.reduced_likelihood_function() if np.isinf(self.reduced_likelihood_function_value_): raise Exception("Bad point. Try increasing theta0.") self.beta = par['beta'] self.gamma = par['gamma'] self.sigma2 = par['sigma2'] self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] if self.storage_mode == 'light': # Delete heavy data (it will be computed again if required) # (it is required only when MSE is wanted in self.predict) if self.verbose: print("Light storage mode specified. " "Flushing autocorrelation matrix...") self.D = None self.ij = None self.F = None self.C = None self.Ft = None self.G = None return self def predict(self, X, eval_MSE=False, batch_size=None): """ This function evaluates the Gaussian Process model at x. Parameters ---------- X : array_like An array with shape (n_eval, n_features) giving the point(s) at which the prediction(s) should be made. eval_MSE : boolean, optional A boolean specifying whether the Mean Squared Error should be evaluated or not. Default assumes evalMSE = False and evaluates only the BLUP (mean prediction). batch_size : integer, optional An integer giving the maximum number of points that can be evaluated simultaneously (depending on the available memory). Default is None so that all given points are evaluated at the same time. Returns ------- y : array_like, shape (n_samples, ) or (n_samples, n_targets) An array with shape (n_eval, ) if the Gaussian Process was trained on an array of shape (n_samples, ) or an array with shape (n_eval, n_targets) if the Gaussian Process was trained on an array of shape (n_samples, n_targets) with the Best Linear Unbiased Prediction at x. MSE : array_like, optional (if eval_MSE == True) An array with shape (n_eval, ) or (n_eval, n_targets) as with y, with the Mean Squared Error at x. """ check_is_fitted(self, "X") # Check input shapes X = check_array(X) n_eval, _ = X.shape n_samples, n_features = self.X.shape n_samples_y, n_targets = self.y.shape # Run input checks self._check_params(n_samples) if X.shape[1] != n_features: raise ValueError(("The number of features in X (X.shape[1] = %d) " "should match the number of features used " "for fit() " "which is %d.") % (X.shape[1], n_features)) if batch_size is None: # No memory management # (evaluates all given points in a single batch run) # Normalize input X = (X - self.X_mean) / self.X_std # Initialize output y = np.zeros(n_eval) if eval_MSE: MSE = np.zeros(n_eval) # Get pairwise componentwise L1-distances to the input training set dx = manhattan_distances(X, Y=self.X, sum_over_features=False) # Get regression function and correlation f = self.regr(X) r = self.corr(self.theta_, dx).reshape(n_eval, n_samples) # Scaled predictor y_ = np.dot(f, self.beta) + np.dot(r, self.gamma) # Predictor y = (self.y_mean + self.y_std * y_).reshape(n_eval, n_targets) if self.y_ndim_ == 1: y = y.ravel() # Mean Squared Error if eval_MSE: C = self.C if C is None: # Light storage mode (need to recompute C, F, Ft and G) if self.verbose: print("This GaussianProcess used 'light' storage mode " "at instantiation. Need to recompute " "autocorrelation matrix...") reduced_likelihood_function_value, par = \ self.reduced_likelihood_function() self.C = par['C'] self.Ft = par['Ft'] self.G = par['G'] rt = linalg.solve_triangular(self.C, r.T, lower=True) if self.beta0 is None: # Universal Kriging u = linalg.solve_triangular(self.G.T, np.dot(self.Ft.T, rt) - f.T, lower=True) else: # Ordinary Kriging u = np.zeros((n_targets, n_eval)) MSE = np.dot(self.sigma2.reshape(n_targets, 1), (1. - (rt ** 2.).sum(axis=0) + (u ** 2.).sum(axis=0))[np.newaxis, :]) MSE = np.sqrt((MSE ** 2.).sum(axis=0) / n_targets) # Mean Squared Error might be slightly negative depending on # machine precision: force to zero! MSE[MSE < 0.] = 0. if self.y_ndim_ == 1: MSE = MSE.ravel() return y, MSE else: return y else: # Memory management if type(batch_size) is not int or batch_size <= 0: raise Exception("batch_size must be a positive integer") if eval_MSE: y, MSE = np.zeros(n_eval), np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to], MSE[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y, MSE else: y = np.zeros(n_eval) for k in range(max(1, n_eval / batch_size)): batch_from = k * batch_size batch_to = min([(k + 1) * batch_size + 1, n_eval + 1]) y[batch_from:batch_to] = \ self.predict(X[batch_from:batch_to], eval_MSE=eval_MSE, batch_size=None) return y def reduced_likelihood_function(self, theta=None): """ This function determines the BLUP parameters and evaluates the reduced likelihood function for the given autocorrelation parameters theta. Maximizing this function wrt the autocorrelation parameters theta is equivalent to maximizing the likelihood of the assumed joint Gaussian distribution of the observations y evaluated onto the design of experiments X. Parameters ---------- theta : array_like, optional An array containing the autocorrelation parameters at which the Gaussian Process model parameters should be determined. Default uses the built-in autocorrelation parameters (ie ``theta = self.theta_``). Returns ------- reduced_likelihood_function_value : double The value of the reduced likelihood function associated to the given autocorrelation parameters theta. par : dict A dictionary containing the requested Gaussian Process model parameters: sigma2 Gaussian Process variance. beta Generalized least-squares regression weights for Universal Kriging or given beta0 for Ordinary Kriging. gamma Gaussian Process weights. C Cholesky decomposition of the correlation matrix [R]. Ft Solution of the linear equation system : [R] x Ft = F G QR decomposition of the matrix Ft. """ check_is_fitted(self, "X") if theta is None: # Use built-in autocorrelation parameters theta = self.theta_ # Initialize output reduced_likelihood_function_value = - np.inf par = {} # Retrieve data n_samples = self.X.shape[0] D = self.D ij = self.ij F = self.F if D is None: # Light storage mode (need to recompute D, ij and F) D, ij = l1_cross_distances(self.X) if (np.min(np.sum(D, axis=1)) == 0. and self.corr != correlation.pure_nugget): raise Exception("Multiple X are not allowed") F = self.regr(self.X) # Set up R r = self.corr(theta, D) R = np.eye(n_samples) * (1. + self.nugget) R[ij[:, 0], ij[:, 1]] = r R[ij[:, 1], ij[:, 0]] = r # Cholesky decomposition of R try: C = linalg.cholesky(R, lower=True) except linalg.LinAlgError: return reduced_likelihood_function_value, par # Get generalized least squares solution Ft = linalg.solve_triangular(C, F, lower=True) try: Q, G = linalg.qr(Ft, econ=True) except: #/usr/lib/python2.6/dist-packages/scipy/linalg/decomp.py:1177: # DeprecationWarning: qr econ argument will be removed after scipy # 0.7. The economy transform will then be available through the # mode='economic' argument. Q, G = linalg.qr(Ft, mode='economic') sv = linalg.svd(G, compute_uv=False) rcondG = sv[-1] / sv[0] if rcondG < 1e-10: # Check F sv = linalg.svd(F, compute_uv=False) condF = sv[0] / sv[-1] if condF > 1e15: raise Exception("F is too ill conditioned. Poor combination " "of regression model and observations.") else: # Ft is too ill conditioned, get out (try different theta) return reduced_likelihood_function_value, par Yt = linalg.solve_triangular(C, self.y, lower=True) if self.beta0 is None: # Universal Kriging beta = linalg.solve_triangular(G, np.dot(Q.T, Yt)) else: # Ordinary Kriging beta = np.array(self.beta0) rho = Yt - np.dot(Ft, beta) sigma2 = (rho ** 2.).sum(axis=0) / n_samples # The determinant of R is equal to the squared product of the diagonal # elements of its Cholesky decomposition C detR = (np.diag(C) ** (2. / n_samples)).prod() # Compute/Organize output reduced_likelihood_function_value = - sigma2.sum() * detR par['sigma2'] = sigma2 * self.y_std ** 2. par['beta'] = beta par['gamma'] = linalg.solve_triangular(C.T, rho) par['C'] = C par['Ft'] = Ft par['G'] = G return reduced_likelihood_function_value, par def _arg_max_reduced_likelihood_function(self): """ This function estimates the autocorrelation parameters theta as the maximizer of the reduced likelihood function. (Minimization of the opposite reduced likelihood function is used for convenience) Parameters ---------- self : All parameters are stored in the Gaussian Process model object. Returns ------- optimal_theta : array_like The best set of autocorrelation parameters (the sought maximizer of the reduced likelihood function). optimal_reduced_likelihood_function_value : double The optimal reduced likelihood function value. optimal_par : dict The BLUP parameters associated to thetaOpt. """ # Initialize output best_optimal_theta = [] best_optimal_rlf_value = [] best_optimal_par = [] if self.verbose: print("The chosen optimizer is: " + str(self.optimizer)) if self.random_start > 1: print(str(self.random_start) + " random starts are required.") percent_completed = 0. # Force optimizer to fmin_cobyla if the model is meant to be isotropic if self.optimizer == 'Welch' and self.theta0.size == 1: self.optimizer = 'fmin_cobyla' if self.optimizer == 'fmin_cobyla': def minus_reduced_likelihood_function(log10t): return - self.reduced_likelihood_function( theta=10. ** log10t)[0] constraints = [] for i in range(self.theta0.size): constraints.append(lambda log10t, i=i: log10t[i] - np.log10(self.thetaL[0, i])) constraints.append(lambda log10t, i=i: np.log10(self.thetaU[0, i]) - log10t[i]) for k in range(self.random_start): if k == 0: # Use specified starting point as first guess theta0 = self.theta0 else: # Generate a random starting point log10-uniformly # distributed between bounds log10theta0 = (np.log10(self.thetaL) + self.random_state.rand(*self.theta0.shape) * np.log10(self.thetaU / self.thetaL)) theta0 = 10. ** log10theta0 # Run Cobyla try: log10_optimal_theta = \ optimize.fmin_cobyla(minus_reduced_likelihood_function, np.log10(theta0).ravel(), constraints, iprint=0) except ValueError as ve: print("Optimization failed. Try increasing the ``nugget``") raise ve optimal_theta = 10. ** log10_optimal_theta optimal_rlf_value, optimal_par = \ self.reduced_likelihood_function(theta=optimal_theta) # Compare the new optimizer to the best previous one if k > 0: if optimal_rlf_value > best_optimal_rlf_value: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta else: best_optimal_rlf_value = optimal_rlf_value best_optimal_par = optimal_par best_optimal_theta = optimal_theta if self.verbose and self.random_start > 1: if (20 * k) / self.random_start > percent_completed: percent_completed = (20 * k) / self.random_start print("%s completed" % (5 * percent_completed)) optimal_rlf_value = best_optimal_rlf_value optimal_par = best_optimal_par optimal_theta = best_optimal_theta elif self.optimizer == 'Welch': # Backup of the given attributes theta0, thetaL, thetaU = self.theta0, self.thetaL, self.thetaU corr = self.corr verbose = self.verbose # This will iterate over fmin_cobyla optimizer self.optimizer = 'fmin_cobyla' self.verbose = False # Initialize under isotropy assumption if verbose: print("Initialize under isotropy assumption...") self.theta0 = check_array(self.theta0.min()) self.thetaL = check_array(self.thetaL.min()) self.thetaU = check_array(self.thetaU.max()) theta_iso, optimal_rlf_value_iso, par_iso = \ self._arg_max_reduced_likelihood_function() optimal_theta = theta_iso + np.zeros(theta0.shape) # Iterate over all dimensions of theta allowing for anisotropy if verbose: print("Now improving allowing for anisotropy...") for i in self.random_state.permutation(theta0.size): if verbose: print("Proceeding along dimension %d..." % (i + 1)) self.theta0 = check_array(theta_iso) self.thetaL = check_array(thetaL[0, i]) self.thetaU = check_array(thetaU[0, i]) def corr_cut(t, d): return corr(check_array(np.hstack([optimal_theta[0][0:i], t[0], optimal_theta[0][(i + 1)::]])), d) self.corr = corr_cut optimal_theta[0, i], optimal_rlf_value, optimal_par = \ self._arg_max_reduced_likelihood_function() # Restore the given attributes self.theta0, self.thetaL, self.thetaU = theta0, thetaL, thetaU self.corr = corr self.optimizer = 'Welch' self.verbose = verbose else: raise NotImplementedError("This optimizer ('%s') is not " "implemented yet. Please contribute!" % self.optimizer) return optimal_theta, optimal_rlf_value, optimal_par def _check_params(self, n_samples=None): # Check regression model if not callable(self.regr): if self.regr in self._regression_types: self.regr = self._regression_types[self.regr] else: raise ValueError("regr should be one of %s or callable, " "%s was given." % (self._regression_types.keys(), self.regr)) # Check regression weights if given (Ordinary Kriging) if self.beta0 is not None: self.beta0 = np.atleast_2d(self.beta0) if self.beta0.shape[1] != 1: # Force to column vector self.beta0 = self.beta0.T # Check correlation model if not callable(self.corr): if self.corr in self._correlation_types: self.corr = self._correlation_types[self.corr] else: raise ValueError("corr should be one of %s or callable, " "%s was given." % (self._correlation_types.keys(), self.corr)) # Check storage mode if self.storage_mode != 'full' and self.storage_mode != 'light': raise ValueError("Storage mode should either be 'full' or " "'light', %s was given." % self.storage_mode) # Check correlation parameters self.theta0 = np.atleast_2d(self.theta0) lth = self.theta0.size if self.thetaL is not None and self.thetaU is not None: self.thetaL = np.atleast_2d(self.thetaL) self.thetaU = np.atleast_2d(self.thetaU) if self.thetaL.size != lth or self.thetaU.size != lth: raise ValueError("theta0, thetaL and thetaU must have the " "same length.") if np.any(self.thetaL <= 0) or np.any(self.thetaU < self.thetaL): raise ValueError("The bounds must satisfy O < thetaL <= " "thetaU.") elif self.thetaL is None and self.thetaU is None: if np.any(self.theta0 <= 0): raise ValueError("theta0 must be strictly positive.") elif self.thetaL is None or self.thetaU is None: raise ValueError("thetaL and thetaU should either be both or " "neither specified.") # Force verbose type to bool self.verbose = bool(self.verbose) # Force normalize type to bool self.normalize = bool(self.normalize) # Check nugget value self.nugget = np.asarray(self.nugget) if np.any(self.nugget) < 0.: raise ValueError("nugget must be positive or zero.") if (n_samples is not None and self.nugget.shape not in [(), (n_samples,)]): raise ValueError("nugget must be either a scalar " "or array of length n_samples.") # Check optimizer if self.optimizer not in self._optimizer_types: raise ValueError("optimizer should be one of %s" % self._optimizer_types) # Force random_start type to int self.random_start = int(self.random_start)
bsd-3-clause
yaojenkuo/BuildingMachineLearningSystemsWithPython
ch02/seeds_knn_increasing_k.py
24
1437
# This code is supporting material for the book # Building Machine Learning Systems with Python # by Willi Richert and Luis Pedro Coelho # published by PACKT Publishing # # It is made available under the MIT License # Basic imports from __future__ import print_function import numpy as np from matplotlib import pyplot as plt from load import load_dataset from sklearn.neighbors import KNeighborsClassifier from sklearn.cross_validation import cross_val_score from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler features, labels = load_dataset('seeds') # Values of k to consider: all in 1 .. 160 ks = np.arange(1,161) # We build a classifier object here with the default number of neighbors # (It happens to be 5, but it does not matter as we will be changing it below classifier = KNeighborsClassifier() classifier = Pipeline([('norm', StandardScaler()), ('knn', classifier)]) # accuracies will hold our results accuracies = [] for k in ks: # set the classifier parameter classifier.set_params(knn__n_neighbors=k) crossed = cross_val_score(classifier, features, labels) # Save only the average accuracies.append(crossed.mean()) accuracies = np.array(accuracies) # Scale the accuracies by 100 to plot as a percentage instead of as a fraction plt.plot(ks, accuracies*100) plt.xlabel('Value for k (nr. of neighbors)') plt.ylabel('Accuracy (%)') plt.savefig('figure6.png')
mit
SyntaxVoid/PyFusionGUI
pyfusion/examples/wid_specgram.py
1
20425
""" Browse spectrograms, and has built in test function and crude window inspector. Originally was test code to develop shot incrementing widget-like interface David suggests Qt is better for interactive use Advantage of this simple version is it is toolkit independent. See comments in code. usage: run pyfusion/examples/wid_specgram.py shot_number=27233 diag_name='MP_SMALL' run pyfusion/examples/wid_specgram.py NFFT=2048 shot_number=69270 diag_name='H1DTacqAxial' dev_name='H1Local' shot_list=[69270] channel_number=2 Version 6: Add flucstruc overplot and control for size of circles Version 5: Works nicely in ipython, two pulldowns, one for history, one for shot selector, and selector wild card(non-blocking). The history list only includes shots successfully found, and either list can be navigated in order by two down (or up) arrows, then <CR>. Now with balloon help. Notes for pyfusion v1 version: flucstruc overplot commented out tricky options for shot selector list not implemented - just shot_list show signals button not yet implemented Needs process_cmd_line_args.py, in python path (e.g. ~/python dir) """ """ Comments on code development: Notes: Need to include channel selector - hardwired to mirnov 8 at the moment. Initially will use radio button channel selector - one at a time. Ultimate plan is to include an SQL query box to narrow range of shots Should really try to modularise this Display updates are 90% right now - still one step behind in adjusting FFT params Initial addition of Tix interface, creates a shot "box" with pulldowns Tix can be tested using test_Tix_widgets.py (boyd) Only successful with tkagg - otherwise blocks or needs root.mainloop() With gtkagg, sort of works, but doesn't update shot, and blocks """ from matplotlib.widgets import RadioButtons, Button import pylab as pl from numpy import sin, pi, ones, hanning, hamming, bartlett, kaiser, arange, blackman, cos, sqrt, log10, fft import pyfusion try: pyfusion.VERBOSE except: pyfusion.VERBOSE=int(pyfusion.config.get( 'global', 'verbose',vars={'verbose': '1'})) # local definitions for a few windows. mlab.windows are defined # differently, and the returned value is multiplied by the input data # already. There are only two of the mlab.window_hanning and # mlab.window_none. However, to be useful to David's function, they # need to be exported I think. def local_none(vec): return(ones(len(vec))) def local_hanning(vec): return(hanning(len(vec))) def local_hamming(vec): return(hamming(len(vec))) def local_blackman(vec): return(blackman(len(vec))) def local_bartlett(vec): return(bartlett(len(vec))) # not sure about this, but it is pretty right. def local_kaiser3(vec): return(kaiser(len(vec),3*pi)) def local_wider(vec): """ Flat top in middle, cos at edges - meant to be narrower in f but not as good in the wings """ N=len(vec) k=arange(N) w = sqrt(sqrt(1 - cos(2*pi*k/(N-1)))) # w = (1 - 1.93*cos(2*pi*k/(N-1)) + 1.29*cos(4*pi*k/(N-1)) # -0.388*cos(6*pi*k/(N-1)) +0.032*cos(8*pi*k/(N-1))) return(w) def local_flat_top_freq(vec): N=len(vec) k=arange(N) w = (1 - 1.93*cos(2*pi*k/(N-1)) + 1.29*cos(4*pi*k/(N-1)) -0.388*cos(6*pi*k/(N-1)) +0.032*cos(8*pi*k/(N-1))) return(w) global shot_number, shot_list, channel_number, chan_name, marker_size, wild_card, diag_name def get_local_shot_numbers(partial_name): """ This used to be in utils. For now replace by shot_list variable Probably good to keep for locals, and systems that have an easily accessible shot list. But how to deal with a list of 100,000 shots? """ global shot_list return(shot_list) # defaults wild_card = '' dev_name= 'LHD' # 'H1Local' shot_number=None channel_number=None diag_name="" shot_list=[] cmap=None #xextent=None # was here, really belongs in data.spectrogram NFFT=512 Fsamp=2 Fcentre=0 marker_size=0 detrend=pl.detrend_none _window = local_wider # none causes math errors in specgram sometimes foverlap=0.75 # 0 is the cheapest, but 3/4 looks better _type='F' fmod=0 # t_max=0.08 execfile('process_cmd_line_args.py') device = pyfusion.getDevice(dev_name) chan_name='' if dev_name=='TestDevice': chan_name='testch1' shot_number=1000 elif (dev_name=='H1') or(dev_name=='H1Local'): chan_name='mirnov_1_8' shot_number=58123 elif dev_name=='HeliotronJ': chan_name='MP1' shot_number=33911 elif dev_name=='LHD': shot_list = [27233,36150,90091] diag_name='MP_SMALL' chan_name='HMP01' shot_number=90091 elif dev_name=='JT60U': chan_name='PMP01' shot_number=46066 elif dev_name=='TJII': chan_name='mirnov_5p_105' shot_number=18991 else: # need something so process_cmd can work chan_name='' shot_number=0 if wild_card == '': wild_card = chan_name+'*' if pyfusion.VERBOSE>2: print("Using device '%s', chan_name '%s', shot_number %d" % (dev_name, chan_name, shot_number)) if channel_number==None: channel_number=0 # tweak above parameters according to command line args execfile('process_cmd_line_args.py') # arrays for test signal tm=arange(0,0.02,1e-6) y=sin((2e5 + 5e3*sin(fmod*2*pi*tm))*2*pi*tm) def call_spec(): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod, chan_name, diag_name print len(y), NFFT,foverlap, _type, fmod ax = pl.subplot(111) z=_window(y) if _type=='F': shot=callback.get_shot() print("shot=%d") % shot data = device.acq.getdata(shot, diag_name) if chan_name=='': try: ch=data.channels print("Choosing from", [chn.name for chn in ch]) name=ch[channel_number].name except: print "Failed to open channel database - try mirnov_1_8" name='mirnov_1_8' name='mirnov_linear_2' else: name=chan_name # data = pyfusion.load_channel(shot,name) # data = pyfusion.acq.getdata(shot_number, diag_name) if data==None: return(False) if _window==local_none: windowfn=pl.window_none # else: windowfn=pl.window_hanning elif _window==local_hanning: windowfn=pl.window_hanning else: windowfn=_window(arange(NFFT)) clim=(-60,20) # eventually make this adjustable # colorbar commented out because it keeps adding itself data.plot_spectrogram(NFFT=NFFT, windowfn=windowfn, noverlap=foverlap*NFFT, channel_number=channel_number) # colorbar=True, clim=clim) # colorbar() # used to come up on a separate page, fixed, but a little clunky - leave for now return(True) elif _type == 'T': # some matplotlib versions don't know about Fc pl.specgram(z*y, NFFT=NFFT, Fs=Fsamp, detrend=detrend, # window = _window noverlap=foverlap*NFFT, cmap=cmap) elif _type == 'L': pl.plot(20*log10(abs(fft.fft(y*z)))) elif _type == 'W': pl.plot(z) elif _type =='C': pl.plot(hold=0) else: raise ' unknown plot type "' + _type +'"' # pl.show() # ------ END of call_spec oldinter = pl.isinteractive pl.ioff() ax = pl.subplot(111) pl.subplots_adjust(left=0.25) pl.subplots_adjust(right=0.95) # see also the colorbar params in core.py #call_spec() #Buttons Start Here bxl=0.02 bw=0.12 # width (for most) axcolor = 'lightgoldenrodyellow' #define the box where the buttons live rax = pl.axes([bxl, 0.87, bxl+bw, 0.11], axisbg=axcolor) radio = RadioButtons(rax, ('no marker', '40', '80', '120'),active=0) def msfunc(label): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod, marker_size msdict = {'no marker':0, '40':40, '80':80, '120':120} marker_size = msdict[label] print("marker_size", marker_size) callback.redraw() # really should add markers here! (this is a call without getting new data) radio.on_clicked(msfunc) rax = pl.axes([bxl, 0.68, bxl+bw, 0.18], axisbg=axcolor) radio = RadioButtons(rax, ('win 128', '256', '512', '1024','2048','4096'),active=2) def hzfunc(label): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod hzdict = {'win 128':128, '256':256, '512':512, '1024':1024, '2048':2048, '4096':4096} NFFT = hzdict[label] call_spec() radio.on_clicked(hzfunc) rax = pl.axes([bxl, 0.48, bxl+bw, 0.19], axisbg=axcolor) radio = RadioButtons(rax, ('overlap 0', '1/4', '1/2', '3/4','7/8','15/16'),active=3) def ovlfunc(label): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod ovldict = {'overlap 0':0, '1/4':0.25, '1/2':0.5, '3/4':0.75, '7/8':0.875, '15/16':0.9375} foverlap = ovldict[label] call_spec() radio.on_clicked(ovlfunc) rax = pl.axes([bxl, 0.23, bxl+bw, 0.24], axisbg=axcolor) radio = RadioButtons(rax, ('no window', 'Wider', 'Bartlett','Hamming', 'Hanning', 'Blackman', 'Kaiser3','Flat-top-F'), active=1) def winfunc(label): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod windict = {'no window':local_none, 'Hanning':local_hanning, 'Wider': local_wider, 'Hamming':local_hamming, 'Blackman':local_blackman, 'Bartlett':local_bartlett, 'Kaiser3':local_kaiser3, 'Flat-top-F':local_flat_top_freq} _window = windict[label] call_spec() radio.on_clicked(winfunc) rax = pl.axes([bxl, 0.08, bxl+bw, 0.14], axisbg=axcolor) radio = RadioButtons(rax, ('f-t plot', 'test data', 'log-spect', 'window', 'clear')) def typfunc(label): global y,NFFT,Fsamp,Fcentre,foverlap,detrend,_window, _type, fmod typdict = {'f-t plot':'F', 'test data':'T', 'log-spect':'L', 'window':'W', 'clear':'C'} _type = typdict[label] call_spec() radio.on_clicked(typfunc) ############################################################## # This line is where I joined the radio button code to the shot number code # Would be nice to pull this apart into two modules and a short script. ############################################################### # #ax = subplot(111) # #subplots_adjust(left=0.3) x0=0 y0=0.02 try: import Tix HaveTix=True except: print("Tix module not available: shot button inactive") HaveTix=False # before putting these in a module, check that exec works on module vars def make_inherited_var(name): exec('val='+name) return("%s=%s" % (name, val)) def inherited_vars(vars=None, extras=None): """ Return a list of var=value suitable forprocess_cmd_line_args.py vars defaults to a safe, comprehensive set from pyfusion.settings extras as those particular to some routines. """ if vars == None: vars=['pyfusion.settings.SHOT_T_MIN', 'pyfusion.settings.SHOT_T_MAX'] lst = [] if extras != None: for var in extras: lst += [var] for var in vars: lst += [make_inherited_var(var)] return(lst) class IntegerCtl: """ provides an environment for button on_clicked functions to share variables with each other and plotting routines, rather than trying to access everythin back through the events passed. """ # these maybe should be in an init, but this is OK python code. global shot_number shot=shot_number def set_shot(s): shots def get_shot(self): return(self.shot) # probably need to redraw the whole graph def redraw(self): global hist_box, HaveTix, marker_size bshot.label.set_text(str(self.shot)) status=call_spec() if HaveTix: # update shot field in either case, only update history if good # this updates hist_box if the shot was changed by the other (matplotlib) widgets hist_box.set_silent(str(self.shot)) if status==True: hist_box.add_history(str(self.shot)) print("marker_size", marker_size) # if marker_size>0: plot_flucstrucs_for_shot(self.shot, size_factor=marker_size, savefile='') # pl.draw() # what does this do? return(status) # False if no data def frew(self, event): self.shot -= 10 self.redraw() def rew(self, event): self.shot -= 1 self.redraw() def fwd(self, event): self.shot += 1 self.redraw() def ffwd(self, event): self.shot += 10 self.redraw() # extra fast fwd is 100+ def Xffwd(self, event): self.shot += 100 self.redraw() def Xfrew(self, event): self.shot -= 100 self.redraw() def wid_specgram(self, event): import os args = ['python', 'examples/Boyds/wid_specgram.py'] args += inherited_vars() # need to pass a string array otherwise treated as array of chars! args += [str('shot_number=%d' % (self.shot))] if pyfusion.VERBOSE>5: print("args to spawn", args) print("") # so we can see the output os.spawnvp(os.P_NOWAIT,'python', args) self.redraw() def wid_showsigs(self, event): import os args = ['python', 'examples/Boyds/wid_showsigs.py'] args += inherited_vars() # need to pass a string array otherwise treated as array of chars! args += [str('shot_number=%d' % (self.shot))] if pyfusion.VERBOSE>5: print("args to spawn", args) print("") # so we can see the output os.spawnvp(os.P_NOWAIT,'python', args) self.redraw() #-------- End of class IntegerCtl: if HaveTix: ## This is intialization code, even though indented (it is conditional) # from pyfusion.utils import get_local_shot_numbers # from pyfusion.datamining.clustering.plots import plot_flucstrucs_for_shot print('Special import for HaveTix') global shot_string, select_string, do_shot, hist_box, wild_box, select_list, select_box, wild_string # this has to be before Tix.StringVar() is called in ubuntu 10.04 root=Tix.Tk(className='ShotSelect') select_list=[] wild_string= Tix.StringVar() shot_string= Tix.StringVar() select_string= Tix.StringVar() def do_shot(sstr=None): print('sstr=', sstr) if sstr == '': print('No shot defined - is this a blocking problem? - default to 1') sstr='1' callback.shot=int(sstr) if callback.redraw(): print('success') else: print('error') def update_select(partial_name=None): global select_box, select_list, wild_box print("update select, but doesn't work?") # put the name in the wildcard entry area in case we are called from other than the command wild_box.set_silent(partial_name) if partial_name.find('(') >= 0: # a function call print('executing' + partial_name) # does this exec in the local context? exec('select_list='+partial_name) elif partial_name.find('/') >= 0: # a file list # run the file through an awk filter to get the first "word" # on each line, which should be a shot number pass elif partial_name.find('*') >= 0: # really should use a regexp, but then have to get the number part select_list=get_local_shot_numbers( partial_name=string.strip(partial_name,'*')) # get a new list if len(select_list)==0: select_list= ['none?'] # put 'none there if it failed else: for s in select_list: select_box.insert(Tix.END, s) # put in widget list if OK select_box.set_silent(select_list[0]) # and put the top entry in the window def clear_select(): # doesn't work! global select_list select_list=['none'] update_select('') print ('clear', len(select_list)) def ShotWid(): """ this simple widget accepts a shot and sets the current one It is a function in the IntegerCtl class, so it communicates with its vars easily and calls do_shot to update the shot. THe shot pulldown stops working in python (ordinary) after 1 pulldown? """ global hist_box, select_box, wild_box # root=Tix.Tk(className='ShotSelect') # was here but needs to # be in effect before Tix.StringVar() is called top = Tix.Frame(root, bd=1, relief=Tix.RAISED) hist_box=Tix.ComboBox(top, label="Shot", editable=True, history=True, variable=shot_string, command=do_shot, options='entry.width 8 listbox.height 10 ') hist_box.pack(side=Tix.TOP, anchor=Tix.W) hist_box.set_silent('33373') hist_balloon=Tix.Balloon(top) hist_balloon.bind_widget(hist_box, balloonmsg='Choose or enter shot number, valid ones are saved here') wild_box=Tix.ComboBox(top, label="Filter", editable=1, history=1, variable=wild_string, command=update_select, options='entry.width 20 listbox.height 5 ') # allow room for expressions wild_box.pack(side=Tix.TOP, anchor=Tix.W) wild_balloon=Tix.Balloon(top) wild_balloon.bind_widget(wild_box, balloonmsg='Choose or enter new filter in one of three forms,' + 'a Python expression (must have () or []), '+ 'a directory specification including a * or ' + 'the name of a file containing lines beginning with a shot number. ' 'Results can be chosen using "Filtered Shots"') select_box=Tix.ComboBox(top, label="Filtered Shots", history=False, variable=select_string, command=do_shot, options='entry.width 8 listbox.height 40 ') btn = Tix.Button(select_box, text='Clear',command=clear_select) btn.pack(anchor=Tix.CENTER) select_box.pack(side=Tix.TOP, anchor=Tix.W) select_balloon=Tix.Balloon(top) select_balloon.bind_widget(select_box, balloonmsg='pull down to find a shot selected by "Filter""') #wild_box.set_silent('MP1') # not silent - want it all to happen, but setvar doesn't work update_select(partial_name=wild_card) top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1) # no need in pylab provided tkagg is used root.mainloop() # in fact, may conflict and block - hard to sort out what blocks and when, why callback = IntegerCtl() but_h = 0.045 global button_layout_cursor def mybut(text, dummy, xl, yb, xw=0, yh=0, axisbg=None, color=0.85, fun=None, bspace=0.005): """ create axes and populate button with text, automatically adjusting xw if not given. Has a side effect on xl. (button_layout_cursor) dummy is for if and when I can place these on an obect rather than using pylab """ if axisbg==None: axisbg='lightgoldenrodyellow' global button_layout_cursor if xw==0: xw=0.015*(len(text)+1) if yh==0: yh=0.05 ## thisax=fig.add_axes([xl, yb, xw, yh], axisbg=axisbg) fundamentally wrong thisax=pl.axes([xl, yb, xw, yh], axisbg=axisbg) thisbut=Button(thisax, text) thisbut.on_clicked(fun) button_layout_cursor += xw+bspace return(thisbut) button_layout_cursor=0.01 fig=0 spectest=mybut('specgram', fig, button_layout_cursor, y0, 0, but_h, fun=callback.wid_specgram, axisbg='yellow') sigstest=mybut('showsigs', fig, button_layout_cursor, y0, 0, but_h, fun=callback.wid_showsigs) bXfrew=mybut('<<<', fig, button_layout_cursor, y0, 0, but_h, fun=callback.Xfrew) bfrew=mybut('<<', fig, button_layout_cursor, y0, 0, but_h, fun=callback.frew) brew=mybut('<', fig, button_layout_cursor, y0, 0, but_h, fun=callback.rew) bshot=mybut('12345', fig, button_layout_cursor, y0, 0, but_h, fun=callback.shot) bfwd=mybut('>', fig, button_layout_cursor, y0, 0, but_h, fun=callback.fwd) bffwd=mybut('>>', fig, button_layout_cursor, y0, 0, but_h, fun=callback.ffwd) bXffwd=mybut('>>>', fig, button_layout_cursor, y0, 0, but_h, fun=callback.Xffwd) if oldinter: pl.ion() # this is sort of initialisation code, but needed to be in a function if HaveTix: ShotWid() callback.redraw() pl.show()
gpl-3.0
richrr/coremicro
src/generate_graph.py
1
4736
# Copyright 2016, 2017 Richard Rodrigues, Nyle Rodgers, Mark Williams, # Virginia Tech # # This file is part of Coremic. # # Coremic is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Coremic is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Coremic. If not, see <http://www.gnu.org/licenses/>. import StringIO import numpy import logging from collections import namedtuple import base64 from core.parse_inputs import samples import web_config # matplotlib can't be run on the development server if web_config.IS_PRODUCTION: import matplotlib.pyplot as plt # Namedtuple to hold statistics calculated for otus Stats = namedtuple('Stats', ['otu', 'i_average', 'i_frequency', 'i_error', 'o_average', 'o_frequency', 'o_error']) def generate_graph(inputs, cfg, results): attachments = list() otus = [res['otu'] for res in results] if len(otus) == 0: return attachments stats, i_samples, o_samples = get_stats(inputs, otus, cfg['group'], cfg['out_group'], cfg['min_abundance']) # Sort everything by decreasing average presence in interest group stats = list(reversed(sorted( stats, cmp=lambda a, b: cmp(a.i_average, b.i_average) ))) if web_config.IS_PRODUCTION: attachments.append({ 'Content-Type': 'image/svg+xml', 'Filename': '%s_plot_%s.svg' % (cfg['name'], cfg['run_name']), 'content': base64.b64encode(make_graph(stats, cfg['group_name'], cfg['out_group_name'])) }) ref_text = 'ID\tInterest Frequency\tOut Frequency\tOTU\n' for i in range(len(otus)): ref_text += '%s\t%d of %d\t%d of %d\t%s\n' % ( i, stats[i].i_frequency, i_samples, stats[i].o_frequency, o_samples, stats[i].otu) attachments.append({ 'Content-Type': 'text/tab-separated-values', 'Filename': '%s_plot_labels_%s.tsv' % (cfg['name'], cfg['run_name']), 'content': base64.b64encode(ref_text) }) if not web_config.IS_PRODUCTION: logging.warn('Graphs not generated because in development mode') return attachments def get_stats(inputs, otus, i_group, o_group, min_abundance): core = inputs['filtered_data'].filterObservations( lambda values, id, md: id in otus ) interest = core.filterSamples( lambda values, id, md: id in samples(inputs['mapping_dict'], i_group) ) out = core.filterSamples( lambda values, id, md: id in samples(inputs['mapping_dict'], o_group) ) res = list() for ((i_vals, i_otu, i_md), (o_vals, o_id, o_md)) in zip(interest.iterObservations(), out.iterObservations()): res.append(Stats(i_otu, numpy.mean(i_vals), sum([v > min_abundance for v in i_vals]), standard_error(i_vals), numpy.mean(o_vals), sum([v > min_abundance for v in o_vals]), standard_error(o_vals))) i_samples = len(interest.SampleIds) o_samples = len(out.SampleIds) return res, i_samples, o_samples def standard_error(a): return numpy.std(a, ddof=1)/numpy.sqrt(len(a)) def make_graph(stats, i_group_name, o_group_name): width = 0.35 ind = [i + width/2 for i in range(len(stats))] interest = plt.bar(ind, [s.i_average for s in stats], width, color='r', yerr=[s.i_error for s in stats]) out = plt.bar([i + width for i in ind], [s.o_average for o in stats], width, color='y', yerr=[s.o_error for s in stats]) plt.ylabel('Average Abundance') plt.xlabel('Sample ID') plt.xticks([i + width for i in ind], range(len(stats))) plt.legend((interest[0], out[0]), (i_group_name, o_group_name)) plt.title('Abundance of Core Microbes') out = StringIO.StringIO() plt.savefig(out, format='svg') plt.clf() return out.getvalue()
gpl-3.0
ZenDevelopmentSystems/scikit-learn
sklearn/ensemble/forest.py
176
62555
"""Forest of trees-based ensemble methods Those methods include random forests and extremely randomized trees. The module structure is the following: - The ``BaseForest`` base class implements a common ``fit`` method for all the estimators in the module. The ``fit`` method of the base ``Forest`` class calls the ``fit`` method of each sub-estimator on random samples (with replacement, a.k.a. bootstrap) of the training set. The init of the sub-estimator is further delegated to the ``BaseEnsemble`` constructor. - The ``ForestClassifier`` and ``ForestRegressor`` base classes further implement the prediction logic by computing an average of the predicted outcomes of the sub-estimators. - The ``RandomForestClassifier`` and ``RandomForestRegressor`` derived classes provide the user with concrete implementations of the forest ensemble method using classical, deterministic ``DecisionTreeClassifier`` and ``DecisionTreeRegressor`` as sub-estimator implementations. - The ``ExtraTreesClassifier`` and ``ExtraTreesRegressor`` derived classes provide the user with concrete implementations of the forest ensemble method using the extremely randomized trees ``ExtraTreeClassifier`` and ``ExtraTreeRegressor`` as sub-estimator implementations. Single and multi-output problems are both handled. """ # Authors: Gilles Louppe <[email protected]> # Brian Holt <[email protected]> # Joly Arnaud <[email protected]> # Fares Hedayati <[email protected]> # # License: BSD 3 clause from __future__ import division import warnings from warnings import warn from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import issparse from ..base import ClassifierMixin, RegressorMixin from ..externals.joblib import Parallel, delayed from ..externals import six from ..feature_selection.from_model import _LearntSelectorMixin from ..metrics import r2_score from ..preprocessing import OneHotEncoder from ..tree import (DecisionTreeClassifier, DecisionTreeRegressor, ExtraTreeClassifier, ExtraTreeRegressor) from ..tree._tree import DTYPE, DOUBLE from ..utils import check_random_state, check_array, compute_sample_weight from ..utils.validation import DataConversionWarning, NotFittedError from .base import BaseEnsemble, _partition_estimators from ..utils.fixes import bincount __all__ = ["RandomForestClassifier", "RandomForestRegressor", "ExtraTreesClassifier", "ExtraTreesRegressor", "RandomTreesEmbedding"] MAX_INT = np.iinfo(np.int32).max def _generate_sample_indices(random_state, n_samples): """Private function used to _parallel_build_trees function.""" random_instance = check_random_state(random_state) sample_indices = random_instance.randint(0, n_samples, n_samples) return sample_indices def _generate_unsampled_indices(random_state, n_samples): """Private function used to forest._set_oob_score fuction.""" sample_indices = _generate_sample_indices(random_state, n_samples) sample_counts = bincount(sample_indices, minlength=n_samples) unsampled_mask = sample_counts == 0 indices_range = np.arange(n_samples) unsampled_indices = indices_range[unsampled_mask] return unsampled_indices def _parallel_build_trees(tree, forest, X, y, sample_weight, tree_idx, n_trees, verbose=0, class_weight=None): """Private function used to fit a single tree in parallel.""" if verbose > 1: print("building tree %d of %d" % (tree_idx + 1, n_trees)) if forest.bootstrap: n_samples = X.shape[0] if sample_weight is None: curr_sample_weight = np.ones((n_samples,), dtype=np.float64) else: curr_sample_weight = sample_weight.copy() indices = _generate_sample_indices(tree.random_state, n_samples) sample_counts = bincount(indices, minlength=n_samples) curr_sample_weight *= sample_counts if class_weight == 'subsample': with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) curr_sample_weight *= compute_sample_weight('auto', y, indices) elif class_weight == 'balanced_subsample': curr_sample_weight *= compute_sample_weight('balanced', y, indices) tree.fit(X, y, sample_weight=curr_sample_weight, check_input=False) else: tree.fit(X, y, sample_weight=sample_weight, check_input=False) return tree def _parallel_helper(obj, methodname, *args, **kwargs): """Private helper to workaround Python 2 pickle limitations""" return getattr(obj, methodname)(*args, **kwargs) class BaseForest(six.with_metaclass(ABCMeta, BaseEnsemble, _LearntSelectorMixin)): """Base class for forests of trees. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(BaseForest, self).__init__( base_estimator=base_estimator, n_estimators=n_estimators, estimator_params=estimator_params) self.bootstrap = bootstrap self.oob_score = oob_score self.n_jobs = n_jobs self.random_state = random_state self.verbose = verbose self.warm_start = warm_start self.class_weight = class_weight def apply(self, X): """Apply trees in the forest to X, return leaf indices. Parameters ---------- X : array-like or sparse matrix, shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns ------- X_leaves : array_like, shape = [n_samples, n_estimators] For each datapoint x in X and for each tree in the forest, return the index of the leaf x ends up in. """ X = self._validate_X_predict(X) results = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")( delayed(_parallel_helper)(tree, 'apply', X, check_input=False) for tree in self.estimators_) return np.array(results).T def fit(self, X, y, sample_weight=None): """Build a forest of trees from the training set (X, y). Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The training input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csc_matrix``. y : array-like, shape = [n_samples] or [n_samples, n_outputs] The target values (class labels in classification, real numbers in regression). sample_weight : array-like, shape = [n_samples] or None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. In the case of classification, splits are also ignored if they would result in any single class carrying a negative weight in either child node. Returns ------- self : object Returns self. """ # Validate or convert input data X = check_array(X, dtype=DTYPE, accept_sparse="csc") if issparse(X): # Pre-sort indices to avoid that each individual tree of the # ensemble sorts the indices. X.sort_indices() # Remap output n_samples, self.n_features_ = X.shape y = np.atleast_1d(y) if y.ndim == 2 and y.shape[1] == 1: warn("A column-vector y was passed when a 1d array was" " expected. Please change the shape of y to " "(n_samples,), for example using ravel().", DataConversionWarning, stacklevel=2) if y.ndim == 1: # reshape is necessary to preserve the data contiguity against vs # [:, np.newaxis] that does not. y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] y, expanded_class_weight = self._validate_y_class_weight(y) if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous: y = np.ascontiguousarray(y, dtype=DOUBLE) if expanded_class_weight is not None: if sample_weight is not None: sample_weight = sample_weight * expanded_class_weight else: sample_weight = expanded_class_weight # Check parameters self._validate_estimator() if not self.bootstrap and self.oob_score: raise ValueError("Out of bag estimation only available" " if bootstrap=True") random_state = check_random_state(self.random_state) if not self.warm_start: # Free allocated memory, if any self.estimators_ = [] n_more_estimators = self.n_estimators - len(self.estimators_) if n_more_estimators < 0: raise ValueError('n_estimators=%d must be larger or equal to ' 'len(estimators_)=%d when warm_start==True' % (self.n_estimators, len(self.estimators_))) elif n_more_estimators == 0: warn("Warm-start fitting without increasing n_estimators does not " "fit new trees.") else: if self.warm_start and len(self.estimators_) > 0: # We draw from the random state to get the random state we # would have got if we hadn't used a warm_start. random_state.randint(MAX_INT, size=len(self.estimators_)) trees = [] for i in range(n_more_estimators): tree = self._make_estimator(append=False) tree.set_params(random_state=random_state.randint(MAX_INT)) trees.append(tree) # Parallel loop: we use the threading backend as the Cython code # for fitting the trees is internally releasing the Python GIL # making threading always more efficient than multiprocessing in # that case. trees = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")( delayed(_parallel_build_trees)( t, self, X, y, sample_weight, i, len(trees), verbose=self.verbose, class_weight=self.class_weight) for i, t in enumerate(trees)) # Collect newly grown trees self.estimators_.extend(trees) if self.oob_score: self._set_oob_score(X, y) # Decapsulate classes_ attributes if hasattr(self, "classes_") and self.n_outputs_ == 1: self.n_classes_ = self.n_classes_[0] self.classes_ = self.classes_[0] return self @abstractmethod def _set_oob_score(self, X, y): """Calculate out of bag predictions and score.""" def _validate_y_class_weight(self, y): # Default implementation return y, None def _validate_X_predict(self, X): """Validate X whenever one tries to predict, apply, predict_proba""" if self.estimators_ is None or len(self.estimators_) == 0: raise NotFittedError("Estimator not fitted, " "call `fit` before exploiting the model.") return self.estimators_[0]._validate_X_predict(X, check_input=True) @property def feature_importances_(self): """Return the feature importances (the higher, the more important the feature). Returns ------- feature_importances_ : array, shape = [n_features] """ if self.estimators_ is None or len(self.estimators_) == 0: raise NotFittedError("Estimator not fitted, " "call `fit` before `feature_importances_`.") all_importances = Parallel(n_jobs=self.n_jobs, backend="threading")( delayed(getattr)(tree, 'feature_importances_') for tree in self.estimators_) return sum(all_importances) / len(self.estimators_) class ForestClassifier(six.with_metaclass(ABCMeta, BaseForest, ClassifierMixin)): """Base class for forest of trees-based classifiers. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(ForestClassifier, self).__init__( base_estimator, n_estimators=n_estimators, estimator_params=estimator_params, bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) def _set_oob_score(self, X, y): """Compute out-of-bag score""" X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_classes_ = self.n_classes_ n_samples = y.shape[0] oob_decision_function = [] oob_score = 0.0 predictions = [] for k in range(self.n_outputs_): predictions.append(np.zeros((n_samples, n_classes_[k]))) for estimator in self.estimators_: unsampled_indices = _generate_unsampled_indices( estimator.random_state, n_samples) p_estimator = estimator.predict_proba(X[unsampled_indices, :], check_input=False) if self.n_outputs_ == 1: p_estimator = [p_estimator] for k in range(self.n_outputs_): predictions[k][unsampled_indices, :] += p_estimator[k] for k in range(self.n_outputs_): if (predictions[k].sum(axis=1) == 0).any(): warn("Some inputs do not have OOB scores. " "This probably means too few trees were used " "to compute any reliable oob estimates.") decision = (predictions[k] / predictions[k].sum(axis=1)[:, np.newaxis]) oob_decision_function.append(decision) oob_score += np.mean(y[:, k] == np.argmax(predictions[k], axis=1), axis=0) if self.n_outputs_ == 1: self.oob_decision_function_ = oob_decision_function[0] else: self.oob_decision_function_ = oob_decision_function self.oob_score_ = oob_score / self.n_outputs_ def _validate_y_class_weight(self, y): y = np.copy(y) expanded_class_weight = None if self.class_weight is not None: y_original = np.copy(y) self.classes_ = [] self.n_classes_ = [] y_store_unique_indices = np.zeros(y.shape, dtype=np.int) for k in range(self.n_outputs_): classes_k, y_store_unique_indices[:, k] = np.unique(y[:, k], return_inverse=True) self.classes_.append(classes_k) self.n_classes_.append(classes_k.shape[0]) y = y_store_unique_indices if self.class_weight is not None: valid_presets = ('auto', 'balanced', 'balanced_subsample', 'subsample', 'auto') if isinstance(self.class_weight, six.string_types): if self.class_weight not in valid_presets: raise ValueError('Valid presets for class_weight include ' '"balanced" and "balanced_subsample". Given "%s".' % self.class_weight) if self.class_weight == "subsample": warn("class_weight='subsample' is deprecated and will be removed in 0.18." " It was replaced by class_weight='balanced_subsample' " "using the balanced strategy.", DeprecationWarning) if self.warm_start: warn('class_weight presets "balanced" or "balanced_subsample" are ' 'not recommended for warm_start if the fitted data ' 'differs from the full dataset. In order to use ' '"balanced" weights, use compute_class_weight("balanced", ' 'classes, y). In place of y you can use a large ' 'enough sample of the full training set target to ' 'properly estimate the class frequency ' 'distributions. Pass the resulting weights as the ' 'class_weight parameter.') if (self.class_weight not in ['subsample', 'balanced_subsample'] or not self.bootstrap): if self.class_weight == 'subsample': class_weight = 'auto' elif self.class_weight == "balanced_subsample": class_weight = "balanced" else: class_weight = self.class_weight with warnings.catch_warnings(): if class_weight == "auto": warnings.simplefilter('ignore', DeprecationWarning) expanded_class_weight = compute_sample_weight(class_weight, y_original) return y, expanded_class_weight def predict(self, X): """Predict class for X. The predicted class of an input sample is a vote by the trees in the forest, weighted by their probability estimates. That is, the predicted class is the one with highest mean probability estimate across the trees. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns ------- y : array of shape = [n_samples] or [n_samples, n_outputs] The predicted classes. """ proba = self.predict_proba(X) if self.n_outputs_ == 1: return self.classes_.take(np.argmax(proba, axis=1), axis=0) else: n_samples = proba[0].shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) for k in range(self.n_outputs_): predictions[:, k] = self.classes_[k].take(np.argmax(proba[k], axis=1), axis=0) return predictions def predict_proba(self, X): """Predict class probabilities for X. The predicted class probabilities of an input sample is computed as the mean predicted class probabilities of the trees in the forest. The class probability of a single tree is the fraction of samples of the same class in a leaf. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns ------- p : array of shape = [n_samples, n_classes], or a list of n_outputs such arrays if n_outputs > 1. The class probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ # Check data X = self._validate_X_predict(X) # Assign chunk of trees to jobs n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) # Parallel loop all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( delayed(_parallel_helper)(e, 'predict_proba', X, check_input=False) for e in self.estimators_) # Reduce proba = all_proba[0] if self.n_outputs_ == 1: for j in range(1, len(all_proba)): proba += all_proba[j] proba /= len(self.estimators_) else: for j in range(1, len(all_proba)): for k in range(self.n_outputs_): proba[k] += all_proba[j][k] for k in range(self.n_outputs_): proba[k] /= self.n_estimators return proba def predict_log_proba(self, X): """Predict class log-probabilities for X. The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the trees in the forest. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns ------- p : array of shape = [n_samples, n_classes], or a list of n_outputs such arrays if n_outputs > 1. The class probabilities of the input samples. The order of the classes corresponds to that in the attribute `classes_`. """ proba = self.predict_proba(X) if self.n_outputs_ == 1: return np.log(proba) else: for k in range(self.n_outputs_): proba[k] = np.log(proba[k]) return proba class ForestRegressor(six.with_metaclass(ABCMeta, BaseForest, RegressorMixin)): """Base class for forest of trees-based regressors. Warning: This class should not be used directly. Use derived classes instead. """ @abstractmethod def __init__(self, base_estimator, n_estimators=10, estimator_params=tuple(), bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(ForestRegressor, self).__init__( base_estimator, n_estimators=n_estimators, estimator_params=estimator_params, bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) def predict(self, X): """Predict regression target for X. The predicted regression target of an input sample is computed as the mean predicted regression targets of the trees in the forest. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] The input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csr_matrix``. Returns ------- y : array of shape = [n_samples] or [n_samples, n_outputs] The predicted values. """ # Check data X = self._validate_X_predict(X) # Assign chunk of trees to jobs n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) # Parallel loop all_y_hat = Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( delayed(_parallel_helper)(e, 'predict', X, check_input=False) for e in self.estimators_) # Reduce y_hat = sum(all_y_hat) / len(self.estimators_) return y_hat def _set_oob_score(self, X, y): """Compute out-of-bag scores""" X = check_array(X, dtype=DTYPE, accept_sparse='csr') n_samples = y.shape[0] predictions = np.zeros((n_samples, self.n_outputs_)) n_predictions = np.zeros((n_samples, self.n_outputs_)) for estimator in self.estimators_: unsampled_indices = _generate_unsampled_indices( estimator.random_state, n_samples) p_estimator = estimator.predict( X[unsampled_indices, :], check_input=False) if self.n_outputs_ == 1: p_estimator = p_estimator[:, np.newaxis] predictions[unsampled_indices, :] += p_estimator n_predictions[unsampled_indices, :] += 1 if (n_predictions == 0).any(): warn("Some inputs do not have OOB scores. " "This probably means too few trees were used " "to compute any reliable oob estimates.") n_predictions[n_predictions == 0] = 1 predictions /= n_predictions self.oob_prediction_ = predictions if self.n_outputs_ == 1: self.oob_prediction_ = \ self.oob_prediction_.reshape((n_samples, )) self.oob_score_ = 0.0 for k in range(self.n_outputs_): self.oob_score_ += r2_score(y[:, k], predictions[:, k]) self.oob_score_ /= self.n_outputs_ class RandomForestClassifier(ForestClassifier): """A random forest classifier. A random forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if `bootstrap=True` (default). Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="gini") The function to measure the quality of a split. Supported criteria are "gini" for the Gini impurity and "entropy" for the information gain. Note: this parameter is tree-specific. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=sqrt(n_features)`. - If "sqrt", then `max_features=sqrt(n_features)` (same as "auto"). - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. Note: this parameter is tree-specific. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Ignored if ``max_leaf_nodes`` is not None. Note: this parameter is tree-specific. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. Note: this parameter is tree-specific. min_samples_leaf : integer, optional (default=1) The minimum number of samples in newly created leaves. A split is discarded if after the split, one of the leaves would contain less then ``min_samples_leaf`` samples. Note: this parameter is tree-specific. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. Note: this parameter is tree-specific. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. Note: this parameter is tree-specific. bootstrap : boolean, optional (default=True) Whether bootstrap samples are used when building trees. oob_score : bool Whether to use out-of-bag samples to estimate the generalization error. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. class_weight : dict, list of dicts, "balanced", "balanced_subsample" or None, optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` The "balanced_subsample" mode is the same as "balanced" except that weights are computed based on the bootstrap sample for every tree grown. For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. classes_ : array of shape = [n_classes] or a list of such arrays The classes labels (single output problem), or a list of arrays of class labels (multi-output problem). n_classes_ : int or list The number of classes (single output problem), or a list containing the number of classes for each output (multi-output problem). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_decision_function_ : array of shape = [n_samples, n_classes] Decision function computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, `oob_decision_function_` might contain NaN. References ---------- .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32, 2001. See also -------- DecisionTreeClassifier, ExtraTreesClassifier """ def __init__(self, n_estimators=10, criterion="gini", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(RandomForestClassifier, self).__init__( base_estimator=DecisionTreeClassifier(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes class RandomForestRegressor(ForestRegressor): """A random forest regressor. A random forest is a meta estimator that fits a number of classifying decision trees on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if `bootstrap=True` (default). Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="mse") The function to measure the quality of a split. The only supported criterion is "mse" for the mean squared error. Note: this parameter is tree-specific. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=n_features`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. Note: this parameter is tree-specific. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Ignored if ``max_leaf_nodes`` is not None. Note: this parameter is tree-specific. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. Note: this parameter is tree-specific. min_samples_leaf : integer, optional (default=1) The minimum number of samples in newly created leaves. A split is discarded if after the split, one of the leaves would contain less then ``min_samples_leaf`` samples. Note: this parameter is tree-specific. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. Note: this parameter is tree-specific. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. Note: this parameter is tree-specific. bootstrap : boolean, optional (default=True) Whether bootstrap samples are used when building trees. oob_score : bool whether to use out-of-bag samples to estimate the generalization error. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeRegressor The collection of fitted sub-estimators. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_prediction_ : array of shape = [n_samples] Prediction computed with out-of-bag estimate on the training set. References ---------- .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32, 2001. See also -------- DecisionTreeRegressor, ExtraTreesRegressor """ def __init__(self, n_estimators=10, criterion="mse", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, bootstrap=True, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(RandomForestRegressor, self).__init__( base_estimator=DecisionTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes class ExtraTreesClassifier(ForestClassifier): """An extra-trees classifier. This class implements a meta estimator that fits a number of randomized decision trees (a.k.a. extra-trees) on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="gini") The function to measure the quality of a split. Supported criteria are "gini" for the Gini impurity and "entropy" for the information gain. Note: this parameter is tree-specific. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=sqrt(n_features)`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. Note: this parameter is tree-specific. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Ignored if ``max_leaf_nodes`` is not None. Note: this parameter is tree-specific. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. Note: this parameter is tree-specific. min_samples_leaf : integer, optional (default=1) The minimum number of samples in newly created leaves. A split is discarded if after the split, one of the leaves would contain less then ``min_samples_leaf`` samples. Note: this parameter is tree-specific. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. Note: this parameter is tree-specific. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. Note: this parameter is tree-specific. bootstrap : boolean, optional (default=False) Whether bootstrap samples are used when building trees. oob_score : bool Whether to use out-of-bag samples to estimate the generalization error. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. class_weight : dict, list of dicts, "balanced", "balanced_subsample" or None, optional Weights associated with classes in the form ``{class_label: weight}``. If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` The "balanced_subsample" mode is the same as "balanced" except that weights are computed based on the bootstrap sample for every tree grown. For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. classes_ : array of shape = [n_classes] or a list of such arrays The classes labels (single output problem), or a list of arrays of class labels (multi-output problem). n_classes_ : int or list The number of classes (single output problem), or a list containing the number of classes for each output (multi-output problem). feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features when ``fit`` is performed. n_outputs_ : int The number of outputs when ``fit`` is performed. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_decision_function_ : array of shape = [n_samples, n_classes] Decision function computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, `oob_decision_function_` might contain NaN. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. See also -------- sklearn.tree.ExtraTreeClassifier : Base classifier for this ensemble. RandomForestClassifier : Ensemble Classifier based on trees with optimal splits. """ def __init__(self, n_estimators=10, criterion="gini", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False, class_weight=None): super(ExtraTreesClassifier, self).__init__( base_estimator=ExtraTreeClassifier(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start, class_weight=class_weight) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes class ExtraTreesRegressor(ForestRegressor): """An extra-trees regressor. This class implements a meta estimator that fits a number of randomized decision trees (a.k.a. extra-trees) on various sub-samples of the dataset and use averaging to improve the predictive accuracy and control over-fitting. Read more in the :ref:`User Guide <forest>`. Parameters ---------- n_estimators : integer, optional (default=10) The number of trees in the forest. criterion : string, optional (default="mse") The function to measure the quality of a split. The only supported criterion is "mse" for the mean squared error. Note: this parameter is tree-specific. max_features : int, float, string or None, optional (default="auto") The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a percentage and `int(max_features * n_features)` features are considered at each split. - If "auto", then `max_features=n_features`. - If "sqrt", then `max_features=sqrt(n_features)`. - If "log2", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. Note: this parameter is tree-specific. max_depth : integer or None, optional (default=None) The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Ignored if ``max_leaf_nodes`` is not None. Note: this parameter is tree-specific. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. Note: this parameter is tree-specific. min_samples_leaf : integer, optional (default=1) The minimum number of samples in newly created leaves. A split is discarded if after the split, one of the leaves would contain less then ``min_samples_leaf`` samples. Note: this parameter is tree-specific. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. Note: this parameter is tree-specific. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. Note: this parameter is tree-specific. bootstrap : boolean, optional (default=False) Whether bootstrap samples are used when building trees. Note: this parameter is tree-specific. oob_score : bool Whether to use out-of-bag samples to estimate the generalization error. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeRegressor The collection of fitted sub-estimators. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). n_features_ : int The number of features. n_outputs_ : int The number of outputs. oob_score_ : float Score of the training dataset obtained using an out-of-bag estimate. oob_prediction_ : array of shape = [n_samples] Prediction computed with out-of-bag estimate on the training set. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. See also -------- sklearn.tree.ExtraTreeRegressor: Base estimator for this ensemble. RandomForestRegressor: Ensemble regressor using trees with optimal splits. """ def __init__(self, n_estimators=10, criterion="mse", max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_features="auto", max_leaf_nodes=None, bootstrap=False, oob_score=False, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(ExtraTreesRegressor, self).__init__( base_estimator=ExtraTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "random_state"), bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = criterion self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = max_features self.max_leaf_nodes = max_leaf_nodes class RandomTreesEmbedding(BaseForest): """An ensemble of totally random trees. An unsupervised transformation of a dataset to a high-dimensional sparse representation. A datapoint is coded according to which leaf of each tree it is sorted into. Using a one-hot encoding of the leaves, this leads to a binary coding with as many ones as there are trees in the forest. The dimensionality of the resulting representation is ``n_out <= n_estimators * max_leaf_nodes``. If ``max_leaf_nodes == None``, the number of leaf nodes is at most ``n_estimators * 2 ** max_depth``. Read more in the :ref:`User Guide <random_trees_embedding>`. Parameters ---------- n_estimators : int Number of trees in the forest. max_depth : int The maximum depth of each tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. Ignored if ``max_leaf_nodes`` is not None. min_samples_split : integer, optional (default=2) The minimum number of samples required to split an internal node. min_samples_leaf : integer, optional (default=1) The minimum number of samples in newly created leaves. A split is discarded if after the split, one of the leaves would contain less then ``min_samples_leaf`` samples. min_weight_fraction_leaf : float, optional (default=0.) The minimum weighted fraction of the input samples required to be at a leaf node. max_leaf_nodes : int or None, optional (default=None) Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. If not None then ``max_depth`` will be ignored. sparse_output : bool, optional (default=True) Whether or not to return a sparse CSR matrix, as default behavior, or to return a dense array compatible with dense pipeline operators. n_jobs : integer, optional (default=1) The number of jobs to run in parallel for both `fit` and `predict`. If -1, then the number of jobs is set to the number of cores. random_state : int, RandomState instance or None, optional (default=None) If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by `np.random`. verbose : int, optional (default=0) Controls the verbosity of the tree building process. warm_start : bool, optional (default=False) When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. Attributes ---------- estimators_ : list of DecisionTreeClassifier The collection of fitted sub-estimators. References ---------- .. [1] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized trees", Machine Learning, 63(1), 3-42, 2006. .. [2] Moosmann, F. and Triggs, B. and Jurie, F. "Fast discriminative visual codebooks using randomized clustering forests" NIPS 2007 """ def __init__(self, n_estimators=10, max_depth=5, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0., max_leaf_nodes=None, sparse_output=True, n_jobs=1, random_state=None, verbose=0, warm_start=False): super(RandomTreesEmbedding, self).__init__( base_estimator=ExtraTreeRegressor(), n_estimators=n_estimators, estimator_params=("criterion", "max_depth", "min_samples_split", "min_samples_leaf", "min_weight_fraction_leaf", "max_features", "max_leaf_nodes", "random_state"), bootstrap=False, oob_score=False, n_jobs=n_jobs, random_state=random_state, verbose=verbose, warm_start=warm_start) self.criterion = 'mse' self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.min_weight_fraction_leaf = min_weight_fraction_leaf self.max_features = 1 self.max_leaf_nodes = max_leaf_nodes self.sparse_output = sparse_output def _set_oob_score(self, X, y): raise NotImplementedError("OOB score not supported by tree embedding") def fit(self, X, y=None, sample_weight=None): """Fit estimator. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) The input samples. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csc_matrix`` for maximum efficiency. Returns ------- self : object Returns self. """ self.fit_transform(X, y, sample_weight=sample_weight) return self def fit_transform(self, X, y=None, sample_weight=None): """Fit estimator and transform dataset. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) Input data used to build forests. Use ``dtype=np.float32`` for maximum efficiency. Returns ------- X_transformed : sparse matrix, shape=(n_samples, n_out) Transformed dataset. """ # ensure_2d=False because there are actually unit test checking we fail # for 1d. X = check_array(X, accept_sparse=['csc'], ensure_2d=False) if issparse(X): # Pre-sort indices to avoid that each individual tree of the # ensemble sorts the indices. X.sort_indices() rnd = check_random_state(self.random_state) y = rnd.uniform(size=X.shape[0]) super(RandomTreesEmbedding, self).fit(X, y, sample_weight=sample_weight) self.one_hot_encoder_ = OneHotEncoder(sparse=self.sparse_output) return self.one_hot_encoder_.fit_transform(self.apply(X)) def transform(self, X): """Transform dataset. Parameters ---------- X : array-like or sparse matrix, shape=(n_samples, n_features) Input data to be transformed. Use ``dtype=np.float32`` for maximum efficiency. Sparse matrices are also supported, use sparse ``csr_matrix`` for maximum efficiency. Returns ------- X_transformed : sparse matrix, shape=(n_samples, n_out) Transformed dataset. """ return self.one_hot_encoder_.transform(self.apply(X))
bsd-3-clause
Antiun/yelizariev-addons
import_custom/wizard/upload.py
16
1822
from openerp.osv import osv, fields from openerp.tools.translate import _ from openerp import tools import logging _logger = logging.getLogger(__name__) import base64 import tempfile try: import MySQLdb import MySQLdb.cursors from pandas import DataFrame except ImportError: pass from ..import_custom import import_custom import tarfile import shutil try: from cStringIO import StringIO except ImportError: from StringIO import StringIO import os import glob class import_custom_upload(osv.TransientModel): _name = "import_custom.upload" _description = "Upload dumps" _columns = { 'file': fields.char('file (*.tar.gz)'), } def upload_button(self, cr, uid, ids, context=None): record = self.browse(cr, uid, ids[0]) tmp_dir,files = self.unzip_file(record.file.strip(), pattern='*.csv') _logger.info('files: %s'%files) instance = import_custom(self.pool, cr, uid, 'yelizariev', #instance_name 'import_custom', # module_name run_import=False, import_dir = '/home/tmp/', context={'csv_files': files}, ) instance.run() try: shutil.rmtree(tmp_dir) except: pass return instance def unzip_file(self, filename, pattern='*'): ''' extract *.tar.gz files returns list of extracted file names ''' tar = tarfile.open(name=filename) dir = tempfile.mkdtemp(prefix='tmp_import_custom') tar.extractall(path=dir) return dir, glob.glob('%s/%s' % (dir, pattern))+glob.glob('%s/*/%s' % (dir, pattern))
lgpl-3.0
olologin/scikit-learn
sklearn/tests/test_kernel_approximation.py
78
7586
import numpy as np from scipy.sparse import csr_matrix from sklearn.utils.testing import assert_array_equal, assert_equal, assert_true from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_array_almost_equal, assert_raises from sklearn.utils.testing import assert_less_equal from sklearn.metrics.pairwise import kernel_metrics from sklearn.kernel_approximation import RBFSampler from sklearn.kernel_approximation import AdditiveChi2Sampler from sklearn.kernel_approximation import SkewedChi2Sampler from sklearn.kernel_approximation import Nystroem from sklearn.metrics.pairwise import polynomial_kernel, rbf_kernel # generate data rng = np.random.RandomState(0) X = rng.random_sample(size=(300, 50)) Y = rng.random_sample(size=(300, 50)) X /= X.sum(axis=1)[:, np.newaxis] Y /= Y.sum(axis=1)[:, np.newaxis] def test_additive_chi2_sampler(): # test that AdditiveChi2Sampler approximates kernel on random data # compute exact kernel # abbreviations for easier formula X_ = X[:, np.newaxis, :] Y_ = Y[np.newaxis, :, :] large_kernel = 2 * X_ * Y_ / (X_ + Y_) # reduce to n_samples_x x n_samples_y by summing over features kernel = (large_kernel.sum(axis=2)) # approximate kernel mapping transform = AdditiveChi2Sampler(sample_steps=3) X_trans = transform.fit_transform(X) Y_trans = transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) assert_array_almost_equal(kernel, kernel_approx, 1) X_sp_trans = transform.fit_transform(csr_matrix(X)) Y_sp_trans = transform.transform(csr_matrix(Y)) assert_array_equal(X_trans, X_sp_trans.A) assert_array_equal(Y_trans, Y_sp_trans.A) # test error is raised on negative input Y_neg = Y.copy() Y_neg[0, 0] = -1 assert_raises(ValueError, transform.transform, Y_neg) # test error on invalid sample_steps transform = AdditiveChi2Sampler(sample_steps=4) assert_raises(ValueError, transform.fit, X) # test that the sample interval is set correctly sample_steps_available = [1, 2, 3] for sample_steps in sample_steps_available: # test that the sample_interval is initialized correctly transform = AdditiveChi2Sampler(sample_steps=sample_steps) assert_equal(transform.sample_interval, None) # test that the sample_interval is changed in the fit method transform.fit(X) assert_not_equal(transform.sample_interval_, None) # test that the sample_interval is set correctly sample_interval = 0.3 transform = AdditiveChi2Sampler(sample_steps=4, sample_interval=sample_interval) assert_equal(transform.sample_interval, sample_interval) transform.fit(X) assert_equal(transform.sample_interval_, sample_interval) def test_skewed_chi2_sampler(): # test that RBFSampler approximates kernel on random data # compute exact kernel c = 0.03 # abbreviations for easier formula X_c = (X + c)[:, np.newaxis, :] Y_c = (Y + c)[np.newaxis, :, :] # we do it in log-space in the hope that it's more stable # this array is n_samples_x x n_samples_y big x n_features log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) + np.log(2.) - np.log(X_c + Y_c)) # reduce to n_samples_x x n_samples_y by summing over features in log-space kernel = np.exp(log_kernel.sum(axis=2)) # approximate kernel mapping transform = SkewedChi2Sampler(skewedness=c, n_components=1000, random_state=42) X_trans = transform.fit_transform(X) Y_trans = transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) assert_array_almost_equal(kernel, kernel_approx, 1) # test error is raised on negative input Y_neg = Y.copy() Y_neg[0, 0] = -1 assert_raises(ValueError, transform.transform, Y_neg) def test_rbf_sampler(): # test that RBFSampler approximates kernel on random data # compute exact kernel gamma = 10. kernel = rbf_kernel(X, Y, gamma=gamma) # approximate kernel mapping rbf_transform = RBFSampler(gamma=gamma, n_components=1000, random_state=42) X_trans = rbf_transform.fit_transform(X) Y_trans = rbf_transform.transform(Y) kernel_approx = np.dot(X_trans, Y_trans.T) error = kernel - kernel_approx assert_less_equal(np.abs(np.mean(error)), 0.01) # close to unbiased np.abs(error, out=error) assert_less_equal(np.max(error), 0.1) # nothing too far off assert_less_equal(np.mean(error), 0.05) # mean is fairly close def test_input_validation(): # Regression test: kernel approx. transformers should work on lists # No assertions; the old versions would simply crash X = [[1, 2], [3, 4], [5, 6]] AdditiveChi2Sampler().fit(X).transform(X) SkewedChi2Sampler().fit(X).transform(X) RBFSampler().fit(X).transform(X) X = csr_matrix(X) RBFSampler().fit(X).transform(X) def test_nystroem_approximation(): # some basic tests rnd = np.random.RandomState(0) X = rnd.uniform(size=(10, 4)) # With n_components = n_samples this is exact X_transformed = Nystroem(n_components=X.shape[0]).fit_transform(X) K = rbf_kernel(X) assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K) trans = Nystroem(n_components=2, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) # test callable kernel linear_kernel = lambda X, Y: np.dot(X, Y.T) trans = Nystroem(n_components=2, kernel=linear_kernel, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) # test that available kernels fit and transform kernels_available = kernel_metrics() for kern in kernels_available: trans = Nystroem(n_components=2, kernel=kern, random_state=rnd) X_transformed = trans.fit(X).transform(X) assert_equal(X_transformed.shape, (X.shape[0], 2)) def test_nystroem_singular_kernel(): # test that nystroem works with singular kernel matrix rng = np.random.RandomState(0) X = rng.rand(10, 20) X = np.vstack([X] * 2) # duplicate samples gamma = 100 N = Nystroem(gamma=gamma, n_components=X.shape[0]).fit(X) X_transformed = N.transform(X) K = rbf_kernel(X, gamma=gamma) assert_array_almost_equal(K, np.dot(X_transformed, X_transformed.T)) assert_true(np.all(np.isfinite(Y))) def test_nystroem_poly_kernel_params(): # Non-regression: Nystroem should pass other parameters beside gamma. rnd = np.random.RandomState(37) X = rnd.uniform(size=(10, 4)) K = polynomial_kernel(X, degree=3.1, coef0=.1) nystroem = Nystroem(kernel="polynomial", n_components=X.shape[0], degree=3.1, coef0=.1) X_transformed = nystroem.fit_transform(X) assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K) def test_nystroem_callable(): # Test Nystroem on a callable. rnd = np.random.RandomState(42) n_samples = 10 X = rnd.uniform(size=(n_samples, 4)) def logging_histogram_kernel(x, y, log): """Histogram kernel that writes to a log.""" log.append(1) return np.minimum(x, y).sum() kernel_log = [] X = list(X) # test input validation Nystroem(kernel=logging_histogram_kernel, n_components=(n_samples - 1), kernel_params={'log': kernel_log}).fit(X) assert_equal(len(kernel_log), n_samples * (n_samples - 1) / 2)
bsd-3-clause
0x0all/scikit-learn
sklearn/linear_model/tests/test_sparse_coordinate_descent.py
28
10014
import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_less from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_greater from sklearn.utils.testing import ignore_warnings from sklearn.linear_model.coordinate_descent import (Lasso, ElasticNet, LassoCV, ElasticNetCV) def test_sparse_coef(): """ Check that the sparse_coef propery works """ clf = ElasticNet() clf.coef_ = [1, 2, 3] assert_true(sp.isspmatrix(clf.sparse_coef_)) assert_equal(clf.sparse_coef_.toarray().tolist()[0], clf.coef_) def test_normalize_option(): """ Check that the normalize option in enet works """ X = sp.csc_matrix([[-1], [0], [1]]) y = [-1, 0, 1] clf_dense = ElasticNet(fit_intercept=True, normalize=True) clf_sparse = ElasticNet(fit_intercept=True, normalize=True) clf_dense.fit(X, y) X = sp.csc_matrix(X) clf_sparse.fit(X, y) assert_almost_equal(clf_dense.dual_gap_, 0) assert_array_almost_equal(clf_dense.coef_, clf_sparse.coef_) def test_lasso_zero(): """Check that the sparse lasso can handle zero data without crashing""" X = sp.csc_matrix((3, 1)) y = [0, 0, 0] T = np.array([[1], [2], [3]]) clf = Lasso().fit(X, y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0]) assert_array_almost_equal(pred, [0, 0, 0]) assert_almost_equal(clf.dual_gap_, 0) def test_enet_toy_list_input(): """Test ElasticNet for various values of alpha and l1_ratio with list X""" X = np.array([[-1], [0], [1]]) X = sp.csc_matrix(X) Y = [-1, 0, 1] # just a straight line T = np.array([[2], [3], [4]]) # test sample # this should be the same as unregularized least squares clf = ElasticNet(alpha=0, l1_ratio=1.0) # catch warning about alpha=0. # this is discouraged but should work. ignore_warnings(clf.fit)(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [1]) assert_array_almost_equal(pred, [2, 3, 4]) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=1000) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.50819], decimal=3) assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.5) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.45454], 3) assert_array_almost_equal(pred, [0.9090, 1.3636, 1.8181], 3) assert_almost_equal(clf.dual_gap_, 0) def test_enet_toy_explicit_sparse_input(): """Test ElasticNet for various values of alpha and l1_ratio with sparse X""" f = ignore_warnings # training samples X = sp.lil_matrix((3, 1)) X[0, 0] = -1 # X[1, 0] = 0 X[2, 0] = 1 Y = [-1, 0, 1] # just a straight line (the identity function) # test samples T = sp.lil_matrix((3, 1)) T[0, 0] = 2 T[1, 0] = 3 T[2, 0] = 4 # this should be the same as lasso clf = ElasticNet(alpha=0, l1_ratio=1.0) f(clf.fit)(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [1]) assert_array_almost_equal(pred, [2, 3, 4]) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.3, max_iter=1000) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.50819], decimal=3) assert_array_almost_equal(pred, [1.0163, 1.5245, 2.0327], decimal=3) assert_almost_equal(clf.dual_gap_, 0) clf = ElasticNet(alpha=0.5, l1_ratio=0.5) clf.fit(X, Y) pred = clf.predict(T) assert_array_almost_equal(clf.coef_, [0.45454], 3) assert_array_almost_equal(pred, [0.9090, 1.3636, 1.8181], 3) assert_almost_equal(clf.dual_gap_, 0) def make_sparse_data(n_samples=100, n_features=100, n_informative=10, seed=42, positive=False, n_targets=1): random_state = np.random.RandomState(seed) # build an ill-posed linear regression problem with many noisy features and # comparatively few samples # generate a ground truth model w = random_state.randn(n_features, n_targets) w[n_informative:] = 0.0 # only the top features are impacting the model if positive: w = np.abs(w) X = random_state.randn(n_samples, n_features) rnd = random_state.uniform(size=(n_samples, n_features)) X[rnd > 0.5] = 0.0 # 50% of zeros in input signal # generate training ground truth labels y = np.dot(X, w) X = sp.csc_matrix(X) if n_targets == 1: y = np.ravel(y) return X, y def _test_sparse_enet_not_as_toy_dataset(alpha, fit_intercept, positive): n_samples, n_features, max_iter = 100, 100, 1000 n_informative = 10 X, y = make_sparse_data(n_samples, n_features, n_informative, positive=positive) X_train, X_test = X[n_samples // 2:], X[:n_samples // 2] y_train, y_test = y[n_samples // 2:], y[:n_samples // 2] s_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept, max_iter=max_iter, tol=1e-7, positive=positive, warm_start=True) s_clf.fit(X_train, y_train) assert_almost_equal(s_clf.dual_gap_, 0, 4) assert_greater(s_clf.score(X_test, y_test), 0.85) # check the convergence is the same as the dense version d_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept, max_iter=max_iter, tol=1e-7, positive=positive, warm_start=True) d_clf.fit(X_train.toarray(), y_train) assert_almost_equal(d_clf.dual_gap_, 0, 4) assert_greater(d_clf.score(X_test, y_test), 0.85) assert_almost_equal(s_clf.coef_, d_clf.coef_, 5) assert_almost_equal(s_clf.intercept_, d_clf.intercept_, 5) # check that the coefs are sparse assert_less(np.sum(s_clf.coef_ != 0.0), 2 * n_informative) def test_sparse_enet_not_as_toy_dataset(): _test_sparse_enet_not_as_toy_dataset(alpha=0.1, fit_intercept=False, positive=False) _test_sparse_enet_not_as_toy_dataset(alpha=0.1, fit_intercept=True, positive=False) _test_sparse_enet_not_as_toy_dataset(alpha=1e-3, fit_intercept=False, positive=True) _test_sparse_enet_not_as_toy_dataset(alpha=1e-3, fit_intercept=True, positive=True) def test_sparse_lasso_not_as_toy_dataset(): n_samples = 100 max_iter = 1000 n_informative = 10 X, y = make_sparse_data(n_samples=n_samples, n_informative=n_informative) X_train, X_test = X[n_samples // 2:], X[:n_samples // 2] y_train, y_test = y[n_samples // 2:], y[:n_samples // 2] s_clf = Lasso(alpha=0.1, fit_intercept=False, max_iter=max_iter, tol=1e-7) s_clf.fit(X_train, y_train) assert_almost_equal(s_clf.dual_gap_, 0, 4) assert_greater(s_clf.score(X_test, y_test), 0.85) # check the convergence is the same as the dense version d_clf = Lasso(alpha=0.1, fit_intercept=False, max_iter=max_iter, tol=1e-7) d_clf.fit(X_train.toarray(), y_train) assert_almost_equal(d_clf.dual_gap_, 0, 4) assert_greater(d_clf.score(X_test, y_test), 0.85) # check that the coefs are sparse assert_equal(np.sum(s_clf.coef_ != 0.0), n_informative) def test_enet_multitarget(): n_targets = 3 X, y = make_sparse_data(n_targets=n_targets) estimator = ElasticNet(alpha=0.01, fit_intercept=True, precompute=None) # XXX: There is a bug when precompute is not None! estimator.fit(X, y) coef, intercept, dual_gap = (estimator.coef_, estimator.intercept_, estimator.dual_gap_) for k in range(n_targets): estimator.fit(X, y[:, k]) assert_array_almost_equal(coef[k, :], estimator.coef_) assert_array_almost_equal(intercept[k], estimator.intercept_) assert_array_almost_equal(dual_gap[k], estimator.dual_gap_) def test_path_parameters(): X, y = make_sparse_data() max_iter = 50 n_alphas = 10 clf = ElasticNetCV(n_alphas=n_alphas, eps=1e-3, max_iter=max_iter, l1_ratio=0.5, fit_intercept=False) ignore_warnings(clf.fit)(X, y) # new params assert_almost_equal(0.5, clf.l1_ratio) assert_equal(n_alphas, clf.n_alphas) assert_equal(n_alphas, len(clf.alphas_)) sparse_mse_path = clf.mse_path_ ignore_warnings(clf.fit)(X.toarray(), y) # compare with dense data assert_almost_equal(clf.mse_path_, sparse_mse_path) def test_same_output_sparse_dense_lasso_and_enet_cv(): X, y = make_sparse_data(n_samples=40, n_features=10) for normalize in [True, False]: clfs = ElasticNetCV(max_iter=100, cv=5, normalize=normalize) ignore_warnings(clfs.fit)(X, y) clfd = ElasticNetCV(max_iter=100, cv=5, normalize=normalize) ignore_warnings(clfd.fit)(X.toarray(), y) assert_almost_equal(clfs.alpha_, clfd.alpha_, 7) assert_almost_equal(clfs.intercept_, clfd.intercept_, 7) assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_) assert_array_almost_equal(clfs.alphas_, clfd.alphas_) clfs = LassoCV(max_iter=100, cv=4, normalize=normalize) ignore_warnings(clfs.fit)(X, y) clfd = LassoCV(max_iter=100, cv=4, normalize=normalize) ignore_warnings(clfd.fit)(X.toarray(), y) assert_almost_equal(clfs.alpha_, clfd.alpha_, 7) assert_almost_equal(clfs.intercept_, clfd.intercept_, 7) assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_) assert_array_almost_equal(clfs.alphas_, clfd.alphas_)
bsd-3-clause
wzbozon/scikit-learn
examples/ensemble/plot_random_forest_embedding.py
286
3531
""" ========================================================= Hashing feature transformation using Totally Random Trees ========================================================= RandomTreesEmbedding provides a way to map data to a very high-dimensional, sparse representation, which might be beneficial for classification. The mapping is completely unsupervised and very efficient. This example visualizes the partitions given by several trees and shows how the transformation can also be used for non-linear dimensionality reduction or non-linear classification. Points that are neighboring often share the same leaf of a tree and therefore share large parts of their hashed representation. This allows to separate two concentric circles simply based on the principal components of the transformed data. In high-dimensional spaces, linear classifiers often achieve excellent accuracy. For sparse binary data, BernoulliNB is particularly well-suited. The bottom row compares the decision boundary obtained by BernoulliNB in the transformed space with an ExtraTreesClassifier forests learned on the original data. """ import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_circles from sklearn.ensemble import RandomTreesEmbedding, ExtraTreesClassifier from sklearn.decomposition import TruncatedSVD from sklearn.naive_bayes import BernoulliNB # make a synthetic dataset X, y = make_circles(factor=0.5, random_state=0, noise=0.05) # use RandomTreesEmbedding to transform data hasher = RandomTreesEmbedding(n_estimators=10, random_state=0, max_depth=3) X_transformed = hasher.fit_transform(X) # Visualize result using PCA pca = TruncatedSVD(n_components=2) X_reduced = pca.fit_transform(X_transformed) # Learn a Naive Bayes classifier on the transformed data nb = BernoulliNB() nb.fit(X_transformed, y) # Learn an ExtraTreesClassifier for comparison trees = ExtraTreesClassifier(max_depth=3, n_estimators=10, random_state=0) trees.fit(X, y) # scatter plot of original and reduced data fig = plt.figure(figsize=(9, 8)) ax = plt.subplot(221) ax.scatter(X[:, 0], X[:, 1], c=y, s=50) ax.set_title("Original Data (2d)") ax.set_xticks(()) ax.set_yticks(()) ax = plt.subplot(222) ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, s=50) ax.set_title("PCA reduction (2d) of transformed data (%dd)" % X_transformed.shape[1]) ax.set_xticks(()) ax.set_yticks(()) # Plot the decision in original space. For that, we will assign a color to each # point in the mesh [x_min, m_max] x [y_min, y_max]. h = .01 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # transform grid using RandomTreesEmbedding transformed_grid = hasher.transform(np.c_[xx.ravel(), yy.ravel()]) y_grid_pred = nb.predict_proba(transformed_grid)[:, 1] ax = plt.subplot(223) ax.set_title("Naive Bayes on Transformed data") ax.pcolormesh(xx, yy, y_grid_pred.reshape(xx.shape)) ax.scatter(X[:, 0], X[:, 1], c=y, s=50) ax.set_ylim(-1.4, 1.4) ax.set_xlim(-1.4, 1.4) ax.set_xticks(()) ax.set_yticks(()) # transform grid using ExtraTreesClassifier y_grid_pred = trees.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] ax = plt.subplot(224) ax.set_title("ExtraTrees predictions") ax.pcolormesh(xx, yy, y_grid_pred.reshape(xx.shape)) ax.scatter(X[:, 0], X[:, 1], c=y, s=50) ax.set_ylim(-1.4, 1.4) ax.set_xlim(-1.4, 1.4) ax.set_xticks(()) ax.set_yticks(()) plt.tight_layout() plt.show()
bsd-3-clause
nschloe/pynosh
tools/triangle_test.py
1
7656
# -*- coding: utf-8 -*- # """Checker for triangle coefficients. """ import numpy as np # import pynosh.numerical_methods as nm # import pynosh.ginla_modelevaluator as gm def _main(): args = _parse_input_arguments() # # define triangle # x0 = np.array([0.0, 0.0]) # v1 = np.array([1.0, 1.0]) # v1 = v1 / np.linalg.norm(v1) # v2 = np.array([0.0, 1.0]) # v2 = v2 / np.linalg.norm(v2) # alpha1 = np.sqrt(2) * 0.1 # alpha2 = 0.1 # triangle_vertices = np.vstack([x0, x0 + alpha1 * v1, x0 + alpha2 * v2]) # triangle_vertices = 2 * np.random.rand(3, 2) - 1.0 cc = compute_triangle_cc(triangle_vertices) triangle_vol = compute_triangle_vol(triangle_vertices) edges = np.array( [ triangle_vertices[1] - triangle_vertices[0], triangle_vertices[2] - triangle_vertices[1], triangle_vertices[0] - triangle_vertices[2], ] ) edge_lenghts = np.array([np.linalg.norm(e) for e in edges]) midpoints = 0.5 * np.array( [ triangle_vertices[1] + triangle_vertices[0], triangle_vertices[2] + triangle_vertices[1], triangle_vertices[0] + triangle_vertices[2], ] ) if args.show_example: _show_example(triangle_vertices, midpoints, cc) # Find the coefficients numerically. A = np.dot(edges, edges.T) # Careful here! As of NumPy 1.7, np.diag() returns a view. rhs = triangle_vol * np.diag(A).copy() A = A ** 2 weights = np.zeros(3) # Append the the resulting coefficients to the coefficient cache. # The system is posdef iff the simplex isn't degenerate. try: weights += np.linalg.solve(A, rhs) except np.linalg.linalg.LinAlgError: # The matrix A appears to be singular, # and the only circumstance that makes this # happening is the cell being degenerate. # Hence, it has volume 0, and so all the edge # coefficients are 0, too. # Hence, do nothing. pass check_weights(weights, edges, triangle_vol) # Qiang's formula. # theta = np.array([angle(triangle_vertices[0] - triangle_vertices[2], # triangle_vertices[1] - triangle_vertices[2]), # angle(triangle_vertices[2] - triangle_vertices[0], # triangle_vertices[1] - triangle_vertices[0]), # angle(triangle_vertices[2] - triangle_vertices[1], # triangle_vertices[0] - triangle_vertices[1]) # ]) theta = np.array( [ angle(edges[2], -edges[1]), angle(-edges[2], edges[0]), angle(edges[1], -edges[0]), ] ) qweights = 0.5 * np.cos(theta) / np.sin(theta) # check_weights(qweights, edges, triangle_vol) print("Compare weights with the previous... ") err = np.linalg.norm(qweights - weights) if err > 1.0e-14: print(("Ah! Diff =", qweights - weights)) else: print("Cool.") ## possible extension to qiang's formula # u0 = [ rand(2,1); 0 ]; # u1 = [ rand(2,1); 0 ]; # p0 = u0'*u1 * triangle_volume( triangle_vertices[0], triangle_vertices[1], triangle_vertices[2] ); # p1 = cot(theta(1)) * (u0'*edge[0])*(u1'*edge[0]) ... # + cot(theta(2)) * (u0'*e{2})*(u1'*e{2}) ... # + cot(theta(3)) * (u0'*e{3})*(u1'*e{3}); # p0 - 0.5 * p1 # Qiang's formula, passing the angle calculations. t = np.array( [ np.dot(edges[2] / edge_lenghts[2], -edges[1] / edge_lenghts[1]), np.dot(-edges[2] / edge_lenghts[2], edges[0] / edge_lenghts[0]), np.dot(edges[1] / edge_lenghts[1], -edges[0] / edge_lenghts[0]), ] ) qweights = 0.5 * t / np.sqrt(1.0 - t ** 2) # check_weights(qweights, edges, triangle_vol) print("Compare weights with the previous... ", end=" ") err = np.linalg.norm(qweights - weights) if err > 1.0e-14: print("Ah! Diff =", qweights - weights) else: print("Cool.") ## alternative computation of the weights # covolumes = np.array([np.linalg.norm(midpoints[0] - cc), # np.linalg.norm(midpoints[1] - cc), # np.linalg.norm(midpoints[2] - cc) # ]) # edge_lenghts = np.array([np.linalg.norm(e) for e in edges]) # cweights = covolumes / edge_lenghts # print 'Compare weights with the previous... ', # err = np.linalg.norm(cweights - weights) # if err > 1.0e-14: # print 'Ah! Diff =', cweights - weights # else: # print 'Cool.' return def _show_example(triangle_vertices, midpoints, cc): """Show an example situation.""" from matplotlib import pyplot as pp # Plot the situation. for i in range(3): for j in range(i + 1, 3): # Edge (i,j). pp.plot( [triangle_vertices[i][0], triangle_vertices[j][0]], [triangle_vertices[i][1], triangle_vertices[j][1]], "k-", ) # Line midpoint(edge(i,j))---circumcenter. midpoint = 0.5 * (triangle_vertices[i] + triangle_vertices[j]) pp.plot([midpoint[0], cc[0]], [midpoint[1], cc[1]], color="0.5") # plot circumcenter pp.plot(cc[0], cc[1], "or") pp.show() return def compute_triangle_cc(node_coords): """Compute circumcenter.""" from vtk import vtkTriangle cc = np.empty([2, 1]) vtkTriangle.Circumcircle(node_coords[0], node_coords[1], node_coords[2], cc) return cc def compute_triangle_vol(node_coords): """Compute triangle volume.""" # Shoelace formula. return 0.5 * abs( node_coords[0][0] * node_coords[1][1] - node_coords[0][1] * node_coords[1][0] + node_coords[1][0] * node_coords[2][1] - node_coords[1][1] * node_coords[2][0] + node_coords[2][0] * node_coords[0][1] - node_coords[2][1] * node_coords[0][0] ) def angle(u, v): """Computes the angle between two vectors.""" return np.arccos(np.dot(u / np.linalg.norm(u), v / np.linalg.norm(v))) def check_weights(weights, edges, vol, tol=1.0e-14): """Check if the given weights are correct.""" print( "Checking weights %g, %g, %g..." % (weights[0], weights[1], weights[2]), end=" " ) # try out the weight with a bunch of other random vectors m = 1000 found_mismatch = False for i in range(m): u = np.random.rand(2) + 1j * np.random.rand(2) v = np.random.rand(2) + 1j * np.random.rand(2) control_value = np.vdot(u, v) * vol p1 = 0.0 for j in range(3): p1 += np.vdot(u, edges[j]) * np.vdot(edges[j], v) * weights[j] err = abs(control_value - p1) if err > tol: found_mismatch = True print("Found mismatch by %g.\n" % err) break if not found_mismatch: print("Cool.") return def _parse_input_arguments(): """Parse input arguments. """ import argparse parser = argparse.ArgumentParser( description="Test edge coefficients for the triangle." ) parser.add_argument( "-s", "--show-example", action="store_true", default=False, help="Show an example triangle with points highlighted (default: False).", ) # parser.add_argument('filename', # metavar = 'FILE', # type = str, # help = 'ExodusII file containing the geometry and initial state' # ) # parser.add_argument('--show', '-s', # action = 'store_true', # default = False, # help = 'show the relative residuals of each linear iteration (default: False)' # ) return parser.parse_args() if __name__ == "__main__": _main()
mit
shoyer/xarray
xarray/tests/test_formatting.py
1
14112
import sys from textwrap import dedent import numpy as np import pandas as pd import pytest import xarray as xr from xarray.core import formatting from . import raises_regex class TestFormatting: def test_get_indexer_at_least_n_items(self): cases = [ ((20,), (slice(10),), (slice(-10, None),)), ((3, 20), (0, slice(10)), (-1, slice(-10, None))), ((2, 10), (0, slice(10)), (-1, slice(-10, None))), ((2, 5), (slice(2), slice(None)), (slice(-2, None), slice(None))), ((1, 2, 5), (0, slice(2), slice(None)), (-1, slice(-2, None), slice(None))), ((2, 3, 5), (0, slice(2), slice(None)), (-1, slice(-2, None), slice(None))), ( (1, 10, 1), (0, slice(10), slice(None)), (-1, slice(-10, None), slice(None)), ), ( (2, 5, 1), (slice(2), slice(None), slice(None)), (slice(-2, None), slice(None), slice(None)), ), ((2, 5, 3), (0, slice(4), slice(None)), (-1, slice(-4, None), slice(None))), ( (2, 3, 3), (slice(2), slice(None), slice(None)), (slice(-2, None), slice(None), slice(None)), ), ] for shape, start_expected, end_expected in cases: actual = formatting._get_indexer_at_least_n_items(shape, 10, from_end=False) assert start_expected == actual actual = formatting._get_indexer_at_least_n_items(shape, 10, from_end=True) assert end_expected == actual def test_first_n_items(self): array = np.arange(100).reshape(10, 5, 2) for n in [3, 10, 13, 100, 200]: actual = formatting.first_n_items(array, n) expected = array.flat[:n] assert (expected == actual).all() with raises_regex(ValueError, "at least one item"): formatting.first_n_items(array, 0) def test_last_n_items(self): array = np.arange(100).reshape(10, 5, 2) for n in [3, 10, 13, 100, 200]: actual = formatting.last_n_items(array, n) expected = array.flat[-n:] assert (expected == actual).all() with raises_regex(ValueError, "at least one item"): formatting.first_n_items(array, 0) def test_last_item(self): array = np.arange(100) reshape = ((10, 10), (1, 100), (2, 2, 5, 5)) expected = np.array([99]) for r in reshape: result = formatting.last_item(array.reshape(r)) assert result == expected def test_format_item(self): cases = [ (pd.Timestamp("2000-01-01T12"), "2000-01-01T12:00:00"), (pd.Timestamp("2000-01-01"), "2000-01-01"), (pd.Timestamp("NaT"), "NaT"), (pd.Timedelta("10 days 1 hour"), "10 days 01:00:00"), (pd.Timedelta("-3 days"), "-3 days +00:00:00"), (pd.Timedelta("3 hours"), "0 days 03:00:00"), (pd.Timedelta("NaT"), "NaT"), ("foo", "'foo'"), (b"foo", "b'foo'"), (1, "1"), (1.0, "1.0"), ] for item, expected in cases: actual = formatting.format_item(item) assert expected == actual def test_format_items(self): cases = [ (np.arange(4) * np.timedelta64(1, "D"), "0 days 1 days 2 days 3 days"), ( np.arange(4) * np.timedelta64(3, "h"), "00:00:00 03:00:00 06:00:00 09:00:00", ), ( np.arange(4) * np.timedelta64(500, "ms"), "00:00:00 00:00:00.500000 00:00:01 00:00:01.500000", ), (pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"), ( pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]), "1 days 01:00:00 1 days 00:00:00 0 days 00:00:00", ), ([1, 2, 3], "1 2 3"), ] for item, expected in cases: actual = " ".join(formatting.format_items(item)) assert expected == actual def test_format_array_flat(self): actual = formatting.format_array_flat(np.arange(100), 2) expected = "..." assert expected == actual actual = formatting.format_array_flat(np.arange(100), 9) expected = "0 ... 99" assert expected == actual actual = formatting.format_array_flat(np.arange(100), 10) expected = "0 1 ... 99" assert expected == actual actual = formatting.format_array_flat(np.arange(100), 13) expected = "0 1 ... 98 99" assert expected == actual actual = formatting.format_array_flat(np.arange(100), 15) expected = "0 1 2 ... 98 99" assert expected == actual # NB: Probably not ideal; an alternative would be cutting after the # first ellipsis actual = formatting.format_array_flat(np.arange(100.0), 11) expected = "0.0 ... ..." assert expected == actual actual = formatting.format_array_flat(np.arange(100.0), 12) expected = "0.0 ... 99.0" assert expected == actual actual = formatting.format_array_flat(np.arange(3), 5) expected = "0 1 2" assert expected == actual actual = formatting.format_array_flat(np.arange(4.0), 11) expected = "0.0 ... 3.0" assert expected == actual actual = formatting.format_array_flat(np.arange(0), 0) expected = "" assert expected == actual actual = formatting.format_array_flat(np.arange(1), 1) expected = "0" assert expected == actual actual = formatting.format_array_flat(np.arange(2), 3) expected = "0 1" assert expected == actual actual = formatting.format_array_flat(np.arange(4), 7) expected = "0 1 2 3" assert expected == actual actual = formatting.format_array_flat(np.arange(5), 7) expected = "0 ... 4" assert expected == actual long_str = [" ".join(["hello world" for _ in range(100)])] actual = formatting.format_array_flat(np.asarray([long_str]), 21) expected = "'hello world hello..." assert expected == actual def test_pretty_print(self): assert formatting.pretty_print("abcdefghij", 8) == "abcde..." assert formatting.pretty_print("ß", 1) == "ß" def test_maybe_truncate(self): assert formatting.maybe_truncate("ß", 10) == "ß" def test_format_timestamp_out_of_bounds(self): from datetime import datetime date = datetime(1300, 12, 1) expected = "1300-12-01" result = formatting.format_timestamp(date) assert result == expected date = datetime(2300, 12, 1) expected = "2300-12-01" result = formatting.format_timestamp(date) assert result == expected def test_attribute_repr(self): short = formatting.summarize_attr("key", "Short string") long = formatting.summarize_attr("key", 100 * "Very long string ") newlines = formatting.summarize_attr("key", "\n\n\n") tabs = formatting.summarize_attr("key", "\t\t\t") assert short == " key: Short string" assert len(long) <= 80 assert long.endswith("...") assert "\n" not in newlines assert "\t" not in tabs def test_diff_array_repr(self): da_a = xr.DataArray( np.array([[1, 2, 3], [4, 5, 6]], dtype="int64"), dims=("x", "y"), coords={ "x": np.array(["a", "b"], dtype="U1"), "y": np.array([1, 2, 3], dtype="int64"), }, attrs={"units": "m", "description": "desc"}, ) da_b = xr.DataArray( np.array([1, 2], dtype="int64"), dims="x", coords={ "x": np.array(["a", "c"], dtype="U1"), "label": ("x", np.array([1, 2], dtype="int64")), }, attrs={"units": "kg"}, ) byteorder = "<" if sys.byteorder == "little" else ">" expected = dedent( """\ Left and right DataArray objects are not identical Differing dimensions: (x: 2, y: 3) != (x: 2) Differing values: L array([[1, 2, 3], [4, 5, 6]], dtype=int64) R array([1, 2], dtype=int64) Differing coordinates: L * x (x) %cU1 'a' 'b' R * x (x) %cU1 'a' 'c' Coordinates only on the left object: * y (y) int64 1 2 3 Coordinates only on the right object: label (x) int64 1 2 Differing attributes: L units: m R units: kg Attributes only on the left object: description: desc""" % (byteorder, byteorder) ) actual = formatting.diff_array_repr(da_a, da_b, "identical") try: assert actual == expected except AssertionError: # depending on platform, dtype may not be shown in numpy array repr assert actual == expected.replace(", dtype=int64", "") va = xr.Variable( "x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"} ) vb = xr.Variable(("x", "y"), np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")) expected = dedent( """\ Left and right Variable objects are not equal Differing dimensions: (x: 3) != (x: 2, y: 3) Differing values: L array([1, 2, 3], dtype=int64) R array([[1, 2, 3], [4, 5, 6]], dtype=int64)""" ) actual = formatting.diff_array_repr(va, vb, "equals") try: assert actual == expected except AssertionError: assert actual == expected.replace(", dtype=int64", "") @pytest.mark.filterwarnings("error") def test_diff_attrs_repr_with_array(self): attrs_a = {"attr": np.array([0, 1])} attrs_b = {"attr": 1} expected = dedent( """\ Differing attributes: L attr: [0 1] R attr: 1 """ ).strip() actual = formatting.diff_attrs_repr(attrs_a, attrs_b, "equals") assert expected == actual attrs_b = {"attr": np.array([-3, 5])} expected = dedent( """\ Differing attributes: L attr: [0 1] R attr: [-3 5] """ ).strip() actual = formatting.diff_attrs_repr(attrs_a, attrs_b, "equals") assert expected == actual # should not raise a warning attrs_b = {"attr": np.array([0, 1, 2])} expected = dedent( """\ Differing attributes: L attr: [0 1] R attr: [0 1 2] """ ).strip() actual = formatting.diff_attrs_repr(attrs_a, attrs_b, "equals") assert expected == actual def test_diff_dataset_repr(self): ds_a = xr.Dataset( data_vars={ "var1": (("x", "y"), np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")), "var2": ("x", np.array([3, 4], dtype="int64")), }, coords={ "x": np.array(["a", "b"], dtype="U1"), "y": np.array([1, 2, 3], dtype="int64"), }, attrs={"units": "m", "description": "desc"}, ) ds_b = xr.Dataset( data_vars={"var1": ("x", np.array([1, 2], dtype="int64"))}, coords={ "x": ("x", np.array(["a", "c"], dtype="U1"), {"source": 0}), "label": ("x", np.array([1, 2], dtype="int64")), }, attrs={"units": "kg"}, ) byteorder = "<" if sys.byteorder == "little" else ">" expected = dedent( """\ Left and right Dataset objects are not identical Differing dimensions: (x: 2, y: 3) != (x: 2) Differing coordinates: L * x (x) %cU1 'a' 'b' R * x (x) %cU1 'a' 'c' source: 0 Coordinates only on the left object: * y (y) int64 1 2 3 Coordinates only on the right object: label (x) int64 1 2 Differing data variables: L var1 (x, y) int64 1 2 3 4 5 6 R var1 (x) int64 1 2 Data variables only on the left object: var2 (x) int64 3 4 Differing attributes: L units: m R units: kg Attributes only on the left object: description: desc""" % (byteorder, byteorder) ) actual = formatting.diff_dataset_repr(ds_a, ds_b, "identical") assert actual == expected def test_array_repr(self): ds = xr.Dataset(coords={"foo": [1, 2, 3], "bar": [1, 2, 3]}) ds[(1, 2)] = xr.DataArray([0], dims="test") actual = formatting.array_repr(ds[(1, 2)]) expected = dedent( """\ <xarray.DataArray (1, 2) (test: 1)> array([0]) Dimensions without coordinates: test""" ) assert actual == expected def test_set_numpy_options(): original_options = np.get_printoptions() with formatting.set_numpy_options(threshold=10): assert len(repr(np.arange(500))) < 200 # original options are restored assert np.get_printoptions() == original_options def test_short_numpy_repr(): cases = [ np.random.randn(500), np.random.randn(20, 20), np.random.randn(5, 10, 15), np.random.randn(5, 10, 15, 3), ] # number of lines: # for default numpy repr: 167, 140, 254, 248 # for short_numpy_repr: 1, 7, 24, 19 for array in cases: num_lines = formatting.short_numpy_repr(array).count("\n") + 1 assert num_lines < 30
apache-2.0
belltailjp/scikit-learn
sklearn/neighbors/base.py
115
29783
"""Base and mixin classes for nearest neighbors""" # Authors: Jake Vanderplas <[email protected]> # Fabian Pedregosa <[email protected]> # Alexandre Gramfort <[email protected]> # Sparseness support by Lars Buitinck <[email protected]> # Multi-output support by Arnaud Joly <[email protected]> # # License: BSD 3 clause (C) INRIA, University of Amsterdam import warnings from abc import ABCMeta, abstractmethod import numpy as np from scipy.sparse import csr_matrix, issparse from .ball_tree import BallTree from .kd_tree import KDTree from ..base import BaseEstimator from ..metrics import pairwise_distances from ..metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS from ..utils import check_X_y, check_array from ..utils.fixes import argpartition from ..utils.validation import DataConversionWarning from ..utils.validation import NotFittedError from ..externals import six VALID_METRICS = dict(ball_tree=BallTree.valid_metrics, kd_tree=KDTree.valid_metrics, # The following list comes from the # sklearn.metrics.pairwise doc string brute=(list(PAIRWISE_DISTANCE_FUNCTIONS.keys()) + ['braycurtis', 'canberra', 'chebyshev', 'correlation', 'cosine', 'dice', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'yule', 'wminkowski'])) VALID_METRICS_SPARSE = dict(ball_tree=[], kd_tree=[], brute=PAIRWISE_DISTANCE_FUNCTIONS.keys()) class NeighborsWarning(UserWarning): pass # Make sure that NeighborsWarning are displayed more than once warnings.simplefilter("always", NeighborsWarning) def _check_weights(weights): """Check to make sure weights are valid""" if weights in (None, 'uniform', 'distance'): return weights elif callable(weights): return weights else: raise ValueError("weights not recognized: should be 'uniform', " "'distance', or a callable function") def _get_weights(dist, weights): """Get the weights from an array of distances and a parameter ``weights`` Parameters =========== dist: ndarray The input distances weights: {'uniform', 'distance' or a callable} The kind of weighting used Returns ======== weights_arr: array of the same shape as ``dist`` if ``weights == 'uniform'``, then returns None """ if weights in (None, 'uniform'): return None elif weights == 'distance': # if user attempts to classify a point that was zero distance from one # or more training points, those training points are weighted as 1.0 # and the other points as 0.0 if dist.dtype is np.dtype(object): for point_dist_i, point_dist in enumerate(dist): # check if point_dist is iterable # (ex: RadiusNeighborClassifier.predict may set an element of # dist to 1e-6 to represent an 'outlier') if hasattr(point_dist, '__contains__') and 0. in point_dist: dist[point_dist_i] = point_dist == 0. else: dist[point_dist_i] = 1. / point_dist else: with np.errstate(divide='ignore'): dist = 1. / dist inf_mask = np.isinf(dist) inf_row = np.any(inf_mask, axis=1) dist[inf_row] = inf_mask[inf_row] return dist elif callable(weights): return weights(dist) else: raise ValueError("weights not recognized: should be 'uniform', " "'distance', or a callable function") class NeighborsBase(six.with_metaclass(ABCMeta, BaseEstimator)): """Base class for nearest neighbors estimators.""" @abstractmethod def __init__(self): pass def _init_params(self, n_neighbors=None, radius=None, algorithm='auto', leaf_size=30, metric='minkowski', p=2, metric_params=None, **kwargs): if kwargs: warnings.warn("Passing additional arguments to the metric " "function as **kwargs is deprecated " "and will no longer be supported in 0.18. " "Use metric_params instead.", DeprecationWarning, stacklevel=3) if metric_params is None: metric_params = {} metric_params.update(kwargs) self.n_neighbors = n_neighbors self.radius = radius self.algorithm = algorithm self.leaf_size = leaf_size self.metric = metric self.metric_params = metric_params self.p = p if algorithm not in ['auto', 'brute', 'kd_tree', 'ball_tree']: raise ValueError("unrecognized algorithm: '%s'" % algorithm) if algorithm == 'auto': alg_check = 'ball_tree' else: alg_check = algorithm if callable(metric): if algorithm == 'kd_tree': # callable metric is only valid for brute force and ball_tree raise ValueError( "kd_tree algorithm does not support callable metric '%s'" % metric) elif metric not in VALID_METRICS[alg_check]: raise ValueError("Metric '%s' not valid for algorithm '%s'" % (metric, algorithm)) if self.metric_params is not None and 'p' in self.metric_params: warnings.warn("Parameter p is found in metric_params. " "The corresponding parameter from __init__ " "is ignored.", SyntaxWarning, stacklevel=3) effective_p = metric_params['p'] else: effective_p = self.p if self.metric in ['wminkowski', 'minkowski'] and effective_p < 1: raise ValueError("p must be greater than one for minkowski metric") self._fit_X = None self._tree = None self._fit_method = None def _fit(self, X): if self.metric_params is None: self.effective_metric_params_ = {} else: self.effective_metric_params_ = self.metric_params.copy() effective_p = self.effective_metric_params_.get('p', self.p) if self.metric in ['wminkowski', 'minkowski']: self.effective_metric_params_['p'] = effective_p self.effective_metric_ = self.metric # For minkowski distance, use more efficient methods where available if self.metric == 'minkowski': p = self.effective_metric_params_.pop('p', 2) if p < 1: raise ValueError("p must be greater than one " "for minkowski metric") elif p == 1: self.effective_metric_ = 'manhattan' elif p == 2: self.effective_metric_ = 'euclidean' elif p == np.inf: self.effective_metric_ = 'chebyshev' else: self.effective_metric_params_['p'] = p if isinstance(X, NeighborsBase): self._fit_X = X._fit_X self._tree = X._tree self._fit_method = X._fit_method return self elif isinstance(X, BallTree): self._fit_X = X.data self._tree = X self._fit_method = 'ball_tree' return self elif isinstance(X, KDTree): self._fit_X = X.data self._tree = X self._fit_method = 'kd_tree' return self X = check_array(X, accept_sparse='csr') n_samples = X.shape[0] if n_samples == 0: raise ValueError("n_samples must be greater than 0") if issparse(X): if self.algorithm not in ('auto', 'brute'): warnings.warn("cannot use tree with sparse input: " "using brute force") if self.effective_metric_ not in VALID_METRICS_SPARSE['brute']: raise ValueError("metric '%s' not valid for sparse input" % self.effective_metric_) self._fit_X = X.copy() self._tree = None self._fit_method = 'brute' return self self._fit_method = self.algorithm self._fit_X = X if self._fit_method == 'auto': # A tree approach is better for small number of neighbors, # and KDTree is generally faster when available if (self.n_neighbors is None or self.n_neighbors < self._fit_X.shape[0] // 2): if self.effective_metric_ in VALID_METRICS['kd_tree']: self._fit_method = 'kd_tree' else: self._fit_method = 'ball_tree' else: self._fit_method = 'brute' if self._fit_method == 'ball_tree': self._tree = BallTree(X, self.leaf_size, metric=self.effective_metric_, **self.effective_metric_params_) elif self._fit_method == 'kd_tree': self._tree = KDTree(X, self.leaf_size, metric=self.effective_metric_, **self.effective_metric_params_) elif self._fit_method == 'brute': self._tree = None else: raise ValueError("algorithm = '%s' not recognized" % self.algorithm) return self class KNeighborsMixin(object): """Mixin for k-neighbors searches""" def kneighbors(self, X=None, n_neighbors=None, return_distance=True): """Finds the K-neighbors of a point. Returns distance Parameters ---------- X : array-like, last dimension same as that of fit data, optional The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. n_neighbors : int Number of neighbors to get (default is the value passed to the constructor). return_distance : boolean, optional. Defaults to True. If False, distances will not be returned Returns ------- dist : array Array representing the lengths to points, only present if return_distance=True ind : array Indices of the nearest points in the population matrix. Examples -------- In the following example, we construct a NeighborsClassifier class from an array representing our data set and ask who's the closest point to [1,1,1] >>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(n_neighbors=1) >>> neigh.fit(samples) # doctest: +ELLIPSIS NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> print(neigh.kneighbors([1., 1., 1.])) # doctest: +ELLIPSIS (array([[ 0.5]]), array([[2]]...)) As you can see, it returns [[0.5]], and [[2]], which means that the element is at distance 0.5 and is the third element of samples (indexes start at 0). You can also query for multiple points: >>> X = [[0., 1., 0.], [1., 0., 1.]] >>> neigh.kneighbors(X, return_distance=False) # doctest: +ELLIPSIS array([[1], [2]]...) """ if self._fit_method is None: raise NotFittedError("Must fit neighbors before querying.") if n_neighbors is None: n_neighbors = self.n_neighbors if X is not None: query_is_train = False X = check_array(X, accept_sparse='csr') else: query_is_train = True X = self._fit_X # Include an extra neighbor to account for the sample itself being # returned, which is removed later n_neighbors += 1 train_size = self._fit_X.shape[0] if n_neighbors > train_size: raise ValueError( "Expected n_neighbors <= n_samples, " " but n_samples = %d, n_neighbors = %d" % (train_size, n_neighbors) ) n_samples, _ = X.shape sample_range = np.arange(n_samples)[:, None] if self._fit_method == 'brute': # for efficiency, use squared euclidean distances if self.effective_metric_ == 'euclidean': dist = pairwise_distances(X, self._fit_X, 'euclidean', squared=True) else: dist = pairwise_distances(X, self._fit_X, self.effective_metric_, **self.effective_metric_params_) neigh_ind = argpartition(dist, n_neighbors - 1, axis=1) neigh_ind = neigh_ind[:, :n_neighbors] # argpartition doesn't guarantee sorted order, so we sort again neigh_ind = neigh_ind[ sample_range, np.argsort(dist[sample_range, neigh_ind])] if return_distance: if self.effective_metric_ == 'euclidean': result = np.sqrt(dist[sample_range, neigh_ind]), neigh_ind else: result = dist[sample_range, neigh_ind], neigh_ind else: result = neigh_ind elif self._fit_method in ['ball_tree', 'kd_tree']: if issparse(X): raise ValueError( "%s does not work with sparse matrices. Densify the data, " "or set algorithm='brute'" % self._fit_method) result = self._tree.query(X, n_neighbors, return_distance=return_distance) else: raise ValueError("internal: _fit_method not recognized") if not query_is_train: return result else: # If the query data is the same as the indexed data, we would like # to ignore the first nearest neighbor of every sample, i.e # the sample itself. if return_distance: dist, neigh_ind = result else: neigh_ind = result sample_mask = neigh_ind != sample_range # Corner case: When the number of duplicates are more # than the number of neighbors, the first NN will not # be the sample, but a duplicate. # In that case mask the first duplicate. dup_gr_nbrs = np.all(sample_mask, axis=1) sample_mask[:, 0][dup_gr_nbrs] = False neigh_ind = np.reshape( neigh_ind[sample_mask], (n_samples, n_neighbors - 1)) if return_distance: dist = np.reshape( dist[sample_mask], (n_samples, n_neighbors - 1)) return dist, neigh_ind return neigh_ind def kneighbors_graph(self, X=None, n_neighbors=None, mode='connectivity'): """Computes the (weighted) graph of k-Neighbors for points in X Parameters ---------- X : array-like, last dimension same as that of fit data, optional The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. n_neighbors : int Number of neighbors for each sample. (default is value passed to the constructor). mode : {'connectivity', 'distance'}, optional Type of returned matrix: 'connectivity' will return the connectivity matrix with ones and zeros, in 'distance' the edges are Euclidean distance between points. Returns ------- A : sparse matrix in CSR format, shape = [n_samples, n_samples_fit] n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. Examples -------- >>> X = [[0], [3], [1]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(n_neighbors=2) >>> neigh.fit(X) # doctest: +ELLIPSIS NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> A = neigh.kneighbors_graph(X) >>> A.toarray() array([[ 1., 0., 1.], [ 0., 1., 1.], [ 1., 0., 1.]]) See also -------- NearestNeighbors.radius_neighbors_graph """ if n_neighbors is None: n_neighbors = self.n_neighbors # kneighbors does the None handling. if X is not None: X = check_array(X, accept_sparse='csr') n_samples1 = X.shape[0] else: n_samples1 = self._fit_X.shape[0] n_samples2 = self._fit_X.shape[0] n_nonzero = n_samples1 * n_neighbors A_indptr = np.arange(0, n_nonzero + 1, n_neighbors) # construct CSR matrix representation of the k-NN graph if mode == 'connectivity': A_data = np.ones(n_samples1 * n_neighbors) A_ind = self.kneighbors(X, n_neighbors, return_distance=False) elif mode == 'distance': A_data, A_ind = self.kneighbors( X, n_neighbors, return_distance=True) A_data = np.ravel(A_data) else: raise ValueError( 'Unsupported mode, must be one of "connectivity" ' 'or "distance" but got "%s" instead' % mode) kneighbors_graph = csr_matrix((A_data, A_ind.ravel(), A_indptr), shape=(n_samples1, n_samples2)) return kneighbors_graph class RadiusNeighborsMixin(object): """Mixin for radius-based neighbors searches""" def radius_neighbors(self, X=None, radius=None, return_distance=True): """Finds the neighbors within a given radius of a point or points. Return the indices and distances of each point from the dataset lying in a ball with size ``radius`` around the points of the query array. Points lying on the boundary are included in the results. The result points are *not* necessarily sorted by distance to their query point. Parameters ---------- X : array-like, (n_samples, n_features), optional The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. radius : float Limiting distance of neighbors to return. (default is the value passed to the constructor). return_distance : boolean, optional. Defaults to True. If False, distances will not be returned Returns ------- dist : array, shape (n_samples,) of arrays Array representing the distances to each point, only present if return_distance=True. The distance values are computed according to the ``metric`` constructor parameter. ind : array, shape (n_samples,) of arrays An array of arrays of indices of the approximate nearest points from the population matrix that lie within a ball of size ``radius`` around the query points. Examples -------- In the following example, we construct a NeighborsClassifier class from an array representing our data set and ask who's the closest point to [1, 1, 1]: >>> import numpy as np >>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(radius=1.6) >>> neigh.fit(samples) # doctest: +ELLIPSIS NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> rng = neigh.radius_neighbors([1., 1., 1.]) >>> print(np.asarray(rng[0][0])) # doctest: +ELLIPSIS [ 1.5 0.5] >>> print(np.asarray(rng[1][0])) # doctest: +ELLIPSIS [1 2] The first array returned contains the distances to all points which are closer than 1.6, while the second array returned contains their indices. In general, multiple points can be queried at the same time. Notes ----- Because the number of neighbors of each point is not necessarily equal, the results for multiple query points cannot be fit in a standard data array. For efficiency, `radius_neighbors` returns arrays of objects, where each object is a 1D array of indices or distances. """ if self._fit_method is None: raise NotFittedError("Must fit neighbors before querying.") if X is not None: query_is_train = False X = check_array(X, accept_sparse='csr') else: query_is_train = True X = self._fit_X if radius is None: radius = self.radius n_samples = X.shape[0] if self._fit_method == 'brute': # for efficiency, use squared euclidean distances if self.effective_metric_ == 'euclidean': dist = pairwise_distances(X, self._fit_X, 'euclidean', squared=True) radius *= radius else: dist = pairwise_distances(X, self._fit_X, self.effective_metric_, **self.effective_metric_params_) neigh_ind_list = [np.where(d <= radius)[0] for d in dist] # See https://github.com/numpy/numpy/issues/5456 # if you want to understand why this is initialized this way. neigh_ind = np.empty(n_samples, dtype='object') neigh_ind[:] = neigh_ind_list if return_distance: dist_array = np.empty(n_samples, dtype='object') if self.effective_metric_ == 'euclidean': dist_list = [np.sqrt(d[neigh_ind[i]]) for i, d in enumerate(dist)] else: dist_list = [d[neigh_ind[i]] for i, d in enumerate(dist)] dist_array[:] = dist_list results = dist_array, neigh_ind else: results = neigh_ind elif self._fit_method in ['ball_tree', 'kd_tree']: if issparse(X): raise ValueError( "%s does not work with sparse matrices. Densify the data, " "or set algorithm='brute'" % self._fit_method) results = self._tree.query_radius(X, radius, return_distance=return_distance) if return_distance: results = results[::-1] else: raise ValueError("internal: _fit_method not recognized") if not query_is_train: return results else: # If the query data is the same as the indexed data, we would like # to ignore the first nearest neighbor of every sample, i.e # the sample itself. if return_distance: dist, neigh_ind = results else: neigh_ind = results for ind, ind_neighbor in enumerate(neigh_ind): mask = ind_neighbor != ind neigh_ind[ind] = ind_neighbor[mask] if return_distance: dist[ind] = dist[ind][mask] if return_distance: return dist, neigh_ind return neigh_ind def radius_neighbors_graph(self, X=None, radius=None, mode='connectivity'): """Computes the (weighted) graph of Neighbors for points in X Neighborhoods are restricted the points at a distance lower than radius. Parameters ---------- X : array-like, shape = [n_samples, n_features], optional The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. radius : float Radius of neighborhoods. (default is the value passed to the constructor). mode : {'connectivity', 'distance'}, optional Type of returned matrix: 'connectivity' will return the connectivity matrix with ones and zeros, in 'distance' the edges are Euclidean distance between points. Returns ------- A : sparse matrix in CSR format, shape = [n_samples, n_samples] A[i, j] is assigned the weight of edge that connects i to j. Examples -------- >>> X = [[0], [3], [1]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(radius=1.5) >>> neigh.fit(X) # doctest: +ELLIPSIS NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> A = neigh.radius_neighbors_graph(X) >>> A.toarray() array([[ 1., 0., 1.], [ 0., 1., 0.], [ 1., 0., 1.]]) See also -------- kneighbors_graph """ if X is not None: X = check_array(X, accept_sparse=['csr', 'csc', 'coo']) n_samples2 = self._fit_X.shape[0] if radius is None: radius = self.radius # construct CSR matrix representation of the NN graph if mode == 'connectivity': A_ind = self.radius_neighbors(X, radius, return_distance=False) A_data = None elif mode == 'distance': dist, A_ind = self.radius_neighbors(X, radius, return_distance=True) A_data = np.concatenate(list(dist)) else: raise ValueError( 'Unsupported mode, must be one of "connectivity", ' 'or "distance" but got %s instead' % mode) n_samples1 = A_ind.shape[0] n_neighbors = np.array([len(a) for a in A_ind]) A_ind = np.concatenate(list(A_ind)) if A_data is None: A_data = np.ones(len(A_ind)) A_indptr = np.concatenate((np.zeros(1, dtype=int), np.cumsum(n_neighbors))) return csr_matrix((A_data, A_ind, A_indptr), shape=(n_samples1, n_samples2)) class SupervisedFloatMixin(object): def fit(self, X, y): """Fit the model using X as training data and y as target values Parameters ---------- X : {array-like, sparse matrix, BallTree, KDTree} Training data. If array or matrix, shape = [n_samples, n_features] y : {array-like, sparse matrix} Target values, array of float values, shape = [n_samples] or [n_samples, n_outputs] """ if not isinstance(X, (KDTree, BallTree)): X, y = check_X_y(X, y, "csr", multi_output=True) self._y = y return self._fit(X) class SupervisedIntegerMixin(object): def fit(self, X, y): """Fit the model using X as training data and y as target values Parameters ---------- X : {array-like, sparse matrix, BallTree, KDTree} Training data. If array or matrix, shape = [n_samples, n_features] y : {array-like, sparse matrix} Target values of shape = [n_samples] or [n_samples, n_outputs] """ if not isinstance(X, (KDTree, BallTree)): X, y = check_X_y(X, y, "csr", multi_output=True) if y.ndim == 1 or y.ndim == 2 and y.shape[1] == 1: if y.ndim != 1: warnings.warn("A column-vector y was passed when a 1d array " "was expected. Please change the shape of y to " "(n_samples, ), for example using ravel().", DataConversionWarning, stacklevel=2) self.outputs_2d_ = False y = y.reshape((-1, 1)) else: self.outputs_2d_ = True self.classes_ = [] self._y = np.empty(y.shape, dtype=np.int) for k in range(self._y.shape[1]): classes, self._y[:, k] = np.unique(y[:, k], return_inverse=True) self.classes_.append(classes) if not self.outputs_2d_: self.classes_ = self.classes_[0] self._y = self._y.ravel() return self._fit(X) class UnsupervisedMixin(object): def fit(self, X, y=None): """Fit the model using X as training data Parameters ---------- X : {array-like, sparse matrix, BallTree, KDTree} Training data. If array or matrix, shape = [n_samples, n_features] """ return self._fit(X)
bsd-3-clause
trungnt13/scikit-learn
examples/neural_networks/plot_rbm_logistic_classification.py
258
4609
""" ============================================================== Restricted Boltzmann Machine features for digit classification ============================================================== For greyscale image data where pixel values can be interpreted as degrees of blackness on a white background, like handwritten digit recognition, the Bernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM <sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear feature extraction. In order to learn good latent representations from a small dataset, we artificially generate more labeled data by perturbing the training data with linear shifts of 1 pixel in each direction. This example shows how to build a classification pipeline with a BernoulliRBM feature extractor and a :class:`LogisticRegression <sklearn.linear_model.LogisticRegression>` classifier. The hyperparameters of the entire model (learning rate, hidden layer size, regularization) were optimized by grid search, but the search is not reproduced here because of runtime constraints. Logistic regression on raw pixel values is presented for comparison. The example shows that the features extracted by the BernoulliRBM help improve the classification accuracy. """ from __future__ import print_function print(__doc__) # Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve # License: BSD import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import convolve from sklearn import linear_model, datasets, metrics from sklearn.cross_validation import train_test_split from sklearn.neural_network import BernoulliRBM from sklearn.pipeline import Pipeline ############################################################################### # Setting up def nudge_dataset(X, Y): """ This produces a dataset 5 times bigger than the original one, by moving the 8x8 images in X around by 1px to left, right, down, up """ direction_vectors = [ [[0, 1, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 1, 0]]] shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant', weights=w).ravel() X = np.concatenate([X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]) Y = np.concatenate([Y for _ in range(5)], axis=0) return X, Y # Load Data digits = datasets.load_digits() X = np.asarray(digits.data, 'float32') X, Y = nudge_dataset(X, digits.target) X = (X - np.min(X, 0)) / (np.max(X, 0) + 0.0001) # 0-1 scaling X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0) # Models we will use logistic = linear_model.LogisticRegression() rbm = BernoulliRBM(random_state=0, verbose=True) classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logistic)]) ############################################################################### # Training # Hyper-parameters. These were set by cross-validation, # using a GridSearchCV. Here we are not performing cross-validation to # save time. rbm.learning_rate = 0.06 rbm.n_iter = 20 # More components tend to give better prediction performance, but larger # fitting time rbm.n_components = 100 logistic.C = 6000.0 # Training RBM-Logistic Pipeline classifier.fit(X_train, Y_train) # Training Logistic regression logistic_classifier = linear_model.LogisticRegression(C=100.0) logistic_classifier.fit(X_train, Y_train) ############################################################################### # Evaluation print() print("Logistic regression using RBM features:\n%s\n" % ( metrics.classification_report( Y_test, classifier.predict(X_test)))) print("Logistic regression using raw pixel features:\n%s\n" % ( metrics.classification_report( Y_test, logistic_classifier.predict(X_test)))) ############################################################################### # Plotting plt.figure(figsize=(4.2, 4)) for i, comp in enumerate(rbm.components_): plt.subplot(10, 10, i + 1) plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle('100 components extracted by RBM', fontsize=16) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show()
bsd-3-clause
ProjecteGDSA33/SED
super_vector_task2.py
1
5946
# -*- coding: cp1252 -*- import os import xml.etree.ElementTree as ET import numpy as np import cv2 import pandas as pnd import random # # # # # # # # # # # # PART NOVA from sklearn.feature_extraction.text import TfidfVectorizer class Imatge: iden = '' # identificador de la imatge tags = None # llista dels tags sol = '' # solució del fitxer de solucions # directori metadades xml dir_xml = "C:/Users/marc/Documents/GDSA/Projecte/train/metadata/xml/sed2013_task2_dataset_train.xml" # directori imatges jpg dir_img = "C:/Users/marc/Documents/GDSA/Projecte/Imatges/class" # direcotri solucions csv dir_sol = "C:/Users/marc/Documents/GDSA/Projecte/train/annotation/sed2013_task2_dataset_train_gs.csv" nom_imatges = os.listdir(dir_img) sol_imatges = pnd.read_csv(dir_sol,sep='\t') # recordar posar totes les imatges en la carpeta! supervector = set() concert = set() conference = set() exhibition = set() fashion = set() other = set() protest = set() sports = set() theater_dance = set() non_event = set() # fi de les variables del supervector tree = ET.parse(dir_xml) root = tree.getroot() # l'etiqueta "arrel" es <photos> en el nostre xml # # # # # # # # # # # # PART NOVA # # # # # # # # # # # # total = range(len(root)) # entrenament = random.sample(total,len(root)*70/100) # classificacio = list(set(total) - set(entrenament)) # <- Important, en el fitxer task2 es classifiquen # # # # # # # # # # # # # # # # # # # # # # # # # # # # # aquests index for i in entrenament: # # # # PART NOVA foto = root[i] imatge = Imatge() # variable tipus Imatge imatge.tags = [] # inicialitzar les llistes id_tags = [] # variable individual per guardar el identificador i la llista det tags d'una # foto en la pos [0] tenim el id, en la resta de pos tenim els tags att_foto = foto.attrib # tots els atributs de la imatge imatge.iden = att_foto["id"] eti_foto = foto.getchildren() tags = eti_foto[1].getchildren() # entrem en l'etiqueta tags for tag in tags: if (tag.text.encode('ascii','ignore').isdigit() == False and len(tag.text.encode('ascii','ignore')) > 1): # # # # # PART NOVA imatge.tags.append(tag.text.encode('ascii','ignore')) # anem guardant els tags en la variable individual sol_trobada = sol_imatges[sol_imatges.document_id == imatge.iden] # un cop ja guardem la solució l'eliminem de la llista per reduir temps de cerca sol_imatges = sol_imatges[sol_imatges.document_id != imatge.iden] imatge.sol = sol_trobada.event_type.to_string().split()[-1].encode('ascii','ignore') # guardem la solució en la imatge # creem el supervector for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): supervector.add(tag) if imatge.sol == 'concert': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): concert.add(tag) elif imatge.sol == 'conference': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): conference.add(tag) elif imatge.sol == 'exhibition': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): exhibition.add(tag) elif imatge.sol == 'fashion': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): fashion.add(tag) elif imatge.sol == 'other': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): other.add(tag) elif imatge.sol == 'protest': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): protest.add(tag) elif imatge.sol == 'sports': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): sports.add(tag) elif imatge.sol == 'theater_dance': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): theater_dance.add(tag) elif imatge.sol == 'non_event': for tag in imatge.tags: if (tag.isdigit() == False and len(tag) > 1): non_event.add(tag) # fi de la creacio del supervector i += 1 #ordenem alfabeticament i guardem en el .txt supervector = sorted(supervector) concert = sorted(concert) conference = sorted(conference) exhibition = sorted(exhibition) fashion = sorted(fashion) other = sorted(other) protest = sorted(protest) sports = sorted(sports) theater_dance = sorted(theater_dance) non_event = sorted(non_event) text_file = open("supervector_task2.txt", "w") # # # # # # PART NOVA # # # # # # # for i in classificacio: # text_file.write(" "+str(i)) # text_file.write("\n") # # # # # # # # # # # # # # # # # # # text_file.write("supervector") for i in supervector: text_file.write(" "+i) text_file.write("\n") text_file.write("concert") for i in concert: text_file.write(" "+i) text_file.write("\n") text_file.write("conference") for i in conference: text_file.write(" "+i) text_file.write("\n") text_file.write("exhibition") for i in exhibition: text_file.write(" "+i) text_file.write("\n") text_file.write("fashion") for i in fashion: text_file.write(" "+i) text_file.write("\n") text_file.write("other") for i in other: text_file.write(" "+i) text_file.write("\n") text_file.write("protest") for i in protest: text_file.write(" "+i) text_file.write("\n") text_file.write("sports") for i in sports: text_file.write(" "+i) text_file.write("\n") text_file.write("theater_dance") for i in theater_dance: text_file.write(" "+i) text_file.write("\n") text_file.write("non_event") for i in non_event: text_file.write(" "+i) text_file.close()
gpl-3.0
dougthor42/OWT_WM_View
build_executables.py
1
4362
# -*- coding: utf-8 -*- """ Created on Mon Jun 22 13:08:24 2015 @author: dthor """ # --------------------------------------------------------------------------- ### Imports # --------------------------------------------------------------------------- # Standard Library import sys import logging import os.path import glob # Third Party from cx_Freeze import setup, Executable # Package / Application from owt_wm_view import (__version__, __project_url__, __project_name__, __description__, __long_descr__, ) # --------------------------------------------------------------------------- ### General Setup # --------------------------------------------------------------------------- # turn off logging if we're going to build a distribution logging.disable(logging.CRITICAL) # --------------------------------------------------------------------------- ### build_exe Setup # --------------------------------------------------------------------------- # included packages and their submodules packages = [ # 'scipy.stats', # 'scipy', 'numpy', 'keyring', 'cryptography', 'cffi', # needed by cryptography 'win32timezone', # needed by keyring? ] # included modules includes = [ "owt_wm_view/mask_constants", ] # Files to include (and their destinations) include_files = [ # "pybank\\test_database.db", ("log\\README.txt", "log\\README.txt"), # (source, dest) # ("C:\\WinPython34_x64\\python-3.4.3.amd64\\Lib\\site-packages\\scipy\\special\\_ufuncs.pyd", "_ufuncs.pyd"), # ("C:\\WinPython34_x64\\python-3.4.3.amd64\\Lib\\site-packages\\scipy\\special\\_ufuncs_cxx.pyd", "_ufuncs_cxx.pyd"), ] # list of names of files to include when determining dependencies of # binary files that would normally be excluded; note that version # numbers that normally follow the shared object extension are # stripped prior to performing the comparison bin_includes = [ ] # list of names of moduels to exclude excludes = [ "tkinter", "ssl", "pillow", "pil", "PyQt4", "multiprocessing", "bz2", "coverage", "zmq", ] mplBackendsPath = os.path.join(os.path.split(sys.executable)[0], "Lib\\site-packages\\matplotlib\\backends\\backend_*") fileList = glob.glob(mplBackendsPath) moduleList = [] for mod in fileList: module = os.path.splitext(os.path.basename(mod))[0] if not module in ("backend_wxagg", "backend_wx", "backend_agg"): moduleList.append("matplotlib.backends." + module) #excludes += moduleList # Options for build_exe build_exe_opts = { "packages": packages, "includes": includes, "include_files": include_files, # "bin_includes": bin_includes, "excludes": excludes, "silent": True, "include_msvcr": True, } # --------------------------------------------------------------------------- ### Executable Definitions # --------------------------------------------------------------------------- file_to_build = "owt_wm_view\\owt_wafer_map_viewer.py" # Application Base base = None #if sys.platform == 'win32': # uncomment this to remove console window. # base = "Win32GUI" exe1 = Executable(file_to_build, base=base, targetName="WMView.exe", ) # List of which executables to build. exes_to_build = [ exe1, ] # --------------------------------------------------------------------------- ### setup() # --------------------------------------------------------------------------- setup( name=__project_name__, version=__version__, description=__description__, options={"build_exe": build_exe_opts}, executables=exes_to_build, )
gpl-3.0
science-of-imagination/nengo-buffer
Project/mental_translation_training_vertical.py
1
8739
#import matplotlib.pyplot as plt #%matplotlib inline import nengo import numpy as np import scipy.ndimage #import matplotlib.animation as animation #from matplotlib import pylab from PIL import Image import nengo.spa as spa import cPickle import random from nengo_extras.data import load_mnist from nengo_extras.vision import Gabor, Mask #Encode categorical integer features using a one-hot aka one-of-K scheme. def one_hot(labels, c=None): assert labels.ndim == 1 n = labels.shape[0] c = len(np.unique(labels)) if c is None else c y = np.zeros((n, c)) y[np.arange(n), labels] = 1 return y # --- load the data img_rows, img_cols = 28, 28 (X_train, y_train), (X_test, y_test) = load_mnist() X_train = 2 * X_train - 1 # normalize to -1 to 1 X_test = 2 * X_test - 1 # normalize to -1 to 1 train_targets = one_hot(y_train, 10) test_targets = one_hot(y_test, 10) rng = np.random.RandomState(9) # --- set up network parameters #Want to encode and decode the image n_vis = X_train.shape[1] n_out = X_train.shape[1] #number of neurons/dimensions of semantic pointer n_hid = 5000 #Try with more neurons for more accuracy #Want the encoding/decoding done on the training images ens_params = dict( eval_points=X_train, neuron_type=nengo.LIF(), #Why not use LIF? originally used LIFRate() intercepts=nengo.dists.Choice([-0.5]), max_rates=nengo.dists.Choice([100]), ) #Least-squares solver with L2 regularization. solver = nengo.solvers.LstsqL2(reg=0.01) #solver = nengo.solvers.LstsqL2(reg=0.0001) solver2 = nengo.solvers.LstsqL2(reg=0.01) #network that generates the weight matrices between neuron activity and images and the labels with nengo.Network(seed=3) as model: a = nengo.Ensemble(n_hid, n_vis, seed=3, **ens_params) v = nengo.Node(size_in=n_out) conn = nengo.Connection( a, v, synapse=None, eval_points=X_train, function=X_train,#want the same thing out (identity) solver=solver) v2 = nengo.Node(size_in=train_targets.shape[1]) conn2 = nengo.Connection( a, v2, synapse=None, eval_points=X_train, function=train_targets, #Want to get the labels out solver=solver2) # linear filter used for edge detection as encoders, more plausible for human visual system encoders = Gabor().generate(n_hid, (11, 11), rng=rng) encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True) #Set the ensembles encoders to this a.encoders = encoders #Get the one hot labels for the images def get_outs(sim, images): #The activity of the neurons when an image is given as input _, acts = nengo.utils.ensemble.tuning_curves(a, sim, inputs=images) #The activity multiplied by the weight matrix (calculated in the network) to give the one-hot labels return np.dot(acts, sim.data[conn2].weights.T) #Get the neuron activity of an image or group of images (this is the semantic pointer in this case) def get_activities(sim, images): _, acts = nengo.utils.ensemble.tuning_curves(a, sim, inputs=images) return acts #Get the representation of the image after it has gone through the encoders (Gabor filters) but before it is in the neurons #This must be computed to create the weight matrix for rotation from neuron activity to this step # This allows a recurrent connection to be made from the neurons to themselves later def get_encoder_outputs(sim,images): #Pass the images through the encoders outs = np.dot(images,sim.data[a].encoders.T) #before the neurons return outs dim =28 #Shift an image def translate(img,x,y): newImg = scipy.ndimage.interpolation.shift(np.reshape(img, (dim,dim), 'F'),(x,y), cval=-1) return newImg.T.ravel() #Images to train, starting at random translation orig_imgs = X_train[:100000].copy() for img in orig_imgs: img[:] = translate(img,random.randint(-6,6),random.randint(-6,6)) #Images translated up a fixed amount from the original random translation translate_up_imgs = orig_imgs.copy() for img in translate_up_imgs: img[:] = translate(img,0,-1) #Images translated down a fixed amount from the original random translation translate_down_imgs = orig_imgs.copy() for img in translate_down_imgs: img[:] = translate(img,0,1) ''' #Images translated right a fixed amount from the original random translation translate_right_imgs = orig_imgs.copy() for img in translate_right_imgs: img[:] = translate(img,1,0) #Images translated left a fixed amount from the original random translation translate_left_imgs = orig_imgs.copy() for img in translate_left_imgs: img[:] = translate(img,-1,0) ''' #Images not used for training, but for testing (all at random translations) test_imgs = X_test[:1000].copy() for img in test_imgs: img[:] = translate(img,random.randint(-4,4),random.randint(-4,4)) with nengo.Simulator(model) as sim: #Neuron activities of different mnist images #The semantic pointers orig_acts = get_activities(sim,orig_imgs) translate_up_acts = get_activities(sim,translate_up_imgs) translate_down_acts = get_activities(sim,translate_down_imgs) ''' translate_left_acts = get_activities(sim,translate_left_imgs) translate_right_acts = get_activities(sim,translate_right_imgs) ''' test_acts = get_activities(sim,test_imgs) X_test_acts = get_activities(sim,X_test) labels_out = get_outs(sim,X_test) translate_up_after_encoders = get_encoder_outputs(sim,translate_up_imgs) translate_down_after_encoders = get_encoder_outputs(sim,translate_down_imgs) ''' translate_left_after_encoders = get_encoder_outputs(sim,translate_left_imgs) translate_right_after_encoders = get_encoder_outputs(sim,translate_right_imgs) ''' #solvers for a learning rule solver_translate_up = nengo.solvers.LstsqL2(reg=1e-8) solver_translate_down = nengo.solvers.LstsqL2(reg=1e-8) ''' solver_translate_left = nengo.solvers.LstsqL2(reg=1e-8) solver_translate_right = nengo.solvers.LstsqL2(reg=1e-8) ''' solver_word = nengo.solvers.LstsqL2(reg=1e-8) solver_translate_up_encoder = nengo.solvers.LstsqL2(reg=1e-8) solver_translate_down_encoder = nengo.solvers.LstsqL2(reg=1e-8) ''' solver_translate_left_encoder = nengo.solvers.LstsqL2(reg=1e-8) solver_translate_right_encoder = nengo.solvers.LstsqL2(reg=1e-8) ''' #find weight matrix between neuron activity of the original image and the translated image #weights returns a tuple including information about learning process, just want the weight matrix translate_up_weights,_ = solver_translate_up(orig_acts, translate_up_acts) translate_down_weights,_ = solver_translate_down(orig_acts, translate_down_acts) ''' translate_left_weights,_ = solver_translate_left(orig_acts, translate_left_acts) translate_right_weights,_ = solver_translate_right(orig_acts, translate_right_acts) ''' #find weight matrix between labels and neuron activity label_weights,_ = solver_word(labels_out,X_test_acts) translate_up_after_encoder_weights,_ = solver_translate_up_encoder(orig_acts,translate_up_after_encoders) translate_down_after_encoder_weights,_ = solver_translate_down_encoder(orig_acts,translate_down_after_encoders) ''' translate_left_after_encoder_weights,_ = solver_translate_left_encoder(orig_acts,translate_left_after_encoders) translate_right_after_encoder_weights,_ = solver_translate_right_encoder(orig_acts,translate_right_after_encoders) ''' #Saving filename = "activity_to_img_weights_translate" + str(n_hid) +".p" cPickle.dump(sim.data[conn].weights.T, open( filename, "wb" ) ) filename = "translate_up_weights" + str(n_hid) +".p" cPickle.dump(translate_up_weights, open( filename, "wb" ) ) filename = "translate_down_weights" + str(n_hid) +".p" cPickle.dump(translate_down_weights, open( filename, "wb" ) ) ''' filename = "translate_left_weights" + str(n_hid) +".p" cPickle.dump(translate_left_weights, open( filename, "wb" ) ) filename = "translate_right_weights" + str(n_hid) +".p" cPickle.dump(translate_right_weights, open( filename, "wb" ) ) ''' filename = "translate_up_after_encoder_weights" + str(n_hid) +".p" cPickle.dump(translate_up_after_encoder_weights, open( filename, "wb" ) ) filename = "translate_down_after_encoder_weights" + str(n_hid) +".p" cPickle.dump(translate_down_after_encoder_weights, open( filename, "wb" ) ) ''' filename = "translate_left_after_encoder_weights" + str(n_hid) +".p" cPickle.dump(translate_left_after_encoder_weights, open( filename, "wb" ) ) filename = "translate_right_after_encoder_weights" + str(n_hid) +".p" cPickle.dump(translate_right_after_encoder_weights, open( filename, "wb" ) ) '''
gpl-3.0
sauloal/cnidaria
scripts/venv/lib/python2.7/site-packages/cogent/draw/util.py
1
34057
#/usr/bin/env python """Provides different kinds of generally useful plots using matplotlib. Some of these plots are enhancements of the matplotlib versions (e.g. hist() or copies of plot types that have been withdrawn from matplotlib (e.g. scatter_classic). Notable capabilities include automated series coloring and drawing of regression lines, the ability to plot scatterplots with correlated histograms, etc. See individual docstrings for more info. """ from __future__ import division from matplotlib import use, rc, rcParams __author__ = "Stephanie Wilson" __copyright__ = "Copyright 2007-2012, The Cogent Project" __credits__ = ["Rob Knight", "Stephanie Wilson"] __license__ = "GPL" __version__ = "1.5.3" __maintainer__ = "Rob Knight" __email__ = "[email protected]" __status__ = "Production" #use('Agg') #suppress graphical rendering #rc('text', usetex=True) rc('font', family='serif') #required to match latex text and equations try: import Image import ImageFilter except ImportError: Image = ImageFilter = None #only used to smooth contours: skip if no PIL from numpy import array, shape, fromstring, sqrt, zeros, pi from cogent.core.usage import UnsafeCodonUsage as CodonUsage from cogent.maths.stats.test import regress, correlation from pylab import plot, cm, savefig, gca, gcf, arange, text, subplot, \ asarray, iterable, searchsorted, sort, diff, concatenate, silent_list, \ is_string_like, Circle, mean, std, normpdf, legend, contourf, \ colorbar, ravel, imshow, contour from matplotlib.font_manager import FontProperties from os.path import split #module-level constants standard_series_colors=['k','r','g','b', 'm','c'] def hist(x, bins=10, normed='height', bottom=0, \ align='edge', orientation='vertical', width=None, axes=None, **kwargs): """Just like the matplotlib hist, but normalizes bar heights to 1. axes uses gca() by default (built-in hist is a method of Axes). Original docs from matplotlib: HIST(x, bins=10, normed=0, bottom=0, orientiation='vertical', **kwargs) Compute the histogram of x. bins is either an integer number of bins or a sequence giving the bins. x are the data to be binned. The return values is (n, bins, patches) If normed is true, the first element of the return tuple will be the counts normalized to form a probability density, ie, n/(len(x)*dbin) orientation = 'horizontal' | 'vertical'. If horizontal, barh will be used and the "bottom" kwarg will be the left. width: the width of the bars. If None, automatically compute the width. kwargs are used to update the properties of the hist bars """ if axes is None: axes = gca() if not axes._hold: axes.cla() n, bins = norm_hist_bins(x, bins, normed) if width is None: width = 0.9*(bins[1]-bins[0]) if orientation=='horizontal': patches = axes.barh(bins, n, height=width, left=bottom, \ align=align) else: patches = axes.bar(bins, n, width=width, bottom=bottom, \ align=align) for p in patches: p.update(kwargs) return n, bins, silent_list('Patch', patches) def norm_hist_bins(y, bins=10, normed='height'): """Just like the matplotlib mlab.hist, but can normalize by height. normed can be 'area' (produces matplotlib behavior, area is 1), any False value (no normalization), or any True value (normalization). Original docs from matplotlib: Return the histogram of y with bins equally sized bins. If bins is an array, use the bins. Return value is (n,x) where n is the count for each bin in x If normed is False, return the counts in the first element of the return tuple. If normed is True, return the probability density n/(len(y)*dbin) If y has rank>1, it will be raveled Credits: the Numeric 22 documentation """ y = asarray(y) if len(y.shape)>1: y = ravel(y) if not iterable(bins): ymin, ymax = min(y), max(y) if ymin==ymax: ymin -= 0.5 ymax += 0.5 if bins==1: bins=ymax dy = (ymax-ymin)/bins bins = ymin + dy*arange(bins) n = searchsorted(sort(y), bins) n = diff(concatenate([n, [len(y)]])) if normed: if normed == 'area': db = bins[1]-bins[0] else: db = 1.0 return 1/(len(y)*db)*n, bins else: return n, bins def scatter_classic(x, y, s=None, c='b'): """ SCATTER_CLASSIC(x, y, s=None, c='b') Make a scatter plot of x versus y. s is a size (in data coords) and can be either a scalar or an array of the same length as x or y. c is a color and can be a single color format string or an length(x) array of intensities which will be mapped by the colormap jet. If size is None a default size will be used Copied from older version of matplotlib -- removed in version 0.9.1 for whatever reason. """ self = gca() if not self._hold: self.cla() if is_string_like(c): c = [c]*len(x) elif not iterable(c): c = [c]*len(x) else: norm = normalize() norm(c) c = cm.jet(c) if s is None: s = [abs(0.015*(amax(y)-amin(y)))]*len(x) elif not iterable(s): s = [s]*len(x) if len(c)!=len(x): raise ValueError, 'c and x are not equal lengths' if len(s)!=len(x): raise ValueError, 's and x are not equal lengths' patches = [] for thisX, thisY, thisS, thisC in zip(x,y,s,c): circ = Circle( (thisX, thisY), radius=thisS, ) circ.set_facecolor(thisC) self.add_patch(circ) patches.append(circ) self.autoscale_view() return patches def as_species(name, leave_path=False): """Cleans up a filename into a species name, italicizing it in latex.""" #trim extension if present dot_location = name.rfind('.') if dot_location > -1: name = name[:dot_location] #get rid of _small if present -- used for debugging if name.endswith('_small'): name = name[:-len('_small')] if name.endswith('_codon_usage'): name = name[:-len('_codon_usage')] #get rid of path unless told to leave it name = split(name)[-1] #replace underscores with spaces name = name.replace('_', ' ') #make sure the first letter of the genus is caps, and not the first letter #of the species fields = name.split() fields[0] = fields[0].title() #assume second field is species name if len(fields) > 1: fields[1] = fields[1].lower() binomial = ' '.join(fields) if rcParams.get('text.usetex'): binomial = r'\emph{' + binomial + '}' return binomial def frac_to_psq(frac, graph_size): """Converts diameter as fraction of graph to points squared for scatter. frac: fraction of graph (e.g. .01 is 1% of graph size) graph_size: graph size in inches """ points = frac * graph_size * 72 return pi * (points/2.0)**2 def init_graph_display(title=None, aux_title=None, size=4.0, \ graph_shape='sqr', graph_grid=None, x_label='', y_label='', \ dark=False, with_parens=True, prob_axes=True, axes=None, num_genes=None): """Initializes a range of graph settings for standard plots. These settings include: - font sizes based on the size of the graph - graph shape - grid, including lines for x=y or at x and y = 0.5 - title, auxillary title, and x and y axis labels Parameters: title: displayed on left of graph, at the top, latex-format string aux_title: displayed on top right of graph, latex-format string. typically used for number of genes. size: size of graph, in inches graph_shape: 'sqr' for square graphs, 'rect' for graphs that include a colorbar, 3to1: width 3 to height 1. graph_grid: background grid for the graph. Currently recognized grids are '/' (line at x=y) and 't' (cross at x=.5 and y=.5). x_label: label for x axis, latex-format string. y_label: label for y axis, latex-format string. dark: set to True if dark background, reverses text and tick colors. with_parens: if True (default), puts parens around auxillary title returns font, label_font_size (for use in producing additional labels in calling function). """ if dark: color='w' else: color='k' rect_scale_factor = 1.28 #need to allow for legend while keeping graph #square; empirically determined at 1.28 font_size = int(size*3-1) #want 11pt font w/ default graph size 4" sqr label_scale_factor = 0.8 label_font_size = font_size * label_scale_factor label_offset = label_font_size * 0.5 axis_label_font={'fontsize':font_size} font={'fontsize':font_size, 'color':color} if graph_shape == 'sqr': gcf().set_size_inches(size,size) elif graph_shape == 'rect': #scaling for sqr graphs with colorbar gcf().set_size_inches(size*rect_scale_factor,size) elif graph_shape == '3to1': gcf().set_size_inches(3*size, size) elif graph_shape == '2to1': gcf().set_size_inches(2*size, size) else: raise ValueError, "Got unknown graph shape %s" % graph_shape #set or create axes if axes is None: axes = gca() min_x, max_x =axes.get_xlim() min_y, max_y = axes.get_ylim() x_range = abs(max_x - min_x) y_range = abs(max_y - min_y) min_offset = (x_range * 0.05) + min_x #minimum offset, e.g. for text max_offset = max_y - (y_range * 0.05) #draw grid manually: these are in data coordinates. if graph_grid == 't': #grid lines at 0.5 on each axis, horiz & vertic axes.axvline(x=.5, ymin=0, ymax=1, color=color, linestyle=':') axes.axhline(y=.5, xmin=0, xmax=1, color=color, linestyle=':') elif graph_grid == '/': #diagonal gridlines from 0,0 to 1,1. axes.plot([0,1], color=color, linestyle=':') else: pass #ignore other choices #remove default grid axes.grid(False) #set x and y labels axes.set_ylabel(y_label, axis_label_font) axes.set_xlabel(x_label, axis_label_font) #add title/aux_title to graph directly. Note that we want #the tops of these to be fixed, and we want the label to be #left-justified and the number of genes to be right justified, #so that it still works when we resize the graph. if title is not None: axes.text(min_offset, max_offset, str(title), font, \ verticalalignment='top', horizontalalignment='left') #use num_genes as aux_title by default aux_title = num_genes or aux_title if aux_title is not None: if with_parens: aux_title='('+str(aux_title)+')' axes.text(max_offset, max_offset, str(aux_title), font, verticalalignment='top', horizontalalignment='right') if prob_axes: init_ticks(axes, label_font_size, dark) #set x and y label offsets -- currently though rcParams, but should be #able to do at instance level? #rc('xtick.major', pad=label_offset) #rc('ytick.major', pad=label_offset) return font, label_font_size def init_ticks(axes=None, label_font_size=None, dark=False): """Initializes ticks for fingerprint plots or other plots ranging from 0-1. takes axis argument a from a = gca(), or a specified axis sets the ticks to span from 0 to 1 with .1 intervals changes the size of the ticks and the corresponding number labels """ if axes is None: axes = gca() axes.set_xticks(arange(0,1.01,.1),) axes.set_yticks(arange(0,1.01,.1)) #reset sizes for x and y labels x = axes.get_xticklabels() y = axes.get_yticklabels() if label_font_size is not None: for l in axes.get_xticklabels() + axes.get_yticklabels(): l.set_fontsize(label_font_size) #if dark, need to reset color of internal ticks to white if dark: for l in axes.get_xticklines() + axes.get_yticklines(): l.set_markeredgecolor('white') def set_axis_to_probs(axes=None): """sets the axes to span from 0 to 1. Useful for forcing axes to range over probabilities. Axes are sometimes reset by other calls. """ #set axis for probabilities (range 0 to 1) if axes is None: axes = gca() axes.set_xlim([0,1]) axes.set_ylim([0,1]) def plot_regression_line(x,y,line_color='r', axes=None, prob_axes=False, \ axis_range=None): """Plots the regression line, and returns the equation. x and y are the x and y data for a single series line_color is a matplotlib color, will be used for the line axes is the name of the axes the regression will be plotted against prob_axes, if true, forces the axes to be between 0 and 1 range, if not None, forces the axes to be between (xmin, xmax, ymin, ymax). """ if axes is None: axes = gca() m, b = regress(x, y) r, significance = correlation(x,y) #set the a, b, and r values. a is the slope, b is the intercept. r_str = '%0.3g'% (r**2) m_str ='%0.3g' % m b_str = '%0.3g' % b #want to clip the line so it's contained entirely within the graph #coordinates. Basically, we need to find the values of y where x #is at x_min and x_max, and the values of x where y is at y_min and #y_max. #if we didn't set prob_axis or axis_range, just find empirical x and y if (not prob_axes) and (axis_range is None): x1, x2 = min(x), max(x) y1, y2 = m*x1 + b, m*x2 + b x_min, x_max = x1, x2 else: if prob_axes: x_min, x_max = 0, 1 y_min, y_max = 0, 1 else: #axis range must have been set x_min, x_max, y_min, y_max = axis_range #figure out bounds for x_min and y_min y_at_x_min = m*x_min + b if y_at_x_min < y_min: #too low: find x at y_min y1 = y_min x1 = (y_min-b)/m elif y_at_x_min > y_max: #too high: find x at y_max y1 = y_max x1 = (y_max-b)/m else: #just right x1, y1 = x_min, y_at_x_min y_at_x_max = m*x_max + b if y_at_x_max < y_min: #too low: find x at y_min y2 = y_min x2 = (y_min-b)/m elif y_at_x_max > y_max: #too high: find x at y_max y2 = y_max x2 = (y_max-b)/m else: #just right x2, y2 = x_max, y_at_x_max #need to check that the series wasn't entirely in range if (x_min <= x1 <= x_max) and (x_min <= x2 <= x_max): axes.plot([x1,x2],[y1,y2], color=line_color, linewidth=0.5) if b >= 0: sign_str = ' + ' else: sign_str = ' ' equation=''.join(['y= ',m_str,'x',sign_str,b_str,'\nr$^2$=',r_str]) return equation, line_color def add_regression_equations(equations, axes=None, prob_axes=False, \ horizontalalignment='right', verticalalignment='bottom'): """Writes list of regression equations to graph. equations: list of regression equations size: size of the graph in inches """ if axes is None: axes = gca() if prob_axes: min_x, max_x = 0, 1 min_y, max_y = 0, 1 else: min_x, max_x = axes.get_xlim() min_y, max_y = axes.get_ylim() x_range = abs(max_x - min_x) y_range = abs(max_y - min_y) for i, (eq_text, eq_color) in enumerate(equations): axes.text((x_range * 0.98) + min_x, \ (y_range * 0.02 + min_y +(y_range * .1 * i)), \ str(eq_text), \ horizontalalignment=horizontalalignment, \ verticalalignment=verticalalignment, \ color=eq_color) def broadcast(i, n): """Broadcasts i to a vector of length n.""" try: i = list(i) except: i = [i] reps, leftovers = divmod(n, len(i)) return (i * reps) + i[:leftovers] #scatterplot functions and helpers def plot_scatter(data, series_names=None, \ series_color=standard_series_colors, line_color=standard_series_colors,\ alpha=0.25, marker_size=.015, scale_markers=True, show_legend=True,legend_loc='center right', show_regression=True, show_equation=True, prob_axes=False, size=8.0, axes=None, **kwargs): """helper plots one or more series of scatter data of specified color, calls the initializing functions, doesn't print graph takes: plotted_pairs, series_names, show_legend, legend_loc, and **kwargs passed on to init_graph_display (these include title, aux_title, size, graph_shape, graph_grid, x_label, y_label, dark, with_parens). plotted_pairs = (first_pos, second_pos, dot_color, line_color, alpha, show_regression, show_equation) returns the regression str equation (list) if regression is set true suppresses legend if series not named, even if show_legend is True. """ if not axes: axes = gca() #initialize fonts, shape and labels font,label_font_size=init_graph_display(prob_axes=prob_axes, \ size=size, axes=axes, **kwargs) equations = [] #figure out how many series there are, and scale vals accordingly num_series = int(len(data)/2) series_color = broadcast(series_color, num_series) line_color = broadcast(line_color, num_series) alpha = broadcast(alpha, num_series) marker_size = broadcast(marker_size, num_series) if scale_markers: marker_size = [frac_to_psq(m, size) for m in marker_size] series = [] for i in range(num_series): x, y = data[2*i], data[2*i+1] series.append(axes.scatter(x,y,s=marker_size[i],c=series_color[i],\ alpha=alpha[i])) #find the equation and plots the regression line if True if show_regression: equation = plot_regression_line(x,y,line_color[i], axes=axes, \ prob_axes=prob_axes) if show_equation: equations.append(equation) #will be (str, color) tuple #update graph size for new data axes.autoscale_view(tight=True) #print all the regression equations at once -- need to know how many if show_regression: add_regression_equations(equations, axes=axes, prob_axes=prob_axes) #clean up axes if necessary if show_legend and series_names: #suppress legend if series not named axes.legend(series, series_names, legend_loc) if prob_axes: set_axis_to_probs(axes) return equations, font #Contour plots and related functions def plot_filled_contour(plot_data, xy_data=None, show_regression=False, \ show_equation=False, fill_cmap=cm.hot, graph_shape='rect', \ num_contour_lines=10, prob_axes=False, **kwargs): """helper plots one or more series of contour data calls the initializing functions, doesn't output figure takes: plot_data, xy_data, show_regression, show_equation, fill_cmap, and **kwargs passed on to init_graph_display. plot_data = (x_bin, y_bin, data_matrix dot_colors) """ if show_regression: equation = plot_regression_line(xy_data[:,0],xy_data[:,1], \ prob_axes=prob_axes) if show_equation: add_regression_equations([equation]) #init graph display, rectangular due to needed colorbar space init_graph_display(graph_shape=graph_shape, **kwargs) #plots the contour data for x_bin,y_bin,data_matrix in plot_data: contourf(x_bin,y_bin,data_matrix, num_contour_lines, cmap=fill_cmap) #add the colorbar legend to the side colorbar() def plot_contour_lines(plot_data, xy_data=None, show_regression=False, \ show_equation=False, smooth_steps=0, num_contour_lines=10, \ label_contours=False, line_cmap=cm.hot, fill_cmap=cm.gray,dark=True, graph_shape='rect', prob_axes=False, **kwargs): """helper plots one or more series of contour line data calls the initializing functions, doesn't output figure takes: plot_data, xy_data, show_regression, show_equation, smooth, num_contour_lines, label_contours, line_cmap, fill_cmap, graph_shape, and **kwargs passed on to init_graph_display. plot_data = (x_bin, y_bin, data_matrix dot_colors) """ if prob_axes: extent = (0,1,0,1) else: a = gca() extent = a.get_xlim()+a.get_ylim() #init graph display, rectangular due to needed colorbar space init_graph_display(graph_shape=graph_shape, dark=dark, **kwargs) #plots the contour data for x_bin,y_bin,data in plot_data: orig_max = max(ravel(data)) scaled_data = (data/orig_max*255).astype('b') if smooth_steps and (Image is not None): orig_shape = data.shape im = Image.fromstring('L', data.shape, scaled_data) for i in range(smooth_steps): im = im.filter(ImageFilter.BLUR) new_data = fromstring(im.tostring(), 'b') data = reshape(new_data.astype('i')/255.0 * orig_max, orig_shape) if fill_cmap is not None: im = imshow(data, interpolation='bicubic', extent=extent, \ origin='lower', cmap=fill_cmap) result=contour(x_bin,y_bin,data, num_contour_lines, origin='lower',linewidth=2, extent=extent, cmap=line_cmap) if label_contours: clabel(result, fmt='%1.1g') #add the colorbar legend to the side cb = colorbar() cb.ax.axisbg = 'black' if show_regression: equation=plot_regression_line(xy_data[0],xy_data[1],prob_axes=prob_axes) if show_equation: add_regression_equations([equation]) def plot_histograms(data, graph_name='histogram.png', bins=20,\ normal_fit=True, normed=True, colors=None, linecolors=None, \ alpha=0.75, prob_axes=True, series_names=None, show_legend=False,\ y_label=None, **kwargs): """Outputs a histogram with multiple series (must provide a list of series). takes: data: list of arrays of values to plot (needs to be list of arrays so you can pass in arrays with different numbers of elements) graph_name: filename to write graph to bins: number of bins to use normal_fit: whether to show the normal curve best fitting the data normed: whether to normalize the histogram (e.g. so bars sum to 1) colors: list of colors to use for bars linecolors: list of colors to use for fit lines **kwargs are pssed on to init_graph_display. """ rc('patch', linewidth=.2) if y_label is None: if normed: y_label='Frequency' else: y_label='Count' num_series = len(data) if colors is None: if num_series == 1: colors = ['white'] else: colors = standard_series_colors if linecolors is None: if num_series == 1: linecolors = ['red'] else: linecolors = standard_series_colors init_graph_display(prob_axes=prob_axes, y_label=y_label, **kwargs) all_patches = [] for i, d in enumerate(data): fc = colors[i % len(colors)] lc = linecolors[i % len(linecolors)] counts, x_bins, patches = hist(d, bins=bins, normed=normed, \ alpha=alpha, facecolor=fc) all_patches.append(patches[0]) if normal_fit and len(d) > 1: maxv, minv = max(d), min(d) mu = mean(d) sigma = std(d) bin_width = x_bins[-1] - x_bins[-2] #want normpdf to extend over the range normpdf_bins = arange(minv,maxv,(maxv - minv)*.01) y = normpdf(normpdf_bins, mu, sigma) orig_area = sum(counts) * bin_width y = y * orig_area #normpdf area is 1 by default plot(normpdf_bins, y, linestyle='--', color=lc, linewidth=1) if show_legend and series_names: fp = FontProperties() fp.set_size('x-small') legend(all_patches, series_names, prop = fp) #output figure if graph name set -- otherwise, leave for further changes if graph_name is not None: savefig(graph_name) def plot_monte_histograms(data, graph_name='gene_histogram.png', bins=20,\ normal_fit=True, normed=True, colors=None, linecolors=None, \ alpha=0.75, prob_axes=True, series_names=None, show_legend=False,\ y_label=None, x_label=None, **kwargs): """Outputs a histogram with multiple series (must provide a list of series). Differs from regular histogram in that p-value works w/exactly two datasets, where the first dataset is the reference set. Calculates the mean of the reference set, and compares this to the second set (which is assumed to contain the means of many runs producing data comparable to the data in the reference set). takes: data: list of arrays of values to plot (needs to be list of arrays so you can pass in arrays with different numbers of elements) graph_name: filename to write graph to bins: number of bins to use normal_fit: whether to show the normal curve best fitting the data normed: whether to normalize the histogram (e.g. so bars sum to 1) colors: list of colors to use for bars linecolors: list of colors to use for fit lines **kwargs are passed on to init_graph_display. """ rc('patch', linewidth=.2) rc('font', size='x-small') rc('axes', linewidth=.2) rc('axes', labelsize=7) rc('xtick', labelsize=7) rc('ytick', labelsize=7) if y_label is None: if normed: y_label='Frequency' else: y_label='Count' num_series = len(data) if colors is None: if num_series == 1: colors = ['white'] else: colors = standard_series_colors if linecolors is None: if num_series == 1: linecolors = ['red'] else: linecolors = standard_series_colors init_graph_display(prob_axes=prob_axes, y_label=y_label, **kwargs) all_patches = [] for i, d in enumerate(data): fc = colors[i % len(colors)] lc = linecolors[i % len(linecolors)] counts, x_bins, patches = hist(d, bins=bins, normed=normed, \ alpha=alpha, facecolor=fc) all_patches.append(patches[0]) if normal_fit and len(d) > 1: mu = mean(d) sigma = std(d) minv = min(d) maxv = max(d) bin_width = x_bins[-1] - x_bins[-2] #set range for normpdf normpdf_bins = arange(minv,maxv,0.01*(maxv-minv)) y = normpdf(normpdf_bins, mu, sigma) orig_area = sum(counts) * bin_width y = y * orig_area #normpdf area is 1 by default plot(normpdf_bins, y, linestyle='--', color=lc, linewidth=1) font = { 'color': lc, 'fontsize': 11} text(mu, 0.0 , "*", font, verticalalignment='center', horizontalalignment='center') xlabel(x_label) if show_legend and series_names: fp = FontProperties() fp.set_size('x-small') legend(all_patches, series_names, prop = fp) #output figure if graph name set -- otherwise, leave for further changes if graph_name is not None: savefig(graph_name) def plot_scatter_with_histograms(data, graph_name='histo_scatter.png', \ graph_grid='/', prob_axes=False, bins=20, frac=0.9, scatter_alpha=0.5, \ hist_alpha=0.8, colors=standard_series_colors, normed='height', **kwargs): """Plots a scatter plot with histograms showing distribution of x and y. Data should be list of [x1, y1, x2, y2, ...]. """ #set up subplot coords tl=subplot(2,2,1) br=subplot(2,2,4) bl=subplot(2,2,3, sharex=tl, sharey=br) #get_position returns a Bbox relative to figure tl_coords = tl.get_position() bl_coords = bl.get_position() br_coords = br.get_position() left = tl_coords.xmin bottom = bl_coords.ymin width = br_coords.xmax - left height = tl_coords.ymax - bottom bl.set_position([left, bottom, frac*width, frac*height]) tl.set_position([left, bottom+(frac*height), frac*width, (1-frac)*height]) br.set_position([left+(frac*width), bottom, (1-frac)*width, frac*height]) #suppress frame and axis for histograms for i in [tl,br]: i.set_frame_on(False) i.xaxis.set_visible(False) i.yaxis.set_visible(False) plot_scatter(data=data, alpha=scatter_alpha, axes=bl, **kwargs) for i in range(0, len(data), 2): x, y = data[i], data[i+1] color = colors[int((i/2))%len(colors)] hist(x, facecolor=color, bins=bins, alpha=hist_alpha, normed=normed, axes=tl) hist(y, facecolor=color, bins=bins, alpha=hist_alpha, normed=normed, \ axes=br, orientation='horizontal') if prob_axes: bl.set_xlim(0,1) bl.set_ylim(0,1) br.set_ylim(0,1) tl.set_xlim(0,1) #output figure if graph name set -- otherwise, leave for further changes if graph_name is not None: savefig(graph_name) def format_contour_array(data, points_per_cell=20, bulk=0.8): """Formats [x,y] series of data into x_bins, y_bins and data for contour(). data: 2 x n array of float representing x,y coordinates points_per_cell: average points per unit cell in the bulk of the data, default 3 bulk: fraction containing the 'bulk' of the data in x and y, default 0.8 (i.e. 80% of the data will be used in the calculation). returns: x-bin, y-bin, and a square matrix of frequencies to be plotted WARNING: Assumes x and y are in the range 0-1. """ #bind x and y data data_x = sort(data[0]) #note: numpy sort returns a sorted copy data_y = sort(data[1]) num_points = len(data_x) #calculate the x and y bounds holding the bulk of the data low_prob = (1-bulk)/2.0 low_tail = int(num_points*low_prob) high_tail = int(num_points*(1-low_prob)) x_low = data_x[low_tail] x_high = data_x[high_tail] y_low = data_y[low_tail] y_high = data_y[high_tail] #calculate the side length in the bulk that holds the right number of #points delta_x = x_high - x_low delta_y = y_high - y_low points_in_bulk = num_points * bulk #approximate: assumes no correlation area_of_bulk = delta_x * delta_y points_per_area = points_in_bulk/area_of_bulk side_length = sqrt(points_per_cell / points_per_area) #correct the side length so we get an integer number of bins. num_bins = int(1/side_length) corrected_side_length = 1.0/num_bins #figure out how many items are in each grid square in x and y # #this is the tricky part, because contour() takes as its data matrix #the points at the vertices of each cell, rather than the points at #the centers of each cell. this means that if we were going to make #a 3 x 3 grid, we actually have to estimate a 4 x 4 matrix that's offset #by half a unit cell in both x and y. # #if the data are between 0 and 1, the first and last bin in our range are #superfluous because searchsorted will put items before the first #bin into bin 0, and items after the last bin into bin n+1, where #n is the maximum index in the original array. for example, if we #have 3 bins, the values .33 and .66 would suffice to find the centers, #because anything below .33 gets index 0 and anything above .66 gets index #2 (anything between them gets index 1). incidentally, this prevents #issues with floating-point error and values slightly below 0 or above 1 #that might otherwise arise. # #however, for our 3 x 3 case, we actually want to start estimating at the #cell centered at 0, i.e. starting at -.33/2, so that we get the four #estimates centered at (rather than starting at) 0, .33, .66, and 1. #because the data are constrained to be between 0 and 1, we will need to #double the counts at the edges (and quadruple them at the corners) to get #a fair estimate of the density. csl = corrected_side_length #save typing below eps = csl/10 #don't ever want max value to be in the list precisely half_csl = .5*csl bins = arange(half_csl, 1+half_csl-eps, csl) x_coords = searchsorted(bins, data[0]) y_coords = searchsorted(bins, data[1]) #matrix has dimension 1 more than num bins, b/c can be above largest matrix = zeros((num_bins+1, num_bins+1)) #for some reason, need to swap x and y to match up with normal #scatter plots for coord in zip(y_coords, x_coords): matrix[coord] += 1 #we now have estimates of the densities at the edge of each of the #n x n cells in the grid. for example, if we have a 3 x 3 grid, we have #16 densities, one at the center of each grid cell (0, .33, .66, 1 in each #dimension). need to double the counts at edges to reflect places where #we can't observe data because of range restrictions. matrix[0]*=2 matrix[:,0]*=2 matrix[-1]*=2 matrix[:,-1]*=2 #return adjusted_bins as centers, rather than boundaries, of the range x_bins = csl*arange(num_bins+1) return x_bins, x_bins, matrix if __name__ == '__main__': from numpy.random import normal x = normal(0.3, 0.05, 1000) y = normal(0.5, 0.1, 1000) plot_scatter_with_histograms([x,x+y, y, (x+y)/2], prob_axes=True)
mit
AmineEch/BrainCNN
visualise.py
1
4922
from __future__ import print_function, division import matplotlib.pyplot as plt plt.interactive(False) from scipy.stats import pearsonr from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import Dense, Dropout, Flatten, Activation from keras.layers.advanced_activations import LeakyReLU, PReLU from keras import optimizers, callbacks, regularizers, initializers from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from E2E_conv import * import numpy as np import matplotlib.pylab as plt from vis.visualization import visualize_activation from nilearn import plotting from sklearn.cross_validation import StratifiedKFold def plot_matrices(matrices, matrix_kind): n_matrices = len(matrices) plt.figure(figsize=(n_matrices * 4, 4)) for n_subject, matrix in enumerate(matrices): plt.subplot(1, n_matrices, n_subject + 1) matrix = matrix.copy() # avoid side effects # Set diagonal to zero, for better visualization np.fill_diagonal(matrix, 0) vmax = np.max(np.abs(matrix)) plt.imshow(matrix, vmin=-vmax, vmax=vmax, cmap='RdBu_r', interpolation='nearest') plt.title('{0}, subject {1}'.format(matrix_kind, n_subject)) from nilearn import datasets adhd_data = datasets.fetch_adhd(n_subjects=20) msdl_data = datasets.fetch_atlas_msdl() msdl_coords = msdl_data.region_coords n_regions = len(msdl_coords) print('MSDL has {0} ROIs, part of the following networks :\n{1}.'.format( n_regions, msdl_data.networks)) from nilearn import input_data masker = input_data.NiftiMapsMasker( msdl_data.maps, resampling_target="data", t_r=2.5, detrend=True, low_pass=.1, high_pass=.01, memory='nilearn_cache', memory_level=1) adhd_subjects = [] pooled_subjects = [] site_names = [] adhd_labels = [] # 1 if ADHD, 0 if control for func_file, confound_file, phenotypic in zip( adhd_data.func, adhd_data.confounds, adhd_data.phenotypic): time_series = masker.fit_transform(func_file, confounds=confound_file) pooled_subjects.append(time_series) is_adhd = phenotypic['adhd'] if is_adhd: adhd_subjects.append(time_series) site_names.append(phenotypic['site']) adhd_labels.append(is_adhd) print('Data has {0} ADHD subjects.'.format(len(adhd_subjects))) from nilearn.connectome import ConnectivityMeasure conn_measure = ConnectivityMeasure(kind="tangent") x_train = conn_measure.fit_transform(pooled_subjects) print(x_train.shape) print(len(adhd_labels)) y_train = np.array(adhd_labels,dtype="float32") print(y_train.shape) # Prediction ############################### batch_size = 14 dropout = 0.5 momentum = 0.9 lr = 0.001 decay = 0.0005 reg = regularizers.l2(decay) kernel_init = initializers.he_uniform() # Model architecture model = Sequential() model.add(E2E_conv(2,8,(2,39),kernel_regularizer=reg,input_shape=(39,39,1),input_dtype='float32',data_format="channels_last")) print("First layer output shape :"+str(model.output_shape)) model.add(LeakyReLU(alpha=0.33)) #print(model.output_shape) print(model.output_shape) model.add(LeakyReLU(alpha=0.33)) model.add(Convolution2D(32,(1,39),kernel_regularizer=reg,data_format="channels_last")) model.add(LeakyReLU(alpha=0.33)) model.add(Convolution2D(90,(39,1),kernel_regularizer=reg,data_format="channels_last")) model.add(LeakyReLU(alpha=0.33)) #print(model.output_shape) model.add(Dropout(0.5)) model.add(Dense(64,kernel_regularizer=reg,kernel_initializer=kernel_init)) #print(model.output_shape) model.add(LeakyReLU(alpha=0.33)) #print(model.output_shape) model.add(Dropout(0.5)) model.add(Dense(10,kernel_regularizer=reg,kernel_initializer=kernel_init)) model.add(LeakyReLU(alpha=0.33)) #print(model.output_shape) model.add(Dropout(0.5)) model.add(Dense(1,kernel_regularizer=reg,kernel_initializer=kernel_init)) model.add(Flatten()) model.add(Activation('softmax')) model.summary() #print(model.output_shape) opt = optimizers.SGD(nesterov=True,lr=lr) model.compile(optimizer=opt,loss='binary_crossentropy',metrics=['accuracy']) csv_logger = callbacks.CSVLogger('predict_age.log') x_train = x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1) x_train,x_test,y_train,y_test = train_test_split(x_train,y_train,test_size=0.33,random_state=42) command = str(raw_input("Train or predict ? [t/p]")) if command == "t": print("Training the model ...") history=model.fit(x_train,y_train,batch_size=1,nb_epoch=1000,verbose=1,callbacks=[csv_logger]) model.save_weights("Weights/BrainCNNWeights_categ.h5") else: print("[*] Predicting and printing results for the models trained :") model.load_weights("Weights/BrainCNNWeights_categ.h5") heatmap = visualize_activation(model, layer_idx=-1, filter_indices=0, seed_input=x_test[0]) print(heatmap.shape) plt.interactive(False) plotting.plot_connectome()
mit
ChristianKniep/QNIB
serverfiles/usr/local/lib/networkx-1.6/doc/make_examples_rst.py
12
5442
#!/usr/bin/env python """ generate the rst files for the examples by iterating over the networkx examples """ # This code was developed from the Matplotlib gen_rst.py module # and is distributed with the same license as Matplotlib import os, glob import os import re import sys #fileList = [] #rootdir = '../../examples' def out_of_date(original, derived): """ Returns True if derivative is out-of-date wrt original, both of which are full file paths. TODO: this check isn't adequate in some cases. Eg, if we discover a bug when building the examples, the original and derived will be unchanged but we still want to fource a rebuild. We can manually remove from _static, but we may need another solution """ return (not os.path.exists(derived) or os.stat(derived).st_mtime < os.stat(original).st_mtime) def main(exampledir,sourcedir): noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-") datad = {} for root, subFolders, files in os.walk(exampledir): for fname in files: if ( fname.startswith('.') or fname.startswith('#') or fname.startswith('_') or fname.find('.svn')>=0 or not fname.endswith('.py') ): continue fullpath = os.path.join(root,fname) contents = file(fullpath).read() # indent relpath = os.path.split(root)[-1] datad.setdefault(relpath, []).append((fullpath, fname, contents)) subdirs = datad.keys() subdirs.sort() output_dir=os.path.join(sourcedir,'examples') if not os.path.exists(output_dir): os.makedirs(output_dir) fhindex = file(os.path.join(sourcedir,'examples','index.rst'), 'w') fhindex.write("""\ .. _examples-index: ***************** NetworkX Examples ***************** .. only:: html :Release: |version| :Date: |today| .. toctree:: :maxdepth: 2 """) for subdir in subdirs: output_dir= os.path.join(sourcedir,'examples',subdir) if not os.path.exists(output_dir): os.makedirs(output_dir) static_dir = os.path.join(sourcedir, 'static', 'examples') if not os.path.exists(static_dir): os.makedirs(static_dir) subdirIndexFile = os.path.join(subdir, 'index.rst') fhsubdirIndex = file(os.path.join(output_dir,'index.rst'), 'w') fhindex.write(' %s\n\n'%subdirIndexFile) #thumbdir = '../_static/plot_directive/mpl_examples/%s/thumbnails/'%subdir #for thumbname in glob.glob(os.path.join(thumbdir,'*.png')): # fhindex.write(' %s\n'%thumbname) fhsubdirIndex.write("""\ .. _%s-examples-index: ############################################## %s ############################################## .. only:: html :Release: |version| :Date: |today| .. toctree:: :maxdepth: 1 """%(subdir, subdir.title())) data = datad[subdir] data.sort() #parts = os.path.split(static_dir) #thumb_dir = ('../'*(len(parts)-1)) + os.path.join(static_dir, 'thumbnails') for fullpath, fname, contents in data: basename, ext = os.path.splitext(fname) static_file = os.path.join(static_dir, fname) #thumbfile = os.path.join(thumb_dir, '%s.png'%basename) #print ' static_dir=%s, basename=%s, fullpath=%s, fname=%s, thumb_dir=%s, thumbfile=%s'%(static_dir, basename, fullpath, fname, thumb_dir, thumbfile) rstfile = '%s.rst'%basename outfile = os.path.join(output_dir, rstfile) fhsubdirIndex.write(' %s\n'%rstfile) if (not out_of_date(fullpath, static_file) and not out_of_date(fullpath, outfile)): continue print '%s/%s'%(subdir,fname) fhstatic = file(static_file, 'w') fhstatic.write(contents) fhstatic.close() fh = file(outfile, 'w') fh.write('.. _%s-%s:\n\n'%(subdir, basename)) base=fname.partition('.')[0] title = '%s'%(base.replace('_',' ').title()) #title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname) fh.write(title + '\n') fh.write('='*len(title) + '\n\n') pngname=base+".png" png=os.path.join(static_dir,pngname) linkname = os.path.join('..', '..', 'static', 'examples') if os.path.exists(png): fh.write('.. image:: %s \n\n'%os.path.join(linkname,pngname)) linkname = os.path.join('..', '..', '_static', 'examples') fh.write("[`source code <%s>`_]\n\n::\n\n" % os.path.join(linkname,fname)) # indent the contents contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')]) fh.write(contents) # fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)') fh.close() fhsubdirIndex.close() fhindex.close() if __name__ == '__main__': import sys try: arg0,arg1,arg2=sys.argv[:3] except: arg0=sys.argv[0] print """ Usage: %s exampledir sourcedir exampledir: a directory containing the python code for the examples. sourcedir: a directory to put the generated documentation source for these examples. """%arg0 else: main(arg1,arg2)
gpl-2.0
TomAugspurger/pandas
pandas/tests/groupby/aggregate/test_cython.py
1
6844
""" test cython .agg behavior """ import numpy as np import pytest import pandas as pd from pandas import DataFrame, Index, NaT, Series, Timedelta, Timestamp, bdate_range import pandas._testing as tm from pandas.core.groupby.groupby import DataError @pytest.mark.parametrize( "op_name", [ "count", "sum", "std", "var", "sem", "mean", pytest.param( "median", # ignore mean of empty slice # and all-NaN marks=[pytest.mark.filterwarnings("ignore::RuntimeWarning")], ), "prod", "min", "max", ], ) def test_cythonized_aggers(op_name): data = { "A": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1.0, np.nan, np.nan], "B": ["A", "B"] * 6, "C": np.random.randn(12), } df = DataFrame(data) df.loc[2:10:2, "C"] = np.nan op = lambda x: getattr(x, op_name)() # single column grouped = df.drop(["B"], axis=1).groupby("A") exp = {cat: op(group["C"]) for cat, group in grouped} exp = DataFrame({"C": exp}) exp.index.name = "A" result = op(grouped) tm.assert_frame_equal(result, exp) # multiple columns grouped = df.groupby(["A", "B"]) expd = {} for (cat1, cat2), group in grouped: expd.setdefault(cat1, {})[cat2] = op(group["C"]) exp = DataFrame(expd).T.stack(dropna=False) exp.index.names = ["A", "B"] exp.name = "C" result = op(grouped)["C"] if op_name in ["sum", "prod"]: tm.assert_series_equal(result, exp) def test_cython_agg_boolean(): frame = DataFrame( { "a": np.random.randint(0, 5, 50), "b": np.random.randint(0, 2, 50).astype("bool"), } ) result = frame.groupby("a")["b"].mean() expected = frame.groupby("a")["b"].agg(np.mean) tm.assert_series_equal(result, expected) def test_cython_agg_nothing_to_agg(): frame = DataFrame({"a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25}) msg = "No numeric types to aggregate" with pytest.raises(DataError, match=msg): frame.groupby("a")["b"].mean() frame = DataFrame({"a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25}) with pytest.raises(DataError, match=msg): frame[["b"]].groupby(frame["a"]).mean() def test_cython_agg_nothing_to_agg_with_dates(): frame = DataFrame( { "a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25, "dates": pd.date_range("now", periods=50, freq="T"), } ) msg = "No numeric types to aggregate" with pytest.raises(DataError, match=msg): frame.groupby("b").dates.mean() def test_cython_agg_frame_columns(): # #2113 df = DataFrame({"x": [1, 2, 3], "y": [3, 4, 5]}) df.groupby(level=0, axis="columns").mean() df.groupby(level=0, axis="columns").mean() df.groupby(level=0, axis="columns").mean() df.groupby(level=0, axis="columns").mean() def test_cython_agg_return_dict(): # GH 16741 df = DataFrame( { "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], "B": ["one", "one", "two", "three", "two", "two", "one", "three"], "C": np.random.randn(8), "D": np.random.randn(8), } ) ts = df.groupby("A")["B"].agg(lambda x: x.value_counts().to_dict()) expected = Series( [{"two": 1, "one": 1, "three": 1}, {"two": 2, "one": 2, "three": 1}], index=Index(["bar", "foo"], name="A"), name="B", ) tm.assert_series_equal(ts, expected) def test_cython_fail_agg(): dr = bdate_range("1/1/2000", periods=50) ts = Series(["A", "B", "C", "D", "E"] * 10, index=dr) grouped = ts.groupby(lambda x: x.month) summed = grouped.sum() expected = grouped.agg(np.sum) tm.assert_series_equal(summed, expected) @pytest.mark.parametrize( "op, targop", [ ("mean", np.mean), ("median", np.median), ("var", np.var), ("add", np.sum), ("prod", np.prod), ("min", np.min), ("max", np.max), ("first", lambda x: x.iloc[0]), ("last", lambda x: x.iloc[-1]), ], ) def test__cython_agg_general(op, targop): df = DataFrame(np.random.randn(1000)) labels = np.random.randint(0, 50, size=1000).astype(float) result = df.groupby(labels)._cython_agg_general(op) expected = df.groupby(labels).agg(targop) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "op, targop", [ ("mean", np.mean), ("median", lambda x: np.median(x) if len(x) > 0 else np.nan), ("var", lambda x: np.var(x, ddof=1)), ("min", np.min), ("max", np.max), ], ) def test_cython_agg_empty_buckets(op, targop, observed): df = pd.DataFrame([11, 12, 13]) grps = range(0, 55, 5) # calling _cython_agg_general directly, instead of via the user API # which sets different values for min_count, so do that here. g = df.groupby(pd.cut(df[0], grps), observed=observed) result = g._cython_agg_general(op) g = df.groupby(pd.cut(df[0], grps), observed=observed) expected = g.agg(lambda x: targop(x)) tm.assert_frame_equal(result, expected) def test_cython_agg_empty_buckets_nanops(observed): # GH-18869 can't call nanops on empty groups, so hardcode expected # for these df = pd.DataFrame([11, 12, 13], columns=["a"]) grps = range(0, 25, 5) # add / sum result = df.groupby(pd.cut(df["a"], grps), observed=observed)._cython_agg_general( "add" ) intervals = pd.interval_range(0, 20, freq=5) expected = pd.DataFrame( {"a": [0, 0, 36, 0]}, index=pd.CategoricalIndex(intervals, name="a", ordered=True), ) if observed: expected = expected[expected.a != 0] tm.assert_frame_equal(result, expected) # prod result = df.groupby(pd.cut(df["a"], grps), observed=observed)._cython_agg_general( "prod" ) expected = pd.DataFrame( {"a": [1, 1, 1716, 1]}, index=pd.CategoricalIndex(intervals, name="a", ordered=True), ) if observed: expected = expected[expected.a != 1] tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("op", ["first", "last", "max", "min"]) @pytest.mark.parametrize( "data", [Timestamp("2016-10-14 21:00:44.557"), Timedelta("17088 days 21:00:44.557")] ) def test_cython_with_timestamp_and_nat(op, data): # https://github.com/pandas-dev/pandas/issues/19526 df = DataFrame({"a": [0, 1], "b": [data, NaT]}) index = Index([0, 1], name="a") # We will group by a and test the cython aggregations expected = DataFrame({"b": [data, NaT]}, index=index) result = df.groupby("a").aggregate(op) tm.assert_frame_equal(expected, result)
bsd-3-clause
mixturemodel-flow/tensorflow
tensorflow/contrib/timeseries/examples/predict_test.py
80
2487
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Tests that the TensorFlow parts of the prediction example run.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from os import path from tensorflow.contrib.timeseries.examples import predict from tensorflow.python.platform import test _MODULE_PATH = path.dirname(__file__) _DATA_FILE = path.join(_MODULE_PATH, "data/period_trend.csv") class PeriodTrendExampleTest(test.TestCase): def test_shapes_and_variance_structural(self): (times, observed, all_times, mean, upper_limit, lower_limit ) = predict.structural_ensemble_train_and_predict(_DATA_FILE) # Just check that plotting will probably be OK. We can't actually run the # plotting code since we don't want to pull in matplotlib as a dependency # for this test. self.assertAllEqual([500], times.shape) self.assertAllEqual([500], observed.shape) self.assertAllEqual([700], all_times.shape) self.assertAllEqual([700], mean.shape) self.assertAllEqual([700], upper_limit.shape) self.assertAllEqual([700], lower_limit.shape) # Check that variance hasn't blown up too much. This is a relatively good # indication that training was successful. self.assertLess(upper_limit[-1] - lower_limit[-1], 1.5 * (upper_limit[0] - lower_limit[0])) def test_ar(self): (times, observed, all_times, mean, upper_limit, lower_limit) = predict.ar_train_and_predict(_DATA_FILE) self.assertAllEqual(times.shape, observed.shape) self.assertAllEqual(all_times.shape, mean.shape) self.assertAllEqual(all_times.shape, upper_limit.shape) self.assertAllEqual(all_times.shape, lower_limit.shape) self.assertLess((upper_limit - lower_limit).mean(), 4.) if __name__ == "__main__": test.main()
apache-2.0
sriramsitharaman/sp17-i524
project/S17-IO-3012/code/bin/benchmark_version_import.py
19
4590
import matplotlib.pyplot as plt import sys import pandas as pd def get_parm(): """retrieves mandatory parameter to program @param: none @type: n/a """ try: return sys.argv[1] except: print ('Must enter file name as parameter') exit() def read_file(filename): """reads a file into a pandas dataframe @param: filename The name of the file to read @type: string """ try: return pd.read_csv(filename) except: print ('Error retrieving file') exit() def select_data(benchmark_df, mongo_version, config_replicas, mongos_instances, shard_replicas, shards_per_replica): benchmark_df = benchmark_df[benchmark_df.cloud == "chameleon"] benchmark_df = benchmark_df[benchmark_df.test_size == "large"] if mongo_version != 'X': benchmark_df = benchmark_df[benchmark_df.mongo_version == mongo_version] if config_replicas != 'X': benchmark_df = benchmark_df[benchmark_df.config_replicas == config_replicas] if mongos_instances != 'X': benchmark_df = benchmark_df[benchmark_df.mongos_instances == mongos_instances] if shard_replicas != 'X': benchmark_df = benchmark_df[benchmark_df.shard_replicas == shard_replicas] if shards_per_replica != 'X': benchmark_df = benchmark_df[benchmark_df.shards_per_replica == shards_per_replica] #benchmark_df1 = benchmark_df.groupby(['cloud', 'config_replicas', 'mongos_instances', 'shard_replicas', 'shards_per_replica']).mean() #http://stackoverflow.com/questions/10373660/converting-a-pandas-groupby-object-to-dataframe benchmark_df = benchmark_df.groupby(['cloud', 'config_replicas', 'mongos_instances', 'shard_replicas', 'shards_per_replica'], as_index=False).mean() #http://stackoverflow.com/questions/10373660/converting-a-pandas-groupby-object-to-dataframe #print benchmark_df1['shard_replicas'] #print benchmark_df1 #print benchmark_df benchmark_df = benchmark_df.sort_values(by='shard_replicas', ascending=1) return benchmark_df def make_figure(import_seconds_32, shards_32, import_seconds_34, shards_34): """formats and creates a line chart @param1: find_seconds_kilo Array with find_seconds from kilo @type: numpy array @param2: shards_kilo Array with shards from kilo @type: numpy array @param3: find_seconds_chameleon Array with find_seconds from chameleon @type: numpy array @param4: shards_chameleon Array with shards from chameleon @type: numpy array """ fig = plt.figure() #plt.title('Average MongoImport Runtime with Various Numbers of Shards') plt.ylabel('Runtime in Seconds') plt.xlabel('Number of Shards') # Make the chart plt.plot(shards_32, import_seconds_32, label='Version 3.2') plt.plot(shards_34, import_seconds_34, label='Version 3.4') #http://stackoverflow.com/questions/11744990/how-to-set-auto-for-upper-limit-but-keep-a-fixed-lower-limit-with-matplotlib plt.ylim(ymin=0) plt.legend(loc='best') # Show the chart (for testing) # plt.show() # Save the chart fig.savefig('../report/version_import.png') # Run the program by calling the functions if __name__ == "__main__": filename = get_parm() benchmark_df = read_file(filename) mongo_version = 32 config_replicas = 1 mongos_instances = 1 shard_replicas = 'X' shards_per_replica = 1 select_df = select_data(benchmark_df, mongo_version, config_replicas, mongos_instances, shard_replicas, shards_per_replica) #http://stackoverflow.com/questions/31791476/pandas-dataframe-to-numpy-array-valueerror #percentage death=\ import_seconds_32=select_df.as_matrix(columns=[select_df.columns[6]]) shards_32 = select_df.as_matrix(columns=[select_df.columns[3]]) #http://stackoverflow.com/questions/31791476/pandas-dataframe-to-numpy-array-valueerror mongo_version = 34 config_replicas = 1 mongos_instances = 1 shard_replicas = 'X' shards_per_replica = 1 select_df = select_data(benchmark_df, mongo_version, config_replicas, mongos_instances, shard_replicas, shards_per_replica) #http://stackoverflow.com/questions/31791476/pandas-dataframe-to-numpy-array-valueerror #percentage death=\ import_seconds_34=select_df.as_matrix(columns=[select_df.columns[6]]) shards_34 = select_df.as_matrix(columns=[select_df.columns[3]]) #http://stackoverflow.com/questions/31791476/pandas-dataframe-to-numpy-array-valueerror make_figure(import_seconds_32, shards_32, import_seconds_34, shards_34)
apache-2.0
kastnerkyle/crikey
ishaan_model/ishaan_baseline.py
1
13070
from __future__ import print_function import numpy as np import theano from theano import tensor from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams from scipy.io import wavfile import os import sys from kdllib import audio_file_iterator from kdllib import numpy_one_hot, apply_quantize_preproc from kdllib import numpy_softmax, numpy_sample_softmax from kdllib import param, param_search, print_param_info from kdllib import LearnedInitHidden from kdllib import Linear from kdllib import Embedding from kdllib import Igor from kdllib import load_checkpoint, theano_one_hot, concatenate from kdllib import fetch_fruitspeech, list_iterator from kdllib import np_zeros, GRU, GRUFork from kdllib import make_weights, make_biases, relu, run_loop from kdllib import as_shared, adam, gradient_clipping from kdllib import get_values_from_function, set_shared_variables_in_function from kdllib import soundsc, categorical_crossentropy from kdllib import relu, softmax, sample_softmax if __name__ == "__main__": import argparse fs = 16000 minibatch_size = 128 cut_len = 64 n_epochs = 1000 # Used way at the bottom in the training loop! checkpoint_every_n_epochs = 1 checkpoint_every_n_updates = 1000 checkpoint_every_n_seconds = 60 * 60 random_state = np.random.RandomState(1999) filepath = "/Tmp/kastner/blizzard_wav_files/*flac" train_itr = audio_file_iterator(filepath, minibatch_size=minibatch_size, stop_index=.9, preprocess="quantize") valid_itr = audio_file_iterator(filepath, minibatch_size=minibatch_size, start_index=.9, preprocess="quantize") X_mb, X_mb_mask = next(train_itr) train_itr.reset() input_dim = 256 n_embed = 256 n_hid = 512 n_bins = 256 desc = "Speech generation" parser = argparse.ArgumentParser(description=desc) parser.add_argument('-s', '--sample', help='Sample from a checkpoint file', default=None, required=False) def restricted_int(x): if x is None: # None makes it "auto" sample return x x = int(x) if x < 1: raise argparse.ArgumentTypeError("%r not range [1, inf]" % (x,)) return x parser.add_argument('-sl', '--sample_length', help='Number of steps to sample, default is automatic', type=restricted_int, default=None, required=False) def restricted_float(x): if x is None: # None makes it "auto" temperature return x x = float(x) if x <= 0: raise argparse.ArgumentTypeError("%r not range (0, inf]" % (x,)) return x parser.add_argument('-t', '--temperature', help='Sampling temperature for softmax', type=restricted_float, default=None, required=False) parser.add_argument('-c', '--continue', dest="cont", help='Continue training from another saved model', default=None, required=False) args = parser.parse_args() if args.sample is not None: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt checkpoint_file = args.sample if not os.path.exists(checkpoint_file): raise ValueError("Checkpoint file path %s" % checkpoint_file, " does not exist!") print(checkpoint_file) checkpoint_dict = load_checkpoint(checkpoint_file) X_mb, X_mb_mask = next(train_itr) train_itr.reset() prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid)) for i in range(3)] sample_function = checkpoint_dict["sample_function"] if args.temperature is None: args.temperature = 1. if args.sample_length is None: raise ValueError("NYI - use -sl or --sample_length ") else: fixed_steps = args.sample_length temperature = args.temperature completed = [] # 0 is in the middle # CANNOT BE 1 timestep - will get floating point exception! # 2 may still be buggy because X_sym gets sliced and scan gets mad with 1 timestep usually... init_x = 127 + np_zeros((3, minibatch_size, 1)).astype(theano.config.floatX) for i in range(fixed_steps): if i % 100 == 0: print("Sampling step %i" % i) rvals = sample_function(init_x, prev_h1, prev_h2, prev_h3) sampled, h1_s, h2_s, h3_s = rvals pred_s = numpy_softmax(sampled, temperature=temperature) # debug=True gives argmax # use 0 since it is a moving window choice = numpy_sample_softmax(pred_s[0], random_state) choice = choice[None] completed.append(choice) # use 3 since scan is throwing exceptions init_x = np.concatenate((choice[..., None], choice[..., None], choice[..., None]), axis=0) init_x = init_x.astype(theano.config.floatX) # use next step prev_h1 = h1_s[0] prev_h2 = h2_s[0] prev_h3 = h3_s[0] print("Completed sampling after %i steps" % fixed_steps) # mb, length completed = np.array(completed)[:, 0, :] completed = completed.transpose(1, 0) # all samples would be range(len(completed)) for i in range(10): ex = completed[i].ravel() s = "gen_%i.wav" % (i) """ ex = ex.astype("float32") ex -= ex.min() ex /= ex.max() ex -= 0.5 ex *= 0.95 wavfile.write(s, fs, ex) """ wavfile.write(s, fs, soundsc(ex)) print("Sampling complete, exiting...") sys.exit() else: print("No plotting arguments, starting training mode!") X_sym = tensor.tensor3("X_sym") X_sym.tag.test_value = X_mb[:cut_len] X_mask_sym = tensor.matrix("X_mask_sym") X_mask_sym.tag.test_value = X_mb_mask[:cut_len] init_h1_i = tensor.matrix("init_h1") init_h1_i.tag.test_value = np_zeros((minibatch_size, n_hid)) init_h2_i = tensor.matrix("init_h2") init_h2_i.tag.test_value = np_zeros((minibatch_size, n_hid)) init_h3_i = tensor.matrix("init_h3") init_h3_i.tag.test_value = np_zeros((minibatch_size, n_hid)) init_h1, init_h2, init_h3 = LearnedInitHidden( [init_h1_i, init_h2_i, init_h3_i], 3 * [(minibatch_size, n_hid)]) inpt = X_sym[:-1] target = X_sym[1:] mask = X_mask_sym[:-1] embed_dim = 256 embed1 = Embedding(inpt, 256, embed_dim, random_state) in_h1, ingate_h1 = GRUFork([embed1], [embed_dim], n_hid, random_state) in_h2, ingate_h2 = GRUFork([embed1], [embed_dim], n_hid, random_state) in_h3, ingate_h3 = GRUFork([embed1], [embed_dim], n_hid, random_state) def step(in_h1_t, ingate_h1_t, in_h2_t, ingate_h2_t, in_h3_t, ingate_h3_t, h1_tm1, h2_tm1, h3_tm1): h1_t = GRU(in_h1_t, ingate_h1_t, h1_tm1, n_hid, n_hid, random_state) h1_h2_t, h1gate_h2_t = GRUFork([h1_t], [n_hid], n_hid, random_state) h1_h3_t, h1gate_h3_t = GRUFork([h1_t], [n_hid], n_hid, random_state) h2_t = GRU(h1_h2_t + in_h2_t, h1gate_h2_t + ingate_h2_t, h2_tm1, n_hid, n_hid, random_state) h2_h3_t, h2gate_h3_t = GRUFork([h2_t], [n_hid], n_hid, random_state) h3_t = GRU(h2_h3_t + in_h3_t + h1_h3_t, h2gate_h3_t + ingate_h3_t + h1gate_h3_t, h3_tm1, n_hid, n_hid, random_state) return h1_t, h2_t, h3_t (h1, h2, h3), updates = theano.scan( fn=step, sequences=[in_h1, ingate_h1, in_h2, ingate_h2, in_h3, ingate_h3], outputs_info=[init_h1, init_h2, init_h3]) out = Linear([embed1, h1, h2, h3], [embed_dim, n_hid, n_hid, n_hid], n_bins, random_state) pred = softmax(out) shp = target.shape target = target.reshape((shp[0], shp[1])) target = theano_one_hot(target, n_classes=n_bins) # dimshuffle so batch is on last axis cost = categorical_crossentropy(pred, target) cost = cost * mask.dimshuffle(0, 1) # sum over sequence length and features, mean over minibatch cost = cost.dimshuffle(1, 0) cost = cost.mean() # convert to bits vs nats cost = cost * tensor.cast(1.44269504089, theano.config.floatX) params = param_search(cost, lambda x: hasattr(x, "param")) print_param_info(params) grads = tensor.grad(cost, params) grads = [tensor.clip(g, -1., 1.) for g in grads] learning_rate = 1E-3 opt = adam(params, learning_rate) updates = opt.updates(params, grads) if args.cont is not None: print("Continuing training from saved model") continue_path = args.cont if not os.path.exists(continue_path): raise ValueError("Continue model %s, path not " "found" % continue_path) saved_checkpoint = load_checkpoint(continue_path) checkpoint_dict = saved_checkpoint train_function = checkpoint_dict["train_function"] cost_function = checkpoint_dict["cost_function"] predict_function = checkpoint_dict["predict_function"] sample_function = checkpoint_dict["sample_function"] """ trained_weights = get_values_from_function( saved_checkpoint["train_function"]) set_shared_variables_in_function(train_function, trained_weights) """ else: train_function = theano.function([X_sym, X_mask_sym, init_h1_i, init_h2_i, init_h3_i], [cost, h1, h2, h3], updates=updates, on_unused_input="warn") cost_function = theano.function([X_sym, X_mask_sym, init_h1_i, init_h2_i, init_h3_i], [cost, h1, h2, h3], on_unused_input="warn") predict_function = theano.function([inpt, init_h1_i, init_h2_i, init_h3_i], [out, h1, h2, h3], on_unused_input="warn") sample_function = theano.function([inpt, init_h1_i, init_h2_i, init_h3_i], [out, h1, h2, h3], on_unused_input="warn") checkpoint_dict = {} checkpoint_dict["train_function"] = train_function checkpoint_dict["cost_function"] = cost_function checkpoint_dict["predict_function"] = predict_function checkpoint_dict["sample_function"] = sample_function def _loop(function, itr): prev_h1, prev_h2, prev_h3 = [np_zeros((minibatch_size, n_hid)) for i in range(3)] X_mb, X_mb_mask = next(itr) # Sanity check there are no bugs in the mask assert X_mb_mask.min() > 1E-6 n_cuts = len(X_mb) // cut_len + 1 partial_costs = [] for n in range(n_cuts): if n % 100 == 0: print("step %i" % n, end="") else: print(".", end="") start = n * cut_len stop = (n + 1) * cut_len if len(X_mb[start:stop]) < cut_len: # skip end edge case break rval = function(X_mb[start:stop], X_mb_mask[start:stop], prev_h1, prev_h2, prev_h3) current_cost = rval[0] prev_h1, prev_h2, prev_h3 = rval[1:4] prev_h1 = prev_h1[-1] prev_h2 = prev_h2[-1] prev_h3 = prev_h3[-1] partial_costs.append(current_cost) print("") return partial_costs i = Igor(_loop, train_function, train_itr, cost_function, valid_itr, n_epochs=n_epochs, checkpoint_dict=checkpoint_dict, checkpoint_every_n_updates=checkpoint_every_n_updates, checkpoint_every_n_seconds=checkpoint_every_n_seconds, checkpoint_every_n_epochs=checkpoint_every_n_epochs, skip_minimums=True) #i.refresh(_loop, train_function, train_itr, cost_function, valid_itr, # n_epochs, checkpoint_dict) i.run()
bsd-3-clause
vaibhav-mehta/VAE-Torch
plot.py
1
1427
import numpy as np import scipy.stats as sp import matplotlib.pyplot as plt import h5py def manifold(gridSize, binary, epoch): f = h5py.File('params/ff_epoch_' + str(epoch) + '.hdf5','r') wsig = np.matrix(f["wsig"]) bsig = np.matrix(f["bsig"]).T if binary: shape = (28,28) activation = lambda z, wb : activation_binary(z,wb) wtanh = np.matrix(f["wtanh"]) btanh = np.matrix(f["btanh"]).T wb = (wtanh,btanh,wsig,bsig) else: shape = (28,20) activation = lambda z, wb: activation_continuous(z,wb) wrelu = np.matrix(f["wrelu"]) brelu = np.matrix(f["brelu"]).T wb = (wrelu,brelu,wsig,bsig) gridValues = np.linspace(0.05,0.95,gridSize) z = lambda gridpoint: np.matrix(sp.norm.ppf(gridpoint)).T image = np.vstack([np.hstack([activation(z((i,j)),wb).reshape(shape) for j in gridValues]) for i in gridValues]) plt.imshow(image, cmap='Greys') plt.axis('off') plt.show() def activation_binary(z, wb): wtanh, btanh, wsig, bsig = wb h = np.tanh(wtanh.dot(z) + btanh) y = 1 / (1 + np.exp(-(wsig.dot(h) + bsig))) return y def activation_continuous(z, wb): wrelu, brelu, wsig, bsig = wb h = np.log(1 + np.exp(np.dot(wrelu,z) + brelu)) y = 1 / (1 + np.exp(-(np.dot(wsig,h) + bsig))) return y #Gridsize, Binary (True/False), Epoch number for params manifold(10,False,740)
mit
huzq/scikit-learn
examples/mixture/plot_concentration_prior.py
31
5695
""" ======================================================================== Concentration Prior Type Analysis of Variation Bayesian Gaussian Mixture ======================================================================== This example plots the ellipsoids obtained from a toy dataset (mixture of three Gaussians) fitted by the ``BayesianGaussianMixture`` class models with a Dirichlet distribution prior (``weight_concentration_prior_type='dirichlet_distribution'``) and a Dirichlet process prior (``weight_concentration_prior_type='dirichlet_process'``). On each figure, we plot the results for three different values of the weight concentration prior. The ``BayesianGaussianMixture`` class can adapt its number of mixture components automatically. The parameter ``weight_concentration_prior`` has a direct link with the resulting number of components with non-zero weights. Specifying a low value for the concentration prior will make the model put most of the weight on few components set the remaining components weights very close to zero. High values of the concentration prior will allow a larger number of components to be active in the mixture. The Dirichlet process prior allows to define an infinite number of components and automatically selects the correct number of components: it activates a component only if it is necessary. On the contrary the classical finite mixture model with a Dirichlet distribution prior will favor more uniformly weighted components and therefore tends to divide natural clusters into unnecessary sub-components. """ # Author: Thierry Guillemot <[email protected]> # License: BSD 3 clause import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from sklearn.mixture import BayesianGaussianMixture print(__doc__) def plot_ellipses(ax, weights, means, covars): for n in range(means.shape[0]): eig_vals, eig_vecs = np.linalg.eigh(covars[n]) unit_eig_vec = eig_vecs[0] / np.linalg.norm(eig_vecs[0]) angle = np.arctan2(unit_eig_vec[1], unit_eig_vec[0]) # Ellipse needs degrees angle = 180 * angle / np.pi # eigenvector normalization eig_vals = 2 * np.sqrt(2) * np.sqrt(eig_vals) ell = mpl.patches.Ellipse(means[n], eig_vals[0], eig_vals[1], 180 + angle, edgecolor='black') ell.set_clip_box(ax.bbox) ell.set_alpha(weights[n]) ell.set_facecolor('#56B4E9') ax.add_artist(ell) def plot_results(ax1, ax2, estimator, X, y, title, plot_title=False): ax1.set_title(title) ax1.scatter(X[:, 0], X[:, 1], s=5, marker='o', color=colors[y], alpha=0.8) ax1.set_xlim(-2., 2.) ax1.set_ylim(-3., 3.) ax1.set_xticks(()) ax1.set_yticks(()) plot_ellipses(ax1, estimator.weights_, estimator.means_, estimator.covariances_) ax2.get_xaxis().set_tick_params(direction='out') ax2.yaxis.grid(True, alpha=0.7) for k, w in enumerate(estimator.weights_): ax2.bar(k, w, width=0.9, color='#56B4E9', zorder=3, align='center', edgecolor='black') ax2.text(k, w + 0.007, "%.1f%%" % (w * 100.), horizontalalignment='center') ax2.set_xlim(-.6, 2 * n_components - .4) ax2.set_ylim(0., 1.1) ax2.tick_params(axis='y', which='both', left=False, right=False, labelleft=False) ax2.tick_params(axis='x', which='both', top=False) if plot_title: ax1.set_ylabel('Estimated Mixtures') ax2.set_ylabel('Weight of each component') # Parameters of the dataset random_state, n_components, n_features = 2, 3, 2 colors = np.array(['#0072B2', '#F0E442', '#D55E00']) covars = np.array([[[.7, .0], [.0, .1]], [[.5, .0], [.0, .1]], [[.5, .0], [.0, .1]]]) samples = np.array([200, 500, 200]) means = np.array([[.0, -.70], [.0, .0], [.0, .70]]) # mean_precision_prior= 0.8 to minimize the influence of the prior estimators = [ ("Finite mixture with a Dirichlet distribution\nprior and " r"$\gamma_0=$", BayesianGaussianMixture( weight_concentration_prior_type="dirichlet_distribution", n_components=2 * n_components, reg_covar=0, init_params='random', max_iter=1500, mean_precision_prior=.8, random_state=random_state), [0.001, 1, 1000]), ("Infinite mixture with a Dirichlet process\n prior and" r"$\gamma_0=$", BayesianGaussianMixture( weight_concentration_prior_type="dirichlet_process", n_components=2 * n_components, reg_covar=0, init_params='random', max_iter=1500, mean_precision_prior=.8, random_state=random_state), [1, 1000, 100000])] # Generate data rng = np.random.RandomState(random_state) X = np.vstack([ rng.multivariate_normal(means[j], covars[j], samples[j]) for j in range(n_components)]) y = np.concatenate([np.full(samples[j], j, dtype=int) for j in range(n_components)]) # Plot results in two different figures for (title, estimator, concentrations_prior) in estimators: plt.figure(figsize=(4.7 * 3, 8)) plt.subplots_adjust(bottom=.04, top=0.90, hspace=.05, wspace=.05, left=.03, right=.99) gs = gridspec.GridSpec(3, len(concentrations_prior)) for k, concentration in enumerate(concentrations_prior): estimator.weight_concentration_prior = concentration estimator.fit(X) plot_results(plt.subplot(gs[0:2, k]), plt.subplot(gs[2, k]), estimator, X, y, r"%s$%.1e$" % (title, concentration), plot_title=k == 0) plt.show()
bsd-3-clause
irremotus/biology_scripts
nmds_plotter.py
1
2510
#!/usr/bin/python3 # MIT License # # Copyright (c) 2017 Kevin A. Schmittle # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import sys import pandas as pd import random import matplotlib.pyplot as plt import seaborn as sns sns.set(style='ticks', palette='Set2') sns.despine() if len(sys.argv) < 2: print("Usage: {prog} <data file> <map file> <title> <x axis> <y axis> <legend title> <output file>".format( prog=sys.argv[0] )) sys.exit(1) data_filename = sys.argv[1] map_filename = sys.argv[2] title = sys.argv[3] xaxis = sys.argv[4] yaxis = sys.argv[5] legend = sys.argv[6] outname = sys.argv[7] df = pd.DataFrame() with open(data_filename, "r") as f: header = f.readline().split() names = [] x = [] y = [] z = [] for line in f.readlines(): parts = line.split() names.append(parts[0]) x.append(float(parts[1])) y.append(float(parts[2])) with open(map_filename, "r") as f: header = f.readline().split() mapping = {} for line in f.readlines(): parts = line.split() mapping[parts[0]] = (parts[1], parts[3]) df['x_axis_data'] = x df['y_axis_data'] = y df[legend] = [mapping[name][1] for name in names] sns.set_style("ticks") plot = sns.lmplot('x_axis_data', 'y_axis_data', data=df, fit_reg=False, hue=legend, scatter_kws={"marker": "D", "s": 100}) plt.title(title) plt.xlabel(xaxis) plt.ylabel(yaxis) plot.savefig(outname)
mit
Djabbz/scikit-learn
examples/model_selection/plot_roc_crossval.py
247
3253
""" ============================================================= Receiver Operating Characteristic (ROC) with cross validation ============================================================= Example of Receiver Operating Characteristic (ROC) metric to evaluate classifier output quality using cross-validation. ROC curves typically feature true positive rate on the Y axis, and false positive rate on the X axis. This means that the top left corner of the plot is the "ideal" point - a false positive rate of zero, and a true positive rate of one. This is not very realistic, but it does mean that a larger area under the curve (AUC) is usually better. The "steepness" of ROC curves is also important, since it is ideal to maximize the true positive rate while minimizing the false positive rate. This example shows the ROC response of different datasets, created from K-fold cross-validation. Taking all of these curves, it is possible to calculate the mean area under curve, and see the variance of the curve when the training set is split into different subsets. This roughly shows how the classifier output is affected by changes in the training data, and how different the splits generated by K-fold cross-validation are from one another. .. note:: See also :func:`sklearn.metrics.auc_score`, :func:`sklearn.cross_validation.cross_val_score`, :ref:`example_model_selection_plot_roc.py`, """ print(__doc__) import numpy as np from scipy import interp import matplotlib.pyplot as plt from sklearn import svm, datasets from sklearn.metrics import roc_curve, auc from sklearn.cross_validation import StratifiedKFold ############################################################################### # Data IO and generation # import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target X, y = X[y != 2], y[y != 2] n_samples, n_features = X.shape # Add noisy features random_state = np.random.RandomState(0) X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] ############################################################################### # Classification and ROC analysis # Run classifier with cross-validation and plot ROC curves cv = StratifiedKFold(y, n_folds=6) classifier = svm.SVC(kernel='linear', probability=True, random_state=random_state) mean_tpr = 0.0 mean_fpr = np.linspace(0, 1, 100) all_tpr = [] for i, (train, test) in enumerate(cv): probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test]) # Compute ROC curve and area the curve fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1]) mean_tpr += interp(mean_fpr, fpr, tpr) mean_tpr[0] = 0.0 roc_auc = auc(fpr, tpr) plt.plot(fpr, tpr, lw=1, label='ROC fold %d (area = %0.2f)' % (i, roc_auc)) plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck') mean_tpr /= len(cv) mean_tpr[-1] = 1.0 mean_auc = auc(mean_fpr, mean_tpr) plt.plot(mean_fpr, mean_tpr, 'k--', label='Mean ROC (area = %0.2f)' % mean_auc, lw=2) plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic example') plt.legend(loc="lower right") plt.show()
bsd-3-clause
317070/kaggle-heart
util_scripts/test_shearing.py
1
1842
import numpy as np import numpy as np import skimage.io import skimage.transform from image_transform import perturb import matplotlib.pyplot as plt from matplotlib import animation import matplotlib.pyplot as plt from skimage import io image = np.clip(io.imread("dickbutt.jpg"),0.0, 1.0)[:,:,0] print image.shape result = perturb(image, target_shape=(500, 500), augmentation_params={"zoom_range":[0.05, 0.05], "rotation_range":[0.0, 0.0], "shear_range":[0, 0], "skew_x_range":[0, 0], "skew_y_range":[0, 0], "translation_range":[0.0, 0.0], "do_flip":False, "allow_stretch":False}) fig = plt.figure() mngr = plt.get_current_fig_manager() # to put it into the upper left corner for example: mngr.window.setGeometry(50, 100, 600, 300) im1 = fig.gca().imshow(result, cmap='gist_gray_r', vmin=0, vmax=1) def init(): im1.set_data(result) def animate(i): result = perturb(image, target_shape=(500, 500), augmentation_params={"rotation_range":[float(i), float(i)], "zoom_range":[0.5, 0.5], "skew_x_range":[-20, 20], "skew_y_range":[-20, 20], "do_flip":False, "allow_stretch":True}) fig.suptitle("shear %f"%float(i)) im1.set_data(result) return im1 anim = animation.FuncAnimation(fig, animate, init_func=init, frames=360, interval=50) #anim.save('my_animation.mp4') plt.show()
mit
mayblue9/scikit-learn
examples/ensemble/plot_bias_variance.py
357
7324
""" ============================================================ Single estimator versus bagging: bias-variance decomposition ============================================================ This example illustrates and compares the bias-variance decomposition of the expected mean squared error of a single estimator against a bagging ensemble. In regression, the expected mean squared error of an estimator can be decomposed in terms of bias, variance and noise. On average over datasets of the regression problem, the bias term measures the average amount by which the predictions of the estimator differ from the predictions of the best possible estimator for the problem (i.e., the Bayes model). The variance term measures the variability of the predictions of the estimator when fit over different instances LS of the problem. Finally, the noise measures the irreducible part of the error which is due the variability in the data. The upper left figure illustrates the predictions (in dark red) of a single decision tree trained over a random dataset LS (the blue dots) of a toy 1d regression problem. It also illustrates the predictions (in light red) of other single decision trees trained over other (and different) randomly drawn instances LS of the problem. Intuitively, the variance term here corresponds to the width of the beam of predictions (in light red) of the individual estimators. The larger the variance, the more sensitive are the predictions for `x` to small changes in the training set. The bias term corresponds to the difference between the average prediction of the estimator (in cyan) and the best possible model (in dark blue). On this problem, we can thus observe that the bias is quite low (both the cyan and the blue curves are close to each other) while the variance is large (the red beam is rather wide). The lower left figure plots the pointwise decomposition of the expected mean squared error of a single decision tree. It confirms that the bias term (in blue) is low while the variance is large (in green). It also illustrates the noise part of the error which, as expected, appears to be constant and around `0.01`. The right figures correspond to the same plots but using instead a bagging ensemble of decision trees. In both figures, we can observe that the bias term is larger than in the previous case. In the upper right figure, the difference between the average prediction (in cyan) and the best possible model is larger (e.g., notice the offset around `x=2`). In the lower right figure, the bias curve is also slightly higher than in the lower left figure. In terms of variance however, the beam of predictions is narrower, which suggests that the variance is lower. Indeed, as the lower right figure confirms, the variance term (in green) is lower than for single decision trees. Overall, the bias- variance decomposition is therefore no longer the same. The tradeoff is better for bagging: averaging several decision trees fit on bootstrap copies of the dataset slightly increases the bias term but allows for a larger reduction of the variance, which results in a lower overall mean squared error (compare the red curves int the lower figures). The script output also confirms this intuition. The total error of the bagging ensemble is lower than the total error of a single decision tree, and this difference indeed mainly stems from a reduced variance. For further details on bias-variance decomposition, see section 7.3 of [1]_. References ---------- .. [1] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical Learning", Springer, 2009. """ print(__doc__) # Author: Gilles Louppe <[email protected]> # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import BaggingRegressor from sklearn.tree import DecisionTreeRegressor # Settings n_repeat = 50 # Number of iterations for computing expectations n_train = 50 # Size of the training set n_test = 1000 # Size of the test set noise = 0.1 # Standard deviation of the noise np.random.seed(0) # Change this for exploring the bias-variance decomposition of other # estimators. This should work well for estimators with high variance (e.g., # decision trees or KNN), but poorly for estimators with low variance (e.g., # linear models). estimators = [("Tree", DecisionTreeRegressor()), ("Bagging(Tree)", BaggingRegressor(DecisionTreeRegressor()))] n_estimators = len(estimators) # Generate data def f(x): x = x.ravel() return np.exp(-x ** 2) + 1.5 * np.exp(-(x - 2) ** 2) def generate(n_samples, noise, n_repeat=1): X = np.random.rand(n_samples) * 10 - 5 X = np.sort(X) if n_repeat == 1: y = f(X) + np.random.normal(0.0, noise, n_samples) else: y = np.zeros((n_samples, n_repeat)) for i in range(n_repeat): y[:, i] = f(X) + np.random.normal(0.0, noise, n_samples) X = X.reshape((n_samples, 1)) return X, y X_train = [] y_train = [] for i in range(n_repeat): X, y = generate(n_samples=n_train, noise=noise) X_train.append(X) y_train.append(y) X_test, y_test = generate(n_samples=n_test, noise=noise, n_repeat=n_repeat) # Loop over estimators to compare for n, (name, estimator) in enumerate(estimators): # Compute predictions y_predict = np.zeros((n_test, n_repeat)) for i in range(n_repeat): estimator.fit(X_train[i], y_train[i]) y_predict[:, i] = estimator.predict(X_test) # Bias^2 + Variance + Noise decomposition of the mean squared error y_error = np.zeros(n_test) for i in range(n_repeat): for j in range(n_repeat): y_error += (y_test[:, j] - y_predict[:, i]) ** 2 y_error /= (n_repeat * n_repeat) y_noise = np.var(y_test, axis=1) y_bias = (f(X_test) - np.mean(y_predict, axis=1)) ** 2 y_var = np.var(y_predict, axis=1) print("{0}: {1:.4f} (error) = {2:.4f} (bias^2) " " + {3:.4f} (var) + {4:.4f} (noise)".format(name, np.mean(y_error), np.mean(y_bias), np.mean(y_var), np.mean(y_noise))) # Plot figures plt.subplot(2, n_estimators, n + 1) plt.plot(X_test, f(X_test), "b", label="$f(x)$") plt.plot(X_train[0], y_train[0], ".b", label="LS ~ $y = f(x)+noise$") for i in range(n_repeat): if i == 0: plt.plot(X_test, y_predict[:, i], "r", label="$\^y(x)$") else: plt.plot(X_test, y_predict[:, i], "r", alpha=0.05) plt.plot(X_test, np.mean(y_predict, axis=1), "c", label="$\mathbb{E}_{LS} \^y(x)$") plt.xlim([-5, 5]) plt.title(name) if n == 0: plt.legend(loc="upper left", prop={"size": 11}) plt.subplot(2, n_estimators, n_estimators + n + 1) plt.plot(X_test, y_error, "r", label="$error(x)$") plt.plot(X_test, y_bias, "b", label="$bias^2(x)$"), plt.plot(X_test, y_var, "g", label="$variance(x)$"), plt.plot(X_test, y_noise, "c", label="$noise(x)$") plt.xlim([-5, 5]) plt.ylim([0, 0.1]) if n == 0: plt.legend(loc="upper left", prop={"size": 11}) plt.show()
bsd-3-clause
PatrickChrist/scikit-learn
sklearn/svm/classes.py
126
40114
import warnings import numpy as np from .base import _fit_liblinear, BaseSVC, BaseLibSVM from ..base import BaseEstimator, RegressorMixin from ..linear_model.base import LinearClassifierMixin, SparseCoefMixin, \ LinearModel from ..feature_selection.from_model import _LearntSelectorMixin from ..utils import check_X_y from ..utils.validation import _num_samples class LinearSVC(BaseEstimator, LinearClassifierMixin, _LearntSelectorMixin, SparseCoefMixin): """Linear Support Vector Classification. Similar to SVC with parameter kernel='linear', but implemented in terms of liblinear rather than libsvm, so it has more flexibility in the choice of penalties and loss functions and should scale better to large numbers of samples. This class supports both dense and sparse input and the multiclass support is handled according to a one-vs-the-rest scheme. Read more in the :ref:`User Guide <svm_classification>`. Parameters ---------- C : float, optional (default=1.0) Penalty parameter C of the error term. loss : string, 'hinge' or 'squared_hinge' (default='squared_hinge') Specifies the loss function. 'hinge' is the standard SVM loss (used e.g. by the SVC class) while 'squared_hinge' is the square of the hinge loss. penalty : string, 'l1' or 'l2' (default='l2') Specifies the norm used in the penalization. The 'l2' penalty is the standard used in SVC. The 'l1' leads to `coef_` vectors that are sparse. dual : bool, (default=True) Select the algorithm to either solve the dual or primal optimization problem. Prefer dual=False when n_samples > n_features. tol : float, optional (default=1e-4) Tolerance for stopping criteria. multi_class: string, 'ovr' or 'crammer_singer' (default='ovr') Determines the multi-class strategy if `y` contains more than two classes. `ovr` trains n_classes one-vs-rest classifiers, while `crammer_singer` optimizes a joint objective over all classes. While `crammer_singer` is interesting from a theoretical perspective as it is consistent, it is seldom used in practice as it rarely leads to better accuracy and is more expensive to compute. If `crammer_singer` is chosen, the options loss, penalty and dual will be ignored. fit_intercept : boolean, optional (default=True) Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be already centered). intercept_scaling : float, optional (default=1) When self.fit_intercept is True, instance vector x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. class_weight : {dict, 'balanced'}, optional Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` verbose : int, (default=0) Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in liblinear that, if enabled, may not work properly in a multithreaded context. random_state : int seed, RandomState instance, or None (default=None) The seed of the pseudo random number generator to use when shuffling the data. max_iter : int, (default=1000) The maximum number of iterations to be run. Attributes ---------- coef_ : array, shape = [n_features] if n_classes == 2 else [n_classes, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is a readonly property derived from `raw_coef_` that follows the internal memory layout of liblinear. intercept_ : array, shape = [1] if n_classes == 2 else [n_classes] Constants in decision function. Notes ----- The underlying C implementation uses a random number generator to select features when fitting the model. It is thus not uncommon to have slightly different results for the same input data. If that happens, try with a smaller ``tol`` parameter. The underlying implementation (liblinear) uses a sparse internal representation for the data that will incur a memory copy. Predict output may not match that of standalone liblinear in certain cases. See :ref:`differences from liblinear <liblinear_differences>` in the narrative documentation. **References:** `LIBLINEAR: A Library for Large Linear Classification <http://www.csie.ntu.edu.tw/~cjlin/liblinear/>`__ See also -------- SVC Implementation of Support Vector Machine classifier using libsvm: the kernel can be non-linear but its SMO algorithm does not scale to large number of samples as LinearSVC does. Furthermore SVC multi-class mode is implemented using one vs one scheme while LinearSVC uses one vs the rest. It is possible to implement one vs the rest with SVC by using the :class:`sklearn.multiclass.OneVsRestClassifier` wrapper. Finally SVC can fit dense data without memory copy if the input is C-contiguous. Sparse data will still incur memory copy though. sklearn.linear_model.SGDClassifier SGDClassifier can optimize the same cost function as LinearSVC by adjusting the penalty and loss parameters. In addition it requires less memory, allows incremental (online) learning, and implements various loss functions and regularization regimes. """ def __init__(self, penalty='l2', loss='squared_hinge', dual=True, tol=1e-4, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None, max_iter=1000): self.dual = dual self.tol = tol self.C = C self.multi_class = multi_class self.fit_intercept = fit_intercept self.intercept_scaling = intercept_scaling self.class_weight = class_weight self.verbose = verbose self.random_state = random_state self.max_iter = max_iter self.penalty = penalty self.loss = loss def fit(self, X, y): """Fit the model according to the given training data. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vector, where n_samples in the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target vector relative to X Returns ------- self : object Returns self. """ # FIXME Remove l1/l2 support in 1.0 ----------------------------------- loss_l = self.loss.lower() msg = ("loss='%s' has been deprecated in favor of " "loss='%s' as of 0.16. Backward compatibility" " for the loss='%s' will be removed in %s") # FIXME change loss_l --> self.loss after 0.18 if loss_l in ('l1', 'l2'): old_loss = self.loss self.loss = {'l1': 'hinge', 'l2': 'squared_hinge'}.get(loss_l) warnings.warn(msg % (old_loss, self.loss, old_loss, '1.0'), DeprecationWarning) # --------------------------------------------------------------------- if self.C < 0: raise ValueError("Penalty term must be positive; got (C=%r)" % self.C) X, y = check_X_y(X, y, accept_sparse='csr', dtype=np.float64, order="C") self.classes_ = np.unique(y) self.coef_, self.intercept_, self.n_iter_ = _fit_liblinear( X, y, self.C, self.fit_intercept, self.intercept_scaling, self.class_weight, self.penalty, self.dual, self.verbose, self.max_iter, self.tol, self.random_state, self.multi_class, self.loss) if self.multi_class == "crammer_singer" and len(self.classes_) == 2: self.coef_ = (self.coef_[1] - self.coef_[0]).reshape(1, -1) if self.fit_intercept: intercept = self.intercept_[1] - self.intercept_[0] self.intercept_ = np.array([intercept]) return self class LinearSVR(LinearModel, RegressorMixin): """Linear Support Vector Regression. Similar to SVR with parameter kernel='linear', but implemented in terms of liblinear rather than libsvm, so it has more flexibility in the choice of penalties and loss functions and should scale better to large numbers of samples. This class supports both dense and sparse input. Read more in the :ref:`User Guide <svm_regression>`. Parameters ---------- C : float, optional (default=1.0) Penalty parameter C of the error term. The penalty is a squared l2 penalty. The bigger this parameter, the less regularization is used. loss : string, 'epsilon_insensitive' or 'squared_epsilon_insensitive' (default='epsilon_insensitive') Specifies the loss function. 'l1' is the epsilon-insensitive loss (standard SVR) while 'l2' is the squared epsilon-insensitive loss. epsilon : float, optional (default=0.1) Epsilon parameter in the epsilon-insensitive loss function. Note that the value of this parameter depends on the scale of the target variable y. If unsure, set epsilon=0. dual : bool, (default=True) Select the algorithm to either solve the dual or primal optimization problem. Prefer dual=False when n_samples > n_features. tol : float, optional (default=1e-4) Tolerance for stopping criteria. fit_intercept : boolean, optional (default=True) Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be already centered). intercept_scaling : float, optional (default=1) When self.fit_intercept is True, instance vector x becomes [x, self.intercept_scaling], i.e. a "synthetic" feature with constant value equals to intercept_scaling is appended to the instance vector. The intercept becomes intercept_scaling * synthetic feature weight Note! the synthetic feature weight is subject to l1/l2 regularization as all other features. To lessen the effect of regularization on synthetic feature weight (and therefore on the intercept) intercept_scaling has to be increased. verbose : int, (default=0) Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in liblinear that, if enabled, may not work properly in a multithreaded context. random_state : int seed, RandomState instance, or None (default=None) The seed of the pseudo random number generator to use when shuffling the data. max_iter : int, (default=1000) The maximum number of iterations to be run. Attributes ---------- coef_ : array, shape = [n_features] if n_classes == 2 else [n_classes, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is a readonly property derived from `raw_coef_` that follows the internal memory layout of liblinear. intercept_ : array, shape = [1] if n_classes == 2 else [n_classes] Constants in decision function. See also -------- LinearSVC Implementation of Support Vector Machine classifier using the same library as this class (liblinear). SVR Implementation of Support Vector Machine regression using libsvm: the kernel can be non-linear but its SMO algorithm does not scale to large number of samples as LinearSVC does. sklearn.linear_model.SGDRegressor SGDRegressor can optimize the same cost function as LinearSVR by adjusting the penalty and loss parameters. In addition it requires less memory, allows incremental (online) learning, and implements various loss functions and regularization regimes. """ def __init__(self, epsilon=0.0, tol=1e-4, C=1.0, loss='epsilon_insensitive', fit_intercept=True, intercept_scaling=1., dual=True, verbose=0, random_state=None, max_iter=1000): self.tol = tol self.C = C self.epsilon = epsilon self.fit_intercept = fit_intercept self.intercept_scaling = intercept_scaling self.verbose = verbose self.random_state = random_state self.max_iter = max_iter self.dual = dual self.loss = loss def fit(self, X, y): """Fit the model according to the given training data. Parameters ---------- X : {array-like, sparse matrix}, shape = [n_samples, n_features] Training vector, where n_samples in the number of samples and n_features is the number of features. y : array-like, shape = [n_samples] Target vector relative to X Returns ------- self : object Returns self. """ # FIXME Remove l1/l2 support in 1.0 ----------------------------------- loss_l = self.loss.lower() msg = ("loss='%s' has been deprecated in favor of " "loss='%s' as of 0.16. Backward compatibility" " for the loss='%s' will be removed in %s") # FIXME change loss_l --> self.loss after 0.18 if loss_l in ('l1', 'l2'): old_loss = self.loss self.loss = {'l1': 'epsilon_insensitive', 'l2': 'squared_epsilon_insensitive' }.get(loss_l) warnings.warn(msg % (old_loss, self.loss, old_loss, '1.0'), DeprecationWarning) # --------------------------------------------------------------------- if self.C < 0: raise ValueError("Penalty term must be positive; got (C=%r)" % self.C) X, y = check_X_y(X, y, accept_sparse='csr', dtype=np.float64, order="C") penalty = 'l2' # SVR only accepts l2 penalty self.coef_, self.intercept_, self.n_iter_ = _fit_liblinear( X, y, self.C, self.fit_intercept, self.intercept_scaling, None, penalty, self.dual, self.verbose, self.max_iter, self.tol, self.random_state, loss=self.loss, epsilon=self.epsilon) self.coef_ = self.coef_.ravel() return self class SVC(BaseSVC): """C-Support Vector Classification. The implementation is based on libsvm. The fit time complexity is more than quadratic with the number of samples which makes it hard to scale to dataset with more than a couple of 10000 samples. The multiclass support is handled according to a one-vs-one scheme. For details on the precise mathematical formulation of the provided kernel functions and how `gamma`, `coef0` and `degree` affect each other, see the corresponding section in the narrative documentation: :ref:`svm_kernels`. Read more in the :ref:`User Guide <svm_classification>`. Parameters ---------- C : float, optional (default=1.0) Penalty parameter C of the error term. kernel : string, optional (default='rbf') Specifies the kernel type to be used in the algorithm. It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or a callable. If none is given, 'rbf' will be used. If a callable is given it is used to pre-compute the kernel matrix from data matrices; that matrix should be an array of shape ``(n_samples, n_samples)``. degree : int, optional (default=3) Degree of the polynomial kernel function ('poly'). Ignored by all other kernels. gamma : float, optional (default='auto') Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma is 'auto' then 1/n_features will be used instead. coef0 : float, optional (default=0.0) Independent term in kernel function. It is only significant in 'poly' and 'sigmoid'. probability : boolean, optional (default=False) Whether to enable probability estimates. This must be enabled prior to calling `fit`, and will slow down that method. shrinking : boolean, optional (default=True) Whether to use the shrinking heuristic. tol : float, optional (default=1e-3) Tolerance for stopping criterion. cache_size : float, optional Specify the size of the kernel cache (in MB). class_weight : {dict, 'balanced'}, optional Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` verbose : bool, default: False Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context. max_iter : int, optional (default=-1) Hard limit on iterations within solver, or -1 for no limit. decision_function_shape : 'ovo', 'ovr' or None, default=None Whether to return a one-vs-rest ('ovr') ecision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one ('ovo') decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). The default of None will currently behave as 'ovo' for backward compatibility and raise a deprecation warning, but will change 'ovr' in 0.18. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data for probability estimation. Attributes ---------- support_ : array-like, shape = [n_SV] Indices of support vectors. support_vectors_ : array-like, shape = [n_SV, n_features] Support vectors. n_support_ : array-like, dtype=int32, shape = [n_class] Number of support vectors for each class. dual_coef_ : array, shape = [n_class-1, n_SV] Coefficients of the support vector in the decision function. For multiclass, coefficient for all 1-vs-1 classifiers. The layout of the coefficients in the multiclass case is somewhat non-trivial. See the section about multi-class classification in the SVM section of the User Guide for details. coef_ : array, shape = [n_class-1, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is a readonly property derived from `dual_coef_` and `support_vectors_`. intercept_ : array, shape = [n_class * (n_class-1) / 2] Constants in decision function. Examples -------- >>> import numpy as np >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> y = np.array([1, 1, 2, 2]) >>> from sklearn.svm import SVC >>> clf = SVC() >>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False) >>> print(clf.predict([[-0.8, -1]])) [1] See also -------- SVR Support Vector Machine for Regression implemented using libsvm. LinearSVC Scalable Linear Support Vector Machine for classification implemented using liblinear. Check the See also section of LinearSVC for more comparison element. """ def __init__(self, C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False, tol=1e-3, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape=None, random_state=None): super(SVC, self).__init__( impl='c_svc', kernel=kernel, degree=degree, gamma=gamma, coef0=coef0, tol=tol, C=C, nu=0., shrinking=shrinking, probability=probability, cache_size=cache_size, class_weight=class_weight, verbose=verbose, max_iter=max_iter, decision_function_shape=decision_function_shape, random_state=random_state) class NuSVC(BaseSVC): """Nu-Support Vector Classification. Similar to SVC but uses a parameter to control the number of support vectors. The implementation is based on libsvm. Read more in the :ref:`User Guide <svm_classification>`. Parameters ---------- nu : float, optional (default=0.5) An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. kernel : string, optional (default='rbf') Specifies the kernel type to be used in the algorithm. It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or a callable. If none is given, 'rbf' will be used. If a callable is given it is used to precompute the kernel matrix. degree : int, optional (default=3) Degree of the polynomial kernel function ('poly'). Ignored by all other kernels. gamma : float, optional (default='auto') Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma is 'auto' then 1/n_features will be used instead. coef0 : float, optional (default=0.0) Independent term in kernel function. It is only significant in 'poly' and 'sigmoid'. probability : boolean, optional (default=False) Whether to enable probability estimates. This must be enabled prior to calling `fit`, and will slow down that method. shrinking : boolean, optional (default=True) Whether to use the shrinking heuristic. tol : float, optional (default=1e-3) Tolerance for stopping criterion. cache_size : float, optional Specify the size of the kernel cache (in MB). class_weight : {dict, 'auto'}, optional Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are supposed to have weight one. The 'auto' mode uses the values of y to automatically adjust weights inversely proportional to class frequencies. verbose : bool, default: False Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context. max_iter : int, optional (default=-1) Hard limit on iterations within solver, or -1 for no limit. decision_function_shape : 'ovo', 'ovr' or None, default=None Whether to return a one-vs-rest ('ovr') ecision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one ('ovo') decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). The default of None will currently behave as 'ovo' for backward compatibility and raise a deprecation warning, but will change 'ovr' in 0.18. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data for probability estimation. Attributes ---------- support_ : array-like, shape = [n_SV] Indices of support vectors. support_vectors_ : array-like, shape = [n_SV, n_features] Support vectors. n_support_ : array-like, dtype=int32, shape = [n_class] Number of support vectors for each class. dual_coef_ : array, shape = [n_class-1, n_SV] Coefficients of the support vector in the decision function. For multiclass, coefficient for all 1-vs-1 classifiers. The layout of the coefficients in the multiclass case is somewhat non-trivial. See the section about multi-class classification in the SVM section of the User Guide for details. coef_ : array, shape = [n_class-1, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is readonly property derived from `dual_coef_` and `support_vectors_`. intercept_ : array, shape = [n_class * (n_class-1) / 2] Constants in decision function. Examples -------- >>> import numpy as np >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> y = np.array([1, 1, 2, 2]) >>> from sklearn.svm import NuSVC >>> clf = NuSVC() >>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE NuSVC(cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma='auto', kernel='rbf', max_iter=-1, nu=0.5, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False) >>> print(clf.predict([[-0.8, -1]])) [1] See also -------- SVC Support Vector Machine for classification using libsvm. LinearSVC Scalable linear Support Vector Machine for classification using liblinear. """ def __init__(self, nu=0.5, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False, tol=1e-3, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape=None, random_state=None): super(NuSVC, self).__init__( impl='nu_svc', kernel=kernel, degree=degree, gamma=gamma, coef0=coef0, tol=tol, C=0., nu=nu, shrinking=shrinking, probability=probability, cache_size=cache_size, class_weight=class_weight, verbose=verbose, max_iter=max_iter, decision_function_shape=decision_function_shape, random_state=random_state) class SVR(BaseLibSVM, RegressorMixin): """Epsilon-Support Vector Regression. The free parameters in the model are C and epsilon. The implementation is based on libsvm. Read more in the :ref:`User Guide <svm_regression>`. Parameters ---------- C : float, optional (default=1.0) Penalty parameter C of the error term. epsilon : float, optional (default=0.1) Epsilon in the epsilon-SVR model. It specifies the epsilon-tube within which no penalty is associated in the training loss function with points predicted within a distance epsilon from the actual value. kernel : string, optional (default='rbf') Specifies the kernel type to be used in the algorithm. It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or a callable. If none is given, 'rbf' will be used. If a callable is given it is used to precompute the kernel matrix. degree : int, optional (default=3) Degree of the polynomial kernel function ('poly'). Ignored by all other kernels. gamma : float, optional (default='auto') Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma is 'auto' then 1/n_features will be used instead. coef0 : float, optional (default=0.0) Independent term in kernel function. It is only significant in 'poly' and 'sigmoid'. shrinking : boolean, optional (default=True) Whether to use the shrinking heuristic. tol : float, optional (default=1e-3) Tolerance for stopping criterion. cache_size : float, optional Specify the size of the kernel cache (in MB). verbose : bool, default: False Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context. max_iter : int, optional (default=-1) Hard limit on iterations within solver, or -1 for no limit. Attributes ---------- support_ : array-like, shape = [n_SV] Indices of support vectors. support_vectors_ : array-like, shape = [nSV, n_features] Support vectors. dual_coef_ : array, shape = [1, n_SV] Coefficients of the support vector in the decision function. coef_ : array, shape = [1, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is readonly property derived from `dual_coef_` and `support_vectors_`. intercept_ : array, shape = [1] Constants in decision function. Examples -------- >>> from sklearn.svm import SVR >>> import numpy as np >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = SVR(C=1.0, epsilon=0.2) >>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.2, gamma='auto', kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False) See also -------- NuSVR Support Vector Machine for regression implemented using libsvm using a parameter to control the number of support vectors. LinearSVR Scalable Linear Support Vector Machine for regression implemented using liblinear. """ def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=1e-3, C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1): super(SVR, self).__init__( 'epsilon_svr', kernel=kernel, degree=degree, gamma=gamma, coef0=coef0, tol=tol, C=C, nu=0., epsilon=epsilon, verbose=verbose, shrinking=shrinking, probability=False, cache_size=cache_size, class_weight=None, max_iter=max_iter, random_state=None) class NuSVR(BaseLibSVM, RegressorMixin): """Nu Support Vector Regression. Similar to NuSVC, for regression, uses a parameter nu to control the number of support vectors. However, unlike NuSVC, where nu replaces C, here nu replaces the parameter epsilon of epsilon-SVR. The implementation is based on libsvm. Read more in the :ref:`User Guide <svm_regression>`. Parameters ---------- C : float, optional (default=1.0) Penalty parameter C of the error term. nu : float, optional An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. By default 0.5 will be taken. kernel : string, optional (default='rbf') Specifies the kernel type to be used in the algorithm. It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or a callable. If none is given, 'rbf' will be used. If a callable is given it is used to precompute the kernel matrix. degree : int, optional (default=3) Degree of the polynomial kernel function ('poly'). Ignored by all other kernels. gamma : float, optional (default='auto') Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma is 'auto' then 1/n_features will be used instead. coef0 : float, optional (default=0.0) Independent term in kernel function. It is only significant in 'poly' and 'sigmoid'. shrinking : boolean, optional (default=True) Whether to use the shrinking heuristic. tol : float, optional (default=1e-3) Tolerance for stopping criterion. cache_size : float, optional Specify the size of the kernel cache (in MB). verbose : bool, default: False Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context. max_iter : int, optional (default=-1) Hard limit on iterations within solver, or -1 for no limit. Attributes ---------- support_ : array-like, shape = [n_SV] Indices of support vectors. support_vectors_ : array-like, shape = [nSV, n_features] Support vectors. dual_coef_ : array, shape = [1, n_SV] Coefficients of the support vector in the decision function. coef_ : array, shape = [1, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is readonly property derived from `dual_coef_` and `support_vectors_`. intercept_ : array, shape = [1] Constants in decision function. Examples -------- >>> from sklearn.svm import NuSVR >>> import numpy as np >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = NuSVR(C=1.0, nu=0.1) >>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE NuSVR(C=1.0, cache_size=200, coef0=0.0, degree=3, gamma='auto', kernel='rbf', max_iter=-1, nu=0.1, shrinking=True, tol=0.001, verbose=False) See also -------- NuSVC Support Vector Machine for classification implemented with libsvm with a parameter to control the number of support vectors. SVR epsilon Support Vector Machine for regression implemented with libsvm. """ def __init__(self, nu=0.5, C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, tol=1e-3, cache_size=200, verbose=False, max_iter=-1): super(NuSVR, self).__init__( 'nu_svr', kernel=kernel, degree=degree, gamma=gamma, coef0=coef0, tol=tol, C=C, nu=nu, epsilon=0., shrinking=shrinking, probability=False, cache_size=cache_size, class_weight=None, verbose=verbose, max_iter=max_iter, random_state=None) class OneClassSVM(BaseLibSVM): """Unsupervised Outlier Detection. Estimate the support of a high-dimensional distribution. The implementation is based on libsvm. Read more in the :ref:`User Guide <svm_outlier_detection>`. Parameters ---------- kernel : string, optional (default='rbf') Specifies the kernel type to be used in the algorithm. It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or a callable. If none is given, 'rbf' will be used. If a callable is given it is used to precompute the kernel matrix. nu : float, optional An upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. By default 0.5 will be taken. degree : int, optional (default=3) Degree of the polynomial kernel function ('poly'). Ignored by all other kernels. gamma : float, optional (default='auto') Kernel coefficient for 'rbf', 'poly' and 'sigmoid'. If gamma is 'auto' then 1/n_features will be used instead. coef0 : float, optional (default=0.0) Independent term in kernel function. It is only significant in 'poly' and 'sigmoid'. tol : float, optional Tolerance for stopping criterion. shrinking : boolean, optional Whether to use the shrinking heuristic. cache_size : float, optional Specify the size of the kernel cache (in MB). verbose : bool, default: False Enable verbose output. Note that this setting takes advantage of a per-process runtime setting in libsvm that, if enabled, may not work properly in a multithreaded context. max_iter : int, optional (default=-1) Hard limit on iterations within solver, or -1 for no limit. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data for probability estimation. Attributes ---------- support_ : array-like, shape = [n_SV] Indices of support vectors. support_vectors_ : array-like, shape = [nSV, n_features] Support vectors. dual_coef_ : array, shape = [n_classes-1, n_SV] Coefficients of the support vectors in the decision function. coef_ : array, shape = [n_classes-1, n_features] Weights assigned to the features (coefficients in the primal problem). This is only available in the case of a linear kernel. `coef_` is readonly property derived from `dual_coef_` and `support_vectors_` intercept_ : array, shape = [n_classes-1] Constants in decision function. """ def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=1e-3, nu=0.5, shrinking=True, cache_size=200, verbose=False, max_iter=-1, random_state=None): super(OneClassSVM, self).__init__( 'one_class', kernel, degree, gamma, coef0, tol, 0., nu, 0., shrinking, False, cache_size, None, verbose, max_iter, random_state) def fit(self, X, y=None, sample_weight=None, **params): """ Detects the soft boundary of the set of samples X. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Set of samples, where n_samples is the number of samples and n_features is the number of features. sample_weight : array-like, shape (n_samples,) Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points. Returns ------- self : object Returns self. Notes ----- If X is not a C-ordered contiguous array it is copied. """ super(OneClassSVM, self).fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight, **params) return self def decision_function(self, X): """Distance of the samples X to the separating hyperplane. Parameters ---------- X : array-like, shape (n_samples, n_features) Returns ------- X : array-like, shape (n_samples,) Returns the decision function of the samples. """ dec = self._decision_function(X) return dec
bsd-3-clause
NixaSoftware/CVis
venv/lib/python2.7/site-packages/pandas/tests/sparse/test_frame.py
2
52677
# pylint: disable-msg=E1101,W0612 import operator import pytest from warnings import catch_warnings from numpy import nan import numpy as np import pandas as pd from pandas import Series, DataFrame, bdate_range, Panel from pandas.core.dtypes.common import ( is_bool_dtype, is_float_dtype, is_object_dtype, is_float) from pandas.core.indexes.datetimes import DatetimeIndex from pandas.tseries.offsets import BDay from pandas.util import testing as tm from pandas.compat import lrange from pandas import compat from pandas.core.sparse import frame as spf from pandas._libs.sparse import BlockIndex, IntIndex from pandas.core.sparse.api import SparseSeries, SparseDataFrame, SparseArray from pandas.tests.frame.test_api import SharedWithSparse class TestSparseDataFrame(SharedWithSparse): klass = SparseDataFrame # SharedWithSparse tests use generic, klass-agnostic assertion _assert_frame_equal = staticmethod(tm.assert_sp_frame_equal) _assert_series_equal = staticmethod(tm.assert_sp_series_equal) def setup_method(self, method): self.data = {'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6], 'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6], 'C': np.arange(10, dtype=np.float64), 'D': [0, 1, 2, 3, 4, 5, nan, nan, nan, nan]} self.dates = bdate_range('1/1/2011', periods=10) self.orig = pd.DataFrame(self.data, index=self.dates) self.iorig = pd.DataFrame(self.data, index=self.dates) self.frame = SparseDataFrame(self.data, index=self.dates) self.iframe = SparseDataFrame(self.data, index=self.dates, default_kind='integer') self.mixed_frame = self.frame.copy(False) self.mixed_frame['foo'] = pd.SparseArray(['bar'] * len(self.dates)) values = self.frame.values.copy() values[np.isnan(values)] = 0 self.zorig = pd.DataFrame(values, columns=['A', 'B', 'C', 'D'], index=self.dates) self.zframe = SparseDataFrame(values, columns=['A', 'B', 'C', 'D'], default_fill_value=0, index=self.dates) values = self.frame.values.copy() values[np.isnan(values)] = 2 self.fill_orig = pd.DataFrame(values, columns=['A', 'B', 'C', 'D'], index=self.dates) self.fill_frame = SparseDataFrame(values, columns=['A', 'B', 'C', 'D'], default_fill_value=2, index=self.dates) self.empty = SparseDataFrame() def test_fill_value_when_combine_const(self): # GH12723 dat = np.array([0, 1, np.nan, 3, 4, 5], dtype='float') df = SparseDataFrame({'foo': dat}, index=range(6)) exp = df.fillna(0).add(2) res = df.add(2, fill_value=0) tm.assert_sp_frame_equal(res, exp) def test_as_matrix(self): empty = self.empty.as_matrix() assert empty.shape == (0, 0) no_cols = SparseDataFrame(index=np.arange(10)) mat = no_cols.as_matrix() assert mat.shape == (10, 0) no_index = SparseDataFrame(columns=np.arange(10)) mat = no_index.as_matrix() assert mat.shape == (0, 10) def test_copy(self): cp = self.frame.copy() assert isinstance(cp, SparseDataFrame) tm.assert_sp_frame_equal(cp, self.frame) # as of v0.15.0 # this is now identical (but not is_a ) assert cp.index.identical(self.frame.index) def test_constructor(self): for col, series in compat.iteritems(self.frame): assert isinstance(series, SparseSeries) assert isinstance(self.iframe['A'].sp_index, IntIndex) # constructed zframe from matrix above assert self.zframe['A'].fill_value == 0 tm.assert_numpy_array_equal(pd.SparseArray([1., 2., 3., 4., 5., 6.]), self.zframe['A'].values) tm.assert_numpy_array_equal(np.array([0., 0., 0., 0., 1., 2., 3., 4., 5., 6.]), self.zframe['A'].to_dense().values) # construct no data sdf = SparseDataFrame(columns=np.arange(10), index=np.arange(10)) for col, series in compat.iteritems(sdf): assert isinstance(series, SparseSeries) # construct from nested dict data = {} for c, s in compat.iteritems(self.frame): data[c] = s.to_dict() sdf = SparseDataFrame(data) tm.assert_sp_frame_equal(sdf, self.frame) # TODO: test data is copied from inputs # init dict with different index idx = self.frame.index[:5] cons = SparseDataFrame( self.frame, index=idx, columns=self.frame.columns, default_fill_value=self.frame.default_fill_value, default_kind=self.frame.default_kind, copy=True) reindexed = self.frame.reindex(idx) tm.assert_sp_frame_equal(cons, reindexed, exact_indices=False) # assert level parameter breaks reindex with pytest.raises(TypeError): self.frame.reindex(idx, level=0) repr(self.frame) def test_constructor_ndarray(self): # no index or columns sp = SparseDataFrame(self.frame.values) # 1d sp = SparseDataFrame(self.data['A'], index=self.dates, columns=['A']) tm.assert_sp_frame_equal(sp, self.frame.reindex(columns=['A'])) # raise on level argument pytest.raises(TypeError, self.frame.reindex, columns=['A'], level=1) # wrong length index / columns with tm.assert_raises_regex(ValueError, "^Index length"): SparseDataFrame(self.frame.values, index=self.frame.index[:-1]) with tm.assert_raises_regex(ValueError, "^Column length"): SparseDataFrame(self.frame.values, columns=self.frame.columns[:-1]) # GH 9272 def test_constructor_empty(self): sp = SparseDataFrame() assert len(sp.index) == 0 assert len(sp.columns) == 0 def test_constructor_dataframe(self): dense = self.frame.to_dense() sp = SparseDataFrame(dense) tm.assert_sp_frame_equal(sp, self.frame) def test_constructor_convert_index_once(self): arr = np.array([1.5, 2.5, 3.5]) sdf = SparseDataFrame(columns=lrange(4), index=arr) assert sdf[0].index is sdf[1].index def test_constructor_from_series(self): # GH 2873 x = Series(np.random.randn(10000), name='a') x = x.to_sparse(fill_value=0) assert isinstance(x, SparseSeries) df = SparseDataFrame(x) assert isinstance(df, SparseDataFrame) x = Series(np.random.randn(10000), name='a') y = Series(np.random.randn(10000), name='b') x2 = x.astype(float) x2.loc[:9998] = np.NaN # TODO: x_sparse is unused...fix x_sparse = x2.to_sparse(fill_value=np.NaN) # noqa # Currently fails too with weird ufunc error # df1 = SparseDataFrame([x_sparse, y]) y.loc[:9998] = 0 # TODO: y_sparse is unsused...fix y_sparse = y.to_sparse(fill_value=0) # noqa # without sparse value raises error # df2 = SparseDataFrame([x2_sparse, y]) def test_constructor_preserve_attr(self): # GH 13866 arr = pd.SparseArray([1, 0, 3, 0], dtype=np.int64, fill_value=0) assert arr.dtype == np.int64 assert arr.fill_value == 0 df = pd.SparseDataFrame({'x': arr}) assert df['x'].dtype == np.int64 assert df['x'].fill_value == 0 s = pd.SparseSeries(arr, name='x') assert s.dtype == np.int64 assert s.fill_value == 0 df = pd.SparseDataFrame(s) assert df['x'].dtype == np.int64 assert df['x'].fill_value == 0 df = pd.SparseDataFrame({'x': s}) assert df['x'].dtype == np.int64 assert df['x'].fill_value == 0 def test_constructor_nan_dataframe(self): # GH 10079 trains = np.arange(100) tresholds = [10, 20, 30, 40, 50, 60] tuples = [(i, j) for i in trains for j in tresholds] index = pd.MultiIndex.from_tuples(tuples, names=['trains', 'tresholds']) matrix = np.empty((len(index), len(trains))) matrix.fill(np.nan) df = pd.DataFrame(matrix, index=index, columns=trains, dtype=float) result = df.to_sparse() expected = pd.SparseDataFrame(matrix, index=index, columns=trains, dtype=float) tm.assert_sp_frame_equal(result, expected) def test_type_coercion_at_construction(self): # GH 15682 result = pd.SparseDataFrame( {'a': [1, 0, 0], 'b': [0, 1, 0], 'c': [0, 0, 1]}, dtype='uint8', default_fill_value=0) expected = pd.SparseDataFrame( {'a': pd.SparseSeries([1, 0, 0], dtype='uint8'), 'b': pd.SparseSeries([0, 1, 0], dtype='uint8'), 'c': pd.SparseSeries([0, 0, 1], dtype='uint8')}, default_fill_value=0) tm.assert_sp_frame_equal(result, expected) def test_dtypes(self): df = DataFrame(np.random.randn(10000, 4)) df.loc[:9998] = np.nan sdf = df.to_sparse() result = sdf.get_dtype_counts() expected = Series({'float64': 4}) tm.assert_series_equal(result, expected) def test_shape(self): # see gh-10452 assert self.frame.shape == (10, 4) assert self.iframe.shape == (10, 4) assert self.zframe.shape == (10, 4) assert self.fill_frame.shape == (10, 4) def test_str(self): df = DataFrame(np.random.randn(10000, 4)) df.loc[:9998] = np.nan sdf = df.to_sparse() str(sdf) def test_array_interface(self): res = np.sqrt(self.frame) dres = np.sqrt(self.frame.to_dense()) tm.assert_frame_equal(res.to_dense(), dres) def test_pickle(self): def _test_roundtrip(frame, orig): result = tm.round_trip_pickle(frame) tm.assert_sp_frame_equal(frame, result) tm.assert_frame_equal(result.to_dense(), orig, check_dtype=False) _test_roundtrip(SparseDataFrame(), DataFrame()) self._check_all(_test_roundtrip) def test_dense_to_sparse(self): df = DataFrame({'A': [nan, nan, nan, 1, 2], 'B': [1, 2, nan, nan, nan]}) sdf = df.to_sparse() assert isinstance(sdf, SparseDataFrame) assert np.isnan(sdf.default_fill_value) assert isinstance(sdf['A'].sp_index, BlockIndex) tm.assert_frame_equal(sdf.to_dense(), df) sdf = df.to_sparse(kind='integer') assert isinstance(sdf['A'].sp_index, IntIndex) df = DataFrame({'A': [0, 0, 0, 1, 2], 'B': [1, 2, 0, 0, 0]}, dtype=float) sdf = df.to_sparse(fill_value=0) assert sdf.default_fill_value == 0 tm.assert_frame_equal(sdf.to_dense(), df) def test_density(self): df = SparseSeries([nan, nan, nan, 0, 1, 2, 3, 4, 5, 6]) assert df.density == 0.7 df = SparseDataFrame({'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6], 'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6], 'C': np.arange(10), 'D': [0, 1, 2, 3, 4, 5, nan, nan, nan, nan]}) assert df.density == 0.75 def test_sparse_to_dense(self): pass def test_sparse_series_ops(self): self._check_frame_ops(self.frame) def test_sparse_series_ops_i(self): self._check_frame_ops(self.iframe) def test_sparse_series_ops_z(self): self._check_frame_ops(self.zframe) def test_sparse_series_ops_fill(self): self._check_frame_ops(self.fill_frame) def _check_frame_ops(self, frame): def _compare_to_dense(a, b, da, db, op): sparse_result = op(a, b) dense_result = op(da, db) fill = sparse_result.default_fill_value dense_result = dense_result.to_sparse(fill_value=fill) tm.assert_sp_frame_equal(sparse_result, dense_result, exact_indices=False) if isinstance(a, DataFrame) and isinstance(db, DataFrame): mixed_result = op(a, db) assert isinstance(mixed_result, SparseDataFrame) tm.assert_sp_frame_equal(mixed_result, sparse_result, exact_indices=False) opnames = ['add', 'sub', 'mul', 'truediv', 'floordiv'] ops = [getattr(operator, name) for name in opnames] fidx = frame.index # time series operations series = [frame['A'], frame['B'], frame['C'], frame['D'], frame['A'].reindex(fidx[:7]), frame['A'].reindex(fidx[::2]), SparseSeries( [], index=[])] for op in opnames: _compare_to_dense(frame, frame[::2], frame.to_dense(), frame[::2].to_dense(), getattr(operator, op)) # 2304, no auto-broadcasting for i, s in enumerate(series): f = lambda a, b: getattr(a, op)(b, axis='index') _compare_to_dense(frame, s, frame.to_dense(), s.to_dense(), f) # rops are not implemented # _compare_to_dense(s, frame, s.to_dense(), # frame.to_dense(), f) # cross-sectional operations series = [frame.xs(fidx[0]), frame.xs(fidx[3]), frame.xs(fidx[5]), frame.xs(fidx[7]), frame.xs(fidx[5])[:2]] for op in ops: for s in series: _compare_to_dense(frame, s, frame.to_dense(), s, op) _compare_to_dense(s, frame, s, frame.to_dense(), op) # it works! result = self.frame + self.frame.loc[:, ['A', 'B']] # noqa def test_op_corners(self): empty = self.empty + self.empty assert empty.empty foo = self.frame + self.empty assert isinstance(foo.index, DatetimeIndex) tm.assert_frame_equal(foo, self.frame * np.nan) foo = self.empty + self.frame tm.assert_frame_equal(foo, self.frame * np.nan) def test_scalar_ops(self): pass def test_getitem(self): # 1585 select multiple columns sdf = SparseDataFrame(index=[0, 1, 2], columns=['a', 'b', 'c']) result = sdf[['a', 'b']] exp = sdf.reindex(columns=['a', 'b']) tm.assert_sp_frame_equal(result, exp) pytest.raises(Exception, sdf.__getitem__, ['a', 'd']) def test_iloc(self): # 2227 result = self.frame.iloc[:, 0] assert isinstance(result, SparseSeries) tm.assert_sp_series_equal(result, self.frame['A']) # preserve sparse index type. #2251 data = {'A': [0, 1]} iframe = SparseDataFrame(data, default_kind='integer') tm.assert_class_equal(iframe['A'].sp_index, iframe.iloc[:, 0].sp_index) def test_set_value(self): # ok, as the index gets converted to object frame = self.frame.copy() with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): res = frame.set_value('foobar', 'B', 1.5) assert res.index.dtype == 'object' res = self.frame res.index = res.index.astype(object) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): res = self.frame.set_value('foobar', 'B', 1.5) assert res is not self.frame assert res.index[-1] == 'foobar' with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): assert res.get_value('foobar', 'B') == 1.5 with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): res2 = res.set_value('foobar', 'qux', 1.5) assert res2 is not res tm.assert_index_equal(res2.columns, pd.Index(list(self.frame.columns) + ['qux'])) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): assert res2.get_value('foobar', 'qux') == 1.5 def test_fancy_index_misc(self): # axis = 0 sliced = self.frame.iloc[-2:, :] expected = self.frame.reindex(index=self.frame.index[-2:]) tm.assert_sp_frame_equal(sliced, expected) # axis = 1 sliced = self.frame.iloc[:, -2:] expected = self.frame.reindex(columns=self.frame.columns[-2:]) tm.assert_sp_frame_equal(sliced, expected) def test_getitem_overload(self): # slicing sl = self.frame[:20] tm.assert_sp_frame_equal(sl, self.frame.reindex(self.frame.index[:20])) # boolean indexing d = self.frame.index[5] indexer = self.frame.index > d subindex = self.frame.index[indexer] subframe = self.frame[indexer] tm.assert_index_equal(subindex, subframe.index) pytest.raises(Exception, self.frame.__getitem__, indexer[:-1]) def test_setitem(self): def _check_frame(frame, orig): N = len(frame) # insert SparseSeries frame['E'] = frame['A'] assert isinstance(frame['E'], SparseSeries) tm.assert_sp_series_equal(frame['E'], frame['A'], check_names=False) # insert SparseSeries differently-indexed to_insert = frame['A'][::2] frame['E'] = to_insert expected = to_insert.to_dense().reindex(frame.index) result = frame['E'].to_dense() tm.assert_series_equal(result, expected, check_names=False) assert result.name == 'E' # insert Series frame['F'] = frame['A'].to_dense() assert isinstance(frame['F'], SparseSeries) tm.assert_sp_series_equal(frame['F'], frame['A'], check_names=False) # insert Series differently-indexed to_insert = frame['A'].to_dense()[::2] frame['G'] = to_insert expected = to_insert.reindex(frame.index) expected.name = 'G' tm.assert_series_equal(frame['G'].to_dense(), expected) # insert ndarray frame['H'] = np.random.randn(N) assert isinstance(frame['H'], SparseSeries) to_sparsify = np.random.randn(N) to_sparsify[N // 2:] = frame.default_fill_value frame['I'] = to_sparsify assert len(frame['I'].sp_values) == N // 2 # insert ndarray wrong size pytest.raises(Exception, frame.__setitem__, 'foo', np.random.randn(N - 1)) # scalar value frame['J'] = 5 assert len(frame['J'].sp_values) == N assert (frame['J'].sp_values == 5).all() frame['K'] = frame.default_fill_value assert len(frame['K'].sp_values) == 0 self._check_all(_check_frame) def test_setitem_corner(self): self.frame['a'] = self.frame['B'] tm.assert_sp_series_equal(self.frame['a'], self.frame['B'], check_names=False) def test_setitem_array(self): arr = self.frame['B'] self.frame['E'] = arr tm.assert_sp_series_equal(self.frame['E'], self.frame['B'], check_names=False) self.frame['F'] = arr[:-1] index = self.frame.index[:-1] tm.assert_sp_series_equal(self.frame['E'].reindex(index), self.frame['F'].reindex(index), check_names=False) def test_delitem(self): A = self.frame['A'] C = self.frame['C'] del self.frame['B'] assert 'B' not in self.frame tm.assert_sp_series_equal(self.frame['A'], A) tm.assert_sp_series_equal(self.frame['C'], C) del self.frame['D'] assert 'D' not in self.frame del self.frame['A'] assert 'A' not in self.frame def test_set_columns(self): self.frame.columns = self.frame.columns pytest.raises(Exception, setattr, self.frame, 'columns', self.frame.columns[:-1]) def test_set_index(self): self.frame.index = self.frame.index pytest.raises(Exception, setattr, self.frame, 'index', self.frame.index[:-1]) def test_append(self): a = self.frame[:5] b = self.frame[5:] appended = a.append(b) tm.assert_sp_frame_equal(appended, self.frame, exact_indices=False) a = self.frame.iloc[:5, :3] b = self.frame.iloc[5:] appended = a.append(b) tm.assert_sp_frame_equal(appended.iloc[:, :3], self.frame.iloc[:, :3], exact_indices=False) def test_apply(self): applied = self.frame.apply(np.sqrt) assert isinstance(applied, SparseDataFrame) tm.assert_almost_equal(applied.values, np.sqrt(self.frame.values)) applied = self.fill_frame.apply(np.sqrt) assert applied['A'].fill_value == np.sqrt(2) # agg / broadcast broadcasted = self.frame.apply(np.sum, broadcast=True) assert isinstance(broadcasted, SparseDataFrame) exp = self.frame.to_dense().apply(np.sum, broadcast=True) tm.assert_frame_equal(broadcasted.to_dense(), exp) assert self.empty.apply(np.sqrt) is self.empty from pandas.core import nanops applied = self.frame.apply(np.sum) tm.assert_series_equal(applied, self.frame.to_dense().apply(nanops.nansum)) def test_apply_nonuq(self): orig = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=['a', 'a', 'c']) sparse = orig.to_sparse() res = sparse.apply(lambda s: s[0], axis=1) exp = orig.apply(lambda s: s[0], axis=1) # dtype must be kept assert res.dtype == np.int64 # ToDo: apply must return subclassed dtype assert isinstance(res, pd.Series) tm.assert_series_equal(res.to_dense(), exp) # df.T breaks sparse = orig.T.to_sparse() res = sparse.apply(lambda s: s[0], axis=0) # noqa exp = orig.T.apply(lambda s: s[0], axis=0) # TODO: no non-unique columns supported in sparse yet # tm.assert_series_equal(res.to_dense(), exp) def test_applymap(self): # just test that it works result = self.frame.applymap(lambda x: x * 2) assert isinstance(result, SparseDataFrame) def test_astype(self): sparse = pd.SparseDataFrame({'A': SparseArray([1, 2, 3, 4], dtype=np.int64), 'B': SparseArray([4, 5, 6, 7], dtype=np.int64)}) assert sparse['A'].dtype == np.int64 assert sparse['B'].dtype == np.int64 res = sparse.astype(np.float64) exp = pd.SparseDataFrame({'A': SparseArray([1., 2., 3., 4.], fill_value=0.), 'B': SparseArray([4., 5., 6., 7.], fill_value=0.)}, default_fill_value=np.nan) tm.assert_sp_frame_equal(res, exp) assert res['A'].dtype == np.float64 assert res['B'].dtype == np.float64 sparse = pd.SparseDataFrame({'A': SparseArray([0, 2, 0, 4], dtype=np.int64), 'B': SparseArray([0, 5, 0, 7], dtype=np.int64)}, default_fill_value=0) assert sparse['A'].dtype == np.int64 assert sparse['B'].dtype == np.int64 res = sparse.astype(np.float64) exp = pd.SparseDataFrame({'A': SparseArray([0., 2., 0., 4.], fill_value=0.), 'B': SparseArray([0., 5., 0., 7.], fill_value=0.)}, default_fill_value=0.) tm.assert_sp_frame_equal(res, exp) assert res['A'].dtype == np.float64 assert res['B'].dtype == np.float64 def test_astype_bool(self): sparse = pd.SparseDataFrame({'A': SparseArray([0, 2, 0, 4], fill_value=0, dtype=np.int64), 'B': SparseArray([0, 5, 0, 7], fill_value=0, dtype=np.int64)}, default_fill_value=0) assert sparse['A'].dtype == np.int64 assert sparse['B'].dtype == np.int64 res = sparse.astype(bool) exp = pd.SparseDataFrame({'A': SparseArray([False, True, False, True], dtype=np.bool, fill_value=False), 'B': SparseArray([False, True, False, True], dtype=np.bool, fill_value=False)}, default_fill_value=False) tm.assert_sp_frame_equal(res, exp) assert res['A'].dtype == np.bool assert res['B'].dtype == np.bool def test_fillna(self): df = self.zframe.reindex(lrange(5)) dense = self.zorig.reindex(lrange(5)) result = df.fillna(0) expected = dense.fillna(0) tm.assert_sp_frame_equal(result, expected.to_sparse(fill_value=0), exact_indices=False) tm.assert_frame_equal(result.to_dense(), expected) result = df.copy() result.fillna(0, inplace=True) expected = dense.fillna(0) tm.assert_sp_frame_equal(result, expected.to_sparse(fill_value=0), exact_indices=False) tm.assert_frame_equal(result.to_dense(), expected) result = df.copy() result = df['A'] result.fillna(0, inplace=True) expected = dense['A'].fillna(0) # this changes internal SparseArray repr # tm.assert_sp_series_equal(result, expected.to_sparse(fill_value=0)) tm.assert_series_equal(result.to_dense(), expected) def test_fillna_fill_value(self): df = pd.DataFrame({'A': [1, 0, 0], 'B': [np.nan, np.nan, 4]}) sparse = pd.SparseDataFrame(df) tm.assert_frame_equal(sparse.fillna(-1).to_dense(), df.fillna(-1), check_dtype=False) sparse = pd.SparseDataFrame(df, default_fill_value=0) tm.assert_frame_equal(sparse.fillna(-1).to_dense(), df.fillna(-1), check_dtype=False) def test_sparse_frame_pad_backfill_limit(self): index = np.arange(10) df = DataFrame(np.random.randn(10, 4), index=index) sdf = df.to_sparse() result = sdf[:2].reindex(index, method='pad', limit=5) expected = sdf[:2].reindex(index).fillna(method='pad') expected = expected.to_dense() expected.values[-3:] = np.nan expected = expected.to_sparse() tm.assert_frame_equal(result, expected) result = sdf[-2:].reindex(index, method='backfill', limit=5) expected = sdf[-2:].reindex(index).fillna(method='backfill') expected = expected.to_dense() expected.values[:3] = np.nan expected = expected.to_sparse() tm.assert_frame_equal(result, expected) def test_sparse_frame_fillna_limit(self): index = np.arange(10) df = DataFrame(np.random.randn(10, 4), index=index) sdf = df.to_sparse() result = sdf[:2].reindex(index) result = result.fillna(method='pad', limit=5) expected = sdf[:2].reindex(index).fillna(method='pad') expected = expected.to_dense() expected.values[-3:] = np.nan expected = expected.to_sparse() tm.assert_frame_equal(result, expected) result = sdf[-2:].reindex(index) result = result.fillna(method='backfill', limit=5) expected = sdf[-2:].reindex(index).fillna(method='backfill') expected = expected.to_dense() expected.values[:3] = np.nan expected = expected.to_sparse() tm.assert_frame_equal(result, expected) def test_rename(self): result = self.frame.rename(index=str) expected = SparseDataFrame(self.data, index=self.dates.strftime( "%Y-%m-%d %H:%M:%S")) tm.assert_sp_frame_equal(result, expected) result = self.frame.rename(columns=lambda x: '%s%d' % (x, len(x))) data = {'A1': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6], 'B1': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6], 'C1': np.arange(10, dtype=np.float64), 'D1': [0, 1, 2, 3, 4, 5, nan, nan, nan, nan]} expected = SparseDataFrame(data, index=self.dates) tm.assert_sp_frame_equal(result, expected) def test_corr(self): res = self.frame.corr() tm.assert_frame_equal(res, self.frame.to_dense().corr()) def test_describe(self): self.frame['foo'] = np.nan self.frame.get_dtype_counts() str(self.frame) desc = self.frame.describe() # noqa def test_join(self): left = self.frame.loc[:, ['A', 'B']] right = self.frame.loc[:, ['C', 'D']] joined = left.join(right) tm.assert_sp_frame_equal(joined, self.frame, exact_indices=False) right = self.frame.loc[:, ['B', 'D']] pytest.raises(Exception, left.join, right) with tm.assert_raises_regex(ValueError, 'Other Series must have a name'): self.frame.join(Series( np.random.randn(len(self.frame)), index=self.frame.index)) def test_reindex(self): def _check_frame(frame): index = frame.index sidx = index[::2] sidx2 = index[:5] # noqa sparse_result = frame.reindex(sidx) dense_result = frame.to_dense().reindex(sidx) tm.assert_frame_equal(sparse_result.to_dense(), dense_result) tm.assert_frame_equal(frame.reindex(list(sidx)).to_dense(), dense_result) sparse_result2 = sparse_result.reindex(index) dense_result2 = dense_result.reindex(index) tm.assert_frame_equal(sparse_result2.to_dense(), dense_result2) # propagate CORRECT fill value tm.assert_almost_equal(sparse_result.default_fill_value, frame.default_fill_value) tm.assert_almost_equal(sparse_result['A'].fill_value, frame['A'].fill_value) # length zero length_zero = frame.reindex([]) assert len(length_zero) == 0 assert len(length_zero.columns) == len(frame.columns) assert len(length_zero['A']) == 0 # frame being reindexed has length zero length_n = length_zero.reindex(index) assert len(length_n) == len(frame) assert len(length_n.columns) == len(frame.columns) assert len(length_n['A']) == len(frame) # reindex columns reindexed = frame.reindex(columns=['A', 'B', 'Z']) assert len(reindexed.columns) == 3 tm.assert_almost_equal(reindexed['Z'].fill_value, frame.default_fill_value) assert np.isnan(reindexed['Z'].sp_values).all() _check_frame(self.frame) _check_frame(self.iframe) _check_frame(self.zframe) _check_frame(self.fill_frame) # with copy=False reindexed = self.frame.reindex(self.frame.index, copy=False) reindexed['F'] = reindexed['A'] assert 'F' in self.frame reindexed = self.frame.reindex(self.frame.index) reindexed['G'] = reindexed['A'] assert 'G' not in self.frame def test_reindex_fill_value(self): rng = bdate_range('20110110', periods=20) result = self.zframe.reindex(rng, fill_value=0) exp = self.zorig.reindex(rng, fill_value=0) exp = exp.to_sparse(self.zframe.default_fill_value) tm.assert_sp_frame_equal(result, exp) def test_reindex_method(self): sparse = SparseDataFrame(data=[[11., 12., 14.], [21., 22., 24.], [41., 42., 44.]], index=[1, 2, 4], columns=[1, 2, 4], dtype=float) # Over indices # default method result = sparse.reindex(index=range(6)) expected = SparseDataFrame(data=[[nan, nan, nan], [11., 12., 14.], [21., 22., 24.], [nan, nan, nan], [41., 42., 44.], [nan, nan, nan]], index=range(6), columns=[1, 2, 4], dtype=float) tm.assert_sp_frame_equal(result, expected) # method='bfill' result = sparse.reindex(index=range(6), method='bfill') expected = SparseDataFrame(data=[[11., 12., 14.], [11., 12., 14.], [21., 22., 24.], [41., 42., 44.], [41., 42., 44.], [nan, nan, nan]], index=range(6), columns=[1, 2, 4], dtype=float) tm.assert_sp_frame_equal(result, expected) # method='ffill' result = sparse.reindex(index=range(6), method='ffill') expected = SparseDataFrame(data=[[nan, nan, nan], [11., 12., 14.], [21., 22., 24.], [21., 22., 24.], [41., 42., 44.], [41., 42., 44.]], index=range(6), columns=[1, 2, 4], dtype=float) tm.assert_sp_frame_equal(result, expected) # Over columns # default method result = sparse.reindex(columns=range(6)) expected = SparseDataFrame(data=[[nan, 11., 12., nan, 14., nan], [nan, 21., 22., nan, 24., nan], [nan, 41., 42., nan, 44., nan]], index=[1, 2, 4], columns=range(6), dtype=float) tm.assert_sp_frame_equal(result, expected) # method='bfill' with pytest.raises(NotImplementedError): sparse.reindex(columns=range(6), method='bfill') # method='ffill' with pytest.raises(NotImplementedError): sparse.reindex(columns=range(6), method='ffill') def test_take(self): result = self.frame.take([1, 0, 2], axis=1) expected = self.frame.reindex(columns=['B', 'A', 'C']) tm.assert_sp_frame_equal(result, expected) def test_to_dense(self): def _check(frame, orig): dense_dm = frame.to_dense() tm.assert_frame_equal(frame, dense_dm) tm.assert_frame_equal(dense_dm, orig, check_dtype=False) self._check_all(_check) def test_stack_sparse_frame(self): with catch_warnings(record=True): def _check(frame): dense_frame = frame.to_dense() # noqa wp = Panel.from_dict({'foo': frame}) from_dense_lp = wp.to_frame() from_sparse_lp = spf.stack_sparse_frame(frame) tm.assert_numpy_array_equal(from_dense_lp.values, from_sparse_lp.values) _check(self.frame) _check(self.iframe) # for now pytest.raises(Exception, _check, self.zframe) pytest.raises(Exception, _check, self.fill_frame) def test_transpose(self): def _check(frame, orig): transposed = frame.T untransposed = transposed.T tm.assert_sp_frame_equal(frame, untransposed) tm.assert_frame_equal(frame.T.to_dense(), orig.T) tm.assert_frame_equal(frame.T.T.to_dense(), orig.T.T) tm.assert_sp_frame_equal(frame, frame.T.T, exact_indices=False) self._check_all(_check) def test_shift(self): def _check(frame, orig): shifted = frame.shift(0) exp = orig.shift(0) tm.assert_frame_equal(shifted.to_dense(), exp) shifted = frame.shift(1) exp = orig.shift(1) tm.assert_frame_equal(shifted, exp) shifted = frame.shift(-2) exp = orig.shift(-2) tm.assert_frame_equal(shifted, exp) shifted = frame.shift(2, freq='B') exp = orig.shift(2, freq='B') exp = exp.to_sparse(frame.default_fill_value, kind=frame.default_kind) tm.assert_frame_equal(shifted, exp) shifted = frame.shift(2, freq=BDay()) exp = orig.shift(2, freq=BDay()) exp = exp.to_sparse(frame.default_fill_value, kind=frame.default_kind) tm.assert_frame_equal(shifted, exp) self._check_all(_check) def test_count(self): dense_result = self.frame.to_dense().count() result = self.frame.count() tm.assert_series_equal(result, dense_result) result = self.frame.count(axis=None) tm.assert_series_equal(result, dense_result) result = self.frame.count(axis=0) tm.assert_series_equal(result, dense_result) result = self.frame.count(axis=1) dense_result = self.frame.to_dense().count(axis=1) # win32 don't check dtype tm.assert_series_equal(result, dense_result, check_dtype=False) def _check_all(self, check_func): check_func(self.frame, self.orig) check_func(self.iframe, self.iorig) check_func(self.zframe, self.zorig) check_func(self.fill_frame, self.fill_orig) def test_numpy_transpose(self): sdf = SparseDataFrame([1, 2, 3], index=[1, 2, 3], columns=['a']) result = np.transpose(np.transpose(sdf)) tm.assert_sp_frame_equal(result, sdf) msg = "the 'axes' parameter is not supported" tm.assert_raises_regex(ValueError, msg, np.transpose, sdf, axes=1) def test_combine_first(self): df = self.frame result = df[::2].combine_first(df) result2 = df[::2].combine_first(df.to_dense()) expected = df[::2].to_dense().combine_first(df.to_dense()) expected = expected.to_sparse(fill_value=df.default_fill_value) tm.assert_sp_frame_equal(result, result2) tm.assert_sp_frame_equal(result, expected) def test_combine_add(self): df = self.frame.to_dense() df2 = df.copy() df2['C'][:3] = np.nan df['A'][:3] = 5.7 result = df.to_sparse().add(df2.to_sparse(), fill_value=0) expected = df.add(df2, fill_value=0).to_sparse() tm.assert_sp_frame_equal(result, expected) def test_isin(self): sparse_df = DataFrame({'flag': [1., 0., 1.]}).to_sparse(fill_value=0.) xp = sparse_df[sparse_df.flag == 1.] rs = sparse_df[sparse_df.flag.isin([1.])] tm.assert_frame_equal(xp, rs) def test_sparse_pow_issue(self): # 2220 df = SparseDataFrame({'A': [1.1, 3.3], 'B': [2.5, -3.9]}) # note : no error without nan df = SparseDataFrame({'A': [nan, 0, 1]}) # note that 2 ** df works fine, also df ** 1 result = 1 ** df r1 = result.take([0], 1)['A'] r2 = result['A'] assert len(r2.sp_values) == len(r1.sp_values) def test_as_blocks(self): df = SparseDataFrame({'A': [1.1, 3.3], 'B': [nan, -3.9]}, dtype='float64') # deprecated 0.21.0 with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): df_blocks = df.blocks assert list(df_blocks.keys()) == ['float64'] tm.assert_frame_equal(df_blocks['float64'], df) @pytest.mark.xfail(reason='nan column names in _init_dict problematic ' '(GH 16894)') def test_nan_columnname(self): # GH 8822 nan_colname = DataFrame(Series(1.0, index=[0]), columns=[nan]) nan_colname_sparse = nan_colname.to_sparse() assert np.isnan(nan_colname_sparse.columns[0]) def test_isna(self): # GH 8276 df = pd.SparseDataFrame({'A': [np.nan, np.nan, 1, 2, np.nan], 'B': [0, np.nan, np.nan, 2, np.nan]}) res = df.isna() exp = pd.SparseDataFrame({'A': [True, True, False, False, True], 'B': [False, True, True, False, True]}, default_fill_value=True) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp) # if fill_value is not nan, True can be included in sp_values df = pd.SparseDataFrame({'A': [0, 0, 1, 2, np.nan], 'B': [0, np.nan, 0, 2, np.nan]}, default_fill_value=0.) res = df.isna() assert isinstance(res, pd.SparseDataFrame) exp = pd.DataFrame({'A': [False, False, False, False, True], 'B': [False, True, False, False, True]}) tm.assert_frame_equal(res.to_dense(), exp) def test_notna(self): # GH 8276 df = pd.SparseDataFrame({'A': [np.nan, np.nan, 1, 2, np.nan], 'B': [0, np.nan, np.nan, 2, np.nan]}) res = df.notna() exp = pd.SparseDataFrame({'A': [False, False, True, True, False], 'B': [True, False, False, True, False]}, default_fill_value=False) exp._default_fill_value = np.nan tm.assert_sp_frame_equal(res, exp) # if fill_value is not nan, True can be included in sp_values df = pd.SparseDataFrame({'A': [0, 0, 1, 2, np.nan], 'B': [0, np.nan, 0, 2, np.nan]}, default_fill_value=0.) res = df.notna() assert isinstance(res, pd.SparseDataFrame) exp = pd.DataFrame({'A': [True, True, True, True, False], 'B': [True, False, True, True, False]}) tm.assert_frame_equal(res.to_dense(), exp) @pytest.mark.parametrize('index', [None, list('abc')]) # noqa: F811 @pytest.mark.parametrize('columns', [None, list('def')]) @pytest.mark.parametrize('fill_value', [None, 0, np.nan]) @pytest.mark.parametrize('dtype', [bool, int, float, np.uint16]) def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype): # GH 4343 tm.skip_if_no_package('scipy') # Make one ndarray and from it one sparse matrix, both to be used for # constructing frames and comparing results arr = np.eye(3, dtype=dtype) # GH 16179 arr[0, 1] = dtype(2) try: spm = spmatrix(arr) assert spm.dtype == arr.dtype except (TypeError, AssertionError): # If conversion to sparse fails for this spmatrix type and arr.dtype, # then the combination is not currently supported in NumPy, so we # can just skip testing it thoroughly return sdf = pd.SparseDataFrame(spm, index=index, columns=columns, default_fill_value=fill_value) # Expected result construction is kind of tricky for all # dtype-fill_value combinations; easiest to cast to something generic # and except later on rarr = arr.astype(object) rarr[arr == 0] = np.nan expected = pd.SparseDataFrame(rarr, index=index, columns=columns).fillna( fill_value if fill_value is not None else np.nan) # Assert frame is as expected sdf_obj = sdf.astype(object) tm.assert_sp_frame_equal(sdf_obj, expected) tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense()) # Assert spmatrices equal assert dict(sdf.to_coo().todok()) == dict(spm.todok()) # Ensure dtype is preserved if possible was_upcast = ((fill_value is None or is_float(fill_value)) and not is_object_dtype(dtype) and not is_float_dtype(dtype)) res_dtype = (bool if is_bool_dtype(dtype) else float if was_upcast else dtype) tm.assert_contains_all(sdf.dtypes, {np.dtype(res_dtype)}) assert sdf.to_coo().dtype == res_dtype # However, adding a str column results in an upcast to object sdf['strings'] = np.arange(len(sdf)).astype(str) assert sdf.to_coo().dtype == np.object_ @pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811 def test_from_to_scipy_object(spmatrix, fill_value): # GH 4343 dtype = object columns = list('cd') index = list('ab') tm.skip_if_no_package('scipy', max_version='0.19.0') # Make one ndarray and from it one sparse matrix, both to be used for # constructing frames and comparing results arr = np.eye(2, dtype=dtype) try: spm = spmatrix(arr) assert spm.dtype == arr.dtype except (TypeError, AssertionError): # If conversion to sparse fails for this spmatrix type and arr.dtype, # then the combination is not currently supported in NumPy, so we # can just skip testing it thoroughly return sdf = pd.SparseDataFrame(spm, index=index, columns=columns, default_fill_value=fill_value) # Expected result construction is kind of tricky for all # dtype-fill_value combinations; easiest to cast to something generic # and except later on rarr = arr.astype(object) rarr[arr == 0] = np.nan expected = pd.SparseDataFrame(rarr, index=index, columns=columns).fillna( fill_value if fill_value is not None else np.nan) # Assert frame is as expected sdf_obj = sdf.astype(object) tm.assert_sp_frame_equal(sdf_obj, expected) tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense()) # Assert spmatrices equal assert dict(sdf.to_coo().todok()) == dict(spm.todok()) # Ensure dtype is preserved if possible res_dtype = object tm.assert_contains_all(sdf.dtypes, {np.dtype(res_dtype)}) assert sdf.to_coo().dtype == res_dtype def test_from_scipy_correct_ordering(spmatrix): # GH 16179 tm.skip_if_no_package('scipy') arr = np.arange(1, 5).reshape(2, 2) try: spm = spmatrix(arr) assert spm.dtype == arr.dtype except (TypeError, AssertionError): # If conversion to sparse fails for this spmatrix type and arr.dtype, # then the combination is not currently supported in NumPy, so we # can just skip testing it thoroughly return sdf = pd.SparseDataFrame(spm) expected = pd.SparseDataFrame(arr) tm.assert_sp_frame_equal(sdf, expected) tm.assert_frame_equal(sdf.to_dense(), expected.to_dense()) def test_from_scipy_fillna(spmatrix): # GH 16112 tm.skip_if_no_package('scipy') arr = np.eye(3) arr[1:, 0] = np.nan try: spm = spmatrix(arr) assert spm.dtype == arr.dtype except (TypeError, AssertionError): # If conversion to sparse fails for this spmatrix type and arr.dtype, # then the combination is not currently supported in NumPy, so we # can just skip testing it thoroughly return sdf = pd.SparseDataFrame(spm).fillna(-1.0) # Returning frame should fill all nan values with -1.0 expected = pd.SparseDataFrame({ 0: pd.SparseSeries([1., -1, -1]), 1: pd.SparseSeries([np.nan, 1, np.nan]), 2: pd.SparseSeries([np.nan, np.nan, 1]), }, default_fill_value=-1) # fill_value is expected to be what .fillna() above was called with # We don't use -1 as initial fill_value in expected SparseSeries # construction because this way we obtain "compressed" SparseArrays, # avoiding having to construct them ourselves for col in expected: expected[col].fill_value = -1 tm.assert_sp_frame_equal(sdf, expected) class TestSparseDataFrameArithmetic(object): def test_numeric_op_scalar(self): df = pd.DataFrame({'A': [nan, nan, 0, 1, ], 'B': [0, 1, 2, nan], 'C': [1., 2., 3., 4.], 'D': [nan, nan, nan, nan]}) sparse = df.to_sparse() tm.assert_sp_frame_equal(sparse + 1, (df + 1).to_sparse()) def test_comparison_op_scalar(self): # GH 13001 df = pd.DataFrame({'A': [nan, nan, 0, 1, ], 'B': [0, 1, 2, nan], 'C': [1., 2., 3., 4.], 'D': [nan, nan, nan, nan]}) sparse = df.to_sparse() # comparison changes internal repr, compare with dense res = sparse > 1 assert isinstance(res, pd.SparseDataFrame) tm.assert_frame_equal(res.to_dense(), df > 1) res = sparse != 0 assert isinstance(res, pd.SparseDataFrame) tm.assert_frame_equal(res.to_dense(), df != 0) class TestSparseDataFrameAnalytics(object): def setup_method(self, method): self.data = {'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6], 'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6], 'C': np.arange(10, dtype=float), 'D': [0, 1, 2, 3, 4, 5, nan, nan, nan, nan]} self.dates = bdate_range('1/1/2011', periods=10) self.frame = SparseDataFrame(self.data, index=self.dates) def test_cumsum(self): expected = SparseDataFrame(self.frame.to_dense().cumsum()) result = self.frame.cumsum() tm.assert_sp_frame_equal(result, expected) result = self.frame.cumsum(axis=None) tm.assert_sp_frame_equal(result, expected) result = self.frame.cumsum(axis=0) tm.assert_sp_frame_equal(result, expected) def test_numpy_cumsum(self): result = np.cumsum(self.frame) expected = SparseDataFrame(self.frame.to_dense().cumsum()) tm.assert_sp_frame_equal(result, expected) msg = "the 'dtype' parameter is not supported" tm.assert_raises_regex(ValueError, msg, np.cumsum, self.frame, dtype=np.int64) msg = "the 'out' parameter is not supported" tm.assert_raises_regex(ValueError, msg, np.cumsum, self.frame, out=result) def test_numpy_func_call(self): # no exception should be raised even though # numpy passes in 'axis=None' or `axis=-1' funcs = ['sum', 'cumsum', 'var', 'mean', 'prod', 'cumprod', 'std', 'min', 'max'] for func in funcs: getattr(np, func)(self.frame)
apache-2.0
rhattersley/iris
lib/iris/tests/system_test.py
8
3910
# (C) British Crown Copyright 2010 - 2015, Met Office # # This file is part of Iris. # # Iris is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the # Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Iris is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with Iris. If not, see <http://www.gnu.org/licenses/>. """ This system test module is useful to identify if some of the key components required for Iris are available. The system tests can be run with ``python setup.py test --system-tests``. """ from __future__ import (absolute_import, division, print_function) from six.moves import (filter, input, map, range, zip) # noqa # import iris tests first so that some things can be initialised before importing anything else import cf_units import numpy as np import iris import iris.fileformats.netcdf as netcdf import iris.fileformats.pp as pp import iris.tests as tests if tests.GRIB_AVAILABLE: import gribapi import iris.fileformats.grib as grib class SystemInitialTest(tests.IrisTest): def system_test_supported_filetypes(self): nx, ny = 60, 60 data = np.arange(nx * ny, dtype='>f4').reshape(nx, ny) laty = np.linspace(0, 59, ny).astype('f8') lonx = np.linspace(30, 89, nx).astype('f8') horiz_cs = lambda : iris.coord_systems.GeogCS(6371229) cm = iris.cube.Cube(data, 'wind_speed', units='m s-1') cm.add_dim_coord( iris.coords.DimCoord(laty, 'latitude', units='degrees', coord_system=horiz_cs()), 0) cm.add_dim_coord( iris.coords.DimCoord(lonx, 'longitude', units='degrees', coord_system=horiz_cs()), 1) cm.add_aux_coord(iris.coords.AuxCoord(np.array([9], 'i8'), 'forecast_period', units='hours')) hours_since_epoch = cf_units.Unit('hours since epoch', cf_units.CALENDAR_GREGORIAN) cm.add_aux_coord(iris.coords.AuxCoord(np.array([3], 'i8'), 'time', units=hours_since_epoch)) cm.add_aux_coord(iris.coords.AuxCoord(np.array([99], 'i8'), long_name='pressure', units='Pa')) filetypes = ('.nc', '.pp') if tests.GRIB_AVAILABLE: filetypes += ('.grib2',) with iris.FUTURE.context(netcdf_no_unlimited=True, netcdf_promote=True, strict_grib_load=True): for filetype in filetypes: saved_tmpfile = iris.util.create_temp_filename(suffix=filetype) iris.save(cm, saved_tmpfile) new_cube = iris.load_cube(saved_tmpfile) self.assertCML(new_cube, ('system', 'supported_filetype_%s.cml' % filetype)) @tests.skip_grib def system_test_grib_patch(self): import gribapi gm = gribapi.grib_new_from_samples("GRIB2") result = gribapi.grib_get_double(gm, "missingValue") new_missing_value = 123456.0 gribapi.grib_set_double(gm, "missingValue", new_missing_value) new_result = gribapi.grib_get_double(gm, "missingValue") self.assertEqual(new_result, new_missing_value) def system_test_imports_general(self): if tests.MPL_AVAILABLE: import matplotlib import netCDF4 if __name__ == '__main__': tests.main()
lgpl-3.0
crichardson17/starburst_atlas
Low_resolution_sims/Dusty_LowRes/Geneva_cont_NoRot/Geneva_cont_NoRot_6/fullgrid/IR.py
1
9786
import csv import matplotlib.pyplot as plt from numpy import * import scipy.interpolate import math from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter import matplotlib.patches as patches from matplotlib.path import Path import os # ------------------------------------------------------------------------------------------------------ #input data files loaded in here print "Starting" numFiles = 3 gridfile = [None]*numFiles Elines = [None]*numFiles for i in range(3): for file in os.listdir('.'): if file.endswith("Geneva_cont_NoRot_6_{:d}.grd".format(i+1)): gridfile[i] = file print file if file.endswith("Geneva_cont_6_{:d}.txt".format(i+1)): Elines[i] = file print file # ------------------------------------------------------------------------------------------------------ #Patches data #for the Kewley and Levesque data verts = [ (1., 7.97712125471966000000), # left, bottom (1., 9.57712125471966000000), # left, top (2., 10.57712125471970000000), # right, top (2., 8.97712125471966000000), # right, bottom (0., 0.), # ignored ] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY, ] path = Path(verts, codes) # ------------------------ #for the Kewley 01 data verts2 = [ (2.4, 9.243038049), # left, bottom (2.4, 11.0211893), # left, top (2.6, 11.0211893), # right, top (2.6, 9.243038049), # right, bottom (0, 0.), # ignored ] path = Path(verts, codes) path2 = Path(verts2, codes) # ------------------------- #for the Moy et al data verts3 = [ (1., 6.86712125471966000000), # left, bottom (1., 10.18712125471970000000), # left, top (3., 12.18712125471970000000), # right, top (3., 8.86712125471966000000), # right, bottom (0., 0.), # ignored ] path = Path(verts, codes) path3 = Path(verts3, codes) # ------------------------------------------------------------------------------------------------------ #the routine to add patches for others peoples' data onto our plots. def add_patches(ax): patch3 = patches.PathPatch(path3, facecolor='yellow', lw=0) patch2 = patches.PathPatch(path2, facecolor='green', lw=0) patch = patches.PathPatch(path, facecolor='red', lw=0) ax1.add_patch(patch3) ax1.add_patch(patch2) ax1.add_patch(patch) # ------------------------------------------------------------------------------------------------------ #the subplot routine; will be called later numplots = 12 def add_sub_plot(sub_num): plt.subplot(3,4,sub_num) rbf = scipy.interpolate.Rbf(x, y, z[:,sub_num-1], function='linear') zi = rbf(xi, yi) contour = plt.contour(xi,yi,zi, levels, colors='c', linestyles = 'dashed') contour2 = plt.contour(xi,yi,zi, levels2, colors='k', linewidths=1.5) plt.scatter(max_values[line[sub_num-1],2], max_values[line[sub_num-1],3], c ='k',marker = '*') plt.annotate(headers[line[sub_num-1]], xy=(8,11), xytext=(4.5,8.5), fontsize = 10) plt.annotate(max_values[line[sub_num-1],0], xy= (max_values[line[sub_num-1],2], max_values[line[sub_num-1],3]), xytext = (0, -10), textcoords = 'offset points', ha = 'right', va = 'bottom', fontsize=10) if sub_num == numplots / 2.: print "half the plots are complete" #axis limits yt_min = 8 yt_max = 23 xt_min = 0 xt_max = 10 plt.ylim(yt_min,yt_max) plt.xlim(xt_min,xt_max) plt.yticks(arange(yt_min+1,yt_max,1),fontsize=10) plt.xticks(arange(xt_min+1,xt_max,1), fontsize = 10) if sub_num in [2,3,4,6,7,8,10,11,12]: plt.tick_params(labelleft = 'off') else: plt.tick_params(labelleft = 'on') plt.ylabel('Log ($ \phi _{\mathrm{H}} $)') if sub_num in [1,2,3,4,5,6,7,8]: plt.tick_params(labelbottom = 'off') else: plt.tick_params(labelbottom = 'on') plt.xlabel('Log($n _{\mathrm{H}} $)') if sub_num == 1: plt.yticks(arange(yt_min+1,yt_max+1,1),fontsize=10) if sub_num == 9: plt.yticks(arange(yt_min,yt_max,1),fontsize=10) plt.xticks(arange(xt_min,xt_max,1), fontsize = 10) if sub_num == 12: plt.xticks(arange(xt_min+1,xt_max+1,1), fontsize = 10) # --------------------------------------------------- #this is where the grid information (phi and hdens) is read in and saved to grid. grid1 = []; grid2 = []; grid3 = []; with open(gridfile[0], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid1.append(row); grid1 = asarray(grid1) with open(gridfile[1], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid2.append(row); grid2 = asarray(grid2) with open(gridfile[2], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') for row in csvReader: grid3.append(row); grid3 = asarray(grid3) #here is where the data for each line is read in and saved to dataEmissionlines dataEmissionlines1 = []; dataEmissionlines2 = []; dataEmissionlines3 = []; with open(Elines[0], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers = csvReader.next() for row in csvReader: dataEmissionlines1.append(row); dataEmissionlines1 = asarray(dataEmissionlines1) with open(Elines[1], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers2 = csvReader.next() for row in csvReader: dataEmissionlines2.append(row); dataEmissionlines2 = asarray(dataEmissionlines2) with open(Elines[2], 'rb') as f: csvReader = csv.reader(f,delimiter='\t') headers3 = csvReader.next() for row in csvReader: dataEmissionlines3.append(row); dataEmissionlines3 = asarray(dataEmissionlines3) print "import files complete" # --------------------------------------------------- #for concatenating grid #pull the phi and hdens values from each of the runs. exclude header lines grid1new = zeros((len(grid1[:,0])-1,2)) grid1new[:,0] = grid1[1:,6] grid1new[:,1] = grid1[1:,7] grid2new = zeros((len(grid2[:,0])-1,2)) x = array(17.00000) grid2new[:,0] = repeat(x,len(grid2[:,0])-1) grid2new[:,1] = grid2[1:,6] grid3new = zeros((len(grid3[:,0])-1,2)) grid3new[:,0] = grid3[1:,6] grid3new[:,1] = grid3[1:,7] grid = concatenate((grid1new,grid2new,grid3new)) hdens_values = grid[:,1] phi_values = grid[:,0] Emissionlines = concatenate((dataEmissionlines1[:,1:],dataEmissionlines2[:,1:],dataEmissionlines3[:,1:])) headers = headers[1:] # --------------------------------------------------- #To fix when hdens > 10 hdens_values_2 = empty(shape=[0, 1]) phi_values_2 = empty(shape=[0, 1]) Emissionlines_2 = empty(shape=[0, len(Emissionlines[0,:])]) for i in range(len(hdens_values)): if float(hdens_values[i]) < 10.100 : hdens_values_2 = append(hdens_values_2, hdens_values[i]) phi_values_2 = append(phi_values_2, phi_values[i]) Emissionlines_2 = vstack([Emissionlines_2, Emissionlines[i,:]]) #overwrite old arrays hdens_values = hdens_values_2 phi_values = phi_values_2 Emissionlines = Emissionlines_2 print "import files complete" # --------------------------------------------------- #for concatenating Emission lines data #for lines concatenated_data = zeros((len(Emissionlines),len(Emissionlines[0]))) max_values = zeros((len(concatenated_data[0]),4)) #constructing grid by scaling #select the scaling factor #for 1215 #incident = Emissionlines[1:,4] #for 4860 incident = concatenated_data[:,57] #take the ratio of incident and all the lines and put it all in an array concatenated_data for i in range(len(Emissionlines)): for j in range(len(Emissionlines[0])): if math.log(4860.*(float(Emissionlines[i,j])/float(Emissionlines[i,57])), 10) > 0: concatenated_data[i,j] = math.log(4860.*(float(Emissionlines[i,j])/float(Emissionlines[i,57])), 10) else: concatenated_data[i,j] == 0 # for 1215 #for i in range(len(Emissionlines)): # for j in range(len(Emissionlines[0])): # if math.log(1215.*(float(Emissionlines[i,j])/float(Emissionlines[i,4])), 10) > 0: # concatenated_data[i,j] = math.log(1215.*(float(Emissionlines[i,j])/float(Emissionlines[i,4])), 10) # else: # concatenated_data[i,j] == 0 # --------------------------------------------------- #find the maxima to plot onto the contour plots for j in range(len(concatenated_data[0])): max_values[j,0] = max(concatenated_data[:,j]) max_values[j,1] = argmax(concatenated_data[:,j], axis = 0) max_values[j,2] = hdens_values[max_values[j,1]] max_values[j,3] = phi_values[max_values[j,1]] #to round off the maxima max_values[:,0] = [ '%.1f' % elem for elem in max_values[:,0] ] print "data arranged" # --------------------------------------------------- #Creating the grid to interpolate with for contours. gridarray = zeros((len(Emissionlines),2)) gridarray[:,0] = hdens_values gridarray[:,1] = phi_values x = gridarray[:,0] y = gridarray[:,1] # --------------------------------------------------- #change desired lines here! line = [75, #AR 3 7135 76, #TOTL 7325 78, #AR 3 7751 79, #6LEV 8446 80, #CA2X 8498 81, #CA2Y 8542 82, #CA2Z 8662 83, #CA 2 8579A 84, #S 3 9069 85, #H 1 9229 86, #S 3 9532 87] #H 1 9546 #create z array for this plot with given lines z = concatenated_data[:,line[:]] # --------------------------------------------------- # Interpolate print "starting interpolation" xi, yi = linspace(x.min(), x.max(), x.max()-x.min()+1), linspace(y.min(), y.max(),x.max()-x.min()+1) xi, yi = meshgrid(xi, yi) # --------------------------------------------------- print "interpolatation complete; now plotting" #plot plt.subplots_adjust(wspace=0, hspace=0) #remove space between plots levels = arange(10**-1,10, .2) levels2 = arange(10**-2,10**2, 1) plt.suptitle("Dusty IR Lines", fontsize=14) # --------------------------------------------------- for i in range(12): add_sub_plot(i) ax1 = plt.subplot(3,4,1) add_patches(ax1) print "figure complete" plt.savefig('Dusty_Near_IR.pdf') plt.clf() print "figure saved"
gpl-2.0
lampts/sklearn_pycon2015
notebooks/fig_code/ML_flow_chart.py
61
4970
""" Tutorial Diagrams ----------------- This script plots the flow-charts used in the scikit-learn tutorials. """ import numpy as np import pylab as pl from matplotlib.patches import Circle, Rectangle, Polygon, Arrow, FancyArrow def create_base(box_bg = '#CCCCCC', arrow1 = '#88CCFF', arrow2 = '#88FF88', supervised=True): fig = pl.figure(figsize=(9, 6), facecolor='w') ax = pl.axes((0, 0, 1, 1), xticks=[], yticks=[], frameon=False) ax.set_xlim(0, 9) ax.set_ylim(0, 6) patches = [Rectangle((0.3, 3.6), 1.5, 1.8, zorder=1, fc=box_bg), Rectangle((0.5, 3.8), 1.5, 1.8, zorder=2, fc=box_bg), Rectangle((0.7, 4.0), 1.5, 1.8, zorder=3, fc=box_bg), Rectangle((2.9, 3.6), 0.2, 1.8, fc=box_bg), Rectangle((3.1, 3.8), 0.2, 1.8, fc=box_bg), Rectangle((3.3, 4.0), 0.2, 1.8, fc=box_bg), Rectangle((0.3, 0.2), 1.5, 1.8, fc=box_bg), Rectangle((2.9, 0.2), 0.2, 1.8, fc=box_bg), Circle((5.5, 3.5), 1.0, fc=box_bg), Polygon([[5.5, 1.7], [6.1, 1.1], [5.5, 0.5], [4.9, 1.1]], fc=box_bg), FancyArrow(2.3, 4.6, 0.35, 0, fc=arrow1, width=0.25, head_width=0.5, head_length=0.2), FancyArrow(3.75, 4.2, 0.5, -0.2, fc=arrow1, width=0.25, head_width=0.5, head_length=0.2), FancyArrow(5.5, 2.4, 0, -0.4, fc=arrow1, width=0.25, head_width=0.5, head_length=0.2), FancyArrow(2.0, 1.1, 0.5, 0, fc=arrow2, width=0.25, head_width=0.5, head_length=0.2), FancyArrow(3.3, 1.1, 1.3, 0, fc=arrow2, width=0.25, head_width=0.5, head_length=0.2), FancyArrow(6.2, 1.1, 0.8, 0, fc=arrow2, width=0.25, head_width=0.5, head_length=0.2)] if supervised: patches += [Rectangle((0.3, 2.4), 1.5, 0.5, zorder=1, fc=box_bg), Rectangle((0.5, 2.6), 1.5, 0.5, zorder=2, fc=box_bg), Rectangle((0.7, 2.8), 1.5, 0.5, zorder=3, fc=box_bg), FancyArrow(2.3, 2.9, 2.0, 0, fc=arrow1, width=0.25, head_width=0.5, head_length=0.2), Rectangle((7.3, 0.85), 1.5, 0.5, fc=box_bg)] else: patches += [Rectangle((7.3, 0.2), 1.5, 1.8, fc=box_bg)] for p in patches: ax.add_patch(p) pl.text(1.45, 4.9, "Training\nText,\nDocuments,\nImages,\netc.", ha='center', va='center', fontsize=14) pl.text(3.6, 4.9, "Feature\nVectors", ha='left', va='center', fontsize=14) pl.text(5.5, 3.5, "Machine\nLearning\nAlgorithm", ha='center', va='center', fontsize=14) pl.text(1.05, 1.1, "New Text,\nDocument,\nImage,\netc.", ha='center', va='center', fontsize=14) pl.text(3.3, 1.7, "Feature\nVector", ha='left', va='center', fontsize=14) pl.text(5.5, 1.1, "Predictive\nModel", ha='center', va='center', fontsize=12) if supervised: pl.text(1.45, 3.05, "Labels", ha='center', va='center', fontsize=14) pl.text(8.05, 1.1, "Expected\nLabel", ha='center', va='center', fontsize=14) pl.text(8.8, 5.8, "Supervised Learning Model", ha='right', va='top', fontsize=18) else: pl.text(8.05, 1.1, "Likelihood\nor Cluster ID\nor Better\nRepresentation", ha='center', va='center', fontsize=12) pl.text(8.8, 5.8, "Unsupervised Learning Model", ha='right', va='top', fontsize=18) def plot_supervised_chart(annotate=False): create_base(supervised=True) if annotate: fontdict = dict(color='r', weight='bold', size=14) pl.text(1.9, 4.55, 'X = vec.fit_transform(input)', fontdict=fontdict, rotation=20, ha='left', va='bottom') pl.text(3.7, 3.2, 'clf.fit(X, y)', fontdict=fontdict, rotation=20, ha='left', va='bottom') pl.text(1.7, 1.5, 'X_new = vec.transform(input)', fontdict=fontdict, rotation=20, ha='left', va='bottom') pl.text(6.1, 1.5, 'y_new = clf.predict(X_new)', fontdict=fontdict, rotation=20, ha='left', va='bottom') def plot_unsupervised_chart(): create_base(supervised=False) if __name__ == '__main__': plot_supervised_chart(False) plot_supervised_chart(True) plot_unsupervised_chart() pl.show()
bsd-3-clause
jaidevd/scikit-learn
examples/plot_multioutput_face_completion.py
330
3019
""" ============================================== Face completion with a multi-output estimators ============================================== This example shows the use of multi-output estimator to complete images. The goal is to predict the lower half of a face given its upper half. The first column of images shows true faces. The next columns illustrate how extremely randomized trees, k nearest neighbors, linear regression and ridge regression complete the lower half of those faces. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import fetch_olivetti_faces from sklearn.utils.validation import check_random_state from sklearn.ensemble import ExtraTreesRegressor from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_model import LinearRegression from sklearn.linear_model import RidgeCV # Load the faces datasets data = fetch_olivetti_faces() targets = data.target data = data.images.reshape((len(data.images), -1)) train = data[targets < 30] test = data[targets >= 30] # Test on independent people # Test on a subset of people n_faces = 5 rng = check_random_state(4) face_ids = rng.randint(test.shape[0], size=(n_faces, )) test = test[face_ids, :] n_pixels = data.shape[1] X_train = train[:, :np.ceil(0.5 * n_pixels)] # Upper half of the faces y_train = train[:, np.floor(0.5 * n_pixels):] # Lower half of the faces X_test = test[:, :np.ceil(0.5 * n_pixels)] y_test = test[:, np.floor(0.5 * n_pixels):] # Fit estimators ESTIMATORS = { "Extra trees": ExtraTreesRegressor(n_estimators=10, max_features=32, random_state=0), "K-nn": KNeighborsRegressor(), "Linear regression": LinearRegression(), "Ridge": RidgeCV(), } y_test_predict = dict() for name, estimator in ESTIMATORS.items(): estimator.fit(X_train, y_train) y_test_predict[name] = estimator.predict(X_test) # Plot the completed faces image_shape = (64, 64) n_cols = 1 + len(ESTIMATORS) plt.figure(figsize=(2. * n_cols, 2.26 * n_faces)) plt.suptitle("Face completion with multi-output estimators", size=16) for i in range(n_faces): true_face = np.hstack((X_test[i], y_test[i])) if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 1) else: sub = plt.subplot(n_faces, n_cols, i * n_cols + 1, title="true faces") sub.axis("off") sub.imshow(true_face.reshape(image_shape), cmap=plt.cm.gray, interpolation="nearest") for j, est in enumerate(sorted(ESTIMATORS)): completed_face = np.hstack((X_test[i], y_test_predict[est][i])) if i: sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j) else: sub = plt.subplot(n_faces, n_cols, i * n_cols + 2 + j, title=est) sub.axis("off") sub.imshow(completed_face.reshape(image_shape), cmap=plt.cm.gray, interpolation="nearest") plt.show()
bsd-3-clause
mdbartos/RIPS
temporary/network_messy.py
1
11230
import numpy as np import pandas as pd import networkx as nx import geopandas as gpd import shapely # class network_graph(): # def __init__(self, lines, subs, loads, transfers='infer'): # t = gpd.read_file(lines) # s = gpd.read_file(subs) # L = gpd.read_file(loads) #### SPECIFY SHAPEFILES translines = '/home/akagi/Desktop/electricity_data/Transmission_Lines.shp' t = gpd.read_file(translines) substations = '/home/akagi/Desktop/electricity_data/Substations.shp' s = gpd.read_file(substations) loads_shp = '/home/akagi/voronoi_stats.shp' L = gpd.read_file(loads_shp) g = '/home/akagi/Desktop/electricity_data/Generation.shp' g = gpd.read_file(g) #### LINE LENGTHS linelength = t.set_index('UNIQUE_ID').to_crs(epsg=2762).length linelength.name = 'length_m' #### EDGES edges = pd.read_csv('edges.csv', index_col=0) # This can probably be done when edges.csv is generated VVV edges = pd.concat([edges.set_index('TRANS_ID'), linelength], axis=1).reset_index() edges['TOT_CAP_KV'][edges['TOT_CAP_KV'] <= 0] = 69 #### GENERATION gen = pd.read_csv('gen_to_sub_static.csv', index_col=0) #### NET DEMAND #net = pd.concat([L.groupby('SUB_ID').sum()['summer_loa'], gen.groupby('SUB_ID').sum()['S_CAP_MW'].fillna(0)], axis=1, join='outer')[['summer_loa', 'S_CAP_MW']].fillna(0) #net = net['S_CAP_MW'] - net['summer_loa'] #### SEPARATE SUB AND GEN; NEED FOR CURRENT subloads = L.groupby('SUB_ID').sum()['summer_loa'].reindex(s['UNIQUE_ID'].values).fillna(0) subgen = gen.groupby('SUB_ID').sum()['S_CAP_MW'].reindex(s['UNIQUE_ID'].values).fillna(0) net = subgen - subloads #### PHOENIX phx_bbox = np.array([-112.690, 32.670, -111.192, 34.138]).reshape(2,2) phx_poly = shapely.geometry.MultiPoint(np.vstack(np.dstack(np.meshgrid(*np.hsplit(phx_bbox, 2)))).tolist()).convex_hull phx_lines = t[t.intersects(phx_poly)]['UNIQUE_ID'].astype(int).values phx_edges = edges[edges['TRANS_ID'].isin(phx_lines)] phx_edges = phx_edges[phx_edges['SUB_1'] != phx_edges['SUB_2']] phx_nodes = np.unique(phx_edges[['SUB_1', 'SUB_2']].values.astype(int).ravel()) # OUTER LINES edgesubs = pd.merge(t[t.intersects(phx_poly.boundary)], edges, left_on='UNIQUE_ID', right_on='TRANS_ID')[['SUB_1_y', 'SUB_2_y']].values.ravel().astype(int) # NODES JUST OUTSIDE OF BBOX (ENTERING) outer_nodes = np.unique(edgesubs[~np.in1d(edgesubs, s[s.within(phx_poly)]['UNIQUE_ID'].values.astype(int))]) weights = s.loc[s['UNIQUE_ID'].astype(int).isin(edgesubs[~np.in1d(edgesubs, s[s.within(phx_poly)]['UNIQUE_ID'].values.astype(int))])].set_index('UNIQUE_ID')['MAX_VOLT'].sort_index() transfers = net[phx_nodes].sum()*(weights/weights.sum()).reindex(s['UNIQUE_ID'].values).fillna(0) phx_loads = net[phx_nodes] + transfers.reindex(phx_nodes).fillna(0) #### G = nx.Graph() for i in phx_nodes: G.add_node(i, load = subloads[i], gen = subgen[i], trans = transfers[i], MW_net = subgen[i] - subloads[i] - transfers[i], I_load = 1000*subloads[i]/69.0, I_gen = 1000*subgen[i]/69.0, I_trans = 1000*transfers[i]/69.0, I_net = 1000*subgen[i]/69.0 - 1000*subloads[i]/69.0 - 1000*transfers[i]/69.0 ) for i in phx_edges.index: row = phx_edges.loc[i] G.add_edge(*tuple(row[['SUB_1', 'SUB_2']].astype(int).values), tot_kv=row['TOT_CAP_KV'], num_lines=int(row['NUM_LINES']), length=row['length_m']) import cable cable_classes = { 525 : {0 : ['Chukar', 'acsr'], 1 : ['Bluebird', 'acsr']}, 345 : {0 : ['Tern', 'acsr']}, 230 : {0 : ['Bittern', 'acsr'], 1 : ['Bluebird', 'acss'], 2 : ['Tern', 'acsr']}, 115 : {0 : ['Bittern', 'acsr'], 1 : ['Bluebird', 'acss'], 2 : ['Tern', 'acsr']}, 69 : {0 : ['Tern', 'acss'], 1 : ['Arbutus', 'aac'], 2 : ['Linnet', 'acsr']} } instance_cables = { 525 : cable.cable(*cable_classes[525][0]), 345 : cable.cable(*cable_classes[345][0]), 230 : cable.cable(*cable_classes[230][0]), 115 : cable.cable(*cable_classes[115][0]), 69 : cable.cable(*cable_classes[69][0])} v_list = np.array([69, 115, 230, 345, 525]) for i in G.edges(): props = G[i[0]][i[1]] kv = props['tot_kv']/float(props['num_lines']) v_class = v_list[np.argmin(abs(v_list - 500.0))] cname = cable_classes[v_class][0][0] model = cable_classes[v_class][0][1] R = instance_cables[v_class].R.mean() G.edge[i[0]][i[1]]['R_unit'] = R G.edge[i[0]][i[1]]['R'] = R*props['length'] G.edge[i[0]][i[1]]['amp_75'] = instance_cables[v_class].models[model][cname]['amp_75'] G.edge[i[0]][i[1]]['tot_amp_75'] = G[i[0]][i[1]]['amp_75']*int(G[i[0]][i[1]]['num_lines']) G.edge[i[0]][i[1]]['tot_MW_cap'] = (G[i[0]][i[1]]['tot_amp_75']/1000.0)*G[i[0]][i[1]]['tot_kv'] #A = np.array([G.node[i]['I_net'] if 'I_net' in G.node[i].keys() else 0 for i in G.nodes()]) #### LINALG SOLVER cycles = nx.cycle_basis(G) cycles = np.array([[tuple([cycles[j][i], cycles[j][i+1]]) if (i < len(cycles[j])-1) else tuple([cycles[j][i], cycles[j][0]]) for i in range(len(cycles[j]))] for j in range(len(cycles))]) #L = [G.node[i]['demand'] for i in G.node.keys()] ### NEED A NEW NAME FOR edges edges = np.array(G.edges()) # Problem because nodes are no longer integer indexed VVV ### QUICKFIX node_idx = pd.Series(range(len(G.nodes())), index=G.nodes()) # edge_idx = np.full((len(G), len(G)), 9999, dtype=int) edge_idx[node_idx[edges[:,0]].values, node_idx[edges[:,1]].values] = np.arange(len(G.edges())) edge_idx[node_idx[edges[:,1]].values, node_idx[edges[:,0]].values] = np.arange(len(G.edges())) edge_dir = np.zeros((len(G), len(G)), dtype=int) edge_dir[node_idx[edges[:,0]].values, node_idx[edges[:,1]].values] = 1 edge_dir[node_idx[edges[:,1]].values, node_idx[edges[:,0]].values] = -1 X = nx.incidence_matrix(G, oriented=True).toarray() S = np.array([G.node[i]['I_net'] if 'I_net' in G.node[i].keys() else 0 for i in G.nodes()]) for u in cycles: R = np.array([G[j[0]][j[1]]['R'] for j in u]) # V = np.array([G[j[0]][j[1]]['tot_kv'] for j in u]) u = np.vstack(u) u = np.column_stack([node_idx[u[:,0]], node_idx[u[:,1]]]) D = np.array(edge_dir[u[:,0], u[:,1]]) z = np.zeros(len(edges)) z[edge_idx[u[:,0], u[:,1]]] = R*D X = np.vstack([X, z]) S = np.append(S, 0) sol = scipy.linalg.lstsq(X, S) #### NETWORK SIMPLEX SOLVER CG = G.subgraph(list(nx.connected_components(G))[0]) C_gen = subgen.loc[CG.nodes()].round().astype(int) C_loads = subloads.loc[CG.nodes()].round().astype(int) weights = s.loc[s['UNIQUE_ID'].astype(int).isin(edgesubs[~np.in1d(edgesubs, s[s.within(phx_poly)]['UNIQUE_ID'].values.astype(int))])].set_index('UNIQUE_ID').loc[CG.nodes()]['MAX_VOLT'].dropna().sort_index() C_transfers = ((C_gen.sum() - C_loads.sum())*(weights/weights.sum()).loc[CG.nodes()].fillna(0)).astype(int) if C_transfers.sum() != (C_gen.sum() - C_loads.sum()): dif = (C_transfers.sum() - (C_gen.sum() - C_loads.sum())) direct = 1 if dif > 0 else -1 nz_idx = pd.Series(np.nonzero(C_transfers.values)[0]) for i in range(abs(dif)): C_transfers.iloc[nz_idx[i]] -= direct for i in CG.nodes(): CG.node[i]['load'] = C_loads[i] CG.node[i]['gen'] = C_gen[i] CG.node[i]['trans'] = C_transfers[i] for i in CG.nodes(): CG.node[i]['MW_net'] = CG.node[i]['gen'] - CG.node[i]['load'] - CG.node[i]['trans'] DG = CG.to_directed() NS = nx.network_simplex(DG, demand='MW_net', weight='R', capacity='tot_MW_cap') #### EVERYTHING BELOW DOESN'T WORK!!! for i in G.nodes(): kv_list = [G.adj[i][j]['tot_kv'] for j in G.adj[i].keys() if isinstance(j, int)] kv_max, kv_min = max(kv_list), min(kv_list) G[i]['max_volt'] = kv_max G[i]['min_volt'] = kv_min #### GET GRID VOLTAGES FROM EIA FORM DATA mwkv = pd.DataFrame(np.zeros(len(G.nodes())), index=G.nodes()) for x in ['load', 'gen', 'trans', 'min_volt', 'max_volt']: mwkv_col = pd.DataFrame(np.vstack([tuple([i, G[i][x]]) for i in G.nodes() if x in G[i].keys()])).rename(columns={1 : x}).set_index(0) mwkv = pd.concat([mwkv, mwkv_col], axis=1) mwkv.replace(-99, 69, inplace=True) mwkv['max_volt'][mwkv['max_volt'] < 69] = 69 mwkv['min_volt'][mwkv['min_volt'] < 69] = 69 mwkv[['min_volt', 'max_volt']].fillna(69, inplace=True) mwkv['I_load'] = (1000*mwkv['load']/mwkv['min_volt']) mwkv['I_gen'] = (1000*mwkv['gen']/mwkv['max_volt']) mwkv['I_trans'] = (1000*mwkv['trans']/mwkv['max_volt']) # plant_860 = pd.read_excel('/home/akagi/Documents/EIA_form_data/eia8602012/PlantY2012.xlsx', header=1) # gen_860 = pd.read_excel('/home/akagi/Documents/EIA_form_data/eia8602012/GeneratorY2012.xlsx', sheetname='Operable', header=1) # plant_cap = pd.merge(plant_860, gen_860, on='Plant Code').groupby('Plant Code').sum()[['Summer Capacity (MW)', 'Winter Capacity (MW)', 'Nameplate Capacity (MW)']] # plant_chars = plant_860.set_index('Plant Code')[['Plant Name', 'Utility ID', 'NERC Region', 'Grid Voltage (kV)', 'Latitude', 'Longitude']] # g_dyn = pd.concat([plant_cap, plant_chars], axis=1).dropna(subset=['Longitude', 'Latitude', 'Grid Voltage (kV)']) # tree = spatial.cKDTree(np.vstack(g.geometry.apply(lambda x: np.concatenate(x.xy)).values)) # tree_query = tree.query(g_dyn[['Longitude', 'Latitude']].values) # gridvolts = g.iloc[tree_query[1]]['UNIQUE_ID'] # gridvolts = pd.DataFrame(np.column_stack([gridvolts.reset_index().values, g_dyn['Grid Voltage (kV)'].values.astype(float)])).drop_duplicates(1).set_index(1)[2] # gridvolts.index = gridvolts.index.values.astype(int) # gen = pd.concat([gridvolts, gen.set_index('GEN_ID')], axis=1).reset_index().rename(columns={2:'Grid_KV', 'index':'GEN_ID'}) #### #s.loc[s['UNIQUE_ID'].astype(int).isin(outer_nodes)].plot() plot(phx_poly.exterior.xy[0], phx_poly.exterior.xy[1]) for i in nx.connected_components(G): geom = s.set_index('UNIQUE_ID').loc[i].dropna() geom['geometry'].plot() #### DEBUGGING CONNECTED COMPONENTS cc = list(nx.connected_components(G)) ss = edges[['SUB_1', 'SUB_2']].astype(int) # Each disconnected component in cc has one or more subs with a missing point of entry into system # This is caused by orphaned subgraphs being created when the bbox is drawn. # Probably can't be fixed -- will simply have to remove disconnected components #### LINALG SOLVER cycles = nx.cycle_basis(G) cycles = np.array([[tuple([cycles[j][i], cycles[j][i+1]]) if (i < len(cycles[j])-1) else tuple([cycles[j][i], cycles[j][0]]) for i in range(len(cycles[j]))] for j in range(len(cycles))]) L = [G.node[i]['demand'] for i in G.node.keys()] edges = np.array(G.edges()) edge_idx = np.full((len(G), len(G)), 9999, dtype=int) edge_idx[edges[:,0], edges[:,1]] = np.arange(len(G.edges())) edge_idx[edges[:,1], edges[:,0]] = np.arange(len(G.edges())) edge_dir = np.zeros((len(G), len(G)), dtype=int) edge_dir[edges[:,0], edges[:,1]] = 1 edge_dir[edges[:,1], edges[:,0]] = -1 X = nx.incidence_matrix(G, oriented=True).toarray() S = np.array(loads) for u in cycles: # R = np.array([G[j[0]][j[1]]['resistance'] for j in u]) V = np.array([G[j[0]][j[1]]['tot_kv'] for j in u]) D = np.array(edge_dir[u[:,0], u[:,1]]) z = np.zeros(len(edges)) z[edge_idx[u[:,0], u[:,1]]] = R*D X = np.vstack([X, z]) S = np.append(S, 0) scipy.linalg.lstsq(X, S)
mit
mne-tools/mne-tools.github.io
0.12/_downloads/plot_raw_objects.py
15
5335
""" .. _tut_raw_objects The :class:`Raw <mne.io.RawFIF>` data structure: continuous data ================================================================ """ from __future__ import print_function import mne import os.path as op from matplotlib import pyplot as plt ############################################################################### # Continuous data is stored in objects of type :class:`Raw <mne.io.RawFIF>`. # The core data structure is simply a 2D numpy array (channels × samples, # `._data`) combined with an :class:`Info <mne.io.meas_info.Info>` object # (`.info`) (:ref:`tut_info_objects`. # # The most common way to load continuous data is from a .fif file. For more # information on :ref:`loading data from other formats <ch_raw>`, or creating # it :ref:`from scratch <tut_creating_data_structures>`. ############################################################################### # Loading continuous data # ----------------------- # Load an example dataset, the preload flag loads the data into memory now data_path = op.join(mne.datasets.sample.data_path(), 'MEG', 'sample', 'sample_audvis_raw.fif') raw = mne.io.RawFIF(data_path, preload=True, verbose=False) # Give the sample rate print('sample rate:', raw.info['sfreq'], 'Hz') # Give the size of the data matrix print('channels x samples:', raw._data.shape) ############################################################################### # Information about the channels contained in the :class:`Raw <mne.io.RawFIF>` # object is contained in the :class:`Info <mne.io.meas_info.Info>` attribute. # This is essentially a dictionary with a number of relevant fields (see # :ref:`tut_info_objects`). ############################################################################### # Indexing data # ------------- # # There are two ways to access the data stored within :class:`Raw # <mne.io.RawFIF>` objects. One is by accessing the underlying data array, and # the other is to index the :class:`Raw <mne.io.RawFIF>` object directly. # # To access the data array of :class:`Raw <mne.io.Raw>` objects, use the # `_data` attribute. Note that this is only present if `preload==True`. print('Shape of data array:', raw._data.shape) array_data = raw._data[0, :1000] _ = plt.plot(array_data) ############################################################################### # You can also pass an index directly to the :class:`Raw <mne.io.RawFIF>` # object. This will return an array of times, as well as the data representing # those timepoints. This may be used even if the data is not preloaded: # Extract data from the first 5 channels, from 1 s to 3 s. sfreq = raw.info['sfreq'] data, times = raw[:5, int(sfreq * 1):int(sfreq * 3)] _ = plt.plot(times, data.T) _ = plt.title('Sample channels') ############################################################################### # ----------------------------------------- # Selecting subsets of channels and samples # ----------------------------------------- # # It is possible to use more intelligent indexing to extract data, using # channel names, types or time ranges. # Pull all MEG gradiometer channels: # Make sure to use copy==True or it will overwrite the data meg_only = raw.pick_types(meg=True, copy=True) eeg_only = raw.pick_types(meg=False, eeg=True, copy=True) # The MEG flag in particular lets you specify a string for more specificity grad_only = raw.pick_types(meg='grad', copy=True) # Or you can use custom channel names pick_chans = ['MEG 0112', 'MEG 0111', 'MEG 0122', 'MEG 0123'] specific_chans = raw.pick_channels(pick_chans, copy=True) print(meg_only, eeg_only, grad_only, specific_chans, sep='\n') ############################################################################### # Notice the different scalings of these types f, (a1, a2) = plt.subplots(2, 1) eeg, times = eeg_only[0, :int(sfreq * 2)] meg, times = meg_only[0, :int(sfreq * 2)] a1.plot(times, meg[0]) a2.plot(times, eeg[0]) ############################################################################### # You can restrict the data to a specific time range restricted = raw.crop(5, 7) # in seconds print('New time range from', restricted.times.min(), 's to', restricted.times.max(), 's') ############################################################################### # And drop channels by name restricted = restricted.drop_channels(['MEG 0241', 'EEG 001']) print('Number of channels reduced from', raw.info['nchan'], 'to', restricted.info['nchan']) ############################################################################### # -------------------------------------------------- # Concatenating :class:`Raw <mne.io.RawFIF>` objects # -------------------------------------------------- # # :class:`Raw <mne.io.RawFIF>` objects can be concatenated in time by using the # :func:`append <mne.io.RawFIF.append>` function. For this to work, they must # have the same number of channels and their :class:`Info # <mne.io.meas_info.Info>` structures should be compatible. # Create multiple :class:`Raw <mne.io.RawFIF>` objects raw1 = raw.copy().crop(0, 10) raw2 = raw.copy().crop(10, 20) raw3 = raw.copy().crop(20, 100) # Concatenate in time (also works without preloading) raw1.append([raw2, raw3]) print('Time extends from', raw1.times.min(), 's to', raw1.times.max(), 's')
bsd-3-clause
cacraig/wxDataGetters
wxdatagetters/objects/gribMap.py
1
1673
import matplotlib matplotlib.use('Agg') from mpl_toolkits.basemap import Basemap ''''' This Class defines a GribMap which has properties: basemap: A matplotlib basemap borderX: Border (padding) Width. borderY: Border (padding) Height. ''''' class GribMap: ''''' This class represents an instance of a GribMap. @return void ''''' def __init__(self, **kwargs): # Default Attributes. self.llcrnrlat = None self.urcrnrlat = None self.llcrnrlon = None self.urcrnrlon = None self.resolution = 'l' self.projection = None self.lat_ts = None self.lat_0 = None self.lon_0 = None self.fix_aspect = None self.borderX = 0. self.borderY = 0. self.hasDoubleYBorder = False # Dynamically set all object attributes from unpacked kwargs. for key, value in kwargs.items(): setattr(self, key, value) self.basemap = Basemap(llcrnrlat=self.llcrnrlat, urcrnrlat=self.urcrnrlat,\ llcrnrlon=self.llcrnrlon,urcrnrlon=self.urcrnrlon, \ resolution=self.resolution,projection=self.projection,\ lat_ts=self.lat_ts,lat_0=self.lat_0,lon_0=self.lon_0, fix_aspect=self.fix_aspect) return def getBorderX(self): return self.borderX def getBorderY(self): return self.borderY def getBaseMap(self): return self.basemap ''''' def convertLon180to360(lon) Converts a [-180,180] longitude into a [0,360] longitude. @param int lon @return int ''''' def convertLon180to360(self, lon): newLon = 0 if int(lon) < 0: newLon = 360 + int(lon) else: newLon = int(lon) return newLon
mit
olologin/scikit-learn
sklearn/neighbors/tests/test_nearest_centroid.py
305
4121
""" Testing for the nearest centroid module. """ import numpy as np from scipy import sparse as sp from numpy.testing import assert_array_equal from numpy.testing import assert_equal from sklearn.neighbors import NearestCentroid from sklearn import datasets from sklearn.metrics.pairwise import pairwise_distances # toy sample X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]] X_csr = sp.csr_matrix(X) # Sparse matrix y = [-1, -1, -1, 1, 1, 1] T = [[-1, -1], [2, 2], [3, 2]] T_csr = sp.csr_matrix(T) true_result = [-1, 1, 1] # also load the iris dataset # and randomly permute it iris = datasets.load_iris() rng = np.random.RandomState(1) perm = rng.permutation(iris.target.size) iris.data = iris.data[perm] iris.target = iris.target[perm] def test_classification_toy(): # Check classification on a toy dataset, including sparse versions. clf = NearestCentroid() clf.fit(X, y) assert_array_equal(clf.predict(T), true_result) # Same test, but with a sparse matrix to fit and test. clf = NearestCentroid() clf.fit(X_csr, y) assert_array_equal(clf.predict(T_csr), true_result) # Fit with sparse, test with non-sparse clf = NearestCentroid() clf.fit(X_csr, y) assert_array_equal(clf.predict(T), true_result) # Fit with non-sparse, test with sparse clf = NearestCentroid() clf.fit(X, y) assert_array_equal(clf.predict(T_csr), true_result) # Fit and predict with non-CSR sparse matrices clf = NearestCentroid() clf.fit(X_csr.tocoo(), y) assert_array_equal(clf.predict(T_csr.tolil()), true_result) def test_precomputed(): clf = NearestCentroid(metric="precomputed") clf.fit(X, y) S = pairwise_distances(T, clf.centroids_) assert_array_equal(clf.predict(S), true_result) def test_iris(): # Check consistency on dataset iris. for metric in ('euclidean', 'cosine'): clf = NearestCentroid(metric=metric).fit(iris.data, iris.target) score = np.mean(clf.predict(iris.data) == iris.target) assert score > 0.9, "Failed with score = " + str(score) def test_iris_shrinkage(): # Check consistency on dataset iris, when using shrinkage. for metric in ('euclidean', 'cosine'): for shrink_threshold in [None, 0.1, 0.5]: clf = NearestCentroid(metric=metric, shrink_threshold=shrink_threshold) clf = clf.fit(iris.data, iris.target) score = np.mean(clf.predict(iris.data) == iris.target) assert score > 0.8, "Failed with score = " + str(score) def test_pickle(): import pickle # classification obj = NearestCentroid() obj.fit(iris.data, iris.target) score = obj.score(iris.data, iris.target) s = pickle.dumps(obj) obj2 = pickle.loads(s) assert_equal(type(obj2), obj.__class__) score2 = obj2.score(iris.data, iris.target) assert_array_equal(score, score2, "Failed to generate same score" " after pickling (classification).") def test_shrinkage_threshold_decoded_y(): clf = NearestCentroid(shrink_threshold=0.01) y_ind = np.asarray(y) y_ind[y_ind == -1] = 0 clf.fit(X, y_ind) centroid_encoded = clf.centroids_ clf.fit(X, y) assert_array_equal(centroid_encoded, clf.centroids_) def test_predict_translated_data(): # Test that NearestCentroid gives same results on translated data rng = np.random.RandomState(0) X = rng.rand(50, 50) y = rng.randint(0, 3, 50) noise = rng.rand(50) clf = NearestCentroid(shrink_threshold=0.1) clf.fit(X, y) y_init = clf.predict(X) clf = NearestCentroid(shrink_threshold=0.1) X_noise = X + noise clf.fit(X_noise, y) y_translate = clf.predict(X_noise) assert_array_equal(y_init, y_translate) def test_manhattan_metric(): # Test the manhattan metric. clf = NearestCentroid(metric='manhattan') clf.fit(X, y) dense_centroid = clf.centroids_ clf.fit(X_csr, y) assert_array_equal(clf.centroids_, dense_centroid) assert_array_equal(dense_centroid, [[-1, -1], [1, 1]])
bsd-3-clause
jmasters/summary
summary.py
1
3388
"""Create a summary HTML page of GBT pipeline-produced images. """ import glob import os.path import argparse import sys from jinja2 import Template from kapteyn import maputils from matplotlib import pylab as plt import matplotlib.gridspec as gridspec # parse the command line to look for an input directory def get_command_line_args(argv): """Read the command line arguments """ if len(argv) == 1: argv.append("-h") parser = argparse.ArgumentParser() parser.add_argument("directory", type=str, help="Directory path containing pipeline images.") args = parser.parse_args() return args def create_images(directory): """Create PNG images for every 'cont' image in the directory """ # create a directory for output images image_dir = './images' if not os.path.exists(image_dir): os.makedirs(image_dir) # look for all 'cont' files in given directory for fname in glob.glob(directory + '/*_cont.fits'): print 'processing file', fname fitsobj = maputils.FITSimage(fname) #print fitsobj.hdr.ascard # set up the matplotlib figure plot object fig = plt.figure(figsize=(9, 9), frameon=True) gspec = gridspec.GridSpec(2, 1, height_ratios=[1, 2]) ax1 = fig.add_subplot(gspec[0]) plt.axis('off') # add title title = """ {object} Observed: {dateobs} Map made: {datemap} Rest frequency: {rest:.2f} GHz Observer: {person} """.format(object=fitsobj.hdr['OBJECT'], dateobs=fitsobj.hdr['DATE-OBS'], datemap=fitsobj.hdr['DATE-MAP'], rest=fitsobj.hdr['RESTFREQ']/1e9, person=fitsobj.hdr['OBSERVER']) ax1.text(0, 0, title, fontsize=12) # add image with coordinates (graticule) ax2 = fig.add_subplot(gspec[1]) mplim = fitsobj.Annotatedimage(ax2, cmap="seismic", blankcolor='white') mplim.Image() mplim.Graticule() # add colorbar units = fitsobj.hdr['BUNIT'] colorbar = mplim.Colorbar(fontsize=12) colorbar.set_label(label=units, fontsize=13) # plot image and save it to disk mplim.plot() basename = os.path.basename(fname) rootname, _ = os.path.splitext(basename) plt.savefig('images/{rootname}.png'.format(rootname=rootname)) plt.close() def create_html_summary(input_directory): """Write an HTML file that shows all the images """ # read in the template html dirname = os.path.dirname(os.path.realpath(__file__)) html_fd = open(dirname + '/summary_template.html', 'r') rawhtml = html_fd.readlines() rawhtml = ''.join(rawhtml) template = Template(rawhtml) files = [] # collect information to go into the template for fname in glob.glob('images/*cont.png'): files.append(fname) # render the html with info we collected html = template.render(dirname=input_directory, files=files) # write the rendered html htmlname = 'image_summary.html' webpage = open(htmlname, 'w') webpage.write(html) webpage.close() print 'wrote', htmlname if __name__ == '__main__': ARGS = get_command_line_args(sys.argv) create_images(ARGS.directory) create_html_summary(ARGS.directory)
gpl-2.0
nelson-liu/scikit-learn
examples/cluster/plot_adjusted_for_chance_measures.py
105
4300
""" ========================================================== Adjustment for chance in clustering performance evaluation ========================================================== The following plots demonstrate the impact of the number of clusters and number of samples on various clustering performance evaluation metrics. Non-adjusted measures such as the V-Measure show a dependency between the number of clusters and the number of samples: the mean V-Measure of random labeling increases significantly as the number of clusters is closer to the total number of samples used to compute the measure. Adjusted for chance measure such as ARI display some random variations centered around a mean score of 0.0 for any number of samples and clusters. Only adjusted measures can hence safely be used as a consensus index to evaluate the average stability of clustering algorithms for a given value of k on various overlapping sub-samples of the dataset. """ print(__doc__) # Author: Olivier Grisel <[email protected]> # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from time import time from sklearn import metrics def uniform_labelings_scores(score_func, n_samples, n_clusters_range, fixed_n_classes=None, n_runs=5, seed=42): """Compute score for 2 random uniform cluster labelings. Both random labelings have the same number of clusters for each value possible value in ``n_clusters_range``. When fixed_n_classes is not None the first labeling is considered a ground truth class assignment with fixed number of classes. """ random_labels = np.random.RandomState(seed).randint scores = np.zeros((len(n_clusters_range), n_runs)) if fixed_n_classes is not None: labels_a = random_labels(low=0, high=fixed_n_classes, size=n_samples) for i, k in enumerate(n_clusters_range): for j in range(n_runs): if fixed_n_classes is None: labels_a = random_labels(low=0, high=k, size=n_samples) labels_b = random_labels(low=0, high=k, size=n_samples) scores[i, j] = score_func(labels_a, labels_b) return scores score_funcs = [ metrics.adjusted_rand_score, metrics.v_measure_score, metrics.adjusted_mutual_info_score, metrics.mutual_info_score, ] # 2 independent random clusterings with equal cluster number n_samples = 100 n_clusters_range = np.linspace(2, n_samples, 10).astype(np.int) plt.figure(1) plots = [] names = [] for score_func in score_funcs: print("Computing %s for %d values of n_clusters and n_samples=%d" % (score_func.__name__, len(n_clusters_range), n_samples)) t0 = time() scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range) print("done in %0.3fs" % (time() - t0)) plots.append(plt.errorbar( n_clusters_range, np.median(scores, axis=1), scores.std(axis=1))[0]) names.append(score_func.__name__) plt.title("Clustering measures for 2 random uniform labelings\n" "with equal number of clusters") plt.xlabel('Number of clusters (Number of samples is fixed to %d)' % n_samples) plt.ylabel('Score value') plt.legend(plots, names) plt.ylim(ymin=-0.05, ymax=1.05) # Random labeling with varying n_clusters against ground class labels # with fixed number of clusters n_samples = 1000 n_clusters_range = np.linspace(2, 100, 10).astype(np.int) n_classes = 10 plt.figure(2) plots = [] names = [] for score_func in score_funcs: print("Computing %s for %d values of n_clusters and n_samples=%d" % (score_func.__name__, len(n_clusters_range), n_samples)) t0 = time() scores = uniform_labelings_scores(score_func, n_samples, n_clusters_range, fixed_n_classes=n_classes) print("done in %0.3fs" % (time() - t0)) plots.append(plt.errorbar( n_clusters_range, scores.mean(axis=1), scores.std(axis=1))[0]) names.append(score_func.__name__) plt.title("Clustering measures for random uniform labeling\n" "against reference assignment with %d classes" % n_classes) plt.xlabel('Number of clusters (Number of samples is fixed to %d)' % n_samples) plt.ylabel('Score value') plt.ylim(ymin=-0.05, ymax=1.05) plt.legend(plots, names) plt.show()
bsd-3-clause
ElDeveloper/scikit-learn
sklearn/metrics/__init__.py
214
3440
""" The :mod:`sklearn.metrics` module includes score functions, performance metrics and pairwise metrics and distance computations. """ from .ranking import auc from .ranking import average_precision_score from .ranking import coverage_error from .ranking import label_ranking_average_precision_score from .ranking import label_ranking_loss from .ranking import precision_recall_curve from .ranking import roc_auc_score from .ranking import roc_curve from .classification import accuracy_score from .classification import classification_report from .classification import cohen_kappa_score from .classification import confusion_matrix from .classification import f1_score from .classification import fbeta_score from .classification import hamming_loss from .classification import hinge_loss from .classification import jaccard_similarity_score from .classification import log_loss from .classification import matthews_corrcoef from .classification import precision_recall_fscore_support from .classification import precision_score from .classification import recall_score from .classification import zero_one_loss from .classification import brier_score_loss from . import cluster from .cluster import adjusted_mutual_info_score from .cluster import adjusted_rand_score from .cluster import completeness_score from .cluster import consensus_score from .cluster import homogeneity_completeness_v_measure from .cluster import homogeneity_score from .cluster import mutual_info_score from .cluster import normalized_mutual_info_score from .cluster import silhouette_samples from .cluster import silhouette_score from .cluster import v_measure_score from .pairwise import euclidean_distances from .pairwise import pairwise_distances from .pairwise import pairwise_distances_argmin from .pairwise import pairwise_distances_argmin_min from .pairwise import pairwise_kernels from .regression import explained_variance_score from .regression import mean_absolute_error from .regression import mean_squared_error from .regression import median_absolute_error from .regression import r2_score from .scorer import make_scorer from .scorer import SCORERS from .scorer import get_scorer __all__ = [ 'accuracy_score', 'adjusted_mutual_info_score', 'adjusted_rand_score', 'auc', 'average_precision_score', 'classification_report', 'cluster', 'completeness_score', 'confusion_matrix', 'consensus_score', 'coverage_error', 'euclidean_distances', 'explained_variance_score', 'f1_score', 'fbeta_score', 'get_scorer', 'hamming_loss', 'hinge_loss', 'homogeneity_completeness_v_measure', 'homogeneity_score', 'jaccard_similarity_score', 'label_ranking_average_precision_score', 'label_ranking_loss', 'log_loss', 'make_scorer', 'matthews_corrcoef', 'mean_absolute_error', 'mean_squared_error', 'median_absolute_error', 'mutual_info_score', 'normalized_mutual_info_score', 'pairwise_distances', 'pairwise_distances_argmin', 'pairwise_distances_argmin_min', 'pairwise_distances_argmin_min', 'pairwise_kernels', 'precision_recall_curve', 'precision_recall_fscore_support', 'precision_score', 'r2_score', 'recall_score', 'roc_auc_score', 'roc_curve', 'SCORERS', 'silhouette_samples', 'silhouette_score', 'v_measure_score', 'zero_one_loss', 'brier_score_loss', ]
bsd-3-clause
LighthouseHPC/lighthouse
sandbox/ml/scikit/NaiveBayes.py
1
1318
# -*- coding: utf-8 -*- """ Created on Mon Jul 17 10:23:41 2017 @author: A """ from sklearn.naive_bayes import GaussianNB import pandas import numpy as np import matplotlib.pylab as plt from sklearn.cross_validation import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import classification_report import sklearn.metrics from sklearn import metrics from sklearn.ensemble import ExtraTreesClassifier from sklearn.metrics import recall_score from sklearn.metrics import accuracy_score import from sklearn.model_selection import KFold #Begin Code: target_names = ['good', 'fair', 'bad'] datafile = input("Enter your datafile: ") print(datafile) target_names = ['good', 'bad', 'fair'] data = pandas.read_csv(datafile) a = len(data.T) - 1 #Again doing this to avoid as much hard coding as possible. X = data.iloc[:,0:a] Y = data.iloc[:, a] #Y is the last colmn, good, bad, fair X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = .34) gnb = GaussianNB() gnb.fit(X_train,Y_train) y_predict_test = gnb.predict(X_test) print(y_predict_test) result = accuracy_score(Y_test, y_predict_test) print(result) results = metrics.classification_report(Y_test, y_predict_test, target_names) print(results) print(time.clock())
mit
google-research/google-research
non_semantic_speech_benchmark/eval_embedding/sklearn/models_test.py
1
1998
# coding=utf-8 # Copyright 2021 The Google Research Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 """Tests for non_semantic_speech_benchmark.eval_embedding.sklearn.models.""" import random as rn from absl import logging from absl.testing import absltest from absl.testing import parameterized import numpy as np from non_semantic_speech_benchmark.eval_embedding.sklearn import models def _get_some_data(num, dims=128): inputs = np.random.rand(num, dims) * 10000 - 5000 sum_np = np.sum(inputs, axis=1) targets = np.where(sum_np < -3000, 0, np.where(sum_np < 1000, 1, 2)) return inputs, targets class ModelsTest(parameterized.TestCase): @parameterized.parameters( ({'model_name': k} for k in models.get_sklearn_models().keys()) ) def test_sklearn_models_sanity(self, model_name): # Set random seed according to: # https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development. np.random.seed(42) rn.seed(42) model = models.get_sklearn_models()[model_name]() # Actually train. inputs, targets = _get_some_data(9000) model.fit(inputs, targets) # Check that performance is near perfect. inputs, targets = _get_some_data(512) acc = model.score(inputs, targets) expected = 0.5 if 'forest' in model_name.lower() else 0.9 self.assertGreater(acc, expected) logging.info('%s final acc: %f', model, acc) if __name__ == '__main__': absltest.main()
apache-2.0
adykstra/mne-python
examples/visualization/make_report.py
7
1590
""" ================================ Make an MNE-Report with a Slider ================================ In this example, MEG evoked data are plotted in an html slider. """ # Authors: Teon Brooks <[email protected]> # Eric Larson <[email protected]> # # License: BSD (3-clause) from mne.report import Report from mne.datasets import sample from mne import read_evokeds from matplotlib import pyplot as plt data_path = sample.data_path() meg_path = data_path + '/MEG/sample' subjects_dir = data_path + '/subjects' evoked_fname = meg_path + '/sample_audvis-ave.fif' ############################################################################### # Do standard folder parsing (this can take a couple of minutes): report = Report(image_format='png', subjects_dir=subjects_dir, info_fname=evoked_fname, subject='sample', raw_psd=True) report.parse_folder(meg_path) ############################################################################### # Add a custom section with an evoked slider: # Load the evoked data evoked = read_evokeds(evoked_fname, condition='Left Auditory', baseline=(None, 0), verbose=False) evoked.crop(0, .2) times = evoked.times[::4] # Create a list of figs for the slider figs = list() for t in times: figs.append(evoked.plot_topomap(t, vmin=-300, vmax=300, res=100, show=False)) plt.close(figs[-1]) report.add_slider_to_section(figs, times, 'Evoked Response', image_format='svg') # to save report # report.save('foobar.html', True)
bsd-3-clause
ds-hwang/deeplearning_udacity
python_practice/01_basics.py
1
4919
# coding: utf-8 # In[ ]: """Summary of tensorflow basics. Parag K. Mital, Jan 2016.""" # In[13]: # %% Import tensorflow and pyplot import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # In[ ]: # %% tf.Graph represents a collection of tf.Operations # You can create operations by writing out equations. # By default, there is a graph: tf.get_default_graph() # and any new operations are added to this graph. # The result of a tf.Operation is a tf.Tensor, which holds # the values. # In[14]: # %% First a tf.Tensor n_values = 32 x = tf.linspace(-3.0, 3.0, n_values) # In[17]: # %% Construct a tf.Session to execute the graph. sess = tf.Session() result = sess.run(x) print(result) # In[20]: # %% Alternatively pass a session to the eval fn: x.eval(session=sess) # x.eval() does not work, as it requires a session! # x.eval() # In[30]: # %% We can setup an interactive session if we don't # want to keep passing the session around: sess.close() sess = tf.InteractiveSession() # In[31]: # %% Now this will work! x.eval() # In[32]: # %% Now a tf.Operation # We'll use our values from [-3, 3] to create a Gaussian Distribution sigma = 1.0 mean = 0.0 z = (tf.exp(tf.neg(tf.pow(x - mean, 2.0) / (2.0 * tf.pow(sigma, 2.0)))) * (1.0 / (sigma * tf.sqrt(2.0 * 3.1415)))) # In[33]: # %% By default, new operations are added to the default Graph assert z.graph is tf.get_default_graph() print z.graph # In[ ]: #plt.close('all') # %% Execute the graph and plot the result plt.plot(z.eval()) plt.show() # In[ ]: # %% We can find out the shape of a tensor like so: print(z.get_shape()) # In[35]: # %% Or in a more friendly format print(z.get_shape().as_list()) # In[36]: # %% Sometimes we may not know the shape of a tensor # until it is computed in the graph. In that case # we should use the tf.shape fn, which will return a # Tensor which can be eval'ed, rather than a discrete # value of tf.Dimension print(tf.shape(z).eval()) # In[ ]: # %% We can combine tensors like so: print(tf.pack([tf.shape(z), tf.shape(z), [3], [4]]).eval()) # In[ ]: # %% Let's multiply the two to get a 2d gaussian z_2d = tf.matmul(tf.reshape(z, [n_values, 1]), tf.reshape(z, [1, n_values])) # In[ ]: # %% Execute the graph and store the value that `out` represents in `result`. plt.imshow(z_2d.eval()) # In[ ]: # %% For fun let's create a gabor patch: x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1]) y = tf.reshape(tf.ones_like(x), [1, n_values]) z = tf.mul(tf.matmul(x, y), z_2d) plt.imshow(z.eval()) # In[ ]: # %% We can also list all the operations of a graph: ops = tf.get_default_graph().get_operations() print([op.name for op in ops]) # In[ ]: # %% Lets try creating a generic function for computing the same thing: def gabor(n_values=32, sigma=1.0, mean=0.0): x = tf.linspace(-3.0, 3.0, n_values) z = (tf.exp(tf.neg(tf.pow(x - mean, 2.0) / (2.0 * tf.pow(sigma, 2.0)))) * (1.0 / (sigma * tf.sqrt(2.0 * 3.1415)))) gauss_kernel = tf.matmul( tf.reshape(z, [n_values, 1]), tf.reshape(z, [1, n_values])) x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1]) y = tf.reshape(tf.ones_like(x), [1, n_values]) gabor_kernel = tf.mul(tf.matmul(x, y), gauss_kernel) return gabor_kernel # In[ ]: # %% Confirm this does something: plt.imshow(gabor().eval()) # In[ ]: # %% And another function which can convolve def convolve(img, W): # The W matrix is only 2D # But conv2d will need a tensor which is 4d: # height x width x n_input x n_output if len(W.get_shape()) == 2: dims = W.get_shape().as_list() + [1, 1] W = tf.reshape(W, dims) if len(img.get_shape()) == 2: # num x height x width x channels dims = [1] + img.get_shape().as_list() + [1] img = tf.reshape(img, dims) elif len(img.get_shape()) == 3: dims = [1] + img.get_shape().as_list() img = tf.reshape(img, dims) # if the image is 3 channels, then our convolution # kernel needs to be repeated for each input channel W = tf.concat(2, [W, W, W]) # Stride is how many values to skip for the dimensions of # num, height, width, channels convolved = tf.nn.conv2d(img, W, strides=[1, 1, 1, 1], padding='SAME') return convolved # In[ ]: # %% Load up an image: from skimage import data img = data.astronaut() plt.imshow(img) plt.show() print(img.shape) # In[ ]: # %% Now create a placeholder for our graph which can store any input: x = tf.placeholder(tf.float32, shape=img.shape) # In[ ]: # %% And a graph which can convolve our image with a gabor out = convolve(x, gabor()) # In[ ]: # %% Now send the image into the graph and compute the result result = tf.squeeze(out).eval(feed_dict={x: img}) plt.imshow(result) plt.show()
mit
Akshay0724/scikit-learn
examples/linear_model/plot_sgd_iris.py
58
2202
""" ======================================== Plot multi-class SGD on the iris dataset ======================================== Plot decision surface of multi-class SGD on iris dataset. The hyperplanes corresponding to the three one-versus-all (OVA) classifiers are represented by the dashed lines. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.linear_model import SGDClassifier # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target colors = "bry" # shuffle idx = np.arange(X.shape[0]) np.random.seed(13) np.random.shuffle(idx) X = X[idx] y = y[idx] # standardize mean = X.mean(axis=0) std = X.std(axis=0) X = (X - mean) / std h = .02 # step size in the mesh clf = SGDClassifier(alpha=0.001, n_iter=100).fit(X, y) # create a mesh to plot in x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired) plt.axis('tight') # Plot also the training points for i, color in zip(clf.classes_, colors): idx = np.where(y == i) plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i], cmap=plt.cm.Paired) plt.title("Decision surface of multi-class SGD") plt.axis('tight') # Plot the three one-against-all classifiers xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() coef = clf.coef_ intercept = clf.intercept_ def plot_hyperplane(c, color): def line(x0): return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1] plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color) for i, color in zip(clf.classes_, colors): plot_hyperplane(i, color) plt.legend() plt.show()
bsd-3-clause
CforED/Machine-Learning
examples/svm/plot_svm_nonlinear.py
268
1091
""" ============== Non-linear SVM ============== Perform binary classification using non-linear SVC with RBF kernel. The target to predict is a XOR of the inputs. The color map illustrates the decision function learned by the SVC. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn import svm xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500)) np.random.seed(0) X = np.random.randn(300, 2) Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0) # fit the model clf = svm.NuSVC() clf.fit(X, Y) # plot the decision function for each datapoint on the grid Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.imshow(Z, interpolation='nearest', extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto', origin='lower', cmap=plt.cm.PuOr_r) contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linetypes='--') plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired) plt.xticks(()) plt.yticks(()) plt.axis([-3, 3, -3, 3]) plt.show()
bsd-3-clause
speed-of-light/slide_video_matcher
pylib/handy_plot.py
1
1135
import matplotlib.gridspec as gs class HandyPlot: """ Author: speed-of-light Purpose: show plot easily """ @staticmethod def table( fig, img_list, title_list=None, row=2, col=2): """Print tablized image list Usage: fig = HandyPlot.table( plt.figure( figsize=(15, 12)), iml, 4, 4) fig.show() """ b = row*col for i, img in enumerate(img_list, 1): ax = fig.add_subplot(row, col, i) title = "img_{}".format(i) if title_list is None else title_list[i-1] ax.set_title(title) ax.imshow(img) if i >= b: break return fig @staticmethod def match_compare(fig, vid_in, sli_in): """ Usage: fig = HandyPlot.amtch_compare( plt.figure( figsize=(14, 4)), vid_data, sli_data) fig.show() """ gsa = gs.GridSpec( 1, 10) gsa.update(left=0.01, right=0.99, hspace=0.25, wspace=.3) ax1 = fig.add_subplot(gsa[:, :-3], title="Input video") ax2 = fig.add_subplot(gsa[:, -3:], title="Self Compare") pax = ax1.matshow(vid_in) fig.colorbar(pax, ax=ax1) cax = ax2.matshow(sli_in) fig.colorbar(cax, ax=ax2) return fig
gpl-2.0
saquiba2/numpy2
numpy/lib/twodim_base.py
83
26903
""" Basic functions for manipulating 2d arrays """ from __future__ import division, absolute_import, print_function from numpy.core.numeric import ( asanyarray, arange, zeros, greater_equal, multiply, ones, asarray, where, int8, int16, int32, int64, empty, promote_types, diagonal, ) from numpy.core import iinfo __all__ = [ 'diag', 'diagflat', 'eye', 'fliplr', 'flipud', 'rot90', 'tri', 'triu', 'tril', 'vander', 'histogram2d', 'mask_indices', 'tril_indices', 'tril_indices_from', 'triu_indices', 'triu_indices_from', ] i1 = iinfo(int8) i2 = iinfo(int16) i4 = iinfo(int32) def _min_int(low, high): """ get small int that fits the range """ if high <= i1.max and low >= i1.min: return int8 if high <= i2.max and low >= i2.min: return int16 if high <= i4.max and low >= i4.min: return int32 return int64 def fliplr(m): """ Flip array in the left/right direction. Flip the entries in each row in the left/right direction. Columns are preserved, but appear in a different order than before. Parameters ---------- m : array_like Input array, must be at least 2-D. Returns ------- f : ndarray A view of `m` with the columns reversed. Since a view is returned, this operation is :math:`\\mathcal O(1)`. See Also -------- flipud : Flip array in the up/down direction. rot90 : Rotate array counterclockwise. Notes ----- Equivalent to A[:,::-1]. Requires the array to be at least 2-D. Examples -------- >>> A = np.diag([1.,2.,3.]) >>> A array([[ 1., 0., 0.], [ 0., 2., 0.], [ 0., 0., 3.]]) >>> np.fliplr(A) array([[ 0., 0., 1.], [ 0., 2., 0.], [ 3., 0., 0.]]) >>> A = np.random.randn(2,3,5) >>> np.all(np.fliplr(A)==A[:,::-1,...]) True """ m = asanyarray(m) if m.ndim < 2: raise ValueError("Input must be >= 2-d.") return m[:, ::-1] def flipud(m): """ Flip array in the up/down direction. Flip the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before. Parameters ---------- m : array_like Input array. Returns ------- out : array_like A view of `m` with the rows reversed. Since a view is returned, this operation is :math:`\\mathcal O(1)`. See Also -------- fliplr : Flip array in the left/right direction. rot90 : Rotate array counterclockwise. Notes ----- Equivalent to ``A[::-1,...]``. Does not require the array to be two-dimensional. Examples -------- >>> A = np.diag([1.0, 2, 3]) >>> A array([[ 1., 0., 0.], [ 0., 2., 0.], [ 0., 0., 3.]]) >>> np.flipud(A) array([[ 0., 0., 3.], [ 0., 2., 0.], [ 1., 0., 0.]]) >>> A = np.random.randn(2,3,5) >>> np.all(np.flipud(A)==A[::-1,...]) True >>> np.flipud([1,2]) array([2, 1]) """ m = asanyarray(m) if m.ndim < 1: raise ValueError("Input must be >= 1-d.") return m[::-1, ...] def rot90(m, k=1): """ Rotate an array by 90 degrees in the counter-clockwise direction. The first two dimensions are rotated; therefore, the array must be at least 2-D. Parameters ---------- m : array_like Array of two or more dimensions. k : integer Number of times the array is rotated by 90 degrees. Returns ------- y : ndarray Rotated array. See Also -------- fliplr : Flip an array horizontally. flipud : Flip an array vertically. Examples -------- >>> m = np.array([[1,2],[3,4]], int) >>> m array([[1, 2], [3, 4]]) >>> np.rot90(m) array([[2, 4], [1, 3]]) >>> np.rot90(m, 2) array([[4, 3], [2, 1]]) """ m = asanyarray(m) if m.ndim < 2: raise ValueError("Input must >= 2-d.") k = k % 4 if k == 0: return m elif k == 1: return fliplr(m).swapaxes(0, 1) elif k == 2: return fliplr(flipud(m)) else: # k == 3 return fliplr(m.swapaxes(0, 1)) def eye(N, M=None, k=0, dtype=float): """ Return a 2-D array with ones on the diagonal and zeros elsewhere. Parameters ---------- N : int Number of rows in the output. M : int, optional Number of columns in the output. If None, defaults to `N`. k : int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : data-type, optional Data-type of the returned array. Returns ------- I : ndarray of shape (N,M) An array where all elements are equal to zero, except for the `k`-th diagonal, whose values are equal to one. See Also -------- identity : (almost) equivalent function diag : diagonal 2-D array from a 1-D array specified by the user. Examples -------- >>> np.eye(2, dtype=int) array([[1, 0], [0, 1]]) >>> np.eye(3, k=1) array([[ 0., 1., 0.], [ 0., 0., 1.], [ 0., 0., 0.]]) """ if M is None: M = N m = zeros((N, M), dtype=dtype) if k >= M: return m if k >= 0: i = k else: i = (-k) * M m[:M-k].flat[i::M+1] = 1 return m def diag(v, k=0): """ Extract a diagonal or construct a diagonal array. See the more detailed documentation for ``numpy.diagonal`` if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using. Parameters ---------- v : array_like If `v` is a 2-D array, return a copy of its `k`-th diagonal. If `v` is a 1-D array, return a 2-D array with `v` on the `k`-th diagonal. k : int, optional Diagonal in question. The default is 0. Use `k>0` for diagonals above the main diagonal, and `k<0` for diagonals below the main diagonal. Returns ------- out : ndarray The extracted diagonal or constructed diagonal array. See Also -------- diagonal : Return specified diagonals. diagflat : Create a 2-D array with the flattened input as a diagonal. trace : Sum along diagonals. triu : Upper triangle of an array. tril : Lower triangle of an array. Examples -------- >>> x = np.arange(9).reshape((3,3)) >>> x array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> np.diag(x) array([0, 4, 8]) >>> np.diag(x, k=1) array([1, 5]) >>> np.diag(x, k=-1) array([3, 7]) >>> np.diag(np.diag(x)) array([[0, 0, 0], [0, 4, 0], [0, 0, 8]]) """ v = asanyarray(v) s = v.shape if len(s) == 1: n = s[0]+abs(k) res = zeros((n, n), v.dtype) if k >= 0: i = k else: i = (-k) * n res[:n-k].flat[i::n+1] = v return res elif len(s) == 2: return diagonal(v, k) else: raise ValueError("Input must be 1- or 2-d.") def diagflat(v, k=0): """ Create a two-dimensional array with the flattened input as a diagonal. Parameters ---------- v : array_like Input data, which is flattened and set as the `k`-th diagonal of the output. k : int, optional Diagonal to set; 0, the default, corresponds to the "main" diagonal, a positive (negative) `k` giving the number of the diagonal above (below) the main. Returns ------- out : ndarray The 2-D output array. See Also -------- diag : MATLAB work-alike for 1-D and 2-D arrays. diagonal : Return specified diagonals. trace : Sum along diagonals. Examples -------- >>> np.diagflat([[1,2], [3,4]]) array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]) >>> np.diagflat([1,2], 1) array([[0, 1, 0], [0, 0, 2], [0, 0, 0]]) """ try: wrap = v.__array_wrap__ except AttributeError: wrap = None v = asarray(v).ravel() s = len(v) n = s + abs(k) res = zeros((n, n), v.dtype) if (k >= 0): i = arange(0, n-k) fi = i+k+i*n else: i = arange(0, n+k) fi = i+(i-k)*n res.flat[fi] = v if not wrap: return res return wrap(res) def tri(N, M=None, k=0, dtype=float): """ An array with ones at and below the given diagonal and zeros elsewhere. Parameters ---------- N : int Number of rows in the array. M : int, optional Number of columns in the array. By default, `M` is taken equal to `N`. k : int, optional The sub-diagonal at and below which the array is filled. `k` = 0 is the main diagonal, while `k` < 0 is below it, and `k` > 0 is above. The default is 0. dtype : dtype, optional Data type of the returned array. The default is float. Returns ------- tri : ndarray of shape (N, M) Array with its lower triangle filled with ones and zero elsewhere; in other words ``T[i,j] == 1`` for ``i <= j + k``, 0 otherwise. Examples -------- >>> np.tri(3, 5, 2, dtype=int) array([[1, 1, 1, 0, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1]]) >>> np.tri(3, 5, -1) array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]]) """ if M is None: M = N m = greater_equal.outer(arange(N, dtype=_min_int(0, N)), arange(-k, M-k, dtype=_min_int(-k, M - k))) # Avoid making a copy if the requested type is already bool m = m.astype(dtype, copy=False) return m def tril(m, k=0): """ Lower triangle of an array. Return a copy of an array with elements above the `k`-th diagonal zeroed. Parameters ---------- m : array_like, shape (M, N) Input array. k : int, optional Diagonal above which to zero elements. `k = 0` (the default) is the main diagonal, `k < 0` is below it and `k > 0` is above. Returns ------- tril : ndarray, shape (M, N) Lower triangle of `m`, of same shape and data-type as `m`. See Also -------- triu : same thing, only for the upper triangle Examples -------- >>> np.tril([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1) array([[ 0, 0, 0], [ 4, 0, 0], [ 7, 8, 0], [10, 11, 12]]) """ m = asanyarray(m) mask = tri(*m.shape[-2:], k=k, dtype=bool) return where(mask, m, zeros(1, m.dtype)) def triu(m, k=0): """ Upper triangle of an array. Return a copy of a matrix with the elements below the `k`-th diagonal zeroed. Please refer to the documentation for `tril` for further details. See Also -------- tril : lower triangle of an array Examples -------- >>> np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1) array([[ 1, 2, 3], [ 4, 5, 6], [ 0, 8, 9], [ 0, 0, 12]]) """ m = asanyarray(m) mask = tri(*m.shape[-2:], k=k-1, dtype=bool) return where(mask, zeros(1, m.dtype), m) # Originally borrowed from John Hunter and matplotlib def vander(x, N=None, increasing=False): """ Generate a Vandermonde matrix. The columns of the output matrix are powers of the input vector. The order of the powers is determined by the `increasing` boolean argument. Specifically, when `increasing` is False, the `i`-th output column is the input vector raised element-wise to the power of ``N - i - 1``. Such a matrix with a geometric progression in each row is named for Alexandre- Theophile Vandermonde. Parameters ---------- x : array_like 1-D input array. N : int, optional Number of columns in the output. If `N` is not specified, a square array is returned (``N = len(x)``). increasing : bool, optional Order of the powers of the columns. If True, the powers increase from left to right, if False (the default) they are reversed. .. versionadded:: 1.9.0 Returns ------- out : ndarray Vandermonde matrix. If `increasing` is False, the first column is ``x^(N-1)``, the second ``x^(N-2)`` and so forth. If `increasing` is True, the columns are ``x^0, x^1, ..., x^(N-1)``. See Also -------- polynomial.polynomial.polyvander Examples -------- >>> x = np.array([1, 2, 3, 5]) >>> N = 3 >>> np.vander(x, N) array([[ 1, 1, 1], [ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]]) >>> np.column_stack([x**(N-1-i) for i in range(N)]) array([[ 1, 1, 1], [ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]]) >>> x = np.array([1, 2, 3, 5]) >>> np.vander(x) array([[ 1, 1, 1, 1], [ 8, 4, 2, 1], [ 27, 9, 3, 1], [125, 25, 5, 1]]) >>> np.vander(x, increasing=True) array([[ 1, 1, 1, 1], [ 1, 2, 4, 8], [ 1, 3, 9, 27], [ 1, 5, 25, 125]]) The determinant of a square Vandermonde matrix is the product of the differences between the values of the input vector: >>> np.linalg.det(np.vander(x)) 48.000000000000043 >>> (5-3)*(5-2)*(5-1)*(3-2)*(3-1)*(2-1) 48 """ x = asarray(x) if x.ndim != 1: raise ValueError("x must be a one-dimensional array or sequence.") if N is None: N = len(x) v = empty((len(x), N), dtype=promote_types(x.dtype, int)) tmp = v[:, ::-1] if not increasing else v if N > 0: tmp[:, 0] = 1 if N > 1: tmp[:, 1:] = x[:, None] multiply.accumulate(tmp[:, 1:], out=tmp[:, 1:], axis=1) return v def histogram2d(x, y, bins=10, range=None, normed=False, weights=None): """ Compute the bi-dimensional histogram of two data samples. Parameters ---------- x : array_like, shape (N,) An array containing the x coordinates of the points to be histogrammed. y : array_like, shape (N,) An array containing the y coordinates of the points to be histogrammed. bins : int or array_like or [int, int] or [array, array], optional The bin specification: * If int, the number of bins for the two dimensions (nx=ny=bins). * If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins). * If [int, int], the number of bins in each dimension (nx, ny = bins). * If [array, array], the bin edges in each dimension (x_edges, y_edges = bins). * A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges. range : array_like, shape(2,2), optional The leftmost and rightmost edges of the bins along each dimension (if not specified explicitly in the `bins` parameters): ``[[xmin, xmax], [ymin, ymax]]``. All values outside of this range will be considered outliers and not tallied in the histogram. normed : bool, optional If False, returns the number of samples in each bin. If True, returns the bin density ``bin_count / sample_count / bin_area``. weights : array_like, shape(N,), optional An array of values ``w_i`` weighing each sample ``(x_i, y_i)``. Weights are normalized to 1 if `normed` is True. If `normed` is False, the values of the returned histogram are equal to the sum of the weights belonging to the samples falling into each bin. Returns ------- H : ndarray, shape(nx, ny) The bi-dimensional histogram of samples `x` and `y`. Values in `x` are histogrammed along the first dimension and values in `y` are histogrammed along the second dimension. xedges : ndarray, shape(nx,) The bin edges along the first dimension. yedges : ndarray, shape(ny,) The bin edges along the second dimension. See Also -------- histogram : 1D histogram histogramdd : Multidimensional histogram Notes ----- When `normed` is True, then the returned histogram is the sample density, defined such that the sum over bins of the product ``bin_value * bin_area`` is 1. Please note that the histogram does not follow the Cartesian convention where `x` values are on the abscissa and `y` values on the ordinate axis. Rather, `x` is histogrammed along the first dimension of the array (vertical), and `y` along the second dimension of the array (horizontal). This ensures compatibility with `histogramdd`. Examples -------- >>> import matplotlib as mpl >>> import matplotlib.pyplot as plt Construct a 2D-histogram with variable bin width. First define the bin edges: >>> xedges = [0, 1, 1.5, 3, 5] >>> yedges = [0, 2, 3, 4, 6] Next we create a histogram H with random bin content: >>> x = np.random.normal(3, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges)) Or we fill the histogram H with a determined bin content: >>> H = np.ones((4, 4)).cumsum().reshape(4, 4) >>> print H[::-1] # This shows the bin content in the order as plotted [[ 13. 14. 15. 16.] [ 9. 10. 11. 12.] [ 5. 6. 7. 8.] [ 1. 2. 3. 4.]] Imshow can only do an equidistant representation of bins: >>> fig = plt.figure(figsize=(7, 3)) >>> ax = fig.add_subplot(131) >>> ax.set_title('imshow: equidistant') >>> im = plt.imshow(H, interpolation='nearest', origin='low', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]]) pcolormesh can display exact bin edges: >>> ax = fig.add_subplot(132) >>> ax.set_title('pcolormesh: exact bin edges') >>> X, Y = np.meshgrid(xedges, yedges) >>> ax.pcolormesh(X, Y, H) >>> ax.set_aspect('equal') NonUniformImage displays exact bin edges with interpolation: >>> ax = fig.add_subplot(133) >>> ax.set_title('NonUniformImage: interpolated') >>> im = mpl.image.NonUniformImage(ax, interpolation='bilinear') >>> xcenters = xedges[:-1] + 0.5 * (xedges[1:] - xedges[:-1]) >>> ycenters = yedges[:-1] + 0.5 * (yedges[1:] - yedges[:-1]) >>> im.set_data(xcenters, ycenters, H) >>> ax.images.append(im) >>> ax.set_xlim(xedges[0], xedges[-1]) >>> ax.set_ylim(yedges[0], yedges[-1]) >>> ax.set_aspect('equal') >>> plt.show() """ from numpy import histogramdd try: N = len(bins) except TypeError: N = 1 if N != 1 and N != 2: xedges = yedges = asarray(bins, float) bins = [xedges, yedges] hist, edges = histogramdd([x, y], bins, range, normed, weights) return hist, edges[0], edges[1] def mask_indices(n, mask_func, k=0): """ Return the indices to access (n, n) arrays, given a masking function. Assume `mask_func` is a function that, for a square array a of size ``(n, n)`` with a possible offset argument `k`, when called as ``mask_func(a, k)`` returns a new array with zeros in certain locations (functions like `triu` or `tril` do precisely this). Then this function returns the indices where the non-zero values would be located. Parameters ---------- n : int The returned indices will be valid to access arrays of shape (n, n). mask_func : callable A function whose call signature is similar to that of `triu`, `tril`. That is, ``mask_func(x, k)`` returns a boolean array, shaped like `x`. `k` is an optional argument to the function. k : scalar An optional argument which is passed through to `mask_func`. Functions like `triu`, `tril` take a second argument that is interpreted as an offset. Returns ------- indices : tuple of arrays. The `n` arrays of indices corresponding to the locations where ``mask_func(np.ones((n, n)), k)`` is True. See Also -------- triu, tril, triu_indices, tril_indices Notes ----- .. versionadded:: 1.4.0 Examples -------- These are the indices that would allow you to access the upper triangular part of any 3x3 array: >>> iu = np.mask_indices(3, np.triu) For example, if `a` is a 3x3 array: >>> a = np.arange(9).reshape(3, 3) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> a[iu] array([0, 1, 2, 4, 5, 8]) An offset can be passed also to the masking function. This gets us the indices starting on the first diagonal right of the main one: >>> iu1 = np.mask_indices(3, np.triu, 1) with which we now extract only three elements: >>> a[iu1] array([1, 2, 5]) """ m = ones((n, n), int) a = mask_func(m, k) return where(a != 0) def tril_indices(n, k=0, m=None): """ Return the indices for the lower-triangle of an (n, m) array. Parameters ---------- n : int The row dimension of the arrays for which the returned indices will be valid. k : int, optional Diagonal offset (see `tril` for details). m : int, optional .. versionadded:: 1.9.0 The column dimension of the arrays for which the returned arrays will be valid. By default `m` is taken equal to `n`. Returns ------- inds : tuple of arrays The indices for the triangle. The returned tuple contains two arrays, each with the indices along one dimension of the array. See also -------- triu_indices : similar function, for upper-triangular. mask_indices : generic function accepting an arbitrary mask function. tril, triu Notes ----- .. versionadded:: 1.4.0 Examples -------- Compute two different sets of indices to access 4x4 arrays, one for the lower triangular part starting at the main diagonal, and one starting two diagonals further right: >>> il1 = np.tril_indices(4) >>> il2 = np.tril_indices(4, 2) Here is how they can be used with a sample array: >>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) Both for indexing: >>> a[il1] array([ 0, 4, 5, 8, 9, 10, 12, 13, 14, 15]) And for assigning values: >>> a[il1] = -1 >>> a array([[-1, 1, 2, 3], [-1, -1, 6, 7], [-1, -1, -1, 11], [-1, -1, -1, -1]]) These cover almost the whole array (two diagonals right of the main one): >>> a[il2] = -10 >>> a array([[-10, -10, -10, 3], [-10, -10, -10, -10], [-10, -10, -10, -10], [-10, -10, -10, -10]]) """ return where(tri(n, m, k=k, dtype=bool)) def tril_indices_from(arr, k=0): """ Return the indices for the lower-triangle of arr. See `tril_indices` for full details. Parameters ---------- arr : array_like The indices will be valid for square arrays whose dimensions are the same as arr. k : int, optional Diagonal offset (see `tril` for details). See Also -------- tril_indices, tril Notes ----- .. versionadded:: 1.4.0 """ if arr.ndim != 2: raise ValueError("input array must be 2-d") return tril_indices(arr.shape[-2], k=k, m=arr.shape[-1]) def triu_indices(n, k=0, m=None): """ Return the indices for the upper-triangle of an (n, m) array. Parameters ---------- n : int The size of the arrays for which the returned indices will be valid. k : int, optional Diagonal offset (see `triu` for details). m : int, optional .. versionadded:: 1.9.0 The column dimension of the arrays for which the returned arrays will be valid. By default `m` is taken equal to `n`. Returns ------- inds : tuple, shape(2) of ndarrays, shape(`n`) The indices for the triangle. The returned tuple contains two arrays, each with the indices along one dimension of the array. Can be used to slice a ndarray of shape(`n`, `n`). See also -------- tril_indices : similar function, for lower-triangular. mask_indices : generic function accepting an arbitrary mask function. triu, tril Notes ----- .. versionadded:: 1.4.0 Examples -------- Compute two different sets of indices to access 4x4 arrays, one for the upper triangular part starting at the main diagonal, and one starting two diagonals further right: >>> iu1 = np.triu_indices(4) >>> iu2 = np.triu_indices(4, 2) Here is how they can be used with a sample array: >>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) Both for indexing: >>> a[iu1] array([ 0, 1, 2, 3, 5, 6, 7, 10, 11, 15]) And for assigning values: >>> a[iu1] = -1 >>> a array([[-1, -1, -1, -1], [ 4, -1, -1, -1], [ 8, 9, -1, -1], [12, 13, 14, -1]]) These cover only a small part of the whole array (two diagonals right of the main one): >>> a[iu2] = -10 >>> a array([[ -1, -1, -10, -10], [ 4, -1, -1, -10], [ 8, 9, -1, -1], [ 12, 13, 14, -1]]) """ return where(~tri(n, m, k=k-1, dtype=bool)) def triu_indices_from(arr, k=0): """ Return the indices for the upper-triangle of arr. See `triu_indices` for full details. Parameters ---------- arr : ndarray, shape(N, N) The indices will be valid for square arrays. k : int, optional Diagonal offset (see `triu` for details). Returns ------- triu_indices_from : tuple, shape(2) of ndarray, shape(N) Indices for the upper-triangle of `arr`. See Also -------- triu_indices, triu Notes ----- .. versionadded:: 1.4.0 """ if arr.ndim != 2: raise ValueError("input array must be 2-d") return triu_indices(arr.shape[-2], k=k, m=arr.shape[-1])
bsd-3-clause
cellnopt/cellnopt
cno/io/adj2sif.py
1
3829
# -*- python -*- # # This file is part of cellnopt software # # Copyright (c) 2012-2014 - EMBL-EBI # # File author(s): Thomas Cokelaer <[email protected]>, # <cokelaer at gmail dot com> # # Distributed under the GPLv3 License. # See accompanying file LICENSE.txt or copy at # http://www.gnu.org/licenses/gpl-3.0.html # # website: www.cellnopt.org # ############################################################################## """:Topic: **adjacency matrix**""" import networkx as nx import numpy class ADJ2SIF(object): """Reads an adjacency matrix (and names) from CSV files .. warning:: API likely to change to use pandas to simplify the API. The instance can then be exported to :class:`~cno.io.sif.SIF` or used as input for the :class:`cno.io.cnograph.CNOGraph` structure. :: >>> from cno.io import ADJ2SIF >>> from cno import getdata >>> f1 = getdata("test_adjacency_matrix.csv") >>> f2 = getdata("test_adjacency_names.csv") >>> s = ADJ2SIF(f1, f2) >>> sif = s.to_sif() >>> c = CNOGraph(s.G) Where the adjacency matrix looks like:: 0,1,0 1,0,0 0,0,1 and names is a 1-column file:: A B C The exported SIF file would look like:: A 1 B A 1 C .. warning:: The adjacency matrix contains only ones (no -1) so future version may need to add that information using incidence matrix for instance """ def __init__(self, filenamePKN=None, filenameNames=None, delimiter=","): """.. rubric:: Constructor :param str filenamePKN: adjacency matrix made of 0's and 1's. :param str filenameNames: names of the columns/rows of the adjacency matrix :param str delimiter: commas by default :: 0,1,0 1,0,0 0,0,1 names:: A B C The 2 files above correspond to this SIF file:: A 1 B A 1 C """ self.filename = filenamePKN self.filenameNames = filenameNames self.delimiter = delimiter self._G = None self._names = None if self.filename: self.load_adjacency() if filenameNames: self.load_names() def _get_names(self): return self._names names = property(_get_names, doc="Names of the nodes read from the the provided filename. Could be empty") def _get_G(self): return self._G G = property(_get_G, doc="The graph created from the input data") def load_adjacency(self, filename=None): """Reads an adjacency matrix filename if no filename is provided, tries to load from the attribute :attr:`filename`. """ if filename: self.filename = filename self._G = nx.Graph(numpy.loadtxt(self.filename, delimiter=self.delimiter)) def load_names(self, filename=None): """Reads the columns/rows names""" if filename: self.filenameNames = filename fh = open(self.filenameNames, "r") data = fh.read() fh.close() self._names = data.split() def to_sif(self, filename=None): """Exports input data files into a SIF instance and save it :param str filename: set this parameter if you want to save the SIF into a file :return: a SIF instance """ from cno.io.sif import SIF s = SIF() for edge in self.G.edges(): reac = self.names[edge[0]] + "=" + self.names[edge[1]] s.add_reaction(reac) if filename: s.save(filename) return s
bsd-2-clause
pjryan126/solid-start-careers
store/api/zillow/venv/lib/python2.7/site-packages/pandas/__init__.py
1
2024
# pylint: disable-msg=W0614,W0401,W0611,W0622 # flake8: noqa __docformat__ = 'restructuredtext' # Let users know if they're missing any of our hard dependencies hard_dependencies = ("numpy", "pytz", "dateutil") missing_dependencies = [] for dependency in hard_dependencies: try: __import__(dependency) except ImportError as e: missing_dependencies.append(dependency) if missing_dependencies: raise ImportError("Missing required dependencies {0}".format(missing_dependencies)) # numpy compat from pandas.compat.numpy_compat import * try: from pandas import hashtable, tslib, lib except ImportError as e: # pragma: no cover module = str(e).lstrip('cannot import name ') # hack but overkill to use re raise ImportError("C extension: {0} not built. If you want to import " "pandas from the source directory, you may need to run " "'python setup.py build_ext --inplace' to build the C " "extensions first.".format(module)) from datetime import datetime from pandas.info import __doc__ # let init-time option registration happen import pandas.core.config_init from pandas.core.api import * from pandas.sparse.api import * from pandas.stats.api import * from pandas.tseries.api import * from pandas.io.api import * from pandas.computation.api import * from pandas.tools.merge import merge, concat, ordered_merge from pandas.tools.pivot import pivot_table, crosstab from pandas.tools.plotting import scatter_matrix, plot_params from pandas.tools.tile import cut, qcut from pandas.tools.util import to_numeric from pandas.core.reshape import melt from pandas.util.print_versions import show_versions # define the testing framework import pandas.util.testing from pandas.util.nosetester import NoseTester test = NoseTester().test del NoseTester # use the closest tagged version if possible from ._version import get_versions v = get_versions() __version__ = v.get('closest-tag',v['version']) del get_versions, v
gpl-2.0
mm14kwn/2015-12-14-Portsmouth-students
ScientificPython/L03-matplotlib/Exercise/solutions/publish.py
2
1389
#!/usr/bin/env python # # Publication-quality plots # # ARCHER, 2015 # Uncomment the following two lines to save an image # without needing an available display # import matplotlib # matplotlib.use("Agg") import sys from matplotlib import rc_file import numpy as np from matplotlib import pyplot as plt def main(argv): # Get the custom options rc_file("matplotlibrc.custom") # Set up the figure with the computed dimensions fig = plt.figure(figsize=figdims(500, 0.7)) # Read the data and plot it data1 = np.genfromtxt('/Users/nbanglawala/Desktop/Work/ARCHER_CSE/CSE_Training/ScientificPython_NB/Exercises/matplotlib/code/random1.dat') plt.subplot(1,1,1) plt.plot(data1[:,0], data1[:,1], 'kx--', label='random1') # Axis labels plt.xlabel('Positions') plt.ylabel('Values') # Save in a nice format fig.tight_layout(pad=0.1) fig.savefig("publish.pdf", dpi=600) # Compute the figure dimenstions based on width (in pts) and # a scale factor def figdims(width, factor): widthpt = width * factor inperpt = 1.0 / 72.27 golden_ratio = (np.sqrt(5) - 1.0) / 2.0 # because it looks good widthin = widthpt * inperpt heightin = widthin * golden_ratio return [widthin, heightin] # Dimensions as list # Function to create tidy way to have main method if __name__ == "__main__": main(sys.argv[1:])
gpl-2.0
gallantlab/pycortex
cortex/polyutils/subsurface.py
1
27001
"""utilities for efficiently working with patches of cortex (aka subsurfaces)""" import numpy as np import scipy.sparse from .misc import _memo class SubsurfaceMixin(object): """mixin for Surface of efficient methods for working with subsurfaces - see pycortex documentation for example usage Use Cases --------- - performing many operations on a subset of cortex - finding patches/patchs in cortical suface (see Performance Characteristics) Performance Characteristics --------------------------- - main use case is faster implementation of geodesic_distance() - original geodesic_distance: - large startup cost (~10 s) - small subsequent cost (~200 ms) - use case: performing many repeated operations on large subsets of cortex - subsurface geodesic_distance: - cost is based on radius - 5 mm -> (~40 ms startup cost) - 25 mm -> (~200 ms startup cost) - use cases: calling operations small number of times or on medium subsets of cortex - [benchmarks recorded on lab desktop workstation] """ def create_subsurface(self, vertex_mask=None, polygon_mask=None): """Create subsurface for efficient operations on subset of Surface - should specify either vertex_mask or polygon_mask - input vertex_mask is not necessarily the final vertex_mask used - final vertex_mask is always derived from polygon_mask - this prevents dangling vertices Parameters ---------- - vertex_mask : boolean array - mask of which vertices to include - polygon_mask : boolean array - mask of which polygons to include """ if polygon_mask is None: if vertex_mask is None: raise Exception('must specify vertex_mask or polygon_mask') polygon_mask = ( vertex_mask[self.polys[:, 0]] * vertex_mask[self.polys[:, 1]] * vertex_mask[self.polys[:, 2]] ) # select only vertices that appear in a polygon of polygon_mask vertex_mask = np.zeros(self.pts.shape[0], dtype=bool) vertex_mask[self.polys[polygon_mask].flat] = True # build map from old index to new index # vertices not in the subsurface are represented with large numbers vertex_map = np.ones(self.pts.shape[0], dtype=np.int) * np.iinfo(np.int32).max vertex_map[vertex_mask] = range(vertex_mask.sum()) # reindex vertices and polygons subsurface_vertices = self.pts[vertex_mask, :] subsurface_polygons = vertex_map[self.polys[polygon_mask, :]] # create subsurface subsurface = self.__class__(pts=subsurface_vertices, polys=subsurface_polygons) subsurface.subsurface_vertex_mask = vertex_mask subsurface.subsurface_vertex_map = vertex_map subsurface.subsurface_polygon_mask = polygon_mask return subsurface @property @_memo def subsurface_vertex_inverse(self): return np.nonzero(self.subsurface_vertex_mask)[0] def get_connected_vertices(self, vertex, mask, old_version=False): """return vertices connected to vertex that satisfy mask - helper method for other methods Parameters ----------- - vertex : one of [scalar int index | list of int indices | numpy array of int indices] vertex or set of vertices to use as seed - mask : boolean array mask of allowed neighbors - old_version : boolean (default=False) True = Use vertex adjacency to select patch (can cause errors in odd situations) False = Use poly adjacency to select patch (solves problem where a single edge but no polys connect two regions within the patch, makes geodesic distance errors) """ n_vertices = self.pts.shape[0] n_polys = self.polys.shape[0] output_mask = np.zeros(n_vertices, dtype=bool) if np.issubdtype(type(vertex), np.integer): add_next = [vertex] output_mask[vertex] = True elif ( isinstance(vertex, list) or (isinstance(vertex, np.ndarray) and np.issubdtype(vertex.dtype, np.integer)) ): add_next = vertex output_mask[vertex] = True else: raise Exception('unknown vertex type:' + str(vertex)) if old_version: while len(add_next) > 0: check = np.zeros(n_vertices, dtype=bool) check[self.adj[add_next, :].indices] = True add_next = check * mask * (~output_mask) output_mask[add_next] = True add_next = np.nonzero(add_next)[0] else: while len(add_next) > 0: check = np.zeros(n_vertices, dtype=bool) # Instead of just adjacent vertices, get adjacent polys check_polys = self.connected[add_next,:].indices # Will be checking within mask in this step for all verts for a poly being in the mask good_polys = check_polys[np.all(mask[self.polys[check_polys,:]], axis=1)] # Then get all verts from the good polys good_verts = np.unique(self.polys[good_polys]) check[good_verts] = True # Mask is already used in selecting checked ones add_next = check * (~output_mask) output_mask[add_next] = True add_next = np.nonzero(add_next)[0] return output_mask def get_euclidean_patch(self, vertex, radius, old_version=False): """return connected vertices within some 3d euclidean distance of a vertex Parameters ---------- - vertex : one of [scalar int index | list of int indices | numpy array of int indices] vertex or set of vertices to use as seed - radius : number distance threshold - old_version : boolean (default=False) True = Use vertex adjacency to select patch (can cause errors in odd situations) False = Use poly adjacency to select patch (solves problem where a single edge but no polys connect two regions within the patch, makes geodesic distance errors) """ if np.issubdtype(type(vertex), np.integer): close_enough = self.get_euclidean_ball(self.pts[vertex, :], radius) elif ( isinstance(vertex, list) or (isinstance(vertex, np.ndarray) and np.issubdtype(vertex.dtype, np.integer)) ): mask_list = [self.get_euclidean_ball(self.pts[index, :], radius) for index in vertex] close_enough = np.array(mask_list).sum(axis=0).astype(bool) else: raise Exception('unknown vertex type: ' + str(type(vertex))) return { 'vertex_mask': self.get_connected_vertices(vertex=vertex, mask=close_enough, old_version=old_version), } def get_euclidean_ball(self, xyz, radius): """return vertices within some 3d euclidean distance of an xyz coordinate Parameters ---------- - xyz : array of shape (3,) center of euclidean ball - radius : number radius of euclidean ball """ # unoptimized version: # distances = ((surface.pts - xyz) ** 2).sum(1) ** 0.5 # return distances < radius # optimized version: diff = self.pts - xyz diff **= 2 diff = diff.dot(np.ones(diff.shape[1])) # precision fine because only summing 3 values diff **= 0.5 return diff < radius def get_geodesic_patch(self, vertex, radius, attempts=5, m=1.0, old_version=False): """return vertices within some 2d geodesic distance of a vertex (or vertices) Parameters ---------- - vertex : int index (or list of int indices) of seed vertex (or vertices) - radius : number radius to use as threshold - attempts : int number of attempts to use for working with singular subsurfaces - m : number reverse Euler step length, passed to geodesic_distance - old_version : boolean (default=False) True = Use vertex adjacency to select patch (can cause errors in odd situations) False = Use poly adjacency to select patch (solves problem where a single edge but no polys connect two regions within the patch, makes geodesic distance errors) Output ------ - 'vertex_mask' : boolean mask of selected vertices - 'geodesic_distance' : array of geodesic distances of selected points """ working_radius = radius for attempt in range(attempts): try: euclidean_vertices = self.get_euclidean_patch(vertex, working_radius, old_version=old_version) vertex_mask = euclidean_vertices['vertex_mask'] if vertex_mask.sum() <= 1: working_radius *= 1.1 continue subsurface = self.create_subsurface(vertex_mask=vertex_mask) vertex_map = subsurface.subsurface_vertex_map if np.isscalar(vertex): vertex = [vertex] geodesic_distance = subsurface.geodesic_distance(vertex_map[vertex], m=m) break except RuntimeError: # singular subsurface working_radius *= 1.1 continue else: raise Exception('could not find suitable radius') close_enough = geodesic_distance <= radius close_enough = subsurface.lift_subsurface_data(close_enough) geodesic_distance = subsurface.lift_subsurface_data(geodesic_distance) geodesic_distance[~close_enough] = np.nan vertex_mask = self.get_connected_vertices(vertex=vertex, mask=close_enough, old_version=old_version) return { 'vertex_mask': vertex_mask, 'geodesic_distance': geodesic_distance[vertex_mask], } def get_geodesic_patches(self, radius, seeds=None, n_random_seeds=None, output='dense'): """create patches of cortex centered around each vertex seed - must specify seeds or n_random_seeds Parameters ---------- - radius : number radius of searchlights - seeds : list of ints centers of each patch - n_random_seeds : int number of vertex seeds to generate - output : 'dense' or 'sparse' 'dense': output as dense binary array (faster, less memory efficient) 'sparse': output as sparse binary array (slower, more memory efficient) """ # gather seeds if n_random_seeds is not None: seeds = np.random.choice(self.pts.shape[0], n_random_seeds, replace=False) if seeds is None: raise Exception('must specify seeds or n_random_seeds') # intialize output output_dims = (len(seeds), self.pts.shape[0]) if output == 'dense': patches = np.zeros(output_dims, dtype=bool) elif output == 'sparse': patches = scipy.sparse.dok_matrix(output_dims, dtype=bool) else: raise Exception('output: ' + str(output)) # compute patches for vs, vertex_seed in enumerate(seeds): patch = self.get_geodesic_patch(radius=radius, vertex=vertex_seed) patches[vs, :] = patch['vertex_mask'] return { 'vertex_masks': patches, } def lift_subsurface_data(self, data, vertex_mask=None): """expand vertex dimension of data to original surface's size - agnostic to dtype and dimensionality of data - vertex dimension should be last dimension Parameters ---------- - data : array data to lift into original surface dimension - vertex_mask : boolean array custom mask to use instead of subsurface_vertex_mask """ if vertex_mask is None: vertex_mask = self.subsurface_vertex_mask new_shape = [vertex_mask.shape[0]] if data.ndim > 1: new_shape = list(data.shape[:-1]) + new_shape lifted = np.zeros(new_shape, dtype=data.dtype) lifted[..., vertex_mask] = data return lifted def get_geodesic_strip_patch(self, v0, v1, radius, room_factor=2, method='bb', graph_search='astar', include_strip_coordinates=True): """return patch that includes v0, v1, their geodesic path, and all points within some radius Algorithms ---------- - selection algorithms: - 'bb' = big ball 1. use euclidean ball big enough to contain v0 and v1 - center = (v0 + v1) / 2 - radius = euclidean_distance(v0, v1) / 2 2. only proceed if geodesic path [v0 -> v1] does not touch boundary - otherwise expand ball and try again 3. go along each point in geodesic path, taking geodesic ball of radius r - 'graph_distance' = get graph shortest graph path from v0 to v1 1. take eucidean tube around graph path 2. will want to use weighted graph instead of just graph - this is the fastest method, but requires further implementation tuning - 'whole_surface' = use entire surface - when geodesic touches the boundary 1. add euclidean ball of boundary point to working set 2. recompute - for now use: - 'bb' when creating single strips or small strips - 'whole_surface' when creating many large strips Parameters ---------- - v0 : int index of start point - v1 : int index of end point - radius : number radius of section around geodesic path - method : str algorithm, either 'bb' or 'graph_distance' - room_factor : number in bb method, how much extra room in big ball - graph_search : 'astar' or 'dijkstra' graph search method to use - include_strip_coordinates : bool whether to compute coordinates of strip """ # find initial submesh that contains v0, v1, and their geodesic path if method == 'bb': # use a big ball centered between v0 and v1 xyz_0 = self.pts[v0] xyz_1 = self.pts[v1] bb_center = (xyz_0 + xyz_1) / 2.0 bb_radius = room_factor * (((xyz_0 - xyz_1) ** 2).sum() ** 0.5) bb = self.get_euclidean_ball(xyz=bb_center, radius=bb_radius) initial_mask = self.get_connected_vertices(vertex=v0, mask=bb) initial_mask += self.get_connected_vertices(vertex=v1, mask=bb) initial_surface = self.create_subsurface(vertex_mask=initial_mask) geodesic_path = initial_surface.geodesic_path( a=initial_surface.subsurface_vertex_map[v0], b=initial_surface.subsurface_vertex_map[v1], ) # collect points within radius of each point in geodesic path strip_mask = self.get_geodesic_patch( vertex=np.where(initial_surface.subsurface_vertex_mask)[0][geodesic_path], radius=radius, ) elif method == 'graph_distance': raise NotImplementedError() # # use shortest path between v0 and v1 along graph edges # import networkx # graph = self.weighted_distance_graph # if graph_search == 'dijkstra': # graph_path = networkx.shortest_path(graph, v0, v1, weight='weight') # elif graph_search == 'astar': # graph_path = networkx.shortest_paths.astar.astar_path(graph, v0, v1, weight='weight') # else: # raise Exception(str(graph_search)) # initial_vertices = self.get_euclidean_patch( # vertex=graph_path, # radius=(radius * room_factor), # ) # initial_mask = initial_vertices['vertex_mask'] # initial_surface = self.create_subsurface(vertex_mask=initial_mask) elif method == 'whole_surface': initial_surface = self geodesic_path = self.geodesic_path(v0, v1) strip_mask = self.get_geodesic_patch( vertex=geodesic_path, radius=radius, ) else: raise Exception('method: ' + str(method)) geodesic_path_mask = np.zeros(initial_surface.pts.shape[0], dtype=bool) geodesic_path_mask[geodesic_path] = True # verify geodesic path does not touch boundary if (geodesic_path_mask * initial_surface.boundary_vertices).sum() > 2: raise Exception('irregular submesh, geodesic path touches boundary') output = { 'vertex_mask': strip_mask['vertex_mask'], 'geodesic_path': geodesic_path, } if include_strip_coordinates: subsurface = self.create_subsurface(vertex_mask=strip_mask['vertex_mask']) coordinates = subsurface.get_strip_coordinates( v0=subsurface.subsurface_vertex_map[v0], v1=subsurface.subsurface_vertex_map[v1], geodesic_path=subsurface.subsurface_vertex_map[geodesic_path], ) output['subsurface'] = subsurface output['coordinates'] = subsurface.lift_subsurface_data(coordinates['coordinates']) return output def get_strip_coordinates(self, v0, v1, geodesic_path=None, distance_algorithm='softmax'): """get 2D coordinates of surface from v0 to v1 - first coordinate: distance along geodesic path from v0 - second coordinate: distance from geodesic path - v0 and v1 should be on boundary of patch - if not, they are reassigned to boundary_vertices - could be optimized by - reusing information from get_geodesic_strip_patch() Parameters ---------- - v0 : int index of starting point - v1 : int index of starting point - geodesic_path : list of int geodesic_path to use - distance_algorithm : str method to use for computing distance along path, 'softmax' or 'closest' """ if geodesic_path is None: geodesic_path = self.geodesic_path(v0, v1) geodesic_distances = np.vstack([self.geodesic_distance([v]) for v in geodesic_path]) v0_distance = geodesic_distances[0, :] bound = self.boundary_vertices # reassign v0 and v1 to border vertices # find boundary vertex maximizing distance to 2nd point in geodesic path # s.t. (distance to second point) - (distance to first point) > 0 if not bound[v0]: # use boundary vertex v that minimizes [ d(geopath[0], v) - d(geopath[1], v) ] & > 0 candidates = bound * (geodesic_distances[0, :] < geodesic_distances[1, :]) if candidates.sum() == 0: bound_max = np.argmax( geodesic_distances[1, bound] - geodesic_distances[0, bound] ) candidates = np.zeros(self.pts.shape[0], dtype=bool) candidates[bound[bound_max]] = True index = np.argmax(geodesic_distances[1, :][candidates]) new_v0 = np.where(candidates)[0][index] new_path_0 = self.geodesic_path(new_v0, v0)[:-1] new_geodesic_distances_0 = np.vstack([self.geodesic_distance([v]) for v in new_path_0]) v0 = new_v0 geodesic_path = np.hstack([new_path_0, geodesic_path]) geodesic_distances = np.vstack([new_geodesic_distances_0, geodesic_distances]) if not bound[v1]: # use boundary vertex v that minimizes [ d(geopath[-1], v) - d(geopath[-2], v) ] & > 0 candidates = bound * (geodesic_distances[-1, :] < geodesic_distances[-2, :]) if candidates.sum() == 0: bound_max = np.argmax( geodesic_distances[-2, bound] - geodesic_distances[-1, bound] ) candidates = np.zeros(self.pts.shape[0], dtype=bool) candidates[bound[bound_max]] = True index = np.argmax(geodesic_distances[1, :][candidates]) new_v1 = np.where(candidates)[0][index] new_path_1 = self.geodesic_path(v1, new_v1)[1:] new_geodesic_distances_1 = np.vstack([self.geodesic_distance([v]) for v in new_path_1]) v1 = new_v1 geodesic_path = np.hstack([geodesic_path, new_path_1]) geodesic_distances = np.vstack([geodesic_distances, new_geodesic_distances_1]) # compute distance along line if distance_algorithm == 'softmax': path_distances = geodesic_distances[0, geodesic_path] exp = np.exp(-geodesic_distances) softmax = (exp / exp.sum(0)) distance_along_line = softmax.T.dot(path_distances) elif distance_algorithm == 'closest': closest_path_vertex = np.array(geodesic_path)[np.argmin(geodesic_distances, axis=0)] distance_along_line = v0_distance[closest_path_vertex] else: raise Exception(distance_algorithm) # compute distance from line # Calling directly self.geodesic_distance(geodesic_path) is somehow # not precise enough on patches, probably because we don't deal # correctly with boundaries in the heat method solver. Here instead, # we call self.geodesic_distance on each point and take the min. distance_from_line = np.min([self.geodesic_distance([ii]) for ii in geodesic_path], axis=0) # compute the sign for each side of the line geodesic_mask = np.zeros(self.pts.shape[0], dtype=bool) geodesic_mask[geodesic_path] = True subsurface = self.create_subsurface(vertex_mask=(~geodesic_mask)) whole_submask = np.ones(subsurface.pts.shape[0], dtype=bool) connected_component = subsurface.get_connected_vertices(vertex=0, mask=whole_submask) subsubmask = np.where(subsurface.subsurface_vertex_mask)[0][connected_component] distance_from_line[subsubmask] *= -1 return { 'geodesic_path': geodesic_path, 'coordinates': np.vstack([distance_along_line, distance_from_line]), 'v0': v0, 'v1': v1, } @property def furthest_border_points(self): """return pair of points on surface border that have largest pairwise geodesic distance""" border_mask = self.boundary_vertices border_vertices = np.nonzero(border_mask)[0] n_border_vertices = border_vertices.shape[0] border_pairwise_distances = np.zeros((n_border_vertices, n_border_vertices)) for v, vertex in enumerate(border_vertices): border_pairwise_distances[v, :] = self.geodesic_distance([vertex])[border_mask] max_index = np.argmax(border_pairwise_distances) v0, v1 = np.unravel_index(max_index, border_pairwise_distances.shape) return {'v0': border_vertices[v0], 'v1': border_vertices[v1]} def plot_subsurface_rotating_gif( self, path, N_frames=48, fps=12, angles=None, vis_data=None, disp_patch_verticies=False, disp_patch_edges=False, disp_patch_triangles=True, disp_subpatch=False, disp_rim_points=True, disp_rim_edges=True, point_color='b', line_color='k', face_color='r' ): """create a rotating gif of subsurface - matplotlib has extremely limited support for 3d plotting - expect graphical artifacts when combining multiple features - e.g. plotted vertices are not properly obscured by plotted faces """ import matplotlib.pyplot as plt import matplotlib.animation as animation import mpl_toolkits.mplot3d as a3 if angles is None: elev = 10 * np.ones((N_frames,)) azim = np.linspace(0, 360, N_frames, endpoint=False) angles = list(zip(elev, azim)) fig = plt.figure() ax = fig.gca(projection='3d') plt.axis('off') def init(): ax.view_init(elev=angles[0][0], azim=angles[0][1]) if vis_data is not None: for plot in vis_data: defaults = { 'color': 'c', 'marker': '.', 'markersize': 15, 'linestyle': '', } defaults.update(plot['kwargs']) ax.plot( self.pts[plot['mask'], 0], self.pts[plot['mask'], 1], self.pts[plot['mask'], 2], **defaults ) tri_poly = a3.art3d.Poly3DCollection( [self.ppts[p, :, :] for p in range(self.ppts.shape[0])], # facecolor='none', alpha=1.0, linewidths=1, ) tri_poly.set_facecolor('red') tri_poly.set_edgecolor('black') ax.add_collection3d(tri_poly) else: if True: alpha = 1 if disp_patch_verticies else 0 ax.plot( self.pts[:, 0], self.pts[:, 1], self.pts[:, 2], (point_color + '.'), markersize=15, alpha=alpha, ) if disp_patch_edges: pass if disp_patch_triangles: tri_poly = a3.art3d.Poly3DCollection( [self.ppts[p, :, :] for p in range(self.ppts.shape[0])], alpha=1.0, ) tri_poly.set_color(face_color) tri_poly.set_edgecolor(line_color) ax.add_collection3d(tri_poly) def animate(i): ax.view_init(elev=angles[i][0], azim=angles[i][1]) return [] anim = animation.FuncAnimation( fig, animate, N_frames, interval=25, blit=False, init_func=init, ) anim.save(path, writer='imagemagick', fps=fps)
bsd-2-clause
kkk669/mxnet
example/bayesian-methods/bdk_demo.py
45
15837
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from __future__ import print_function import mxnet as mx import mxnet.ndarray as nd import numpy import logging import matplotlib.pyplot as plt from scipy.stats import gaussian_kde import argparse from algos import * from data_loader import * from utils import * class CrossEntropySoftmax(mx.operator.NumpyOp): def __init__(self): super(CrossEntropySoftmax, self).__init__(False) def list_arguments(self): return ['data', 'label'] def list_outputs(self): return ['output'] def infer_shape(self, in_shape): data_shape = in_shape[0] label_shape = in_shape[0] output_shape = in_shape[0] return [data_shape, label_shape], [output_shape] def forward(self, in_data, out_data): x = in_data[0] y = out_data[0] y[:] = numpy.exp(x - x.max(axis=1).reshape((x.shape[0], 1))).astype('float32') y /= y.sum(axis=1).reshape((x.shape[0], 1)) def backward(self, out_grad, in_data, out_data, in_grad): l = in_data[1] y = out_data[0] dx = in_grad[0] dx[:] = (y - l) class LogSoftmax(mx.operator.NumpyOp): def __init__(self): super(LogSoftmax, self).__init__(False) def list_arguments(self): return ['data', 'label'] def list_outputs(self): return ['output'] def infer_shape(self, in_shape): data_shape = in_shape[0] label_shape = in_shape[0] output_shape = in_shape[0] return [data_shape, label_shape], [output_shape] def forward(self, in_data, out_data): x = in_data[0] y = out_data[0] y[:] = (x - x.max(axis=1, keepdims=True)).astype('float32') y -= numpy.log(numpy.exp(y).sum(axis=1, keepdims=True)).astype('float32') # y[:] = numpy.exp(x - x.max(axis=1).reshape((x.shape[0], 1))) # y /= y.sum(axis=1).reshape((x.shape[0], 1)) def backward(self, out_grad, in_data, out_data, in_grad): l = in_data[1] y = out_data[0] dx = in_grad[0] dx[:] = (numpy.exp(y) - l).astype('float32') def classification_student_grad(student_outputs, teacher_pred): return [student_outputs[0] - teacher_pred] def regression_student_grad(student_outputs, teacher_pred, teacher_noise_precision): student_mean = student_outputs[0] student_var = student_outputs[1] grad_mean = nd.exp(-student_var) * (student_mean - teacher_pred) grad_var = (1 - nd.exp(-student_var) * (nd.square(student_mean - teacher_pred) + 1.0 / teacher_noise_precision)) / 2 return [grad_mean, grad_var] def get_mnist_sym(output_op=None, num_hidden=400): net = mx.symbol.Variable('data') net = mx.symbol.FullyConnected(data=net, name='mnist_fc1', num_hidden=num_hidden) net = mx.symbol.Activation(data=net, name='mnist_relu1', act_type="relu") net = mx.symbol.FullyConnected(data=net, name='mnist_fc2', num_hidden=num_hidden) net = mx.symbol.Activation(data=net, name='mnist_relu2', act_type="relu") net = mx.symbol.FullyConnected(data=net, name='mnist_fc3', num_hidden=10) if output_op is None: net = mx.symbol.SoftmaxOutput(data=net, name='softmax') else: net = output_op(data=net, name='softmax') return net def synthetic_grad(X, theta, sigma1, sigma2, sigmax, rescale_grad=1.0, grad=None): if grad is None: grad = nd.empty(theta.shape, theta.context) theta1 = theta.asnumpy()[0] theta2 = theta.asnumpy()[1] v1 = sigma1 ** 2 v2 = sigma2 ** 2 vx = sigmax ** 2 denominator = numpy.exp(-(X - theta1) ** 2 / (2 * vx)) + numpy.exp( -(X - theta1 - theta2) ** 2 / (2 * vx)) grad_npy = numpy.zeros(theta.shape) grad_npy[0] = -rescale_grad * ((numpy.exp(-(X - theta1) ** 2 / (2 * vx)) * (X - theta1) / vx + numpy.exp(-(X - theta1 - theta2) ** 2 / (2 * vx)) * ( X - theta1 - theta2) / vx) / denominator).sum() \ + theta1 / v1 grad_npy[1] = -rescale_grad * ((numpy.exp(-(X - theta1 - theta2) ** 2 / (2 * vx)) * ( X - theta1 - theta2) / vx) / denominator).sum() \ + theta2 / v2 grad[:] = grad_npy return grad def get_toy_sym(teacher=True, teacher_noise_precision=None): if teacher: net = mx.symbol.Variable('data') net = mx.symbol.FullyConnected(data=net, name='teacher_fc1', num_hidden=100) net = mx.symbol.Activation(data=net, name='teacher_relu1', act_type="relu") net = mx.symbol.FullyConnected(data=net, name='teacher_fc2', num_hidden=1) net = mx.symbol.LinearRegressionOutput(data=net, name='teacher_output', grad_scale=teacher_noise_precision) else: net = mx.symbol.Variable('data') net = mx.symbol.FullyConnected(data=net, name='student_fc1', num_hidden=100) net = mx.symbol.Activation(data=net, name='student_relu1', act_type="relu") student_mean = mx.symbol.FullyConnected(data=net, name='student_mean', num_hidden=1) student_var = mx.symbol.FullyConnected(data=net, name='student_var', num_hidden=1) net = mx.symbol.Group([student_mean, student_var]) return net def dev(): return mx.gpu() def run_mnist_SGD(training_num=50000): X, Y, X_test, Y_test = load_mnist(training_num) minibatch_size = 100 net = get_mnist_sym() data_shape = (minibatch_size,) + X.shape[1::] data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'softmax_label': nd.zeros((minibatch_size,), ctx=dev())} initializer = mx.init.Xavier(factor_type="in", magnitude=2.34) exe, exe_params, _ = SGD(sym=net, dev=dev(), data_inputs=data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=1000000, initializer=initializer, lr=5E-6, prior_precision=1.0, minibatch_size=100) def run_mnist_SGLD(training_num=50000): X, Y, X_test, Y_test = load_mnist(training_num) minibatch_size = 100 net = get_mnist_sym() data_shape = (minibatch_size,) + X.shape[1::] data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'softmax_label': nd.zeros((minibatch_size,), ctx=dev())} initializer = mx.init.Xavier(factor_type="in", magnitude=2.34) exe, sample_pool = SGLD(sym=net, dev=dev(), data_inputs=data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=1000000, initializer=initializer, learning_rate=4E-6, prior_precision=1.0, minibatch_size=100, thin_interval=100, burn_in_iter_num=1000) def run_mnist_DistilledSGLD(training_num=50000): X, Y, X_test, Y_test = load_mnist(training_num) minibatch_size = 100 if training_num >= 10000: num_hidden = 800 total_iter_num = 1000000 teacher_learning_rate = 1E-6 student_learning_rate = 0.0001 teacher_prior = 1 student_prior = 0.1 perturb_deviation = 0.1 else: num_hidden = 400 total_iter_num = 20000 teacher_learning_rate = 4E-5 student_learning_rate = 0.0001 teacher_prior = 1 student_prior = 0.1 perturb_deviation = 0.001 teacher_net = get_mnist_sym(num_hidden=num_hidden) logsoftmax = LogSoftmax() student_net = get_mnist_sym(output_op=logsoftmax, num_hidden=num_hidden) data_shape = (minibatch_size,) + X.shape[1::] teacher_data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'softmax_label': nd.zeros((minibatch_size,), ctx=dev())} student_data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'softmax_label': nd.zeros((minibatch_size, 10), ctx=dev())} teacher_initializer = BiasXavier(factor_type="in", magnitude=1) student_initializer = BiasXavier(factor_type="in", magnitude=1) student_exe, student_params, _ = \ DistilledSGLD(teacher_sym=teacher_net, student_sym=student_net, teacher_data_inputs=teacher_data_inputs, student_data_inputs=student_data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=total_iter_num, student_initializer=student_initializer, teacher_initializer=teacher_initializer, student_optimizing_algorithm="adam", teacher_learning_rate=teacher_learning_rate, student_learning_rate=student_learning_rate, teacher_prior_precision=teacher_prior, student_prior_precision=student_prior, perturb_deviation=perturb_deviation, minibatch_size=100, dev=dev()) def run_toy_SGLD(): X, Y, X_test, Y_test = load_toy() minibatch_size = 1 teacher_noise_precision = 1.0 / 9.0 net = get_toy_sym(True, teacher_noise_precision) data_shape = (minibatch_size,) + X.shape[1::] data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev())} initializer = mx.init.Uniform(0.07) exe, params, _ = \ SGLD(sym=net, data_inputs=data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=50000, initializer=initializer, learning_rate=1E-4, # lr_scheduler=mx.lr_scheduler.FactorScheduler(100000, 0.5), prior_precision=0.1, burn_in_iter_num=1000, thin_interval=10, task='regression', minibatch_size=minibatch_size, dev=dev()) def run_toy_DistilledSGLD(): X, Y, X_test, Y_test = load_toy() minibatch_size = 1 teacher_noise_precision = 1.0 teacher_net = get_toy_sym(True, teacher_noise_precision) student_net = get_toy_sym(False) data_shape = (minibatch_size,) + X.shape[1::] teacher_data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev())} student_data_inputs = {'data': nd.zeros(data_shape, ctx=dev())} # 'softmax_label': nd.zeros((minibatch_size, 10), ctx=dev())} teacher_initializer = mx.init.Uniform(0.07) student_initializer = mx.init.Uniform(0.07) student_grad_f = lambda student_outputs, teacher_pred: \ regression_student_grad(student_outputs, teacher_pred, teacher_noise_precision) student_exe, student_params, _ = \ DistilledSGLD(teacher_sym=teacher_net, student_sym=student_net, teacher_data_inputs=teacher_data_inputs, student_data_inputs=student_data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=80000, teacher_initializer=teacher_initializer, student_initializer=student_initializer, teacher_learning_rate=1E-4, student_learning_rate=0.01, # teacher_lr_scheduler=mx.lr_scheduler.FactorScheduler(100000, 0.5), student_lr_scheduler=mx.lr_scheduler.FactorScheduler(8000, 0.8), student_grad_f=student_grad_f, teacher_prior_precision=0.1, student_prior_precision=0.001, perturb_deviation=0.1, minibatch_size=minibatch_size, task='regression', dev=dev()) def run_toy_HMC(): X, Y, X_test, Y_test = load_toy() minibatch_size = Y.shape[0] noise_precision = 1 / 9.0 net = get_toy_sym(True, noise_precision) data_shape = (minibatch_size,) + X.shape[1::] data_inputs = {'data': nd.zeros(data_shape, ctx=dev()), 'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev())} initializer = mx.init.Uniform(0.07) sample_pool = HMC(net, data_inputs=data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test, sample_num=300000, initializer=initializer, prior_precision=1.0, learning_rate=1E-3, L=10, dev=dev()) def run_synthetic_SGLD(): theta1 = 0 theta2 = 1 sigma1 = numpy.sqrt(10) sigma2 = 1 sigmax = numpy.sqrt(2) X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100) minibatch_size = 1 total_iter_num = 1000000 lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num, factor=0.55) optimizer = mx.optimizer.create('sgld', learning_rate=None, rescale_grad=1.0, lr_scheduler=lr_scheduler, wd=0) updater = mx.optimizer.get_updater(optimizer) theta = mx.random.normal(0, 1, (2,), mx.cpu()) grad = nd.empty((2,), mx.cpu()) samples = numpy.zeros((2, total_iter_num)) start = time.time() for i in xrange(total_iter_num): if (i + 1) % 100000 == 0: end = time.time() print("Iter:%d, Time spent: %f" % (i + 1, end - start)) start = time.time() ind = numpy.random.randint(0, X.shape[0]) synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad= X.shape[0] / float(minibatch_size), grad=grad) updater('theta', grad, theta) samples[:, i] = theta.asnumpy() plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet) plt.colorbar() plt.show() if __name__ == '__main__': numpy.random.seed(100) mx.random.seed(100) parser = argparse.ArgumentParser( description="Examples in the paper [NIPS2015]Bayesian Dark Knowledge and " "[ICML2011]Bayesian Learning via Stochastic Gradient Langevin Dynamics") parser.add_argument("-d", "--dataset", type=int, default=1, help="Dataset to use. 0 --> TOY, 1 --> MNIST, 2 --> Synthetic Data in " "the SGLD paper") parser.add_argument("-l", "--algorithm", type=int, default=2, help="Type of algorithm to use. 0 --> SGD, 1 --> SGLD, other-->DistilledSGLD") parser.add_argument("-t", "--training", type=int, default=50000, help="Number of training samples") args = parser.parse_args() training_num = args.training if args.dataset == 1: if 0 == args.algorithm: run_mnist_SGD(training_num) elif 1 == args.algorithm: run_mnist_SGLD(training_num) else: run_mnist_DistilledSGLD(training_num) elif args.dataset == 0: if 1 == args.algorithm: run_toy_SGLD() elif 2 == args.algorithm: run_toy_DistilledSGLD() elif 3 == args.algorithm: run_toy_HMC() else: run_synthetic_SGLD()
apache-2.0