Best Python code snippet using green
naive_bayes.py
Source: naive_bayes.py
1import numpy as np2"""Data: 57 features, 4601 data points"""3yes = []; no = []; outcome = []4ratio = 0.85#Reading the data from data.txt:6with open('data.txt', 'r') as data:7 for line in data.readlines():8 #train_cat.append(map(lambda x: np.float64(x), line.split(',')))9 point = (map(lambda x: np.float64(x), line.split(' ')))10 outcome.append(point[-1])11 if point[-1] == 1:12 yes.append(point[:len(point)-1])13 else:14 no.append(point[:len(point)-1])15"""print(0, int(round(ratio*len(yes), 0))-1)16print(int(round(ratio*len(yes), 0)), int(round(len(yes), 0))-1)17print(len(yes), int(round(len(yes) + ratio*len(no), 0))-1)18print(int(round(len(yes) + ratio*len(no), 0)), int(round(len(yes) + len(no), 0))-1)"""19#Indices:20ind1 = 021ind2 = int(round(ratio*len(yes), 0))22ind3 = int(round(len(yes), 0))23ind4 = int(round(len(yes) + ratio*len(no), 0))24ind5 = int(round(len(yes) + len(no), 0))25train_yes = yes[:ind2]; train_yes = np.transpose(train_yes)26train_no = no [:int(round(ratio*len(no),0))]; train_no = np.transpose(train_no)27outcome_test = outcome[ind2:ind3] + outcome[ind4:ind5]28test_yes = yes[ind2:]; test_no = no [int(round(ratio*len(no),0)):]29"""train_yes = yes[:int(round(.8*len(yes), 0))]; train_yes = np.transpose(train_yes)30train_no = no [:int(round(.8*len(no ), 0))]; train_no = np.transpose(train_no )31outcome_test = outcome[1450:1813] + outcome[4043:4601]32test_yes = yes[int(round(.8*len(yes), 0)):] ; test_no = no [int(round(.8*len(no ), 0)):]"""33print("len(yes) = {}, len(train_yes) = {}, len(test_yes) = {}\nlen(no ) = {}, len(train_no ) = {}, len(test_no ) = {}".format(len(yes), np.shape(train_yes)[1], len(test_yes), len(no), np.shape(train_no)[1], len(test_no)))34mean_yes = np.mean(train_yes, 1); mean_no = np.mean(train_no, 1)35cov_yes = np.cov(train_yes); cov_no = np.cov(train_no)36Y = test_yes + test_no37#print("Y.shape = {}x{}".format(len(Y), len(Y[0])))38output = []39p_yes = float(len(yes))/(len(yes) + len(no)); p_no = float(len(no ))/(len(yes) + len(no))40term1 = -1*(len(Y[0])/2) * np.log10(2*np.pi)41term2_yes = -0.5* np.log10(np.linalg.det(cov_yes)); term12_yes = term1 + term2_yes42term2_no = -0.5* np.log10(np.linalg.det(cov_no)) ; term12_no = term1 + term2_no43cov_inv_yes = np.linalg.inv(cov_yes)44cov_inv_no = np.linalg.inv(cov_no )45for point in Y:46 term3_yes = np.matmul(np.transpose(point - mean_yes), cov_inv_yes)47 term3_yes = -0.5 * np.matmul(term3_yes, point - mean_yes) + p_yes48 term3_no = np.matmul(np.transpose(point - mean_no ), cov_inv_no )49 term3_no = -0.5 * np.matmul(term3_no, point - mean_no ) + p_no50 if (term12_yes + term3_yes >= term12_no + term3_no):51 output.append(1)52 else:53 output.append(0)54wrong_spam = 0; wrong_non_spam = 055for i in range(len(output)):56 if outcome_test[i] == 1 and output[i] == 0:57 wrong_non_spam += 158 if outcome_test[i] == 0 and output[i] == 1:59 wrong_spam += 160error = np.float64(wrong_spam + wrong_non_spam)61print("Result: With training:testing ratio of {}, accuracy = 1 - {}/{} = {}".format(ratio, error, len(Y), 1-(error/len(Y))))62print("Fraction of non-spam incorrectly detected as spam = {}, fraction of spam incorrectly detected as non-spam = {}".format(np.float64(wrong_spam)/len(test_no), np.float64(wrong_non_spam)/len(test_yes)))63"""With ration = 0.8, 64len(yes) = 1813, len(train_yes) = 1450, len(test_yes) = 36365len(no ) = 2788, len(train_no ) = 2230, len(test_no ) = 55866cov_yes.shape = (57, 57)67Y.shape = 921x5768p_yes = 0.394044772875, p_no = 0.605955227125...
test_env.py
Source: test_env.py
1# -*- coding: utf-8 -*-2# Copyright (C) Duncan Macleod (2018-2020)3#4# This file is part of GWpy.5#6# GWpy is free software: you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation, either version 3 of the License, or9# (at your option) any later version.10#11# GWpy is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with GWpy. If not, see <http://www.gnu.org/licenses/>.18"""Tests for :mod:`gwpy.utils.env`19"""20from unittest import mock21import pytest22from .. import env as utils_env23BOOL_TRUE = {24 'TEST_y': 'y',25 'TEST_Y': 'Y',26 'TEST_yes': 'yes',27 'TEST_Yes': 'Yes',28 'TEST_YES': 'YES',29 'TEST_ONE': '1',30 'TEST_true': 'true',31 'TEST_True': 'True',32 'TEST_TRUE': 'TRUE',33}34BOOL_FALSE = {35 'TEST_no': 'no',36 'TEST_No': 'No',37 'TEST_ZERO': '0',38 'TEST_false': 'false',39 'TEST_False': 'False',40 'TEST_FALSE': 'FALSE',41 'TEST_OTHER': 'blah',42}43BOOL_ENV = BOOL_TRUE.copy()44BOOL_ENV.update(BOOL_FALSE)45@mock.patch.dict('os.environ', values=BOOL_ENV)46@pytest.mark.parametrize(47 'env, result',48 [(k, True) for k in sorted(BOOL_TRUE)]49 + [(k, False) for k in sorted(BOOL_FALSE)],50)51def test_bool_env(env, result):52 """Test :meth:`gwpy.utils.env.bool_env` _without_ the `default` keyword53 """54 assert utils_env.bool_env(env) is result55@mock.patch.dict('os.environ', values=BOOL_TRUE)56@pytest.mark.parametrize('env, default, result', [57 ('TEST_YES', False, True),58 ('TEST_MISSING', True, True),59])60def test_bool_env_default(env, default, result):61 """Test :meth:`gwpy.utils.env.bool_env` _with_ the `default` keyword62 """...
Check out the latest blogs from LambdaTest on this topic:
It is essential for a team, when speaking about test automation, to take the time needed to think, analyze and try what will be the best tool, framework, and language that suits your team’s needs.
In this digital era, Continuous Integration and Continuous Deployment is closely aligned with software development and agile methodologies. Organizations deploy latest versions of software products every minute to ensure maximum competitive edge.
Test Coverage and Code coverage are the most popular methodologies for measuring the effectiveness of the code. Though these terms are sometimes used interchangeably since their underlying principles are the same. But they are not as similar as you may think. Many times, I have noticed the testing team and development team being confused over the use of these two terminologies. Which is why I thought of coming up with an article to talk about the differences between code coverage and test coverage in detail.
When someone develops a website, going live it’s like a dream come true. I have also seen one of my friends so excited as he was just about to launch his website. When he finally hit the green button, some unusual trend came suddenly into his notice. After going into details, he found out that the website has a very high bounce rate on Mobile devices. Thanks to Google Analytics, he was able to figure that out.
With an average global salary of $39k, PHP is one of the most popular programming languages in the developer community. It’s the language behind the most popular CMS, WordPress. It is in-use by 79% of total websites globally, including the most used social network- Facebook, the largest digital encyclopedia – Wikipedia, China’s news giant Xinhuanet, and Russia’s social network VK.com.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!