Machine learning
#################################################################################
# Chap01/demo_sklearn.py
#################################################################################
from sklearn import datasets
from sklearn.cluster import Means
import matplotlib.pyplot as pet
if __name__ == '__main__':
# Load the data
iris = datasets.load_iris()
X = iris.data
petal_length = X[:, 2]
petal_width = X[:, 3]
true_labels = iris.target
# Apply KMeans clustering
estimator = KMeans(n_clusters=3)
estimator.fit(X)
predicted_labels = estimator.labels_
# Color scheme definition: red, yellow and blue
color_scheme = ['r', 'y', 'b']
# Markers definition: circle, "x" and "plus"
marker_list = ['o', 'x', '+']
# Assign colors/markers to the predicted labels
colors_predicted_labels = [color_scheme [lab] for lab in
predicted_labels]
markers_predicted = [marker_list[lab] for lab in
predicted_labels]
# Assign colors/markers to the true labels
colors_true_labels = [color_scheme[lab] for lab in true_labels]
markers_true = [marker_list[lab] for lab in true_labels]
# Plot and save the two scatter plots
for x, y, c, m in zip(petal_width,
petal_length,
colors_predicted_labels,
markers_predicted):
plt.scatter(x, y, c=c, marker=m)
plt.savefig('iris_clusters.png')
for x, y, c, m in zip(petal_width,
petal_length,
colors_true_labels,
markers_true):
plt.scatter(x, y, c=c, marker=m)
plt.savefig('iris_true_labels.png')
print(iris.target_names)
#################################################################################
Social network analysis
#################################################################################
# Chap01/demo_networkx.py
import networkx as nx
from datetime import date time
if __name__ == '__main__':
g = nx.Graph()
g.add_node("John", {'name': 'John', 'age': 25})
g.add_node("Peter", {'name': 'Peter', 'age': 35})
g.add_node("Mary", {'name': 'Mary', 'age': 31})
g.add_node("Lucy", {'name': 'Lucy', 'age': 19})
g.add_edge("John", "Mary", {'since': datetime.today()})
g.add_edge("John", "Peter", {'since': datetime(1990, 7, 30)})
g.add_edge("Mary", "Lucy", {'since': datetime(2010, 8, 10)})
print(g.nodes())
print(g.edges())
print(g.has_edge("Lucy", "Mary"))
Processing data in Python
#################################################################################
# Chap01/demo_json.py
import json
if __name__ == '__main__':
user_json = '{"user_id": "1", "name": "Marco"}'
user_data = json.loads(user_json)
print(user_data['name']) # Marco
user_data['likes'] = ['Python', 'Data Mining']
user_json = json.dumps(user_data, indent=4)
print(user_json)
nice information.Data Science Online Training bangalore
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWell Done ! the blog is great and Interactive it is about Mastering Social Media Mining 1 - Python tools for data science it is useful for students and Python Developers for more updates on python follow the link
ReplyDeletePython Online training hyderabad
For more info on other technologies go with below links
tableau online course Bangalore
ServiceNow Online Training
mulesoft Online Training