11import pytest
2- import pyjs
32
43
5- def test_numpy_testing ():
6- import numpy .testing as npt
7-
8-
9- # @pytest.mark.skip(reason="failing since scipy build is broken atm")
10- def test_scikit_learn ():
11- print ("1.1" )
4+ def test_logistic_regression ():
125 import numpy as np
6+ from numpy .testing import assert_allclose
137 import sklearn
148 from sklearn .linear_model import LogisticRegression
15- print ("1.2" )
169 rng = np .random .RandomState (42 )
1710 X = rng .rand (100 , 20 )
1811 y = rng .randint (5 , size = 100 )
19- print ("1.3" )
2012 estimator = LogisticRegression (solver = 'newton-cholesky' )
21- print ("1.4" )
2213 estimator .fit (X , y )
23- print (estimator .predict (X ))
24- estimator .score (X , y )
25- print ("1.5" )
14+ p = estimator .predict (X )
15+ assert_allclose (p , [
16+ 0 , 2 , 1 , 1 , 1 , 1 , 4 , 3 , 4 , 4 , 0 , 1 , 3 , 1 , 1 , 0 , 0 , 4 , 4 , 1 , 0 , 3 , 4 , 1 , 0 , 4 , 1 , 3 , 3 ,
17+ 0 , 1 , 1 , 4 , 1 , 0 , 1 , 1 , 1 , 4 , 1 , 2 , 0 , 4 , 0 , 0 , 4 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 3 ,
18+ 1 , 1 , 2 , 3 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 4 , 1 , 0 , 1 , 1 , 1 , 4 , 0 , 1 , 4 , 1 , 1 , 3 , 1 , 0 , 4 , 3 , 1 ,
19+ 0 , 1 , 1 , 3 , 3 , 0 , 0 , 1 , 1 , 3 , 1 , 0 , 1
20+ ])
21+ s = estimator .score (X , y )
22+ assert_allclose (s , 0.57 )
2623
27- # @pytest.mark.skip(reason="failing since scipy build is broken atm")
28- def test_logistic_regression ():
29- print ("2.1" )
30- from sklearn .datasets import load_iris
31- print ("2.2" )
32- from sklearn .linear_model import LogisticRegression
33- print ("2.3" )
34- X , y = load_iris (return_X_y = True )
35- print ("2.4" )
36- clf = LogisticRegression (random_state = 0 , max_iter = 1000 ).fit (X , y )
37- print (clf .predict (X [:2 , :]))
38- print ("2.5" )
39- print (clf .predict_proba (X [:2 , :]))
40- print ("2.6" )
41- print (clf .score (X , y ))
42-
43-
44-
45- # is_browser_worker = pyjs.js.Function('return typeof importScripts === "function"')()
46- # skip_non_worker = pytest.mark.skipif(
47- # not is_browser_worker,
48- # reason="requires browser-worker, not node",
49- # )
50-
51- # @pytest.mark.skip(reason="failing since scipy build is broken atm")
52- def test_dl ():
24+
25+ def test_fetch ():
5326 from sklearn import datasets
54- iris = datasets .fetch_california_housing ()
27+ ds = datasets .fetch_california_housing ()
28+ assert ds ['data' ].shape == (20640 , 8 )
29+ assert ds ['target' ].shape == (20640 ,)
30+
31+
32+ def test_pairwise_distance ():
33+ from numpy .testing import assert_allclose
34+ from sklearn .metrics .pairwise import pairwise_distances
35+ x = [[0 , 0 , 0 ], [1 , 1 , 1 ]]
36+ y = [[1 , 0 , 0 ], [1 , 1 , 0 ]]
37+ d = pairwise_distances (x , y , metric = 'sqeuclidean' )
38+ assert_allclose (d , [[1.0 , 2.0 ], [2.0 , 1.0 ]])
39+
40+
41+ def test_factor_analysis ():
42+ from numpy .testing import assert_allclose
43+ from sklearn .datasets import load_iris
44+ from sklearn .decomposition import PCA , FactorAnalysis
45+ from sklearn .preprocessing import StandardScaler
46+
47+ data = load_iris ()
48+ X = StandardScaler ().fit_transform (data ["data" ])
49+ feature_names = data ["feature_names" ]
50+
51+ n_components = 2
52+ pca = PCA (n_components ).fit (X )
53+ fa_unrotated = FactorAnalysis (n_components ).fit (X )
54+ fa_rotated = FactorAnalysis (n_components , rotation = "varimax" ).fit (X )
55+
56+ assert_allclose (pca .components_ , [
57+ [0.52106591 , - 0.26934744 , 0.580413096 , 0.564856546 ],
58+ [0.3774176 , 0.92329566 , 0.024491609 , 0.066941987 ]
59+ ])
60+ assert_allclose (fa_unrotated .components_ , [
61+ [0.880960087 , - 0.416916045 , 0.99918858 , 0.96228895 ],
62+ [- 0.447286896 , - 0.55390036 , 0.019152833 , 0.058402058 ]
63+ ])
64+ assert_allclose (fa_rotated .components_ , [
65+ [0.98633022 , - 0.16052385 , 0.90809432 , 0.85857475 ],
66+ [- 0.057523335 , - 0.67443065 , 0.41726413 , 0.43847489 ]
67+ ])
0 commit comments