matplotlib的初步认识

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy


# In[2]:


import matplotlib.pyplot as plt


# In[3]:


import numpy as np
x=np.linspace(0,10,100)
print(x)


# In[4]:


y = np.sin(x)


# In[5]:


y


# In[6]:


plt.plot(x,y)
plt.show()


# In[7]:


cosy= np.cos(x)


# In[8]:


cosy


# In[9]:


cosy.shape


# In[10]:


siny=y.copy()


# In[11]:


plt.plot(x,siny)
plt.plot(x,cosy)
plt.show()


# In[12]:


plt.plot(x,siny)
plt.plot(x,cosy,color='red')
plt.show()


# In[13]:


plt.plot(x,siny)
plt.plot(x,cosy,color='red',linestyle = '--')
plt.axis([-1,11,-2,2])
plt.show()


# In[14]:


plt.plot(x, siny, label="sin(x)")
plt.plot(x, cosy, color="red", linestyle="--", label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("Welcome to matplotlib world!")
plt.show()


# In[15]:


plt.plot(x,siny,label="sin(x)")
plt.plot(x,cosy,color="red",linestyle='--',label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("welcom to matplotlib world")
plt.show()


# In[16]:


plt.scatter(x,siny)
plt.show()


# In[17]:


plt.scatter(x,siny)
plt.scatter(x,cosy)
plt.show()


# In[18]:


pip list


# In[19]:


pip install sklearn


# In[20]:


from sklearn import datasets


# In[21]:


iris = datasets.load_iris()


# In[22]:


iris.keys()


# In[24]:


print(iris['DESCR'])


# In[25]:


iris.data


# In[26]:


iris.target.shape


# In[27]:


iris.target_names


# In[28]:


X = iris.data[:,:2]


# In[29]:


plt.scatter(X[:,0], X[:,1])
plt.show()


# In[30]:


y = iris.target


# In[31]:


y


# In[32]:


plt.scatter(X[y==0,0], X[y==0,1], color="red")
plt.scatter(X[y==1,0], X[y==1,1], color="blue")
plt.scatter(X[y==2,0], X[y==2,1], color="green")
plt.show()


# In[33]:


plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()


# In[34]:


plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()


# In[35]:


X = iris.data[:,2:]


# In[36]:


plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()


# In[37]:


raw_data_X = [[3.393533211, 2.331273381],
              [3.110073483, 1.781539638],
              [1.343808831, 3.368360954],
              [3.582294042, 4.679179110],
              [2.280362439, 2.866990263],
              [7.423436942, 4.696522875],
              [5.745051997, 3.533989803],
              [9.172168622, 2.511101045],
              [7.792783481, 3.424088941],
              [7.939820817, 0.791637231]
             ]
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]


# In[38]:


X_train = np.array(raw_data_X)
y_train = np.array(raw_data_y)


# In[39]:


x = np.array([8.093607318, 3.365731514])

plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')
plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')
plt.scatter(x[0], x[1], color='b')
plt.show()


# In[40]:


b=np.array([1,2])


# In[41]:


b**2


# In[42]:


sum(b**2)


# In[45]:


from math import sqrt
distances = [sqrt(np.sum((x_train - x)**2)) for x_train in X_train]


# In[46]:


distances


# In[47]:


nearest = np.argsort(distances)


# In[49]:


k = 6
topK_y = [y_train[neighbor] for neighbor in nearest[:k]]


# In[50]:


topK_y


# In[51]:


from collections import Counter
votes = Counter(topK_y)


# In[52]:


votes.most_common(1)


# In[53]:


predict_y = votes.most_common(1)[0][0]


# In[54]:


predict_y


# In[55]:


bb=np.array([2,3,4,4])


# In[57]:


bb.shape


# In[58]:


bb.shape[0]


# In[59]:


pwd


# In[60]:


y = iris.target


# In[61]:


X.shape


# In[62]:


y


# In[64]:


y.shape


# In[65]:


X = iris.data


# In[66]:


X


# In[67]:


shuffled_indexes = np.random.permutation(len(X))
shuffled_indexes


# In[68]:


test_ratio = 0.2
test_size = int(len(X) * test_ratio)


# In[69]:


test_indexes = shuffled_indexes[:test_size]
train_indexes = shuffled_indexes[test_size:]


# In[70]:


X_train = X[train_indexes]
y_train = y[train_indexes]


# In[71]:


X_test = X[test_indexes]
y_test = y[test_indexes]


# In[72]:


print(X_train.shape)
print(y_train.shape)


# In[73]:


print(X_test.shape)
print(y_test.shape)


# In[74]:


digits = datasets.load_digits()
digits.keys()


# In[75]:


print(digits.DESCR)


# In[76]:


X = digits.data
X.shape


# In[77]:


y = digits.target
y.shape


# In[78]:


y[:10]


# In[79]:


X[:10]


# In[80]:


some_digit = X[66]


# In[81]:


y[66]


# In[82]:


some_digit_image = some_digit.reshape(8, 8)


# In[84]:


import matplotlib
import matplotlib.pyplot as plt
plt.imshow(some_digit_image, cmap = matplotlib.cm.binary)
plt.show()


# In[85]:


iris= datasets.load_iris()


# In[86]:


X=iris.data
y=iris.target


# In[87]:


X[:10,:]


# In[88]:


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=666)


