Python
-> face deteaction
import cv2
import numpy as np
face_detection = cv2.CascadeClassifier('face.xml')
img = cv2.imread('girl.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detection.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 3)
cv2.imshow('Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
-> eye detection
import cv2
import numpy as np
eye_detection = cv2.CascadeClassifier('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('Eye Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
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)
Copy
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()
Copy
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)
Copy
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))
Copy
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()
Copy
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()
Copy
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()
Copy
Program =10. Stemming
import nltk
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
word = "running"
stem = stemmer.stem(word)
print(f"Stemmed word: {stem}")
Copy
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)
Copy
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)
Copy
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)
Copy
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)
xml db creation and table creation
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Database Example"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Database"
android:onClick="createDb"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="40dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
kotlin file
package com.example.myapplicationdbc
import android.database.sqlite.SQLiteDatabase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.Toast
import java.io.File
class MainActivity : AppCompatActivity() {
var myFile: File? = null
var db: SQLiteDatabase? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun createDb(view: View) {
val folder = File(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).toString()+"/Database")
if(!folder.exists()){
folder.mkdir()
}
else{
Toast.makeText(this,folder.absolutePath,Toast.LENGTH_LONG).show()
}
myFile = File(folder,"test.db")
db = SQLiteDatabase.openOrCreateDatabase(myFile!!.absolutePath,null,null)
CreateTable()
}
fun CreateTable(){
val create_table = "CREATE TABLE IF NOT EXISTS stud(sno INTEGER,name TEXT)"
db!!.execSQL(create_table)
Toast.makeText(this,"Database and Table Created",Toast.LENGTH_LONG).show()
}
}
image rotation 1
xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ring"
android:layout_centerVertical="true"/>
</RelativeLayout>
kotlin
package com.example.myapplicationimgr
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image = findViewById<ImageView>(R.id.img)
// Move image automatically
image.animate()
.translationX(500f) // move right
.setDuration(3000) // 3 seconds
.start()
}
}
image rotation 2 - 90 deg.
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_foreground" />
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Image"
android:layout_marginTop="20dp"/>
</LinearLayout>
kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var a = 0f
// private vart angel means this variable can use only in class not other side..
Of meand zero starting degree and f means float.//
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val b = findViewById<ImageView>(R.id.img)
val c = findViewById<Button>(R.id.btn)
c.setOnClickListener {
a += 90f
b.animate().rotation(a).setDuration(500)
}
}
}
img rotation - 3 : Image view - I enter degree in edit text and click button and then image rotate at that degree …
Xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">
<EditText
android:id="@+id/etAngle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Enter angle"
android:inputType="number" />
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_foreground"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Image"
android:layout_marginTop="20dp"/>
</LinearLayout>
Koltin file
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<EditText>(R.id.etAngle)
val imageView = findViewById<ImageView>(R.id.img)
val button = findViewById<Button>(R.id.btnRotate)
button.setOnClickListener {
val angleText = editText.text.toString()
if (angleText.isNotEmpty()) {
val angle = angleText.toFloat()
imageView.animate()
.rotation(angle)
.setDuration(500)
} else {
Toast.makeText(this, "Please enter angle", Toast.LENGTH_SHORT).show()
}
}
}
}