JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
3 months ago0 views
👨‍💻Programming

Python

import cv2
import numpy as np
eye_detection= cv2.CascadeClassifier
('haarcascade_eye.xml')
img = cv2.imread('girl.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
eyes = eye_detection.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in eyes:
img = cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0),3)
cv2.imshow('girl.jpg',img)




Program=1. Bag of words

from sklearn.feature_extraction.text import CountVectorizer

Sentences=['We are using the Bag of Word model', 'Bag of

Wordused for extracting the features.']

vectorizer = CountVectorizer()

features_text = vectorizer.fit_transform(Sentences).todense()

print(vectorizer.vocabulary_)


Program =2 Binarization

import numpy as np
from sklearn import preprocessing
input_data = np.array([[2.1,-1.9,5.5],[-1.5,2.4,3.5],[0.5,- 7.9,5.6],[5.9,2.3,-5.8]])
data_binarized=preprocessing.Binarizer(threshold=0.5).transform(i
nput_data)
print("binarized data\n",data_binarized)



Program =3. Chunking Down

import nltk

sentence = [("a", "DT"),("clever", "JJ"),("fox","NN"),("was","VBP"),("jumping","VBP"),("over","IN"),("the","DT"),(

"wall","NN")]

grammar = "NP:{<DT>?<JJ>*<NN>}"

parser_chunking=nltk.RegexpParser(grammar)

parser_chunking.parse(sentence)

Output_chunk=parser_chunking.parse(sentence)

Output_chunk.draw()

Program =4. K - Means Clustering.

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans
from sklearn.datasets.samples_generator import make_blobs
x,y_true = make_Blobs(n_samples=500,centers=4,cluster_std=0.40,random_state=0)
plt.scatter(X[:,0],X[:,1],s=50);
plt.show()


Program=5. Lemmatization

#print("working:",lemmatizer.lemmatize("working"))
import spacy
# Load the spaCy English model
nlp = spacy.load('en_core_web_sm')
# Define a sample text
text = "The quick brown foxes are jumping over the lazy dogs."# Process the text using spaCy
doc = nlp(text)
# Extract lemmatized tokens
lemmatized_tokens = [token.lemma_ for token in doc]
# Join the lemmatized tokens into a sentence
lemmatized_text = ' '.join(lemmatized_tokens)
# Print the original and lemmatized text
print("Original Text:", text)
print("Lemmatized Text:", lemmatized_text)


Program=6. Mean Removal

import numpy as np
from sklearn import preprocessing
input_data = np.array([[2.1,-1.9,5.5], [-1.5,2.4,3.5], [0.5,-7.9,5.6], [5.9,2.3,-5.8]])
print("mean=",input_data.mean(axis=0))
print("standard deviation=",input_data.std(axis=0))


Program =7. Linear Regression

import numpy as np
import matplotlib.pyplot as plt
def estimate_coefficients(x, y):
# size of the dataset OR number of observations/points
n = np.size(x)
# mean of x and y
# Since we are using numpy just calling mean on numpyis
sufficient
mean_x, mean_y = np.mean(x), np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x - n*mean_y*mean_x)
SS_xx = np.sum(x*x - n*mean_x*mean_x)
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = mean_y - b_1*mean_x
return(b_0, b_1)
# x,y are the location of points on graph
# color of the points change it to red blue orange play arounddef plot_regression_line(x, y, b):
# plotting the points as per dataset on a graph
plt.scatter(x, y, color = "m",marker = "o", s = 30)
# predicted response vector
y_pred = b[0] + b[1]*x
# plotting the regression line
plt.plot(x, y_pred, color = "g")
# putting labels for x and y axis
plt.xlabel('Size')
plt.ylabel('Cost')
# function to show plotted graph
plt.show()
def main():
# Datasets which we create
x = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([300, 350, 500, 700, 800, 850, 900, 900, 1000, 1200])
# estimating coefficients
b = estimate_coefficients(x, y)
print("Estimated coefficients:\nb_0 = {} \nb_1 = {}".format(b[0], b[1]))
# plotting regression line
plot_regression_line(x, y, b)
if __name__ == "__main__":
main()


