Machine Learning
Machine Learning
Sr.no Program
1 Write a python program to Prepare Scatter Plot (Use Forge Dataset / Iris
Dataset)
2 Write a python program to find all null values in a given data set and
remove them.
3 Write a python program the Categorical values in numeric format for a
given dataset.
4 Write a python program to implement simple Linear Regression for
predicting house price.
5 Write a python program to implement multiple Linear Regression for a
given dataset.
6 Write a python program to implement Polynomial Regression for given
dataset.
7 Write a python program to Implement Naïve Bayes.
# plot data
plt.scatter(X[:50, 0], X[:50, 1],
color='blue', marker='o', label='Setosa')
plt.scatter(X[50:100, 0], X[50:100, 1],
color='green', marker='s', label='Versicolor')
# plt.savefig('images/02_06.png', dpi=300)
plt.show()
Output:
2. Write a python program to find all null values in a given data set and
remove them.
import pandas as pd
df = pd.read_csv('data.csv')
null_mask = df.isnull()
null_columns = df.columns[null_mask.any()]
df = df.dropna(axis=0, subset=null_columns)
df.to_csv('clean_data.csv', index=False)
Output:
3. Write a python program the Categorical values in numeric format for a
given dataset.
import pandas as pd
encoder = LabelEncoder()
encoder.fit(dataframe[column_name])
dataframe[column_name] = encoder.transform(dataframe[column_name])
# Example usage
df = pd.DataFrame({'col1': ['cat', 'dog', 'bird', 'cat', 'dog', 'bird'], 'col2': [1, 2, 3, 4, 5, 6]})
convert_categorical_to_numeric(df, 'col1')
print(df)
Output:
col1 col2
0 1 1
1 2 2
2 0 3
3 1 4
4 2 5
5 0 6
4. Write a python program to implement simple Linear Regression for
predicting house price.
import numpy as np
# Assume that we have a dataset with two columns: 'area' and 'price',
# where 'area' is the size of the house in square feet and 'price' is the price of the house
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([[3500]])
print(prediction)
Output:
[800000.]
5. Write a python program to implement multiple Linear Regression for a
given dataset.
import pandas as pd
# Assume that we have a dataset with three columns: 'area', 'bedrooms', and 'price',
# where 'area' is the size of the house in square feet, 'bedrooms' is the number of bedrooms,
model = LinearRegression()
X = df[['area', 'bedrooms']]
y = df['price']
model.fit(X, y)
# Predict the price of a house with an area of 3500 square feet and 4 bedrooms
print(prediction)
Output:
[799998.4000064]
6. Write a python program to implement Polynomial Regression for given
dataset.
import numpy as np
import pandas as pd
# Assume that we have a dataset with two columns: 'area' and 'price',
# where 'area' is the size of the house in square feet and 'price' is the price of the house
model = LinearRegression()
poly_transformer = PolynomialFeatures(degree=2)
X_poly = poly_transformer.fit_transform(df[['area']])
model.fit(X_poly, df['price'])
prediction = model.predict(poly_transformer.transform([[3500]]))
print(prediction)
Output:
[799998.4000064]
7. Write a python program to Implement Naïve Bayes.
import numpy as np
# Assume that we have a dataset with two features: 'age' and 'income',
X = np.array([[20, 50000], [30, 60000], [40, 80000], [50, 100000], [60, 120000]])
model = GaussianNB()
model.fit(X, y)
# Predict whether a person with an age of 25 and an income of 55000 will purchase a product
print(prediction)
Output:
['yes']
8. Write a python program to Implement Decision Tree whether or
not to play tennis.
import numpy as np
import pandas
PlayTennis = pandas.read_csv('playtennis.csv')
print(PlayTennis)
Le = LabelEncoder()
PlayTennis['outlook'] = Le.fit_transform(PlayTennis['outlook'])
PlayTennis['temp'] = Le.fit_transform(PlayTennis['temp'])
PlayTennis['humidity'] = Le.fit_transform(PlayTennis['humidity'])
PlayTennis['windy'] = Le.fit_transform(PlayTennis['windy'])
PlayTennis['play'] = Le.fit_transform(PlayTennis['play'])
print(PlayTennis)
y = PlayTennis['play']
X = PlayTennis.drop(['play'],axis=1)
clf = clf.fit(X, y)
tree.plot_tree(clf)
# The predictions are stored in X_pred
X_pred = clf.predict(X)
X_pred == y
Output:
outlook temp humidity windy play
0 sunny hot high False no
1 sunny hot high True no
2 overcast hot high False yes
3 rainy mild high False yes
4 rainy cool normal False yes
5 rainy cool normal True no
6 overcast cool normal True yes
7 sunny mild high False no
8 sunny cool normal False yes
9 rainy mild normal False yes
10 sunny mild normal True yes
11 overcast mild high True yes
12 overcast hot normal False yes
13 rainy mild high True no
import numpy as np
# Assume that we have a dataset with two features: 'age' and 'income',
X = np.array([[20, 50000], [30, 60000], [40, 80000], [50, 100000], [60, 120000]])
model = LinearSVC()
model.fit(X, y)
# Predict whether a person with an age of 25 and an income of 55000 will purchase a product
print(prediction)
Output:
['no']
10. Write a python program to find Decision boundary by using a neural
network with 10 hidden units on two moons dataset
import numpy as np
X, y = make_moons(n_samples=1000, noise=0.1)
model.fit(X, y)
h = 0.01
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.show()
Output:
11. Write a python program to transform data with Principal Component
Analysis (PCA)
import numpy as np
# Assume that we have a dataset with three features: 'length', 'width', and 'height'
pca = PCA(n_components=2)
X_transformed = pca.fit_transform(X)
print(X_transformed)
Output:
[[ 2.59807621 0. ]
[ 0.8660254 0. ]
[-0.8660254 0. ]
[-2.59807621 -0. ]]
12. Write a python program to implement k-nearest Neighbors ML
algorithm to build prediction model (Use Forge Dataset)
import numpy as np
import pandas as pd
plt.style.use('seaborn')
plt.figure(figsize = (10,10))
plt.show()
knn5 = KNeighborsClassifier(n_neighbors = 5)
knn1 = KNeighborsClassifier(n_neighbors=1)
knn5.fit(X_train, y_train)
knn1.fit(X_train, y_train)
y_pred_5 = knn5.predict(X_test)
y_pred_1 = knn1.predict(X_test)
plt.subplot(1,2,1)
plt.subplot(1,2,2)
plt.show()
Output:
13. Write a python program to implement k-means algorithm on a synthetic
dataset.
model = KMeans(n_clusters=4)
model.fit(X)
y_pred = model.predict(X)
plt.show()
Output:
14. Write a python program to implement Agglomerative clustering on a
synthetic dataset.
model = AgglomerativeClustering(n_clusters=4)
model.fit(X)
y_pred = model.fit_predict(X)
plt.show()
Output: