Cannot import xgboost in Jupyter notebook
October 16, 2024
Jupyter
Table of Content
Getting this simple problem while importing Xgboost on Jupyter notebook
Sometimes when we try to import xgboost in Jupyter notebook it does not work and throws error. This page contains the step taken to solve the issue successfully.
Issue: Cannot import xgboost in Jupyter notebook
In this case it was a Jupyter note book that was installed locally on Mac.
Error: Following error seen in Jupyter notebook
XGBoost Library ({libname}) could not be loaded.
225 Likely causes:
226 * OpenMP runtime is not installed
227 - vcomp140.dll or libgomp-1.dll for Windows
228 - libomp.dylib for Mac OSX
229 - libgomp.so for Linux and other UNIX-like OSes
230 Mac OSX users: Run `brew install libomp` to install OpenMP runtime.
231
232 * You are running 32-bit Python on a 64-bit OS
Solution
Install libomp
Go to the terminal and install libomp with following command
brew install libomp
Summary
After installing libomp the import of Xgboost worked in Jupyter notebook. Please note that if we dont have brew installed we need to install brew first.
Table of Content
npm and npx
Both npm and npx are tools that come with Node.js, but they serve different purposes.
npm
Full Form: Node Package Manager
Purpose: npm is used for managing packages in Node.js projects. It helps you install, update, and
npm Commands
- remove packages, as well as manage project dependencies.:
- npm install
: Installs a package. - npm uninstall
: Removes a package. - npm update: Updates all packages to the latest versions.
- npm list: Lists installed packages and their versions.
npx
Full Form: Node Package Executor
Purpose: npx is used to execute binaries from Node modules or packages without needing to install them globally. It's especially handy for running CLI tools that are part of your project or that you don't want to install globally.
npx Commands
- npx
: Runs a command from a package. For example, npx create-react-app my-app will run the create-react-app command without needing to install it globally.
Example Scenario
If you want to start a new React project, you could use:
This command runs create-react-app without having it installed globally.
npx create-react-app my-app
If you wanted to install create-react-app globally for repeated use, you would use:
Bellow code uses npm to install create-react-app globally
npm install -g create-react-app
Summary
To run Node.js packages without having to install them globally, npx is very helpful. We can use it to run command-line interface (CLI) tools, run scripts, and carry out other operations.
How to find hamming weight in java
May 08, 2024
Hammingweight
Table of Content
How to find hamming weight in Java
hamming weight for a number is the count of bits that are non zero. For instance for 1001 hamming weight is 2. For 100001111 hamming weight is 5. In this article we will see how to find hamming weight efficiently.
Using Simple divide and reminder
Here we are divide the number by 2 and until it becomes 0 and each step we check if the intermediate gives reminder 1 while dividing by 2.
public static int hammingWeight(int n) {
int count = 0;
while (n != 0) {
if (n % 2 == 1) {
count++;
}
n = n / 2;
}
return count;
}
Using Bit marking
In this example we are using Bit masking. Since the input is an Integer and it contains 32 bits. We do a & (bit wise and) operation for each of its digits.
public static int hammingWeightII(int n) {
int count = 0;
int mask = 1;
for (int i = 0; i <= 31; i++) {
count += (n & mask) == 0 ? 0 : 1;
//expand the mask
mask = mask << 1;
}
return count;
}
Full Example
package ic.binary;
public class NumberOf1s {
public static void main(String[] args) {
int n = 5;
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
n = 8;
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
System.out.println("Number of 1 bits for :" + n + " -> " + NumberOf1s.hammingWeight(n));
}
public static int hammingWeight(int n) {
int count = 0;
while (n != 0) {
if (n % 2 == 1) {
count++;
}
n = n / 2;
}
return count;
}
public static int hammingWeightII(int n) {
int count = 0;
int mask = 1;
for (int i = 0; i <= 31; i++) {
count += (n & mask) == 0 ? 0 : 1;
mask = mask << 1;
}
return count;
}
}
Output of above program
Number of 1 bits for :5 -> 2 Number of 1 bits for :5 -> 2 Number of 1 bits for :8 -> 1 Number of 1 bits for :8 -> 1
</
Table of Content
Java OptionalInt example
OptionalInt allows us to create an object which may or may not contain a int value. If a value is present, isPresent() will return true and getAsInt() will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse().
Other classes similar to OptionalInt are OptionalFloat, OptionalDouble, Optional. These can help us eliminate exceptions that occur due to the absence of a value at runtime. Basically we need to first check if the Optional is carrying any value then only try to get value.
In this example we are returning an OptionalInt from Stream created from integer array and finally returning the sum using reduce method. If the Value is present then only we are trying to pring the value by calling result.getAsint()
package javaexp.blogspot.stream;
import java.util.Arrays;
import java.util.OptionalInt;
public class OptionalIntExample {
public static void main(String[] args) {
int iarray[] = {9, 10, 11, 12, 15, 15, 25};
OptionalInt result = Arrays.stream(iarray).reduce((left, right) ->left );
if (result.isPresent() ) {
System.out.println("Sum of Array " + result.getAsInt());
}
}
}
Summary
OptionalInt and other respective Optional classes helping in protecting from Nullpointer exception when we try to get value (say integer value) from and Integer object which is null.
Bucket sort implementation in Java
May 05, 2024
Sorting
Table of Content
Bucket Sort in Java
In this article we will go though a simple implementation of Bucket sort in Java
What is Bucket sort
Bucket sort is a sorting algorithm that divides the inputs into several buckets. Once the buckets are populated with input data, then all these buckets are sorted individually using a different sorting mechanism. After individual buckets are sorted they are contaminated together and returned as final result.
Following is the Pseudocode for Bucket sort