Machine Learning Program
Machine Learning Program
import numpy as np
import pandas as pd
dataset = pd.read_csv("D:/book1.csv",delimiter=',')
print(dataset)
dataset.info()
dataset.isna()
dataset_1=dataset.dropna()
print(dataset_1)
x=dataset.iloc[:,[0,1,2]]
y=dataset.iloc[:,[3]]
print(x)
print(y)
lEncoder=LabelEncoder()
x.iloc[:,0]=lEncoder.fit_transform(x.iloc[:,0])
print(x)
OUTPUT:
0 France 44 72000 no
2 Germany 30 64000 no
3 Spain 38 61000 no
8 Germany 50 83000 no
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
dtypes: object(4)
0 France 44 72000 no
2 Germany 30 64000 no
3 Spain 38 61000 no
8 Germany 50 83000 no
0 France 44 72000
1 Spain 27 48000
2 Germany 30 64000
3 Spain 38 61000
4 Germany 40 Nan
5 France 38 54000
7 France 48 74000
8 Germany 50 83000
9 France 37 67000
PURCHASED
0 no
1 yes
2 no
3 no
4 yes
5 yes
6 no
7 yes
8 no
9 yes
0 0 44 72000
1 2 27 48000
2 1 30 64000
3 2 38 61000
4 1 40 Nan
5 0 38 54000
6 2 Nan 62000
7 0 48 74000
8 1 50 83000
9 0 37 67000
Data from database:
import mysql.connector
# Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "",database="SampleDB")
# Creating the cursor object
cur = myconn.cursor()
# Executing the query
cur.execute("select * from students")
# Fetching the rows from the cursor object
result = cur.fetchall()
print("Student Details are :")
# Printing the result
for x in result:
print(x);
# Commit the transaction
myconn.commit()
# Close the connection
myconn.close()
Output:
K-means clustering:
#plt.scatter(x, y)
for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)
Output:
K-nearest neighbours:
# Loading data
data_iris = load_iris()
# To get list of target names
label_target = data_iris.target_names
print()
print("Sample Data from Iris Dataset")
print("*"*30)
# to display the sample data from the iris dataset
for i in range(10):
rn = random.randint(0,120)
print(data_iris.data[rn],"===>",label_target[data_iris.target[rn]])
knn.fit(X_train, y_train)
# to display the score
print("The Score is :",knn.score(X_test, y_test))
# To get test data from the user
test_data = input("Enter Test Data :").split(",")
for i in range(len(test_data)):
test_data[i] = float(test_data[i])
print()
v = knn.predict([test_data])
print("Predicted output is :",label_target[v])
except:
print("Please supply valid input......")
Output:
Linear regression:
import numpy as np
import matplotlib.pyplot as plt
# putting labels
plt.xlabel('x')
plt.ylabel('y')
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
import numpy as np
x,y=make_blobs(n_samples=500,centers=2,random_state=0,cluster_std=0.40)
xfit=np.linspace(-1,3.5)
plt.scatter(x[:,0],x[:,1],c=y,s=50,cmap='spring')
yfit=m*xfit+b
plt.plot(xfit,yfit,'-k')
plt.fill_between(xfit,yfit-d,yfit+d,edgecolor='none',color='#AAAAAA',alpha=0.4)
plt.xlim(-1,3.5);
plt.show()