Program =8. Logistic Regression

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data[:, :2]
y = (iris.target != 0) * 1
plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1')
plt.legend();
class LogisticRegression:
def __init__(self, lr=0.01, num_iter=100000, fit_intercept=True, verbose=False):
self.lr = lr
self.num_iter = num_iter
self.fit_intercept = fit_intercept
self.verbose = verbose
def __add_intercept(self, X):
intercept = np.ones((X.shape[0], 1))
return np.concatenate((intercept, X), axis=1)
def __sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def __loss(self, h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
def fit(self, X, y):
if self.fit_intercept:
X = self.__add_intercept(X)
# weights initialization
self.theta = np.zeros(X.shape[1])
for i in range(self.num_iter):
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
gradient = np.dot(X.T, (h - y)) / y.size
self.theta -= self.lr * gradient
z = np.dot(X, self.theta)
h = self.__sigmoid(z)
loss = self.__loss(h, y)
if(self.verbose ==True and i % 10000 == 0):
print(f'loss: {loss} \t')
def predict_prob(self, X):
if self.fit_intercept:
X = self.__add_intercept(X)
return self.__sigmoid(np.dot(X, self.theta))
def predict(self, X):
return self.predict_prob(X).round()
model = LogisticRegression(lr=0.1, num_iter=300000)
model.fit(X, y)
preds = model.predict(X)
(preds == y).mean()
plt.figure(figsize=(10, 6))
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0')
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1')
plt.legend()
x1_min, x1_max = X[:,0].min(), X[:,0].max(), x2_min, x2_max = X[:,1].min(), X[:,1].max(), xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
grid = np.c_[xx1.ravel(), xx2.ravel()]
probs = model.predict_prob(grid).reshape(xx1.shape)
plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black')
plt.show()


Program =9. Meanshift

import numpy as np
from sklearn.cluster import MeanShift
import matplotlib.pyplot as plt
from matplotlib import style
from sklearn.datasets.samples_generator import make_blobs
style.use("ggplot")
centers = [[2,2] , [4,5] , [3,10]]
X,_=make_blobs(n_samples = 500 , centers=centers,cluster_std=1)plt.scatter(X[:,0],X[:,1])
plt.show()


Program =10. Stemming

import nltk
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
word = "running"
stem = stemmer.stem(word)
print(f"Stemmed word: {stem}")


Program =11. Stopword_Removal

import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
example_sent = """This is a sample sentence, showing off the stop words filtration."""
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
# converts the words in word_tokens to lower case and thencheckswhether
#they are present in stop_words or not
filtered_sentence = [w for w in word_tokens if not w.lower() instop_words]
#with no lower case conversion
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)


Program =12.Stopwords

import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
print(stopwords.words('english'))
whitespace_tokenize
from nltk.tokenize import WhitespaceTokenizer
# Create a reference variable for Class WhitespaceTokenizer
tk = WhitespaceTokenizer()
# Create a string input
gfg = "GeeksforGeeks \nis\t for geeks"
print (gfg)
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)


Program =13. Word_Tokenize1

import nltk

nltk.download('punkt')

nltk.download('punkt_tab')

nltk.download('wordnet')

nltk.download('omw-1.4')

from nltk.tokenize import word_tokenize

text = "natural language process with python is best"

tokens = word_tokenize(text)

print(tokens)

Program =14.

Wordcount Using TextBlob

from textblob import TextBlob

gfg = TextBlob("I am confused sometime, sometime i amhappy.")# using TextBlob.word_counts() method

gfg = gfg.word_counts['sometime']

print(gfg)

Program =15. Whitespace_Tokenize

from nltk.tokenize import WhitespaceTokenizer
# Create a reference variable for Class WhitespaceTokenizer
tk = WhitespaceTokenizer()
# Create a string input
gfg = "GeeksforGeeks \nis\t for geeks"
print (gfg)
# Use tokenize method
geek = tk.tokenize(gfg)
print(geek)


Program=16. Scaling

import numpy as np

from sklearn import preprocessing

input_data = np.array([[2.1,-1.9,5.5], [-1.5,2.4,3.5], [0.5,-7.9,5.6], [5.9,2.3,-5.8]])

data_scaler_minmax=preprocessing.MinMaxScaler(feature_range=(0,1))

data_scaled_minmax=data_scaler_minmax.fit_transform(input_dat

a)

print("\n min max scaled data:\n",data_scaled_minmax)

Program=17. Noun_Phrases

from textblob import TextBlob
gfg = TextBlob("I am reading a blog post on Analytics. I amloving it!")
# using TextBlob.noun_phrases method
gfg = gfg.noun_phrases
print(gfg)

← Back to timeline