# ## sklearn 中的standardscaler

# In[89]:


from sklearn.preprocessing import StandardScaler


# In[93]:


standardScaler = StandardScaler()


# In[94]:


standardScaler.fit(X_train)


# In[95]:


standardScaler.mean_


# In[96]:


standardScaler.scale_


# In[98]:


X_train=standardScaler.transform(X_train)


# In[99]:


X_test_standard=standardScaler.transform(X_test)


# In[100]:


x=np.array([1,2,3,4,5])
y=np.array([1,3,2,3,5])


# In[101]:


x_mean=np.mean(x)
y_mean=np.mean(y)


# In[102]:


num=0
d=0
for x_i,y_i in zip(x,y):
    num+=(x_i-x_mean)*(y_i-y_mean)
    d+=(x_i-x_mean)**2


# In[103]:


a=num/d
a


# In[104]:


b=y_mean-a*x_mean


# In[105]:


b


# In[106]:


y_hat=a*x+b
plt.scatter(x,y)
plt.plot(x,y_hat,color='r')
plt.show()


# In[111]:


boston = datasets.load_boston()


# In[113]:


print(boston.DESCR)


# In[114]:


x = boston.data[:,5] # 只使用房间数量这个特征


# In[116]:


y = boston.target
x = x[y < 50.0]
y = y[y < 50.0]


# In[117]:


plt.scatter(x, y)
plt.show()


# ## 使用简单线性回归法

# In[125]:


from sklearn import datasets

iris = datasets.load_iris()
X = iris.data[:,2:]
y = iris.target
plt.scatter(X[y==2,0], X[y==2,1])
plt.scatter(X[y==1,0], X[y==1,1])
plt.scatter(X[y==0,0], X[y==0,1])
plt.show()


# In[ ]:





# In[129]:


import numpy as np
x=np.linspace(0.01,0.99,200)


# In[132]:


def entropy(p):
    return -p*np.log(p)-(1-p)*np.log(1-p)


# In[133]:


plt.plot(x,entropy(x))


# In[134]:


def split(X, y, d, value):
    index_a = (X[:,d] <= value)
    index_b = (X[:,d] > value)
    return X[index_a], X[index_b], y[index_a], y[index_b]


# In[ ]:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
制作Mininst格式的数据,研究sklearn库等机器学习算法与库用得到。C++的VS2017测试通过。适用于车牌和数字识别。下面是Skearn测试程序。 #导入必备的包 import numpy as np import struct import matplotlib.pyplot as plt import os ##加载svm模型 from sklearn import svm ###用于做数据预处理 from sklearn import preprocessing import time #加载数据的路径 path='.' def load_mnist_train(path, kind='train'): labels_path = os.path.join(path,'%s-labels-idx1-ubyte'% kind) images_path = os.path.join(path,'%s-images-idx3-ubyte'% kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II',lbpath.read(8)) labels = np.fromfile(lbpath,dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16)) images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784) return images, labels def load_mnist_test(path, kind='t10k'): labels_path = os.path.join(path,'%s-labels-idx1-ubyte'% kind) images_path = os.path.join(path,'%s-images-idx3-ubyte'% kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II',lbpath.read(8)) labels = np.fromfile(lbpath,dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16)) images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784) return images, labels train_images,train_labels=load_mnist_train(path) test_images,test_labels=load_mnist_test(path) X=preprocessing.StandardScaler().fit_transform(train_images) X_train=X[0:60000] y_train=train_labels[0:60000] print(time.strftime('%Y-%m-%d %H:%M:%S')) model_svc = svm.LinearSVC() #model_svc = svm.SVC() model_svc.fit(X_train,y_train) print(time.strftime('%Y-%m-%d %H:%M:%S')) ##显示前30个样本的真实标签和预测值,用图显示 x=preprocessing.StandardScaler().fit_transform(test_images) x_test=x[0:10000] y_pred=test_labels[0:10000] print(model_svc.score(x_test,y_pred)) y=model_svc.predict(x) fig1=plt.figure(figsize=(8,8)) fig1.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05) for i in range(100): ax=fig1.add_subplot(10,10,i+1,xticks=[],yticks=[]) ax.imshow(np.reshape(test_images[i], [28,28]),cmap=plt.cm.binary,interpolation='nearest') ax.text(0,2,"pred:"+str(y[i]),color='red') #ax.text(0,32,"real:"+str(test_labels[i]),color='blue') plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值